- @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>
108 lines
5.6 KiB
JavaScript
108 lines
5.6 KiB
JavaScript
// WCAG contrast of the token pairs the components actually use, in light and dark,
|
|
// for every venue theme. Reads tokens.css itself, so a colour edit that breaks
|
|
// readability fails here.
|
|
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
import { test } from 'node:test'
|
|
import { PALETTES } from '../src/theme.js'
|
|
|
|
const css = readFileSync(new URL('../src/tokens.css', import.meta.url), 'utf8')
|
|
|
|
/** Channel declarations of the first rule whose selector list is exactly `selector`. */
|
|
function rule(selector) {
|
|
const re = /([^{}]+)\{([^{}]*)\}/g
|
|
for (const m of css.matchAll(re)) {
|
|
const sel = m[1].replace(/\/\*[\s\S]*?\*\//g, '').trim().replace(/\s+/g, ' ')
|
|
if (sel !== selector) continue
|
|
return Object.fromEntries([...m[2].matchAll(/--x-([\w-]+)-rgb:\s*([\d\s]+);/g)].map(([, k, v]) => [k, v.trim().split(/\s+/).map(Number)]))
|
|
}
|
|
throw new Error(`no rule ${selector}`)
|
|
}
|
|
|
|
const lum = ([r, g, b]) => {
|
|
const f = c => { c /= 255; return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4 }
|
|
return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b)
|
|
}
|
|
const ratio = (a, b) => { const [x, y] = [lum(a), lum(b)].sort((p, q) => q - p); return (x + 0.05) / (y + 0.05) }
|
|
|
|
const TEXT = 4.5 // normal-size text (AA)
|
|
const UI = 3 // component boundaries and states (AA, 1.4.11)
|
|
const pairs = [
|
|
['ink', 'surface', TEXT], ['ink-2', 'surface', TEXT], ['ink-3', 'surface', TEXT], ['ink', 'canvas', TEXT],
|
|
['ink-2', 'surface-2', TEXT],
|
|
['canvas', 'ink', TEXT], // InfoTip popover
|
|
['accent-fg', 'accent', TEXT], ['accent-fg', 'accent-strong', TEXT],
|
|
['accent-ink', 'accent-soft', TEXT], ['accent-ink', 'surface', TEXT],
|
|
['accent', 'surface', UI], // switch on, focus, filled PIN dots
|
|
['ink-3', 'surface', UI], // switch off track
|
|
...['success', 'warning', 'danger', 'info', 'special'].flatMap(t => [
|
|
[`${t}-fg`, t, TEXT], [`${t}-ink`, `${t}-soft`, TEXT], [`${t}-ink`, 'surface', TEXT], [t, 'surface', UI],
|
|
]),
|
|
]
|
|
|
|
const venues = ['teal', 'amber', 'blue', 'violet', 'rose']
|
|
const light = rule(':root')
|
|
const dark = rule('[data-theme="dark"]')
|
|
|
|
for (const venue of venues) {
|
|
for (const [mode, base, sel] of [
|
|
['light', light, `[data-venue-theme="${venue}"]`],
|
|
['dark', dark, `[data-theme="dark"][data-venue-theme="${venue}"], [data-theme="dark"] [data-venue-theme="${venue}"]`],
|
|
]) {
|
|
test(`${venue} / ${mode}`, () => {
|
|
const t = { ...base, ...rule(sel) }
|
|
const failures = pairs
|
|
.map(([fg, bg, min]) => [fg, bg, min, ratio(t[fg], t[bg])])
|
|
.filter(([, , min, r]) => r < min)
|
|
.map(([fg, bg, min, r]) => `${fg} on ${bg}: ${r.toFixed(2)} < ${min}`)
|
|
assert.deepEqual(failures, [])
|
|
})
|
|
}
|
|
}
|
|
|
|
// Palettes (6.15): every palette in theme.js, light and dark, the same pairs as the default plus
|
|
// the nav rail. A palette block sets every colour itself, so it is checked on its own.
|
|
const navPairs = [['nav-ink', 'nav-bg', TEXT], ['nav-ink-2', 'nav-bg', TEXT], ['nav-ink-3', 'nav-bg', TEXT], ['nav-ink', 'nav-active', TEXT], ['nav-ink-2', 'nav-raised', TEXT]]
|
|
const check = (t, list) => list.filter(([fg, bg]) => t[fg] && t[bg]).map(([fg, bg, min]) => [fg, bg, min, ratio(t[fg], t[bg])])
|
|
.filter(([, , min, r]) => r < min).map(([fg, bg, min, r]) => `${fg} on ${bg}: ${r.toFixed(2)} < ${min}`)
|
|
for (const palette of PALETTES.filter(p => p !== 'default')) {
|
|
for (const [mode, sel] of [['light', `[data-palette="${palette}"]`], ['dark', `[data-palette="${palette}"][data-theme="dark"]`]]) {
|
|
test(`palette ${palette} / ${mode}`, () => {
|
|
const t = rule(sel)
|
|
const missing = [...pairs, ...navPairs].flatMap(([fg, bg]) => [fg, bg]).filter(k => !t[k])
|
|
assert.deepEqual([...new Set(missing)], [], 'a palette sets every colour token')
|
|
assert.deepEqual(check(t, [...pairs, ...navPairs]), [])
|
|
})
|
|
}
|
|
}
|
|
test('the default nav rail is readable', () => {
|
|
const nav = rule(':root') // first :root holds the colours; the nav set is its own :root block
|
|
const navRoot = Object.fromEntries([...css.slice(css.indexOf('/* ── Nav')).matchAll(/--x-(nav-[\w-]+)-rgb:\s*([\d\s]+);/g)].slice(0, 8).map(([, k, v]) => [k, v.trim().split(/\s+/).map(Number)]))
|
|
assert.ok(nav.canvas && navRoot['nav-bg'])
|
|
assert.deepEqual(check(navRoot, navPairs), [])
|
|
})
|
|
|
|
test('system theme mirrors dark', () => {
|
|
const sys = css.slice(css.indexOf('@media (prefers-color-scheme: dark)'))
|
|
assert.match(sys, /\[data-theme="system"\]/)
|
|
const darkCanvas = dark.canvas.join(' ')
|
|
assert.ok(sys.includes(`--x-canvas-rgb: ${darkCanvas};`), 'system dark uses the dark canvas')
|
|
})
|
|
|
|
// The board (kitchen display, 3.2b-2): always dark, so one set. Done items (card-muted) and
|
|
// empty states (ink-4) are meant to recede and aren't checked.
|
|
test('board token pairs are readable', () => {
|
|
const b = Object.fromEntries([...css.matchAll(/--x-board-([\w-]+)-rgb:\s*([\d\s]+);/g)].map(([, k, v]) => [k, v.trim().split(/\s+/).map(Number)]))
|
|
const boardPairs = [
|
|
['ink', 'bg', TEXT], ['ink-2', 'bg', TEXT], ['ink-2', 'line', TEXT], ['ink-3', 'rail', TEXT], ['bg', 'tab-active', TEXT],
|
|
['card-ink', 'card', TEXT], ['card-ink-2', 'card-soft', TEXT], ['note-ink', 'note', TEXT],
|
|
['late-deep', 'card', TEXT], ['fresh-deep', 'card', TEXT], ['on-status', 'fresh-deep', TEXT], ['on-status', 'late-deep', TEXT],
|
|
['warning-deep', 'card', UI], ['progress', 'card', UI], ['on-status', 'warning-deep', UI],
|
|
]
|
|
for (const [fg, bg, min] of boardPairs) {
|
|
assert.ok(b[fg] && b[bg], `board tokens ${fg} / ${bg} exist`)
|
|
const r = ratio(b[fg], b[bg])
|
|
assert.ok(r >= min, `board ${fg} on ${bg}: ${r.toFixed(2)} < ${min}`)
|
|
}
|
|
})
|