Files
wibosim/server/public/js/theme.js
T

71 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Farbschema: „Dunkel“, „Hell“, „Börse“ (dunkler Terminal-Look wie die Startseite) oder „System“ (folgt prefers-color-scheme).
// „Börse“ setzt data-theme="dark" plus data-look="boerse" – alle dunklen Regeln gelten, der Look legt sich darüber.
// Die Wahl liegt in localStorage (wibo.theme); angewendet wird sie als data-theme="light"|"dark" auf <html>.
// Den ersten Stand setzt js/theme-init.js bereits im <head>. Bei jedem Wechsel feuert document das Ereignis
// „wibo:theme“ – die Canvas-Diagramme zeichnen sich daraufhin neu.
import { store, $$ } from './util.js';
import { icon } from './icons.js';
export const THEMES = ['system', 'light', 'dark', 'boerse'];
const LABEL = { system: 'System', light: 'Hell', dark: 'Dunkel', boerse: 'Börse' };
// Symbole aus dem Icon-System (Halbkreis = System, Sonne = Hell, Mond = Dunkel)
const ICON = { system: icon('contrast', { size: 20 }), light: icon('sun', { size: 20 }), dark: icon('moon', { size: 20 }), boerse: icon('chart-up', { size: 20 }) };
const mq = matchMedia('(prefers-color-scheme: light)');
/** gespeicherte Wahl: 'system' | 'light' | 'dark' | 'boerse' */
// Standard für alle ohne eigene Wahl: „Börse“. „System“ wird deshalb ausdrücklich gespeichert.
export const DEFAULT_THEME = 'boerse';
export const themePref = () => { const v = store.get('theme', DEFAULT_THEME); return THEMES.includes(v) ? v : DEFAULT_THEME; };
/** tatsächlich angezeigtes Schema: 'light' | 'dark' */
export const resolvedTheme = (pref = themePref()) => (pref === 'system' ? (mq.matches ? 'light' : 'dark') : pref === 'boerse' ? 'dark' : pref);
function apply() {
const pref = themePref();
const t = resolvedTheme(pref);
const root = document.documentElement;
const look = pref === 'boerse' ? 'boerse' : '';
const changedTheme = root.dataset.theme !== t || (root.dataset.look ?? '') !== look;
root.dataset.theme = t;
if (look) root.dataset.look = look; else delete root.dataset.look;
const bg = getComputedStyle(root).getPropertyValue('--bg').trim();
const meta = document.querySelector('meta[name="theme-color"]');
if (meta && bg) meta.setAttribute('content', bg);
syncButtons(pref);
if (changedTheme) document.dispatchEvent(new CustomEvent('wibo:theme', { detail: { theme: t, pref } }));
}
/** Schaltflächen aktualisieren: Kopfzeilen-Umschalter (data-act="theme" ohne data-mode) und Auswahlgruppen (mit data-mode) */
export function syncButtons(pref = themePref()) {
for (const b of $$('[data-act="theme"]')) {
if (b.dataset.mode) { b.setAttribute('aria-pressed', String(b.dataset.mode === pref)); continue; }
const txt = `Farbschema: ${LABEL[pref]}`;
b.setAttribute('aria-label', `${txt} – zum Wechseln klicken`);
b.title = txt;
const ic = b.querySelector('.theme-ic') ?? b;
if (ic.dataset.pref !== pref) { ic.innerHTML = ICON[pref]; ic.dataset.pref = pref; }
}
}
/** Farbschema festlegen; ohne Argument zum nächsten wechseln (System → Hell → Dunkel → Börse → System) */
export function setTheme(pref) {
const next = THEMES.includes(pref) ? pref : THEMES[(THEMES.indexOf(themePref()) + 1) % THEMES.length];
store.set('theme', next);
apply();
}
/** HTML für die Auswahl in den Einstellungen */
export const themeChooser = () => {
const pref = themePref();
return `<div class="seg w-auto" role="group" aria-label="Farbschema">${THEMES.map((m) => `<button type="button" data-act="theme" data-mode="${m}" aria-pressed="${m === pref}"><span class="theme-ic" aria-hidden="true">${ICON[m]}</span> ${LABEL[m]}</button>`).join('')}</div>`;
};
let inited = false;
export function initTheme() {
if (inited) return;
inited = true;
mq.addEventListener('change', () => { if (themePref() === 'system') apply(); });
// Wechsel in einem anderen Tab übernehmen
addEventListener('storage', (e) => { if (e.key === null || e.key === 'wibo.theme') apply(); });
apply();
}