mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-05 14:19:56 -05:00
ui: theme tokens and a four-theme runtime
Every color now resolves through CSS custom properties on html[data-theme=...], so a theme is a table of values rather than code. Four themes ship: light, dark, night (red hues only, to preserve dark adaptation off-grid) and amber (monochrome amber terminal). Fonts are exposed as two roles, --font-body and --font-mono, which a theme assigns a bundled face to; amber uses the mono face. Faces must be self-hosted, never fetched from a CDN, so the app keeps working offline. Night mode also filters media that cannot consume tokens - video, images, PDFs, embedded pages, canvases - through an SVG color matrix that maps luminance onto the red channel. Tag such an element with `.media`; a filter on an app-level ancestor would create a containing block and break fixed positioning. ThemeProvider persists the choice and index.html stamps data-theme from localStorage before the bundle loads, so the first paint already matches the saved theme on a slow connection. The OS preference is followed only while "system" is selected; night and amber are explicit choices. The nav toggle becomes a five-item picker. Semantic components still render themselves, so only the shell is token-driven for now; the inverted props stay in place until each component is migrated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
616424b223
commit
806727023d
12 changed files with 669 additions and 92 deletions
37
app/src/themes/MediaFilterDefs.tsx
Normal file
37
app/src/themes/MediaFilterDefs.tsx
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import React from 'react';
|
||||
|
||||
/**
|
||||
* SVG filter definitions used by monochrome themes to remap media that cannot
|
||||
* consume theme tokens: video, images, PDFs, embedded pages, canvases.
|
||||
*
|
||||
* Mounted once, near the app root. See tokens.css for the `.media` rule that
|
||||
* applies these, and ui-design.md for the night-vision rationale.
|
||||
*/
|
||||
export function MediaFilterDefs() {
|
||||
return <svg
|
||||
aria-hidden='true'
|
||||
focusable='false'
|
||||
style={{position: 'absolute', width: 0, height: 0, overflow: 'hidden'}}
|
||||
>
|
||||
<defs>
|
||||
{/*
|
||||
Night: luminance -> red channel, green and blue driven to zero, so
|
||||
every filtered pixel lights only the red subpixel. The gamma lift
|
||||
brightens midtones; red is perceptually dim, and without it text on
|
||||
a filtered document or PDF is unreadable.
|
||||
*/}
|
||||
<filter id='wrolpi-night-red' colorInterpolationFilters='sRGB'>
|
||||
<feColorMatrix
|
||||
type='matrix'
|
||||
values='0.2126 0.7152 0.0722 0 0
|
||||
0 0 0 0 0
|
||||
0 0 0 0 0
|
||||
0 0 0 1 0'
|
||||
/>
|
||||
<feComponentTransfer>
|
||||
<feFuncR type='gamma' amplitude='1' exponent='0.65' offset='0'/>
|
||||
</feComponentTransfer>
|
||||
</filter>
|
||||
</defs>
|
||||
</svg>
|
||||
}
|
||||
25
app/src/themes/fonts.css
Normal file
25
app/src/themes/fonts.css
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Bundled font registry.
|
||||
*
|
||||
* Fonts are exposed to themes as ROLES (--font-body, --font-mono), never as
|
||||
* families. A theme assigns one of the bundled `--font-<name>` stacks to a
|
||||
* role; it may not name an arbitrary family, because a theme config file
|
||||
* cannot ship font files and an unknown family would silently fall back —
|
||||
* offline, with no error.
|
||||
*
|
||||
* Every face here must be available offline. The system stacks cost nothing.
|
||||
* Additional faces are added as `@fontsource/*` npm packages (woff2 bundled at
|
||||
* build time) — NEVER a fonts CDN <link>.
|
||||
*/
|
||||
|
||||
:root {
|
||||
/* Available faces. */
|
||||
--font-system-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue',
|
||||
Arial, 'Noto Sans', sans-serif;
|
||||
--font-system-mono: 'SF Mono', 'Cascadia Code', 'DejaVu Sans Mono', Menlo, Monaco, Consolas,
|
||||
'Liberation Mono', monospace;
|
||||
|
||||
/* Role assignments. Themes override these. */
|
||||
--font-body: var(--font-system-sans);
|
||||
--font-mono: var(--font-system-mono);
|
||||
}
|
||||
178
app/src/themes/tokens.css
Normal file
178
app/src/themes/tokens.css
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
* WROLPi theme tokens.
|
||||
*
|
||||
* Every color in the interface resolves through these custom properties, so a
|
||||
* theme is a table of values and components never branch on the current mode.
|
||||
* See ui-design.md for the design rules and ui-migration-plan.md for the
|
||||
* migration this is part of.
|
||||
*
|
||||
* Four built-in themes:
|
||||
* light - default
|
||||
* dark - default when the OS prefers dark
|
||||
* night - night-vision preserving; red hues ONLY. Status is encoded by
|
||||
* brightness, danger by dashed borders. Explicit choice only.
|
||||
* amber - monochrome amber terminal palette. Explicit choice only.
|
||||
*
|
||||
* `data-theme` is stamped on <html> by an inline script in index.html before
|
||||
* first paint, then maintained by ThemeProvider.
|
||||
*/
|
||||
|
||||
/* Light is also the fallback when `data-theme` is missing or unrecognized. */
|
||||
:root,
|
||||
html[data-theme="light"] {
|
||||
--bg: #f3f4f3;
|
||||
--panel: #ffffff;
|
||||
--head: #e4e6e5;
|
||||
--border: #8f9698;
|
||||
--text: #22282a;
|
||||
--muted: #5f686a;
|
||||
|
||||
--red: #b23a3a;
|
||||
--amber: #95661a;
|
||||
--orange: #b05a24;
|
||||
--yellow: #96821c;
|
||||
--olive: #6f7a24;
|
||||
--green: #3f7a2e;
|
||||
--teal: #2a7d78;
|
||||
--blue: #20629c;
|
||||
--violet: #5c4fa8;
|
||||
--purple: #7b3f9c;
|
||||
--pink: #b03a72;
|
||||
--brown: #7a5233;
|
||||
--grey: #6a7072;
|
||||
--black: #22282a;
|
||||
--white: #ffffff;
|
||||
|
||||
/* Text drawn on top of a filled color button. */
|
||||
--btn-text: #ffffff;
|
||||
}
|
||||
|
||||
/*
|
||||
* Role aliases. Declared once: `var()` resolves on <html>, where each theme
|
||||
* has already redefined the color it points at.
|
||||
*/
|
||||
:root {
|
||||
--primary: var(--blue);
|
||||
--success: var(--green);
|
||||
--warning: var(--amber);
|
||||
--danger: var(--red);
|
||||
}
|
||||
|
||||
html[data-theme="dark"] {
|
||||
--bg: #1c1f21;
|
||||
--panel: #26292b;
|
||||
--head: #141617;
|
||||
--border: #4a5052;
|
||||
--text: #e6e8e6;
|
||||
--muted: #93999b;
|
||||
|
||||
--red: #e06c6c;
|
||||
--amber: #d6a656;
|
||||
--orange: #dd8a55;
|
||||
--yellow: #d6c156;
|
||||
--olive: #b5bd63;
|
||||
--green: #7fb069;
|
||||
--teal: #63bdb6;
|
||||
--blue: #5c9dd6;
|
||||
--violet: #9a8fe0;
|
||||
--purple: #b884d6;
|
||||
--pink: #dd7fae;
|
||||
--brown: #b08a68;
|
||||
--grey: #93999b;
|
||||
--black: #0d0f10;
|
||||
--white: #e6e8e6;
|
||||
|
||||
--btn-text: #1c1f21;
|
||||
}
|
||||
|
||||
/*
|
||||
* Night: red hues only. A single non-red pixel defeats the purpose of the
|
||||
* mode, so the whole palette collapses onto one red ramp ordered by
|
||||
* brightness — dim for structural roles, brightest reserved for danger.
|
||||
*/
|
||||
html[data-theme="night"] {
|
||||
--bg: #0a0000;
|
||||
--panel: #160404;
|
||||
--head: #000000;
|
||||
--border: #5c1010;
|
||||
--text: #e04a4a;
|
||||
--muted: #7a2525;
|
||||
|
||||
--red: #ff3b3b;
|
||||
--amber: #d94040;
|
||||
--orange: #e04a4a;
|
||||
--yellow: #d94040;
|
||||
--olive: #5c1a1a;
|
||||
--green: #b03030;
|
||||
--teal: #963030;
|
||||
--blue: #8a2828;
|
||||
--violet: #7a2929;
|
||||
--purple: #6e2020;
|
||||
--pink: #c04848;
|
||||
--brown: #451212;
|
||||
--grey: #7a2525;
|
||||
--black: #2a0808;
|
||||
--white: #ff8a8a;
|
||||
|
||||
--btn-text: #0a0000;
|
||||
}
|
||||
|
||||
/* Amber: a monochrome amber terminal palette, same flat style as the rest. */
|
||||
html[data-theme="amber"] {
|
||||
--bg: #0d0600;
|
||||
--panel: #1a0f00;
|
||||
--head: #000000;
|
||||
--border: #5c3a08;
|
||||
--text: #ff9500;
|
||||
--muted: #8a5810;
|
||||
|
||||
--red: #ffc040;
|
||||
--amber: #e08a20;
|
||||
--orange: #f09428;
|
||||
--yellow: #e08a20;
|
||||
--olive: #6e4610;
|
||||
--green: #c97a10;
|
||||
--teal: #8a581c;
|
||||
--blue: #a86618;
|
||||
--violet: #7a4c14;
|
||||
--purple: #6e4410;
|
||||
--pink: #d08430;
|
||||
--brown: #452708;
|
||||
--grey: #8a5810;
|
||||
--black: #241504;
|
||||
--white: #ffc875;
|
||||
|
||||
--btn-text: #0d0600;
|
||||
|
||||
/* Amber assigns the mono face to the body role; that is its character. */
|
||||
--font-body: var(--font-system-mono);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* Shell */
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
body {
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
font-family: var(--font-body);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* Media filtering */
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/*
|
||||
* Videos, images, PDFs, embedded pages, and the map canvas cannot consume
|
||||
* tokens, so night mode passes them through an SVG color matrix that maps
|
||||
* luminance onto the red channel and drives green and blue to zero. Nothing
|
||||
* a user opens can break their dark adaptation.
|
||||
*
|
||||
* Tag the media element (or its immediate wrapper) with `.media`. Never
|
||||
* filter an app-level ancestor: a CSS filter creates a containing block, which
|
||||
* breaks `position: fixed` descendants like modals and the nav bar.
|
||||
*
|
||||
* Filter definitions live in <MediaFilterDefs/> (mounted once by App).
|
||||
*/
|
||||
html[data-theme="night"] .media {
|
||||
filter: url(#wrolpi-night-red);
|
||||
}
|
||||
76
app/src/themes/tokens.test.js
Normal file
76
app/src/themes/tokens.test.js
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import {themeNames, themeSessionKey} from '../components/Theme';
|
||||
|
||||
const readFile = (...parts) => fs.readFileSync(path.join(__dirname, ...parts), 'utf8');
|
||||
|
||||
const tokensCss = readFile('tokens.css');
|
||||
const fontsCss = readFile('fonts.css');
|
||||
const indexHtml = readFile('..', '..', 'public', 'index.html');
|
||||
|
||||
/** The custom properties declared in the block for one theme. */
|
||||
const themeTokens = (theme) => {
|
||||
// Light doubles as the fallback, so its block is a grouped selector.
|
||||
const pattern = theme === 'light'
|
||||
? /:root,\s*html\[data-theme="light"]\s*{([^}]*)}/
|
||||
: new RegExp(`html\\[data-theme="${theme}"]\\s*{([^}]*)}`);
|
||||
const match = tokensCss.match(pattern);
|
||||
expect(match).not.toBeNull();
|
||||
return new Set([...match[1].matchAll(/(--[\w-]+)\s*:/g)].map(m => m[1]));
|
||||
};
|
||||
|
||||
describe('theme tokens', () => {
|
||||
it('declares a block for every theme', () => {
|
||||
themeNames.forEach(theme => expect(themeTokens(theme).size).toBeGreaterThan(0));
|
||||
});
|
||||
|
||||
it('defines the same tokens in every theme', () => {
|
||||
// A token missing from one theme inherits the light value, which is how a stray
|
||||
// white or blue ends up in night mode.
|
||||
const light = themeTokens('light');
|
||||
themeNames.filter(theme => theme !== 'light').forEach(theme => {
|
||||
const missing = [...light].filter(token => !themeTokens(theme).has(token)
|
||||
// Fonts are a role assignment, not a color; only amber overrides one.
|
||||
&& !token.startsWith('--font-'));
|
||||
expect({theme, missing}).toEqual({theme, missing: []});
|
||||
});
|
||||
});
|
||||
|
||||
it('uses only red hues in the night theme', () => {
|
||||
// Night's whole purpose is that no non-red pixel reaches the eye.
|
||||
const hexes = [...tokensCss.match(/html\[data-theme="night"]\s*{([^}]*)}/)[1]
|
||||
.matchAll(/#([0-9a-f]{6})/gi)].map(m => m[1]);
|
||||
expect(hexes.length).toBeGreaterThan(10);
|
||||
hexes.forEach(hex => {
|
||||
const [r, g, b] = [0, 2, 4].map(i => parseInt(hex.slice(i, i + 2), 16));
|
||||
expect({hex, redDominant: r >= g && r >= b, neutralGreenBlue: g === b}).toEqual(
|
||||
{hex, redDominant: true, neutralGreenBlue: true}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('resolves fonts through role tokens only', () => {
|
||||
expect(fontsCss).toMatch(/--font-body:/);
|
||||
expect(fontsCss).toMatch(/--font-mono:/);
|
||||
// Nothing may hardcode a family: a theme swaps the role, not the rule.
|
||||
expect(tokensCss).toMatch(/font-family:\s*var\(--font-body\)/);
|
||||
expect(tokensCss).not.toMatch(/font-family:(?!\s*var\()/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pre-paint theme script', () => {
|
||||
// The inline script in index.html duplicates the theme names and storage key so the
|
||||
// first paint matches the saved theme. These assertions fail if either side is renamed.
|
||||
it('reads the same localStorage key as ThemeProvider', () => {
|
||||
expect(indexHtml).toContain(`localStorage.getItem('${themeSessionKey}')`);
|
||||
});
|
||||
|
||||
it('recognizes every theme name', () => {
|
||||
themeNames.forEach(theme => expect(indexHtml).toContain(`'${theme}'`));
|
||||
});
|
||||
|
||||
it('stamps data-theme before the bundle loads', () => {
|
||||
const head = indexHtml.slice(0, indexHtml.indexOf('</head>'));
|
||||
expect(head).toContain("document.documentElement.setAttribute('data-theme'");
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue