Files
breadcrumb-the-shire/web/js/components/app-theme-toggle.js
fs d9f07dcd63 fix: web/ quick wins — CSS cleanup, ARIA tabs, and file header comments
CSS:
- Remove duplicate li::before block in app-search.css
- Fix typo: search-reuslt → search-result in app-search.css
- Remove commented-out CSS rules in app-flash.css
- Add descriptive header comment to all 27 CSS component files

JS:
- Complete WAI-ARIA Tabs pattern: generate IDs, add aria-controls on
  tab buttons and aria-labelledby on tab panels (app-tabs.js)
- Add JSDoc header comments to ~33 JS files (core + components)
- Add explanatory block comment to app-boot.js (why classic IIFE)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 22:21:37 +01:00

121 lines
3.3 KiB
JavaScript

/**
* Theme switcher — light/dark toggle and menu with server-side persistence.
*/
import { optionalEl, warnOnce } from '../core/app-dom.js';
const setTheme = (theme) => {
document.documentElement.dataset.theme = theme;
};
const isDarkTheme = (theme) => theme && theme.startsWith('dark');
const setIcon = (iconEl, theme) => {
if (!iconEl) {return;}
iconEl.classList.remove('bi-sun-fill', 'bi-moon-stars-fill');
iconEl.classList.add(isDarkTheme(theme) ? 'bi-moon-stars-fill' : 'bi-sun-fill');
};
const updateTheme = async (menu, theme) => {
const url = menu.dataset.themeUrl;
const csrfKey = menu.dataset.csrfKey;
const csrfToken = menu.dataset.csrfToken;
if (!url || !csrfKey || !csrfToken) {
return true;
}
const body = new URLSearchParams({ theme, [csrfKey]: csrfToken });
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'X-Requested-With': 'fetch'
},
body
});
return response.ok;
};
const initThemeMenu = () => {
const menu = optionalEl('[data-theme-menu]');
if (!menu) {return;}
const icon = menu.querySelector('[data-theme-icon]');
const options = Array.from(menu.querySelectorAll('[data-theme-option]'));
if (!options.length) {
warnOnce('UI_EL_MISSING', 'Missing theme options', { module: 'theme-toggle' });
return;
}
let pending = false;
const getCurrent = () => document.documentElement.dataset.theme || '';
const setActive = (theme) => {
options.forEach((option) => {
const isActive = option.dataset.themeValue === theme;
option.classList.toggle('active', isActive);
if (isActive) {
option.setAttribute('aria-current', 'true');
} else {
option.removeAttribute('aria-current');
}
});
};
setIcon(icon, getCurrent());
setActive(getCurrent());
options.forEach((option) => {
option.addEventListener('click', async (event) => {
event.preventDefault();
if (pending) {return;}
const next = option.dataset.themeValue || '';
if (!next) {return;}
const current = getCurrent();
if (next === current) {return;}
pending = true;
setTheme(next);
setIcon(icon, next);
setActive(next);
const ok = await updateTheme(menu, next);
if (!ok) {
setTheme(current);
setIcon(icon, current);
setActive(current);
}
pending = false;
});
});
};
const initThemeToggle = () => {
const button = optionalEl('[data-theme-toggle]');
if (!button) {return;}
const icon = button.querySelector('i');
let pending = false;
const getCurrent = () => document.documentElement.dataset.theme || '';
setIcon(icon, getCurrent());
button.addEventListener('click', async () => {
if (pending) {return;}
const current = getCurrent();
const next = isDarkTheme(current) ? 'light' : 'dark';
pending = true;
setTheme(next);
setIcon(icon, next);
const ok = await updateTheme(button, next);
if (!ok) {
setTheme(current);
setIcon(icon, current);
}
pending = false;
});
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
initThemeMenu();
initThemeToggle();
});
} else {
initThemeMenu();
initThemeToggle();
}