1
0
mirror of synced 2025-12-22 03:16:52 -05:00
Files
docs/components/page-header/LanguagePicker.tsx
Mike Surowiec d81f51ebf7 Picker improvements (#21765)
* close Language and ArticleVersion pickers after click

* cleanup ArticleGridLayout due to VersionPicker changes

* fix tsc errors resulting from primer upgrade

* fix / update tests

* cleanup mobile pickers visual consistency

* use btn-sm on VersionPicker

* update translation and close on click for enterprise releases

Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
2021-09-30 20:22:13 +00:00

86 lines
2.6 KiB
TypeScript

import { useRouter } from 'next/router'
import { Box, Dropdown, Details, Text, 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, setOpen } = useDetails({ closeOnOutsideClick: true })
const locale = router.locale || 'en'
const langs = Object.values(languages)
const selectedLang = languages[locale]
if (variant === 'inline') {
return (
<Details {...getDetailsProps()} data-testid="language-picker">
<summary
className="d-block btn btn-invisible color-text-primary"
aria-label="Toggle language list"
>
<div className="d-flex flex-items-center flex-justify-between">
<Text>{selectedLang.nativeName || selectedLang.name}</Text>
<ChevronDownIcon size={24} className="arrow ml-md-1" />
</div>
</summary>
<Box mt={1}>
{langs.map((lang) => {
if (lang.wip) {
return null
}
return (
<Dropdown.Item onClick={() => setOpen(false)} key={lang.code}>
<Link href={router.asPath} locale={lang.code}>
{lang.nativeName ? (
<>
{lang.nativeName} ({lang.name})
</>
) : (
lang.name
)}
</Link>
</Dropdown.Item>
)
})}
</Box>
</Details>
)
}
return (
<Details {...getDetailsProps()} data-testid="language-picker" className="position-relative">
<summary className="d-block btn btn-invisible color-text-primary">
<Text>{selectedLang.nativeName || selectedLang.name}</Text>
<Dropdown.Caret />
</summary>
<Dropdown.Menu direction="sw" style={{ width: 'unset' }}>
{langs.map((lang) => {
if (lang.wip) {
return null
}
return (
<Dropdown.Item key={lang.code} onClick={() => setOpen(false)}>
<Link href={router.asPath} locale={lang.code}>
{lang.nativeName ? (
<>
{lang.nativeName} ({lang.name})
</>
) : (
lang.name
)}
</Link>
</Dropdown.Item>
)
})}
</Dropdown.Menu>
</Details>
)
}