- @xenia/ui theme.js: a look = mode (light | dark | system) + palette (data-palette) + style (data-style); applyLook / getStoredLook (per device), 'system' resolved with matchMedia and followed live, so a palette is just a light and a dark token block; applyTheme kept for older callers (keeps palette and style) - tokens.css: 'Palettes' (first: Nord, light + dark, every colour and the nav rail) and 'Styles' (first: sharp: small corners, 1.5px borders, flat cards); --x-border-width used by every component and Tailwind's 'border' - contrast test: every palette in both modes (+ the nav rail); it caught the default rail's active item at 3.7:1 -> 5.5:1 - manager: the saved look applied at start (was light only); profile menu -> Look: Light / Dark / Device, Colours (Xenia, Nord), Shapes (Rounded, Sharp), applied at once and kept on this device - e2e: manager_look.mjs (every page in light and dark, no large near-white area in dark, contact sheets; 'nord-sharp' adds that look; in run-all), manager.mjs 'look:' (dark at once, kept after a reload, Device follows the device) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
99 lines
4.4 KiB
JavaScript
99 lines
4.4 KiB
JavaScript
// The look of an app (plan steps 2.6f, 6.15): three independent choices, each an attribute on
|
|
// <html> 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
|
|
}
|