theme: dark mode + theme tokens (parity with web-at-transit)
- ui/theme.css: token set copied value-for-value from the Next app (--c-surface/-ink/-border/-brand/-tint/-glass + radius), light and dark blocks - ui/theme.js: 3-mode switch (light / dark / auto=prefers-color-scheme) with a header dropdown, aria-haspopup/menu, Esc/outside close, live matchMedia listener - index.html: pre-paint theme script (no flash), theme.css link before _shared.css - remapped ~290 literal colours in _shared.css/index.html/partials to var(--c-*) so both projects render from the same tokens; dark patch block covers the remaining decorative gradients - DESIGN.md: new section 3.2 (theme tokens + where to add colours) - VERSION 2026.09.27.114
This commit is contained in:
1 parent
bea8f0773e
commit
cdfd77ffff
18 files changed
+742
-329
No files matched your search
+146
@@ -0,0 +1,146 @@
|
||||
/* ============================================================
|
||||
theme.js — ธีม 3 โหมด (สว่าง / มืด / ตามอุปกรณ์)
|
||||
------------------------------------------------------------
|
||||
พอร์ตจาก web-at-transit/frontend/src/components/ThemeSwitch.tsx
|
||||
ให้พฤติกรรมเหมือนกันเป๊ะ:
|
||||
light = สว่าง · dark = มืด · auto = ตามธีมอุปกรณ์ (prefers-color-scheme)
|
||||
- เก็บที่ localStorage 'at_theme' · ตั้ง <html data-theme> + data-theme-mode
|
||||
- auto ฟัง event ของ matchMedia → เปลี่ยนทันทีที่ระบบสลับโหมด (ไม่ reload)
|
||||
- มีสคริปต์ inline ใน index.html ตั้งธีมก่อน paint (กันจอขาววูบ)
|
||||
- ปุ่มใน header = dropdown (aria-haspopup/menu, ปิดด้วย Esc/คลิกนอก)
|
||||
============================================================ */
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var KEY = 'at_theme';
|
||||
var DARK_QUERY = '(prefers-color-scheme: dark)';
|
||||
var MODES = ['light', 'dark', 'auto'];
|
||||
|
||||
var LABEL = { light: 'สว่าง', dark: 'มืด', auto: 'ตามอุปกรณ์' };
|
||||
var LABEL_EN = { light: 'Light', dark: 'Dark', auto: 'Device' };
|
||||
|
||||
function mq() { return window.matchMedia ? window.matchMedia(DARK_QUERY) : null; }
|
||||
function deviceTheme() { var m = mq(); return (m && m.matches) ? 'dark' : 'light'; }
|
||||
function resolve(mode) { return mode === 'auto' ? deviceTheme() : mode; }
|
||||
|
||||
function readMode() {
|
||||
try {
|
||||
var v = localStorage.getItem(KEY);
|
||||
if (MODES.indexOf(v) !== -1) return v;
|
||||
} catch (e) { /* localStorage ปิด */ }
|
||||
return 'auto';
|
||||
}
|
||||
|
||||
function isEn() { return window.I18N && I18N.getLang && I18N.getLang() === 'en'; }
|
||||
function label(mode) { return (isEn() ? LABEL_EN : LABEL)[mode]; }
|
||||
|
||||
/* ไอคอน 3 แบบ (inline — sprite ของ mock ไม่มีชุดนี้) */
|
||||
var ICON = {
|
||||
light: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M6.3 17.7l-1.4 1.4M19.1 4.9l-1.4 1.4"/></svg>',
|
||||
dark: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>',
|
||||
auto: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="2" y="4" width="20" height="14" rx="2"/><path d="M8 21h8M12 18v3"/></svg>'
|
||||
};
|
||||
|
||||
var currentMode = readMode();
|
||||
|
||||
function paintUi() {
|
||||
var btn = document.getElementById('theme-btn');
|
||||
var icon = document.getElementById('theme-icon');
|
||||
if (icon) icon.innerHTML = ICON[currentMode] || ICON.auto;
|
||||
if (btn) {
|
||||
btn.setAttribute('aria-label', 'ธีม: ' + label(currentMode) + ' / Theme');
|
||||
btn.title = 'ธีม: ' + label(currentMode);
|
||||
}
|
||||
var items = document.querySelectorAll('#theme-menu [data-theme-mode]');
|
||||
Array.prototype.forEach.call(items, function (b) {
|
||||
var on = b.getAttribute('data-theme-mode') === currentMode;
|
||||
b.setAttribute('aria-checked', on ? 'true' : 'false');
|
||||
var lbl = b.querySelector('.mu-theme-label');
|
||||
if (lbl) lbl.textContent = label(b.getAttribute('data-theme-mode'));
|
||||
});
|
||||
}
|
||||
|
||||
function apply(mode) {
|
||||
if (MODES.indexOf(mode) === -1) mode = 'auto';
|
||||
currentMode = mode;
|
||||
var resolved = resolve(mode);
|
||||
var html = document.documentElement;
|
||||
html.dataset.theme = resolved;
|
||||
html.dataset.themeMode = mode;
|
||||
html.style.colorScheme = resolved;
|
||||
paintUi();
|
||||
}
|
||||
|
||||
function choose(mode) {
|
||||
try { localStorage.setItem(KEY, mode); } catch (e) { /* เพิกเฉย */ }
|
||||
apply(mode);
|
||||
}
|
||||
|
||||
/* auto: ตามอุปกรณ์แบบสด ๆ */
|
||||
function watchDevice() {
|
||||
var m = mq();
|
||||
if (!m) return;
|
||||
var onChange = function () { if (currentMode === 'auto') apply('auto'); };
|
||||
if (typeof m.addEventListener === 'function') m.addEventListener('change', onChange);
|
||||
else if (typeof m.addListener === 'function') m.addListener(onChange);
|
||||
}
|
||||
|
||||
function wireMenu() {
|
||||
var wrap = document.getElementById('theme-switch');
|
||||
var btn = document.getElementById('theme-btn');
|
||||
var menu = document.getElementById('theme-menu');
|
||||
if (!wrap || !btn || !menu) return;
|
||||
|
||||
function setOpen(on) {
|
||||
menu.hidden = !on;
|
||||
btn.setAttribute('aria-expanded', on ? 'true' : 'false');
|
||||
}
|
||||
|
||||
btn.addEventListener('click', function (e) {
|
||||
e.stopPropagation();
|
||||
setOpen(menu.hidden);
|
||||
});
|
||||
|
||||
menu.addEventListener('click', function (e) {
|
||||
var item = e.target.closest('[data-theme-mode]');
|
||||
if (!item) return;
|
||||
choose(item.getAttribute('data-theme-mode'));
|
||||
setOpen(false);
|
||||
btn.focus();
|
||||
});
|
||||
|
||||
document.addEventListener('click', function (e) {
|
||||
if (!wrap.contains(e.target)) setOpen(false);
|
||||
});
|
||||
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if (e.key === 'Escape' && !menu.hidden) {
|
||||
setOpen(false);
|
||||
btn.focus();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
window.AtTheme = {
|
||||
mode: function () { return currentMode; },
|
||||
resolved: function () { return resolve(currentMode); },
|
||||
set: choose,
|
||||
apply: apply,
|
||||
label: label,
|
||||
refresh: paintUi
|
||||
};
|
||||
|
||||
/* ตั้งธีมทันที (สคริปต์ใน head ตั้ง data-theme ไปแล้ว — ที่นี่แค่ซิงก์ state) */
|
||||
apply(currentMode);
|
||||
watchDevice();
|
||||
wireMenu();
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', paintUi);
|
||||
} else {
|
||||
paintUi();
|
||||
}
|
||||
|
||||
/* เปลี่ยนภาษา → ป้ายในเมนูธีมต้องเปลี่ยนตาม */
|
||||
window.addEventListener('langchange', paintUi);
|
||||
})();
|
||||
Reference in new issue
Block a user