Files
Bkk1234 1adc2a6cd0 navbar: theme icons + CTA order to match Next exactly
- theme.js: icon SVGs now carry width/height 18 (sun / moon / monitor) so the
  icon renders inside the 40px button and switches with the mode
- .mu-topbar-tools { order: 2 } — the CTA now sits right after the credits link
  and the theme/lang tools move to the far right, same visual order as
  web-at-transit (CTA 1144-1273 vs tools 1291-1405 at 1600px)
- icon sizing scoped (#theme-icon svg) so the language chevron stays 12px;
  lang button back to 59px, tools 107px == Next
- VERSION 2026.09.27.123
2026-11-04 01:43:18 +07:00

148 lines
6.2 KiB
JavaScript

/* ============================================================
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" width="18" height="18" 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" width="18" height="18" 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" width="18" height="18" 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');
if (on) b.setAttribute('data-active', '1'); else b.removeAttribute('data-active');
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);
})();