- theme.css: semantic tokens (bg, surface, ink, border, brand, tints, status, glass) - ThemeSwitch.tsx: radiogroup with light/dark/auto, persisted in localStorage - auto mode follows device time (06:00-18:00 light), rechecks every minute and on visibilitychange - inline pre-paint script in layout.tsx prevents flash of wrong theme - remapped ~250 hardcoded hex declarations across styles to tokens - fixed --c-ink-2 contrast 4.25 -> 5.36 (WCAG AA) in light mode - 0 contrast failures below 4.5:1 across 8 pages in both themes - DESIGN.md: documented theme system
126 lines
4.5 KiB
TypeScript
126 lines
4.5 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef } from "react";
|
|
import { usePathname } from "next/navigation";
|
|
import ThemeSwitch from "@/components/ThemeSwitch";
|
|
|
|
const NAV = [
|
|
{ href: "/", key: "nav.home", label: "หน้าแรก" },
|
|
{ href: "/stops", key: "nav.stops", label: "ป้ายหยุดรถ" },
|
|
{ href: "/routes", key: "nav.routes", label: "รายละเอียดสายรถ" },
|
|
{ href: "/about", key: "nav.about", label: "เกี่ยวกับเรา" },
|
|
{ href: "/blog", key: "nav.blog", label: "บ่นไปเรื่อย" },
|
|
{ href: "/credits", key: "nav.credits", label: "เครดิต" },
|
|
];
|
|
|
|
function isActive(pathname: string, href: string): boolean {
|
|
if (href === "/") return pathname === "/";
|
|
return pathname === href || pathname.startsWith(`${href}/`);
|
|
}
|
|
|
|
export default function SiteHeader() {
|
|
const pathname = usePathname() || "/";
|
|
const menuRef = useRef<HTMLDetailsElement>(null);
|
|
|
|
// ปิดเมนู hamburger เมื่อเปลี่ยนหน้า หรือคลิกนอกเมนู (เดิมอยู่ใน index.html)
|
|
useEffect(() => {
|
|
if (menuRef.current) menuRef.current.removeAttribute("open");
|
|
function onDocClick(event: MouseEvent) {
|
|
const menu = menuRef.current;
|
|
if (!menu) return;
|
|
const target = event.target as HTMLElement;
|
|
if (target.closest(".mu-menu-panel a")) {
|
|
menu.removeAttribute("open");
|
|
return;
|
|
}
|
|
if (!target.closest(".mu-menu")) menu.removeAttribute("open");
|
|
}
|
|
document.addEventListener("click", onDocClick);
|
|
return () => document.removeEventListener("click", onDocClick);
|
|
}, [pathname]);
|
|
|
|
return (
|
|
<header className="mu-topbar">
|
|
<div className="mu-container mu-topbar-inner">
|
|
<a className="mu-brand" href="/" aria-label="Atroute — หน้าแรก">
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img
|
|
className="mu-logo-img"
|
|
src="/image/icons/minibus-512x512.png"
|
|
alt="Atroute logo"
|
|
width={44}
|
|
height={44}
|
|
decoding="async"
|
|
/>
|
|
<span>
|
|
<span className="mu-brand-title">Atroute</span>
|
|
<span className="mu-brand-subtitle" data-i18n="brand.subtitle">
|
|
สถานีขนส่งผู้โดยสารจังหวัดอ่างทอง
|
|
</span>
|
|
</span>
|
|
</a>
|
|
|
|
<nav className="mu-nav" aria-label="เมนูหลัก">
|
|
{NAV.map((item) => (
|
|
<a
|
|
key={item.href}
|
|
className={isActive(pathname, item.href) ? "active" : undefined}
|
|
href={item.href}
|
|
data-i18n={item.key}
|
|
>
|
|
{item.label}
|
|
</a>
|
|
))}
|
|
</nav>
|
|
|
|
<details className="mu-menu" id="topnav" ref={menuRef}>
|
|
<summary className="mu-menu-btn" aria-label="เมนูหลัก" aria-haspopup="true">
|
|
<svg
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
strokeWidth={2}
|
|
strokeLinecap="round"
|
|
aria-hidden="true"
|
|
>
|
|
<line x1="4" x2="20" y1="6" y2="6" />
|
|
<line x1="4" x2="20" y1="12" y2="12" />
|
|
<line x1="4" x2="20" y1="18" y2="18" />
|
|
</svg>
|
|
</summary>
|
|
<div className="mu-menu-panel" role="menu">
|
|
{NAV.slice(0, 3).map((item) => (
|
|
<a key={item.href} href={item.href} data-i18n={item.key} role="menuitem">
|
|
{item.label}
|
|
</a>
|
|
))}
|
|
<a href="/check" data-i18n="cta.check" role="menuitem">
|
|
เช็กเวลาเดินรถ
|
|
</a>
|
|
{NAV.slice(3).map((item) => (
|
|
<a key={item.href} href={item.href} data-i18n={item.key} role="menuitem">
|
|
{item.label}
|
|
</a>
|
|
))}
|
|
</div>
|
|
</details>
|
|
|
|
<ThemeSwitch />
|
|
|
|
<div className="mu-lang-switch" role="group" aria-label="ภาษา / Language">
|
|
<button type="button" className="mu-lang-btn" data-i18n-switch="th">
|
|
ไทย
|
|
</button>
|
|
<button type="button" className="mu-lang-btn" data-i18n-switch="en">
|
|
EN
|
|
</button>
|
|
</div>
|
|
|
|
<a className="mu-action-btn" href="/check" data-i18n="cta.check">
|
|
เช็กเวลาเดินรถ
|
|
</a>
|
|
</div>
|
|
</header>
|
|
);
|
|
}
|