theme & language: device-following auto theme, Thai default, dropdown sync

- ThemeSwitch: auto now follows the device (prefers-color-scheme) with a live
  matchMedia listener instead of a 06:00-18:00 clock rule; labels become
  'ตามอุปกรณ์ / Device'; auto icon is a monitor
- layout.tsx pre-paint script resolved with matchMedia (no theme flash)
- i18n: default language is Thai (no navigator guess) in lib/ui/i18n.ts;
  new 'i18nready' event so LangSwitch syncs its label after runtime boot
  (button previously stuck on ไทย when the page loaded in English)
- VERSION 2026.10.24.5
This commit is contained in:
Bkk1234 committed 2026-11-04 01:17:39 +07:00
1 parent 4b5a4d33d3
commit 29b126b188
6 files changed
+54 -30

No files matched your search

+1 -1
View File
@@ -133,7 +133,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
{/* ตั้งธีมก่อนวาดหน้าจอ (กันจอขาววูบตอนเปิดแอปในโหมดมืด) */}
<script
dangerouslySetInnerHTML={{
__html: `(function(){try{var m=localStorage.getItem('at_theme')||'auto';var h=new Date().getHours();var r=m==='auto'?(h>=6&&h<18?'light':'dark'):m;document.documentElement.dataset.theme=r;document.documentElement.dataset.themeMode=m;document.documentElement.style.colorScheme=r;}catch(e){}})();`,
__html: `(function(){try{var m=localStorage.getItem('at_theme')||'auto';var d=window.matchMedia&&window.matchMedia('(prefers-color-scheme: dark)').matches;var r=m==='auto'?(d?'dark':'light'):m;document.documentElement.dataset.theme=r;document.documentElement.dataset.themeMode=m;document.documentElement.style.colorScheme=r;}catch(e){}})();`,
}}
/>
<script src="/vendor/leaflet/leaflet.js" defer />
+8 -2
View File
@@ -49,12 +49,18 @@ export default function LangSwitch() {
const boxRef = useRef<HTMLDivElement>(null);
const btnRef = useRef<HTMLButtonElement>(null);
/* sync กับ I18N — หลัง mount และทุกครั้งที่ภาษาเปลี่ยน */
/* sync กับ I18N — หลัง mount และทุกครั้งที่ภาษาเปลี่ยน
• langchange = ผู้ใช้สลับภาษา
• i18nready = runtime โหลด i18n เสร็จ (ตอน mount I18N อาจยังไม่มี → ปุ่มจะค้างภาษาเดิม) */
useEffect(() => {
setLang(currentLang());
const onLang = () => setLang(currentLang());
window.addEventListener("langchange", onLang);
return () => window.removeEventListener("langchange", onLang);
window.addEventListener("i18nready", onLang);
return () => {
window.removeEventListener("langchange", onLang);
window.removeEventListener("i18nready", onLang);
};
}, []);
/* ปิดเมนู: คลิกนอก / Esc */
+37 -22
View File
@@ -3,16 +3,15 @@
*
* light = สว่าง
* dark = มืด
* auto = ตามเวลาเครื่องโทรศัพท์ (06:00–18:00 สว่าง, นอกนั้นมืด)
* auto = ตามธีมของอุปกรณ์ (prefers-color-scheme) ← ค่าเริ่มต้น
*
* หลักการ
* - เก็บค่าที่ localStorage 'at_theme' ({ mode, at })
* - เก็บค่าที่ localStorage 'at_theme' ("light" | "dark" | "auto")
* - ตั้ง <html data-theme="light|dark"> ให้ CSS ใน theme.css ทำงาน
* - โหมด auto ตรวจซ้ำทุก 1 นาที + ทุกครั้งที่กลับเข้าแอป (visibilitychange)
* จึงเปลี่ยนเองแม้ผู้ใช้เปิดแอปค้างไว้ข้ามช่วงหัวค่ำ
* - โหมด auto ฟัง event ของ matchMedia → เปลี่ยนตามอุปกรณ์ทันทีที่ผู้ใช้
* สลับโหมดมืด/สว่างที่ระบบ (ไม่ต้อง reload, ไม่ผูกกับเวลาเครื่อง)
* - กันจอขาววูบ: มีสคริปต์ inline ใน layout.tsx ที่ตั้ง data-theme ก่อน paint
*
* ค่า default ของเครื่องนี้: auto
* (ใช้ logic เดียวกันกับที่นี่)
*/
"use client";
@@ -21,13 +20,20 @@ import { useCallback, useEffect, useRef, useState } from "react";
export type ThemeMode = "light" | "dark" | "auto";
const LS_KEY = "at_theme";
const DAY_START = 6; // 06:00
const DAY_END = 18; // 18:00
const DARK_QUERY = "(prefers-color-scheme: dark)";
/** เวลาที่ใช้ตัดสิน: 06:00–17:59 = สว่าง, 18:00–05:59 = มืด */
export function themeForNow(d = new Date()): "light" | "dark" {
const h = d.getHours();
return h >= DAY_START && h < DAY_END ? "light" : "dark";
/** ธีมที่อุปกรณ์กำลังใช้อยู่ */
export function deviceTheme(): "light" | "dark" {
try {
return window.matchMedia(DARK_QUERY).matches ? "dark" : "light";
} catch {
return "light";
}
}
/** ค่าที่ใช้วาดจริง (auto → ตามอุปกรณ์) */
export function resolveTheme(mode: ThemeMode): "light" | "dark" {
return mode === "auto" ? deviceTheme() : mode;
}
function readMode(): ThemeMode {
@@ -41,7 +47,7 @@ function readMode(): ThemeMode {
}
function applyMode(mode: ThemeMode) {
const resolved = mode === "auto" ? themeForNow() : mode;
const resolved = resolveTheme(mode);
document.documentElement.dataset.theme = resolved;
document.documentElement.style.colorScheme = resolved;
// ให้ CSS รู้ว่ากำลังใช้โหมดไหน (สำหรับไอคอน active)
@@ -51,13 +57,13 @@ function applyMode(mode: ThemeMode) {
const LABEL: Record<ThemeMode, string> = {
light: "สว่าง",
dark: "มืด",
auto: "ตามเวลา",
auto: "ตามอุปกรณ์",
};
const LABEL_EN: Record<ThemeMode, string> = {
light: "Light",
dark: "Dark",
auto: "Auto",
auto: "Device",
};
function Icon({ mode, size = 18 }: { mode: ThemeMode; size?: number }) {
@@ -87,8 +93,8 @@ function Icon({ mode, size = 18 }: { mode: ThemeMode; size?: number }) {
);
return (
<svg {...common}>
<circle cx="12" cy="12" r="9" />
<path d="M12 7v5l3 2" />
<rect x="2" y="4" width="20" height="14" rx="2" />
<path d="M8 21h8M12 18v3" />
</svg>
);
}
@@ -123,17 +129,26 @@ export default function ThemeSwitch() {
btnRef.current?.focus();
}, []);
/* โหมด auto: ตรวจเวลาซ้ำทุกนาที + ตอนกลับเข้าแอป */
/* โหมด auto: ตามธีมอุปกรณ์ — ฟัง event แทนการตั้งเวลา
อุปกรณ์สลับโหมดมืด/สว่าง (ตั้งเวลาเอง/กดที่ระบบ) → หน้าอัปเดตทันที */
useEffect(() => {
if (mode !== "auto") return;
const tick = () => applyMode("auto");
const id = window.setInterval(tick, 60_000);
const mq = window.matchMedia(DARK_QUERY);
const onChange = () => applyMode("auto");
/* Safari < 14 ใช้ addListener */
if (typeof mq.addEventListener === "function") mq.addEventListener("change", onChange);
else mq.addListener(onChange);
const onVisible = () => {
if (!document.hidden) tick();
if (!document.hidden) applyMode("auto");
};
document.addEventListener("visibilitychange", onVisible);
return () => {
window.clearInterval(id);
if (typeof mq.removeEventListener === "function") mq.removeEventListener("change", onChange);
else mq.removeListener(onChange);
document.removeEventListener("visibilitychange", onVisible);
};
}, [mode]);
+5 -2
View File
@@ -37,8 +37,9 @@
var saved = localStorage.getItem(LS_KEY);
if (saved && SUPPORTED.indexOf(saved) !== -1) return saved;
} catch (e) { /* localStorage ไม่พร้อมใช้งาน */ }
var nav = (navigator.language || BASE).toLowerCase();
return nav.indexOf('en') === 0 ? 'en' : BASE;
/* ภาษาเริ่มต้น = ไทยเสมอ (ไม่เดาจาก navigator.language)
ผู้ใช้เปลี่ยนเองได้ที่ปุ่ม dropdown แล้วเลือกจำไว้ใน at_lang */
return BASE;
}
/* ---------- โหลดไฟล์ภาษา (cache ต่อภาษา · ล้มเหลว = {} ไม่ให้พัง) ---------- */
@@ -131,6 +132,8 @@
Promise.all([dict(BASE), dict(current)]).then(function () {
apply();
resolveReady();
/* บอก UI ที่ mount ไปก่อนภาษาโหลดเสร็จ (เช่นปุ่ม dropdown) ให้ sync สถานะ */
window.dispatchEvent(new CustomEvent('i18nready', { detail: current }));
});
})();
export {};