87 lines
2.9 KiB
TypeScript
87 lines
2.9 KiB
TypeScript
import { useI18n } from "@/lib/i18n";
|
|
import { LOCALE_OPTIONS } from "@/lib/i18n/messages";
|
|
import { COLORS } from "@/lib/design-tokens";
|
|
|
|
/** ธงชาติไทยย่อส่วน */
|
|
function FlagTh() {
|
|
return (
|
|
<span
|
|
className="block h-4 w-[21px] shrink-0 rounded-[1px]"
|
|
style={{ background: COLORS.flagGradient }}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/** ธงสหราชอาณาจักรย่อส่วน */
|
|
function FlagEn() {
|
|
return (
|
|
<span
|
|
className="relative block h-4 w-[21px] shrink-0 overflow-hidden rounded-[1px]"
|
|
style={{ background: "#012169" }}
|
|
>
|
|
<span
|
|
className="absolute inset-0"
|
|
style={{
|
|
background:
|
|
"linear-gradient(45deg, transparent 44%, #F4F5F8 44% 56%, transparent 56%), linear-gradient(-45deg, transparent 44%, #F4F5F8 44% 56%, transparent 56%)",
|
|
}}
|
|
/>
|
|
<span
|
|
className="absolute inset-0"
|
|
style={{
|
|
background:
|
|
"linear-gradient(90deg, transparent 40%, #F4F5F8 40% 60%, transparent 60%), linear-gradient(0deg, transparent 33%, #F4F5F8 33% 67%, transparent 67%)",
|
|
}}
|
|
/>
|
|
<span
|
|
className="absolute inset-0"
|
|
style={{
|
|
background:
|
|
"linear-gradient(90deg, transparent 44%, #C8102E 44% 56%, transparent 56%), linear-gradient(0deg, transparent 40%, #C8102E 40% 60%, transparent 60%)",
|
|
}}
|
|
/>
|
|
</span>
|
|
);
|
|
}
|
|
|
|
const FLAGS = { th: FlagTh, en: FlagEn } as const;
|
|
|
|
/**
|
|
* สลับภาษาเป็นปุ่มสองปุ่ม (TH / EN) — ไม่มีเมนูให้เปิด/ปิด
|
|
* จึงไม่มีปัญหาคลิกพลาดหรือเมนูถูกบัง และกดครั้งเดียวเปลี่ยนทันที
|
|
*/
|
|
export function LocaleSwitcher() {
|
|
const { locale, setLocale } = useI18n();
|
|
|
|
return (
|
|
<div role="group" aria-label="ภาษา" className="flex items-center gap-1">
|
|
{LOCALE_OPTIONS.map((option) => {
|
|
const Flag = FLAGS[option.locale];
|
|
const isActive = option.locale === locale;
|
|
return (
|
|
<button
|
|
key={option.locale}
|
|
type="button"
|
|
lang={option.locale}
|
|
onClick={() => setLocale(option.locale)}
|
|
aria-pressed={isActive}
|
|
title={option.name}
|
|
className={`flex items-center gap-1.5 rounded-lg px-1.5 py-1 text-sm transition sm:gap-2 sm:px-2 ${
|
|
isActive
|
|
? "bg-[#242424] font-semibold text-[#D47272]"
|
|
: "text-[#B3B3B3] hover:text-white"
|
|
}`}
|
|
>
|
|
<span className="hidden sm:block">
|
|
<Flag />
|
|
</span>
|
|
<span className="text-base font-medium leading-none sm:text-lg">
|
|
{option.code}
|
|
</span>
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|