Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

LanguagePicker.tsx 2.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  1. import cx from 'classnames'
  2. import { useRouter } from 'next/router'
  3. import { Dropdown, Details, useDetails } from '@primer/components'
  4. import { ChevronDownIcon } from '@primer/octicons-react'
  5. import { Link } from 'components/Link'
  6. import { useMainContext } from './context/MainContext'
  7. type Props = {
  8. variant?: 'inline'
  9. }
  10. export const LanguagePicker = ({ variant }: Props) => {
  11. const router = useRouter()
  12. const { languages } = useMainContext()
  13. const { getDetailsProps } = useDetails({})
  14. const locale = router.locale || 'en'
  15. const langs = Object.values(languages)
  16. const selectedLang = languages[locale]
  17. if (variant === 'inline') {
  18. return (
  19. <Details {...getDetailsProps()} className="details-reset">
  20. <summary aria-label="Toggle language list">
  21. <div className="d-flex flex-items-center flex-justify-between py-2">
  22. <span>{selectedLang.nativeName || selectedLang.name}</span>
  23. <ChevronDownIcon size={24} className="arrow ml-md-1" />
  24. </div>
  25. </summary>
  26. <div>
  27. {langs.map((lang) => {
  28. return (
  29. <Link
  30. key={lang.code}
  31. href={router.asPath}
  32. locale={lang.hreflang}
  33. className={cx(
  34. 'd-block py-2',
  35. lang.code === router.locale
  36. ? 'color-text-link text-underline active'
  37. : 'Link--primary no-underline'
  38. )}
  39. >
  40. {lang.nativeName ? (
  41. <>
  42. {lang.nativeName} ({lang.name})
  43. </>
  44. ) : (
  45. lang.name
  46. )}
  47. </Link>
  48. )
  49. })}
  50. </div>
  51. </Details>
  52. )
  53. }
  54. return (
  55. <Dropdown
  56. css={`
  57. ul {
  58. width: unset;
  59. }
  60. `}
  61. >
  62. <summary>
  63. {selectedLang.nativeName || selectedLang.name}
  64. <Dropdown.Caret />
  65. </summary>
  66. <Dropdown.Menu direction="sw">
  67. {langs.map((lang) => {
  68. return (
  69. <Dropdown.Item key={lang.code}>
  70. <Link href={router.asPath} locale={lang.hreflang}>
  71. {lang.nativeName ? (
  72. <>
  73. {lang.nativeName} ({lang.name})
  74. </>
  75. ) : (
  76. lang.name
  77. )}
  78. </Link>
  79. </Dropdown.Item>
  80. )
  81. })}
  82. </Dropdown.Menu>
  83. </Dropdown>
  84. )
  85. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...