// The look of an app (plan steps 2.6f, 6.15): three independent choices, each an attribute on // that tokens.css reads. Nothing else changes: every component draws from the tokens. // // mode data-theme light | dark | system (follows the device, live) // palette data-palette the colours: default | nord | … (light and dark versions each) // style data-style the shape of the components: rounded | sharp | … (radius, shadows, borders) // // "system" is resolved here (matchMedia), so data-theme is always light or dark and a palette // needs only a light and a dark block. The choice is per device (localStorage), like a screen // setting. No React here. // // applyLook(getStoredLook()) at startup // applyLook({ mode: 'dark', palette: 'nord' }, { save: true }) when the user picks // applyTheme('dark') mode only (older callers) // // Adding a palette or a style: a token block in tokens.css (see "Palettes" / "Styles" there), its // name in PALETTES / STYLES below, and its label in the apps' texts. The contrast test checks // every palette in both modes. export const THEMES = ['light', 'dark', 'system'] export const PALETTES = ['default', 'nord'] export const STYLES = ['rounded', 'sharp'] export const THEME_STORAGE_KEY = 'xenia_theme' export const PALETTE_STORAGE_KEY = 'xenia_palette' export const STYLE_STORAGE_KEY = 'xenia_style' const browserStorage = () => { try { return globalThis.localStorage ?? null } catch { return null } } const read = (storage, key, list, fallback) => { try { const v = storage?.getItem(key) return list.includes(v) ? v : fallback } catch { return fallback } } const prefersDark = () => { try { return !!globalThis.matchMedia?.('(prefers-color-scheme: dark)').matches } catch { return false } } /** The saved mode, or 'light'. */ export function getStoredTheme(storage = browserStorage()) { return read(storage, THEME_STORAGE_KEY, THEMES, 'light') } /** The saved look: { mode, palette, style } (unknown or missing values: the defaults). */ export function getStoredLook(storage = browserStorage()) { return { mode: getStoredTheme(storage), palette: read(storage, PALETTE_STORAGE_KEY, PALETTES, 'default'), style: read(storage, STYLE_STORAGE_KEY, STYLES, 'rounded'), } } /** 'light' or 'dark' for a mode ('system' asks the device). */ export function resolveMode(mode, dark = prefersDark) { return mode === 'dark' || (mode === 'system' && dark()) ? 'dark' : 'light' } let unwatch = null /** Set the look on the root element (and save it when asked). Returns what was applied. */ export function applyLook(look = {}, { root = globalThis.document?.documentElement, storage = browserStorage(), save = false, dark = prefersDark } = {}) { const mode = THEMES.includes(look.mode) ? look.mode : 'light' const palette = PALETTES.includes(look.palette) ? look.palette : 'default' const style = STYLES.includes(look.style) ? look.style : 'rounded' const set = () => root?.setAttribute('data-theme', resolveMode(mode, dark)) set() root?.setAttribute('data-theme-choice', mode) root?.setAttribute('data-palette', palette) root?.setAttribute('data-style', style) // "system": follow the device when it switches unwatch?.() unwatch = null if (mode === 'system' && root) { try { const mq = globalThis.matchMedia?.('(prefers-color-scheme: dark)') if (mq?.addEventListener) { mq.addEventListener('change', set) unwatch = () => mq.removeEventListener('change', set) } } catch { /* no matchMedia: stays as resolved */ } } if (save) { try { storage?.setItem(THEME_STORAGE_KEY, mode) storage?.setItem(PALETTE_STORAGE_KEY, palette) storage?.setItem(STYLE_STORAGE_KEY, style) } catch { /* private mode: not remembered */ } } return { mode, palette, style } } /** The mode only; the palette and the style stay as they are on the page (older callers). */ export function applyTheme(theme, { root = globalThis.document?.documentElement, storage = browserStorage(), save = false, dark = prefersDark } = {}) { const { mode } = applyLook({ mode: theme, palette: root?.getAttribute?.('data-palette'), style: root?.getAttribute?.('data-style') }, { root, storage, dark }) if (save) { try { storage?.setItem(THEME_STORAGE_KEY, mode) } catch { /* private mode: not remembered */ } } return mode }