import { IconCheck, IconChevronDown } from "@tabler/icons-react"; import { useEffect, useRef, useState, type KeyboardEvent as ReactKeyboardEvent, } from "react"; import { COLORS } from "@/lib/design-tokens"; import { useI18n } from "@/lib/i18n"; import { LOCALE_OPTIONS, type Locale } from "@/lib/i18n/messages"; /** ธงชาติไทยย่อส่วน */ function FlagTh() { return ( ); } /** ธงสหราชอาณาจักรย่อส่วน */ function FlagEn() { return ( ); } const FLAGS = { th: FlagTh, en: FlagEn } as const; /** * สลับภาษาเป็น "ปุ่มเดียว + dropdown" * * - ปุ่มแสดงภาษาปัจจุบัน (ธง + รหัส) พร้อมลูกศรบอกว่ามีกั้นลงมา * - เลือกแล้วเปลี่ยนภาษาทันทีและปิดเมนู คืนโฟกัสกลับไปที่ปุ่ม * - ปิดได้ด้วยคลิกนอกเมนู หรือกด Escape * (เดิมเป็นปุ่ม TH/EN สองปุ่ม — ยุบเหลือปุ่มเดียวเพื่อประหยัดที่ใน header มือถือ) */ export function LocaleSwitcher() { const { locale, setLocale, messages } = useI18n(); const [open, setOpen] = useState(false); const wrapperRef = useRef(null); const triggerRef = useRef(null); // ปิดเมนูเมื่อคลิกที่อื่น หรือกด Escape useEffect(() => { if (!open) return; // ให้คีย์บอร์ดเริ่มที่ตัวเลือกปัจจุบัน (มาตรฐานของ role="menu") wrapperRef.current ?.querySelector( '[role="menuitemradio"][aria-checked="true"]', ) ?.focus(); function handlePointerDown(event: MouseEvent) { if (!wrapperRef.current?.contains(event.target as Node)) setOpen(false); } function handleKeyDown(event: KeyboardEvent) { if (event.key !== "Escape") return; setOpen(false); triggerRef.current?.focus(); } document.addEventListener("mousedown", handlePointerDown); document.addEventListener("keydown", handleKeyDown); return () => { document.removeEventListener("mousedown", handlePointerDown); document.removeEventListener("keydown", handleKeyDown); }; }, [open]); const current = LOCALE_OPTIONS.find((option) => option.locale === locale) ?? LOCALE_OPTIONS[0]; const CurrentFlag = FLAGS[current.locale]; /** เลื่อนโฟกัสด้วยลูกศรขึ้น/ลงในเมนู (role="menu" ต้องรองรับคีย์บอร์ด) */ function handleMenuKeyDown(event: ReactKeyboardEvent) { if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return; event.preventDefault(); const items = Array.from( wrapperRef.current?.querySelectorAll( '[role="menuitemradio"]', ) ?? [], ); if (items.length === 0) return; const currentIndex = items.indexOf( document.activeElement as HTMLButtonElement, ); const step = event.key === "ArrowDown" ? 1 : -1; const nextIndex = (currentIndex + step + items.length) % items.length; items[nextIndex]?.focus(); } function choose(next: Locale) { setLocale(next); setOpen(false); triggerRef.current?.focus(); } return ( setOpen((value) => !value)} className="flex min-h-10 items-center gap-1.5 rounded-lg px-2 text-sm text-[#B3B3B3] transition hover:text-white focus-visible:outline focus-visible:outline-2 focus-visible:outline-[#A92626] after:absolute after:-inset-y-1 after:-inset-x-0.5 after:content-[''] sm:gap-2" > {current.code} {open ? ( {LOCALE_OPTIONS.map((option) => { const Flag = FLAGS[option.locale]; const isActive = option.locale === locale; return ( choose(option.locale)} className={`flex min-h-11 items-center gap-2 rounded-lg px-2.5 text-sm transition ${ isActive ? "bg-[#242424] font-semibold text-[#D47272]" : "text-[#B3B3B3] hover:bg-[#1B1B1B] hover:text-white" }`} > {option.name} {isActive ? : null} ); })} ) : null} ); }