Files
Bkk1234 319c271d23 feat: บทความ 2 หน้า + แชร์/ลิงก์ย่อ + PWA + แอนิเมชัน + SEO/AI + เอกสาร
- บทความจากหลังบ้าน: /for-film-lovers, /for-cinemas (ArticlePage + ArticleEditor แบบบล็อกย่อหน้า/รูป)
- แชร์: ปุ่มบนการ์ดและท้ายหน้ารายละเอียด · ลิงก์ย่อ /c/<slug> (302) · ธีมแก้วตาม DESIGN.md
- PWA: manifest/sw/offline + ถอดการล็อกซูม (WCAG 1.4.4) + touch target ≥44px
- แอนิเมชัน reveal: IntersectionObserver + gate ด้วย html.fx-motion · เคารพ prefers-reduced-motion
- ค้นหา: normalize/ตัดวรรณยุกต์ + AND หลายคำ + จัดอันดับ · รองรับ /?q= จาก SearchAction
- SEO: pageMeta (canonical/OG/Twitter) + JSON-LD (WebSite/Organization/BreadcrumbList/MovieTheater/Article)
- ไม่ฝังโดเมนในโค้ด: origin มาจาก request (x-forwarded-host/proto) ทั้ง canonical, sitemap, robots, ลิงก์แชร์
- กัน AI: robots.txt (Nitro route, บล็อก AI 21 ตัว) + ai.txt + OG image 1200x630
- เอกสาร: README สรุปโปรเจกต์, DESIGN.md (motion/PWA/SEO/ข้อมูล), architecture.html, admin-guide
- tests: 135 ผ่าน (16 ไฟล์)
2026-11-10 02:31:10 +07:00

186 lines
6.8 KiB
TypeScript

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 (
<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;
/**
* สลับภาษาเป็น "ปุ่มเดียว + dropdown"
*
* - ปุ่มแสดงภาษาปัจจุบัน (ธง + รหัส) พร้อมลูกศรบอกว่ามีกั้นลงมา
* - เลือกแล้วเปลี่ยนภาษาทันทีและปิดเมนู คืนโฟกัสกลับไปที่ปุ่ม
* - ปิดได้ด้วยคลิกนอกเมนู หรือกด Escape
* (เดิมเป็นปุ่ม TH/EN สองปุ่ม — ยุบเหลือปุ่มเดียวเพื่อประหยัดที่ใน header มือถือ)
*/
export function LocaleSwitcher() {
const { locale, setLocale, messages } = useI18n();
const [open, setOpen] = useState(false);
const wrapperRef = useRef<HTMLDivElement>(null);
const triggerRef = useRef<HTMLButtonElement>(null);
// ปิดเมนูเมื่อคลิกที่อื่น หรือกด Escape
useEffect(() => {
if (!open) return;
// ให้คีย์บอร์ดเริ่มที่ตัวเลือกปัจจุบัน (มาตรฐานของ role="menu")
wrapperRef.current
?.querySelector<HTMLButtonElement>(
'[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<HTMLDivElement>) {
if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return;
event.preventDefault();
const items = Array.from(
wrapperRef.current?.querySelectorAll<HTMLButtonElement>(
'[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 (
<div ref={wrapperRef} className="relative">
<button
ref={triggerRef}
type="button"
aria-haspopup="menu"
aria-expanded={open}
aria-label={`${messages.nav.language}: ${current.name}`}
title={current.name}
onClick={() => 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"
>
<CurrentFlag />
<span className="font-medium leading-none">{current.code}</span>
<IconChevronDown
size={14}
aria-hidden="true"
className={`transition-transform duration-200 ${open ? "rotate-180" : ""}`}
/>
</button>
{open ? (
<div
role="menu"
aria-label={messages.nav.language}
onKeyDown={handleMenuKeyDown}
className="absolute right-0 top-[calc(100%+8px)] z-30 flex w-[190px] flex-col gap-1 rounded-xl border border-[#303030] bg-[#141414] p-2 shadow-lg"
>
{LOCALE_OPTIONS.map((option) => {
const Flag = FLAGS[option.locale];
const isActive = option.locale === locale;
return (
<button
key={option.locale}
type="button"
role="menuitemradio"
aria-checked={isActive}
lang={option.locale}
onClick={() => 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"
}`}
>
<Flag />
<span className="flex-1 text-left">{option.name}</span>
{isActive ? <IconCheck size={16} aria-hidden="true" /> : null}
</button>
);
})}
</div>
) : null}
</div>
);
}