1
0
mirror of synced 2026-01-01 09:04:46 -05:00
Files
docs/components/page-header/LanguagePicker.tsx
Mike Surowiec d76c16da19 Scope markdown body (#21082)
* update article content to markdown ui component

* decouple lunr indexing from class name

* remove summary outline none rule, apply utility class instead

* improve typing

* scope more styles down to markdown-body/extended-markdown

* move all markdown-body style overrides to MarkdownContent component

* fix class targeting within css module

* clean up MarkdownContent header style

* rename data-lunr to data-search

* fix: inline code color issue

* fix: update article markdown to work with MarkdownContent
2021-08-30 14:24:49 +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 className="outline-none" 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>
)
}