|
1 | 1 | "use client"; |
2 | 2 |
|
| 3 | +import { useState, useRef, useEffect } from "react"; |
3 | 4 | import { useLocale } from "next-intl"; |
4 | 5 | import { usePathname, useRouter } from "@/i18n/navigation"; |
5 | 6 | import { routing } from "@/i18n/routing"; |
| 7 | +import { CountryFlag } from "./CountryFlag"; |
6 | 8 |
|
7 | 9 | const LOCALE_OPTIONS = [ |
8 | | - { value: "en", label: "English", flag: "🇺🇸" }, |
9 | | - { value: "de", label: "Deutsch", flag: "🇩🇪" }, |
10 | | - { value: "fr", label: "Français", flag: "🇫🇷" }, |
11 | | - { value: "zh", label: "简体中文", flag: "🇨🇳" }, |
12 | | - { value: "es", label: "Español", flag: "🇪🇸" }, |
13 | | - { value: "pl", label: "Polski", flag: "🇵🇱" }, |
14 | | - { value: "it", label: "Italiano", flag: "🇮🇹" }, |
15 | | - { value: "ko", label: "한국어", flag: "🇰🇷" }, |
16 | | - { value: "pt", label: "Português", flag: "🇧🇷" }, |
17 | | - { value: "ja", label: "日本語", flag: "🇯🇵" }, |
18 | | -] as const satisfies { value: (typeof routing.locales)[number]; label: string; flag: string }[]; |
| 10 | + { value: "en", label: "English", country: "US" }, |
| 11 | + { value: "de", label: "Deutsch", country: "DE" }, |
| 12 | + { value: "fr", label: "Français", country: "FR" }, |
| 13 | + { value: "zh", label: "简体中文", country: "CN" }, |
| 14 | + { value: "es", label: "Español", country: "ES" }, |
| 15 | + { value: "pl", label: "Polski", country: "PL" }, |
| 16 | + { value: "it", label: "Italiano", country: "IT" }, |
| 17 | + { value: "ko", label: "한국어", country: "KR" }, |
| 18 | + { value: "pt", label: "Português", country: "BR" }, |
| 19 | + { value: "ja", label: "日本語", country: "JP" }, |
| 20 | +] as const satisfies { |
| 21 | + value: (typeof routing.locales)[number]; |
| 22 | + label: string; |
| 23 | + country: string; |
| 24 | +}[]; |
19 | 25 |
|
20 | 26 | export function LanguageSwitcher() { |
21 | 27 | const currentLocale = useLocale(); |
22 | 28 | const router = useRouter(); |
23 | 29 | const pathname = usePathname(); |
| 30 | + const [open, setOpen] = useState(false); |
| 31 | + const ref = useRef<HTMLDivElement>(null); |
24 | 32 |
|
25 | | - function handleLocaleChange(e: React.ChangeEvent<HTMLSelectElement>) { |
26 | | - router.replace(pathname, { locale: e.target.value }); |
| 33 | + const current = LOCALE_OPTIONS.find((o) => o.value === currentLocale) ?? LOCALE_OPTIONS[0]; |
| 34 | + |
| 35 | + function handleLocaleChange(locale: string) { |
| 36 | + router.replace(pathname, { locale }); |
| 37 | + setOpen(false); |
27 | 38 | } |
28 | 39 |
|
| 40 | + useEffect(() => { |
| 41 | + function onClickOutside(e: MouseEvent) { |
| 42 | + if (ref.current && !ref.current.contains(e.target as Node)) { |
| 43 | + setOpen(false); |
| 44 | + } |
| 45 | + } |
| 46 | + document.addEventListener("mousedown", onClickOutside); |
| 47 | + return () => document.removeEventListener("mousedown", onClickOutside); |
| 48 | + }, []); |
| 49 | + |
29 | 50 | return ( |
30 | | - <select |
31 | | - value={currentLocale} |
32 | | - onChange={handleLocaleChange} |
33 | | - className="bg-transparent text-sm text-neutral-500 dark:text-neutral-400 hover:text-neutral-900 dark:hover:text-white transition-colors cursor-pointer border border-neutral-300 dark:border-neutral-700 rounded-md px-2 py-1 focus:outline-none focus:ring-1 focus:ring-neutral-400" |
34 | | - aria-label="Select language" |
35 | | - > |
36 | | - {LOCALE_OPTIONS.map((option) => ( |
37 | | - <option key={option.value} value={option.value}> |
38 | | - {option.flag} {option.label} |
39 | | - </option> |
40 | | - ))} |
41 | | - </select> |
| 51 | + <div className="relative" ref={ref}> |
| 52 | + <button |
| 53 | + type="button" |
| 54 | + onClick={() => setOpen((v) => !v)} |
| 55 | + className="flex items-center gap-2 bg-transparent text-sm text-neutral-500 dark:text-neutral-400 hover:text-neutral-900 dark:hover:text-white transition-colors cursor-pointer border border-neutral-300 dark:border-neutral-700 rounded-md px-2 py-1 focus:outline-none focus:ring-1 focus:ring-neutral-400" |
| 56 | + aria-label="Select language" |
| 57 | + > |
| 58 | + <CountryFlag country={current.country} className="w-4" /> |
| 59 | + {current.label} |
| 60 | + </button> |
| 61 | + {open && ( |
| 62 | + <div className="absolute right-0 bottom-full mb-1 z-50 min-w-[160px] rounded-md border border-neutral-300 dark:border-neutral-700 bg-white dark:bg-neutral-900 shadow-lg py-1"> |
| 63 | + {LOCALE_OPTIONS.map((option) => ( |
| 64 | + <button |
| 65 | + key={option.value} |
| 66 | + type="button" |
| 67 | + onClick={() => handleLocaleChange(option.value)} |
| 68 | + className={`flex w-full items-center gap-2 px-3 py-1.5 text-sm hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors ${ |
| 69 | + option.value === currentLocale |
| 70 | + ? "text-neutral-900 dark:text-white font-medium" |
| 71 | + : "text-neutral-600 dark:text-neutral-400" |
| 72 | + }`} |
| 73 | + > |
| 74 | + <CountryFlag country={option.country} className="w-4" /> |
| 75 | + {option.label} |
| 76 | + </button> |
| 77 | + ))} |
| 78 | + </div> |
| 79 | + )} |
| 80 | + </div> |
42 | 81 | ); |
43 | 82 | } |
0 commit comments