1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/components/page-header/LanguagePicker.tsx
Mike Surowiec a511c95c7f SCSS and Component cleanup (pt 1) (#20572)
* turn article.scss into a module + componentized

* Update Survey to use only component styles, add cancel button

* Update GenericError + 404 page to use only standard classes

* update LearningTrack to not use markdown-body

* remove / consolidate stylesheets

* cleanup Graphiql explorer page and scss

* Componentize Breadcrumb styles

* Componentize DeprecationBanner styles

* scope h2 a link style to markdown-body

* cleanup nav, organize page-header and page-footer components

* remove unused scroll-button.scss

* organize LanguagePicker and ProductPicker

* add declarations file

* remove featured-links.scss, update tests

* update list utility and toc test

* fix bad merge resolution

* update breadcrumbs test
2021-07-29 17:27:20 +00:00

100 lines
2.7 KiB
TypeScript

import cx from 'classnames'
import { useRouter } from 'next/router'
import { Dropdown, Details, useDetails } from '@primer/components'
import { ChevronDownIcon } from '@primer/octicons-react'
import { Link } from 'components/Link'
import { useLanguages } from 'components/context/LanguagesContext'
type Props = {
variant?: 'inline'
}
export const LanguagePicker = ({ variant }: Props) => {
const router = useRouter()
const { languages } = useLanguages()
const { getDetailsProps } = useDetails({})
const locale = router.locale || 'en'
const langs = Object.values(languages)
const selectedLang = languages[locale]
if (variant === 'inline') {
return (
<Details {...getDetailsProps()} className="details-reset">
<summary aria-label="Toggle language list">
<div className="d-flex flex-items-center flex-justify-between py-2">
<span>{selectedLang.nativeName || selectedLang.name}</span>
<ChevronDownIcon size={24} className="arrow ml-md-1" />
</div>
</summary>
<div>
{langs.map((lang) => {
if (lang.wip) {
return null
}
return (
<Link
key={lang.code}
href={router.asPath}
locale={lang.code}
disableClientTransition={true}
className={cx(
'd-block py-2',
lang.code === router.locale
? 'color-text-link text-underline active'
: 'Link--primary no-underline'
)}
>
{lang.nativeName ? (
<>
{lang.nativeName} ({lang.name})
</>
) : (
lang.name
)}
</Link>
)
})}
</div>
</Details>
)
}
return (
<Dropdown
css={`
ul {
width: unset;
}
`}
data-testid="language-picker"
>
<summary>
{selectedLang.nativeName || selectedLang.name}
<Dropdown.Caret />
</summary>
<Dropdown.Menu direction="sw">
{langs.map((lang) => {
if (lang.wip) {
return null
}
return (
<Dropdown.Item key={lang.code}>
<Link href={router.asPath} locale={lang.code} disableClientTransition={true}>
{lang.nativeName ? (
<>
{lang.nativeName} ({lang.name})
</>
) : (
lang.name
)}
</Link>
</Dropdown.Item>
)
})}
</Dropdown.Menu>
</Dropdown>
)
}