Declutter navbar: one button per tool (theme, language)

The topbar carried five small buttons in a row (3 theme + 2 language).
Replaced with two single buttons that open their own popover menu:

- ThemeSwitch: single icon button showing the active mode, menu with
  light/dark/auto as menuitemradio
- LangSwitch: new component showing the active code (ไทย / EN), menu
  keeps the existing [data-i18n-switch] mechanism from i18n.ts
- .mu-topbar-tools wrapper, 8px gap (WCAG 2.5.8), 40px buttons / 44px on touch
- popovers close on select, outside click and Escape (focus returns to button)

Also fixed two bugs found on /stops:
- .mu-btn.outline had no CSS rule, so the browser painted a light grey
  background that ignored the dark theme
- deleted the unused StopSearch component which still carried the old
  number pad and its delete key, plus the now-unused stops.padDelete key

Verified at 375px: no horizontal overflow, buttons 40x40 and 59x40,
8px gap; menus open/close correctly in both themes.
This commit is contained in:
Bkk1234 committed 2026-10-23 23:37:44 +07:00
1 parent bf42076b52
commit cb4905a12d
11 files changed
+338 -283

No files matched your search

+123
View File
@@ -0,0 +1,123 @@
/**
* Atroute — ปุ่มสลับภาษา (ปุ่มเดียว + เมนู)
*
* เดิม: ปุ่ม "ไทย" และ "EN" วางคู่กันในแถบนำทาง → navbar แน่น
* ตอนนี้: ปุ่มเดียวแสดงรหัสภาษาที่ใช้อยู่ (ไทย / EN) กดแล้วเปิดเมนู
*
* การสลับภาษายังใช้กลไกเดิมของ i18n.ts — ปุ่มในเมนูมี [data-i18n-switch]
* ซึ่ง i18n ฟังคลิกอยู่แล้ว จึงไม่ต้องเขียน logic แปลภาษาใหม่
*
* Accessibility: aria-haspopup/aria-expanded · role="menuitemradio" ·
* ปิดด้วย Esc หรือคลิกนอกเมนู · คืนโฟกัสที่ปุ่มเมื่อปิด
*/
"use client";
import { useEffect, useRef, useState } from "react";
type Lang = "th" | "en";
const SHORT: Record<Lang, string> = { th: "ไทย", en: "EN" };
const FULL: Record<Lang, string> = { th: "ภาษาไทย", en: "English" };
function currentLang(): Lang {
const w = window as unknown as { I18N?: { getLang?: () => string } };
const v = w.I18N?.getLang?.();
return v === "en" ? "en" : "th";
}
function Chevron() {
return (
<svg
viewBox="0 0 24 24"
width={12}
height={12}
fill="none"
stroke="currentColor"
strokeWidth={2.5}
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
<path d="m6 9 6 6 6-6" />
</svg>
);
}
export default function LangSwitch() {
const [lang, setLang] = useState<Lang>("th");
const [open, setOpen] = useState(false);
const boxRef = useRef<HTMLDivElement>(null);
const btnRef = useRef<HTMLButtonElement>(null);
/* sync กับ I18N — หลัง mount และทุกครั้งที่ภาษาเปลี่ยน */
useEffect(() => {
setLang(currentLang());
const onLang = () => setLang(currentLang());
window.addEventListener("langchange", onLang);
return () => window.removeEventListener("langchange", onLang);
}, []);
/* ปิดเมนู: คลิกนอก / Esc */
useEffect(() => {
if (!open) return;
const onDown = (e: MouseEvent) => {
if (!boxRef.current?.contains(e.target as Node)) setOpen(false);
};
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") {
setOpen(false);
btnRef.current?.focus();
}
};
document.addEventListener("mousedown", onDown);
document.addEventListener("keydown", onKey);
return () => {
document.removeEventListener("mousedown", onDown);
document.removeEventListener("keydown", onKey);
};
}, [open]);
const langs: Lang[] = ["th", "en"];
return (
<div className="mu-pop" ref={boxRef}>
<button
type="button"
ref={btnRef}
className="mu-pop-btn mu-pop-btn-lang"
aria-haspopup="menu"
aria-expanded={open}
aria-label={`ภาษา: ${FULL[lang]} / Language`}
title={`ภาษา: ${FULL[lang]}`}
onClick={() => setOpen((v) => !v)}
>
<span>{SHORT[lang]}</span>
<Chevron />
</button>
{open ? (
<div className="mu-pop-panel" role="menu" aria-label="ภาษา / Language">
{langs.map((l) => (
<button
key={l}
type="button"
role="menuitemradio"
aria-checked={lang === l}
className="mu-pop-item"
data-active={lang === l ? "1" : undefined}
data-i18n-switch={l}
onClick={() => {
setLang(l);
setOpen(false);
btnRef.current?.focus();
}}
>
<span className="mu-pop-item-th">{FULL[l]}</span>
<span className="mu-pop-item-en">{l.toUpperCase()}</span>
</button>
))}
</div>
) : null}
</div>
);
}
+5 -9
View File
@@ -3,6 +3,7 @@
import { useEffect, useRef } from "react";
import { usePathname } from "next/navigation";
import ThemeSwitch from "@/components/ThemeSwitch";
import LangSwitch from "@/components/LangSwitch";
const NAV = [
{ href: "/", key: "nav.home", label: "หน้าแรก" },
@@ -105,15 +106,10 @@ export default function SiteHeader() {
</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>
{/* ปุ่มลัด: ปุ่มละเรื่อง (ธีม 1 ปุ่ม · ภาษา 1 ปุ่ม) — ลดความแน่นของ navbar */}
<div className="mu-topbar-tools">
<ThemeSwitch />
<LangSwitch />
</div>
<a className="mu-action-btn" href="/check" data-i18n="cta.check">
-177
View File
@@ -1,177 +0,0 @@
"use client";
import { useMemo, useState } from "react";
import { allBusStops, findBusStopByCode, nearestBusStops, type BusStop } from "@/lib/busstops";
import { Icon } from "@/components/Icons";
import { useLang } from "@/components/LangProvider";
type Mode = "code" | "place";
function BusStopCard({ stop, distM }: { stop: BusStop; distM?: number }) {
const { lang } = useLang();
return (
<div className="busstop-result">
<div className="busstop-result__head">
<span className="busstop-result__km">{stop.kilometer}</span>
<span className="busstop-result__name">{stop.route_name}</span>
{distM != null && (
<span className="busstop-result__dist">
{distM < 1000 ? `${Math.round(distM)} m` : `${(distM / 1000).toFixed(1)} km`}
</span>
)}
</div>
<p className="busstop-result__meta">
{stop.position} · {stop.type}
</p>
{(stop["buses-line"] ?? []).length > 0 && (
<div className="busstop-result__chips">
{(stop["buses-line"] ?? []).map((r) => (
<span key={r} className="chip-soft chip-soft--purple">{lang === "en" ? "Route" : "สาย"} {r}</span>
))}
</div>
)}
</div>
);
}
export function StopSearch() {
const { t, lang } = useLang();
const [mode, setMode] = useState<Mode>("code");
const [code, setCode] = useState("");
const [query, setQuery] = useState("");
const [nearby, setNearby] = useState<{ stop: BusStop; distM: number }[] | null>(null);
const [geoErr, setGeoErr] = useState(false);
const pad = (d: string) => {
if (code.length >= 5) return;
setCode(code + d);
setNearby(null);
};
const backspace = () => setCode(code.slice(0, -1));
const clearAll = () => {
setCode("");
setQuery("");
setNearby(null);
setGeoErr(false);
};
const codeResult = code.length === 5 ? findBusStopByCode(code) : undefined;
const placeResults = useMemo(() => {
const q = query.trim();
if (q.length < 2) return [];
return allBusStops()
.filter(
(b) =>
b.route_name.includes(q) || b.type.includes(q) || b.position.includes(q),
)
.slice(0, 20);
}, [query]);
const nearMe = () => {
if (typeof navigator === "undefined" || !navigator.geolocation) {
setGeoErr(true);
return;
}
navigator.geolocation.getCurrentPosition(
(pos) => setNearby(nearestBusStops(pos.coords.latitude, pos.coords.longitude, 8)),
() => setGeoErr(true),
{ timeout: 10000 },
);
};
const N = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
return (
<div className="stop-search">
<div className="stop-search__tabs" role="tablist" aria-label="รูปแบบการค้นหา">
<button
type="button"
className={`stop-search__tab ${mode === "code" ? "on" : ""}`}
onClick={() => setMode("code")}
>
<Icon name="map-pin" size={14} className="icon" /> {t("stops.modeCode")}
</button>
<button
type="button"
className={`stop-search__tab ${mode === "place" ? "on" : ""}`}
onClick={() => setMode("place")}
>
<Icon name="search" size={14} className="icon" /> {t("stops.modePlace")}
</button>
</div>
{mode === "code" ? (
<div className="stop-search__panel">
<div className="code-boxes" aria-label={t("stops.codePh")}>
{[0, 1, 2, 3, 4].map((i) => (
<span key={i} className={`code-cell ${i < code.length ? "filled" : ""}`}>
{code[i] ?? ""}
</span>
))}
</div>
<div className="numpad">
{N.map((n) => (
<button key={n} type="button" onClick={() => pad(String(n))}>
{n}
</button>
))}
<button type="button" onClick={backspace} aria-label={t("stops.padDelete")}>
<Icon name="arrow-right" size={16} className="icon" style={{ transform: "rotate(180deg)" }} />
</button>
</div>
<div className="stop-search__actions">
<button type="button" className="mu-btn ghost" onClick={clearAll}>
{t("stops.reset")}
</button>
<button type="button" className="mu-btn ghost" onClick={nearMe}>
<Icon name="map-pin" size={14} className="icon" /> {t("map.useMyLocation")}
</button>
</div>
</div>
) : (
<div className="stop-search__panel">
<div className="stop-search__field">
<Icon name="search" size={16} className="icon" />
<input
type="search"
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder={t("stops.searchPh")}
/>
</div>
{placeResults.length > 0 && (
<ul className="stop-search__suggest">
{placeResults.map((b, i) => (
<li key={`${b.kilometer}-${i}`}>
<button type="button" onClick={() => setCode(b.kilometer.replace("+", ""))}>
<span className="km">{b.kilometer}</span>
<span className="name">{b.route_name}</span>
<span className="meta">{b.position}</span>
</button>
</li>
))}
</ul>
)}
</div>
)}
{geoErr && (
<p className="stop-search__err">
{lang === "en" ? "Cannot access your location." : "ไม่สามารถเข้าถึงตำแหน่งของคุณได้"}
</p>
)}
{codeResult && <BusStopCard stop={codeResult} />}
{nearby && nearby.length > 0 && (
<div className="stop-search__nearby">
<h3>{t("map.nearestTitle")}</h3>
{nearby.map((n, i) => (
<BusStopCard key={`${n.stop.kilometer}-${i}`} stop={n.stop} distM={n.distM} />
))}
</div>
)}
</div>
);
}
+74 -20
View File
@@ -16,7 +16,7 @@
*/
"use client";
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
export type ThemeMode = "light" | "dark" | "auto";
@@ -54,11 +54,17 @@ const LABEL: Record<ThemeMode, string> = {
auto: "ตามเวลา",
};
function Icon({ mode }: { mode: ThemeMode }) {
const LABEL_EN: Record<ThemeMode, string> = {
light: "Light",
dark: "Dark",
auto: "Auto",
};
function Icon({ mode, size = 18 }: { mode: ThemeMode; size?: number }) {
const common = {
viewBox: "0 0 24 24",
width: 18,
height: 18,
width: size,
height: size,
fill: "none",
stroke: "currentColor",
strokeWidth: 2,
@@ -89,6 +95,9 @@ function Icon({ mode }: { mode: ThemeMode }) {
export default function ThemeSwitch() {
const [mode, setMode] = useState<ThemeMode>("auto");
const [open, setOpen] = useState(false);
const boxRef = useRef<HTMLDivElement>(null);
const btnRef = useRef<HTMLButtonElement>(null);
/* อ่านค่าที่เก็บไว้หลัง mount (กัน hydration mismatch) */
useEffect(() => {
@@ -98,13 +107,20 @@ export default function ThemeSwitch() {
}, []);
const choose = useCallback((m: ThemeMode) => {
/* ปิดเมนูก่อน — ถ้า applyMode() ล้ม เมนูต้องไม่ค้างเปิด */
setOpen(false);
setMode(m);
try {
localStorage.setItem(LS_KEY, m);
} catch {
/* เพิกเฉย */
}
applyMode(m);
try {
applyMode(m);
} catch {
/* เขียนธีมไม่สำเร็จ — ไม่ให้ UI พัง */
}
btnRef.current?.focus();
}, []);
/* โหมด auto: ตรวจเวลาซ้ำทุกนาที + ตอนกลับเข้าแอป */
@@ -122,24 +138,62 @@ export default function ThemeSwitch() {
};
}, [mode]);
/* ปิดเมนู: คลิกนอก / Esc */
useEffect(() => {
if (!open) return;
const onDown = (e: MouseEvent) => {
if (!boxRef.current?.contains(e.target as Node)) setOpen(false);
};
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") {
setOpen(false);
btnRef.current?.focus();
}
};
document.addEventListener("mousedown", onDown);
document.addEventListener("keydown", onKey);
return () => {
document.removeEventListener("mousedown", onDown);
document.removeEventListener("keydown", onKey);
};
}, [open]);
const modes: ThemeMode[] = ["light", "dark", "auto"];
return (
<div className="mu-theme-switch" role="radiogroup" aria-label="ธีม / Theme">
{modes.map((m) => (
<button
key={m}
type="button"
role="radio"
aria-checked={mode === m}
aria-label={LABEL[m]}
title={LABEL[m]}
className={"mu-theme-btn" + (mode === m ? " active" : "")}
onClick={() => choose(m)}
>
<Icon mode={m} />
</button>
))}
<div className="mu-pop" ref={boxRef}>
<button
type="button"
ref={btnRef}
className="mu-pop-btn"
aria-haspopup="menu"
aria-expanded={open}
aria-label={`ธีม: ${LABEL[mode]} / Theme`}
title={`ธีม: ${LABEL[mode]}`}
onClick={() => setOpen((v) => !v)}
>
<Icon mode={mode} />
</button>
{open ? (
<div className="mu-pop-panel" role="menu" aria-label="ธีม / Theme">
{modes.map((m) => (
<button
key={m}
type="button"
role="menuitemradio"
aria-checked={mode === m}
className="mu-pop-item"
data-active={mode === m ? "1" : undefined}
onClick={() => choose(m)}
>
<Icon mode={m} size={16} />
<span className="mu-pop-item-th">{LABEL[m]}</span>
<span className="mu-pop-item-en">{LABEL_EN[m]}</span>
</button>
))}
</div>
) : null}
</div>
);
}
-1
View File
@@ -138,7 +138,6 @@
"stops.codeFull": "Stop code:",
"stops.placePh": "Ang Thong Hospital",
"stops.placeHint": "Type a place name such as a hospital or land office, then pick a match",
"stops.padDelete": "Del",
"stops.reset": "Clear",
"stops.section": "Section",
"stops.showing": "Showing {a} of {b} stops",
-1
View File
@@ -138,7 +138,6 @@
"stops.codeFull": "รหัสป้าย:",
"stops.placePh": "โรงพยาบาลอ่างทอง",
"stops.placeHint": "พิมพ์ชื่อสถานที่สำคัญ เช่น โรงพยาบาล สำนักงานที่ดิน แล้วเลือกรายการที่ตรงกัน",
"stops.padDelete": "ลบ",
"stops.reset": "ล้างค่า",
"stops.section": "ตอน",
"stops.showing": "แสดง {a} จาก {b} ป้าย",
+117 -72
View File
@@ -417,79 +417,7 @@ body:has(.mu-site-footer) .mu-footer { display: none; }
padding: 3px;
flex-shrink: 0;
}
.mu-lang-btn {
border: 0;
background: transparent;
color: var(--c-brand-ink);
font-family: inherit;
font-weight: 700;
font-size: .72rem;
line-height: 1;
padding: 6px 10px;
border-radius: var(--radius-pill);
cursor: pointer;
transition: background .15s ease, color .15s ease;
}
.mu-lang-btn:hover { color: var(--c-brand-ink); }
.mu-lang-btn.active {
background: var(--c-surface);
color: var(--c-brand-ink);
box-shadow: 0 1px 4px rgba(63, 43, 94, .18);
}
/* ---------- ตัวสลับธีม (สว่าง / มืด / ตามเวลา) ---------- */
.mu-theme-switch {
display: inline-flex;
align-items: center;
gap: 2px;
background: var(--c-tint-purple);
border: 1px solid #e4dcf2;
border-radius: var(--radius-pill);
padding: 3px;
flex-shrink: 0;
}
.mu-theme-btn {
display: inline-flex;
align-items: center;
justify-content: center;
width: 34px;
height: 34px;
min-width: 34px;
border: 0;
border-radius: var(--radius-pill);
background: transparent;
color: var(--c-brand-ink);
cursor: pointer;
transition: background .15s ease, color .15s ease;
}
.mu-theme-btn:hover { color: var(--c-brand-ink); }
.mu-theme-btn.active {
background: var(--c-surface);
color: var(--c-brand-ink);
box-shadow: 0 1px 4px rgba(63, 43, 94, .18);
}
/* โฟกัสคีย์บอร์ดต้องเห็นชัด (WCAG 2.4.7) */
.mu-theme-btn:focus-visible,
.mu-lang-btn:focus-visible {
outline: 2px solid var(--c-focus);
outline-offset: 2px;
}
:root[data-theme="dark"] .mu-theme-switch,
:root[data-theme="dark"] .mu-lang-switch {
background: var(--c-surface-3);
border-color: var(--c-border);
}
:root[data-theme="dark"] .mu-theme-btn,
:root[data-theme="dark"] .mu-lang-btn { color: var(--c-ink-2); }
:root[data-theme="dark"] .mu-theme-btn:hover,
:root[data-theme="dark"] .mu-lang-btn:hover { color: var(--c-ink); }
:root[data-theme="dark"] .mu-lang-btn { background: transparent; }
/* จอสัมผัส: ขยายเป้ากดให้ถึง 44px (WCAG 2.5.5) */
@media (pointer: coarse) {
.mu-theme-btn { width: 40px; height: 40px; min-width: 40px; }
.mu-theme-switch { padding: 2px; }
}
/* ============================================================
ปุ่ม + ฟอร์มคอนโทรล (shared — ทุกหน้าใช้ร่วมกัน)
@@ -548,6 +476,18 @@ body:has(.mu-site-footer) .mu-footer { display: none; }
outline-offset: 2px;
}
/* ปุ่มขอบ (outline) — ตัวอักษร/ขอบม่วง พื้นโปร่งใส
เดิมไม่มีกฎนี้ → เบราว์เซอร์ใส่พื้นเทาอ่อนเอง ทำให้โหมดมืดหลุดธีม */
.mu-btn.outline {
background: transparent;
border-color: var(--c-brand-ink);
color: var(--c-brand-ink);
}
.mu-btn.outline:hover {
background: var(--c-tint-purple);
border-color: var(--c-brand-ink);
}
/* ---------- ปุ่มติดตั้งแอป (PWA — inject จาก pwa.js) ---------- */
.mu-install-btn {
position: fixed;
@@ -665,3 +605,108 @@ body:has(.mu-site-footer) .mu-footer { display: none; }
@media (pointer: coarse) {
.mu-share-btn { min-height: 48px; }
}
/* ============================================================
ปุ่มเครื่องมือในแถบนำทาง — ปุ่มละเรื่อง (ไม่ใช่กลุ่มปุ่มเรียง)
เดิม: ธีม 3 ปุ่ม + ภาษา 2 ปุ่ม = 5 ปุ่มในแถวเดียว → navbar แน่น
ตอนนี้: ธีม 1 ปุ่ม · ภาษา 1 ปุ่ม แต่ละปุ่มกดแล้วเปิดเมนูของตัวเอง
ตรงสเกลเดียวกับ .mu-btn (radius 8px · min-height 44px บนจอสัมผัส)
============================================================ */
.mu-topbar-tools {
display: flex;
align-items: center;
gap: 8px; /* WCAG 2.5.8 — เว้นระหว่างเป้ากด ≥ 8px */
flex-shrink: 0;
}
.mu-pop { position: relative; display: inline-flex; }
.mu-pop-btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 4px;
min-width: 40px;
height: 40px;
padding: 0 8px;
border: 1px solid var(--c-border);
border-radius: var(--radius);
background: var(--c-surface);
color: var(--c-brand-ink);
font-family: inherit;
font-size: .78rem;
font-weight: 700;
line-height: 1;
cursor: pointer;
transition: border-color .15s ease, background .15s ease;
}
.mu-pop-btn-lang { min-width: 56px; padding: 0 10px; }
.mu-pop-btn:hover { border-color: #d8c9f2; background: var(--c-surface-2); }
.mu-pop-btn:active { transform: translateY(1px); }
.mu-pop-btn:focus-visible {
outline: 2px solid var(--c-focus);
outline-offset: 2px;
}
.mu-pop-btn[aria-expanded="true"] {
border-color: var(--c-brand-ink);
background: var(--c-surface-2);
}
/* เมนูที่กางออก */
.mu-pop-panel {
position: absolute;
top: calc(100% + 6px);
right: 0;
z-index: 1200; /* เหนือ topbar (1100) แต่ใต้ visitor dialog (3000) */
min-width: 168px;
padding: 6px;
border: 1px solid var(--c-border);
border-radius: var(--radius);
background: var(--c-surface);
box-shadow: 0 10px 24px var(--c-shadow);
}
.mu-pop-item {
display: flex;
align-items: center;
gap: 8px;
width: 100%;
min-height: 40px;
padding: 8px 10px;
border: 0;
border-radius: var(--radius-sm);
background: transparent;
color: var(--c-ink);
font-family: inherit;
font-size: .85rem;
font-weight: 600;
text-align: start;
cursor: pointer;
}
.mu-pop-item:hover { background: var(--c-surface-2); }
.mu-pop-item:focus-visible {
outline: 2px solid var(--c-focus);
outline-offset: -2px;
}
.mu-pop-item[data-active="1"] {
background: var(--c-tint-purple);
color: var(--c-brand-ink);
font-weight: 700;
}
.mu-pop-item-th { flex: 1 1 auto; }
.mu-pop-item-en {
flex: 0 0 auto;
font-size: .72rem;
font-weight: 600;
color: var(--c-ink-2);
letter-spacing: .04em;
}
.mu-pop-item[data-active="1"] .mu-pop-item-en { color: var(--c-brand-ink); }
/* จอสัมผัส: เป้ากด 44px */
@media (pointer: coarse) {
.mu-pop-btn { min-width: 44px; height: 44px; }
.mu-pop-btn-lang { min-width: 60px; }
.mu-pop-item { min-height: 44px; }
}