refactor(frontend): emit themed tokens as light-dark() pairs (#5356)

This commit is contained in:
Sam Dornan
2026-10-25 20:23:41 -05:00
committed by GitHub
parent a96beba5f2
commit bd938bb7eb
5 changed files with 131 additions and 112 deletions

View File

@@ -16,19 +16,19 @@ description: Theming, design tokens, colors, and visual language in the RomM v2
- **`src/styles/tokens.css`**: _generated_ by `scripts/build-tokens.ts` (`npm run build:tokens`, hooked into `predev`/`prebuild`). **Do not hand-edit.** This is how the vast majority of tokens are consumed: `var(--r-color-...)` in CSS.
- **Direct JS/TS imports** of named exports (`colorCanvas`, `colorCoverArt`, `layout`, …) for the few cases needing a token value in JavaScript: baking colors into an SVG string (`utils/covers`), canvas/QR backgrounds (`Player/Ruffle.vue`, `ShowQRCodeDialog`), and the virtualiser's pixel math (`Gallery/listColumns` reading `layout`).
There is no Vuetify. `tokens.css` emits a palette block per theme under `.r-app.r-dark` / `.r-app.r-light`; `RomM.vue` toggles those classes on `<html>`.
There is no Vuetify. A themed token is one declaration, `light-dark(<light>, <dark>)`, in `tokens.css`, resolved by the `color-scheme` where it is used. `.r-app` sets `color-scheme: light dark`; `.r-app.r-dark` and `.r-app.r-light` pin it, and `RomM.vue` toggles those classes on `<html>`. The generator pairs the light and dark values by CSS name and fails the build when a token exists in only one palette.
### Adding a new token
1. Add it to `src/tokens/index.ts` with a **semantic, role-based** name (`--r-color-danger`, not `--r-color-red`).
2. Provide **both** dark and light values.
2. Provide **both** dark and light values (a themed token needs both, or the generator throws).
3. Consume via `var(--r-...)` (CSS) or the named export (JS).
4. Run `npm run build:tokens` to regenerate `tokens.css`.
5. If the JS→CSS variable name needs an exception (e.g. `--r-nav-h`), add an entry to `NAME_OVERRIDES` in the generator.
### Where the scope classes live, and why `<html>`
`.r-app`, `.r-dark`, `.r-light` go on `<html>` (`RomM.vue` toggles them whenever `uiVersion` or the active theme changes). The overlay primitives (`RDialog`, `RMenu`, `RTooltip`) `<Teleport to="body">`, landing **outside** the app root. Only `<html>` covers both the regular tree and the teleports; without it, overlays lose their tokens.
`.r-app`, `.r-dark`, `.r-light` go on `<html>` (`RomM.vue` toggles them whenever the active theme changes). The overlay primitives (`RDialog`, `RMenu`, `RTooltip`) `<Teleport to="body">`, landing **outside** the app root. Only `<html>` covers both the regular tree and the teleports; without it, overlays lose their tokens.
### Diagnostics: when `var(--r-color-...)` resolves to nothing on an overlay

View File

@@ -54,13 +54,13 @@ export async function fillLoginForm(
await loginButton(page).click();
}
/** Force the v2 UI before the app boots, and the "auto" theme, so the
* browser's `colorScheme` picks it and the account never stores another. */
/** Seed the "auto" theme before the app boots, so the browser's
* `colorScheme` picks it and the account never stores another. */
export async function seedUiState(page: Page) {
await page.addInitScript(() => {
// Init scripts also run in Chrome's own error page, which denies storage.
if (!location.protocol.startsWith("http")) return;
const seeded = { "settings.uiVersion": "v2", "settings.theme": "auto" };
const seeded = { "settings.theme": "auto" };
// The app only adopts an unscoped value when the signed-in user has no
// `user:<id>:` copy, and a saved session carries one.
for (const key of Object.keys(localStorage)) {

View File

@@ -176,37 +176,54 @@ const SHARED: Entry[] = [
...entriesFor(zIndex, "zIndex", "--r-z-index"),
];
// Native `color-scheme` rides with the palette so UA-drawn chrome (scrollbars,
// form controls, embedded viewers) follows the theme instead of staying light.
const DARK: Entry[] = [
["color-scheme", "dark"],
...entriesFor(colorDark, "colorDark", "--r-color"),
];
// Light theme: the surface palette, then the brand overrides that re-tune
// primary/secondary/accent for the off-white page (deeper for contrast).
// light-dark() resolves using the color-scheme where the token is consumed.
const DARK: Entry[] = entriesFor(colorDark, "colorDark", "--r-color");
const LIGHT: Entry[] = [
["color-scheme", "light"],
...entriesFor(colorLight, "colorLight", "--r-color"),
...entriesFor(colorStatusLight, "colorStatusLight", "--r-color"),
...entriesFor(colorBrandLight, "colorBrandLight", "--r-color-brand"),
];
const sharedByName = new Map(SHARED);
const darkByName = new Map(DARK);
const lightByName = new Map(LIGHT);
for (const [n] of DARK) {
if (!lightByName.has(n)) {
throw new Error(`${n} has a dark value but no light value`);
}
}
function themed([n, light]: Entry): Entry {
const dark = darkByName.get(n) ?? sharedByName.get(n);
if (dark === undefined) {
throw new Error(`${n} has a light value but no dark value`);
}
return [n, `light-dark(${light}, ${dark})`];
}
const THEMED = new Set(LIGHT.map(([n]) => n));
const SHARED_UNTHEMED = SHARED.filter(([n]) => !THEMED.has(n));
const HEADER = `/*
* RomM Design Tokens, CSS Custom Properties
*
* GENERATED FILE, do not hand-edit. Source: src/tokens/index.ts
* Regenerate with: npm run build:tokens
*
* Scoped under .r-app. Theme palettes live under
* .r-app.r-dark and .r-app.r-light. The classes are toggled on <html>
* by RomM.vue so teleported overlays (RDialog, RMenu, RTooltip), which
* land in <body> outside the app root, still resolve var(--r-color-*).
* Scoped under .r-app. Themed colours are light-dark() pairs; the scheme is
* the OS preference unless RomM.vue sets .r-dark or .r-light on <html>, so
* teleported overlays (RDialog, RMenu, RTooltip), which land in <body>
* outside the app root, still resolve var(--r-color-*).
*/
`;
const css = [
HEADER,
block(".r-app", SHARED),
// Root only: a nested .r-app (RCarousel) must inherit the pinned scheme.
"html.r-app {\n color-scheme: light dark;\n}",
"",
block(".r-app", [...SHARED_UNTHEMED, ...LIGHT.map(themed)]),
"",
`html[data-input="key"] .r-app {`,
` --r-focus-ring-width: ${focus.ringWidthKey};`,
@@ -215,16 +232,9 @@ const css = [
` --r-focus-ring-width: ${focus.ringWidthPad};`,
`}`,
"",
block(
".r-app.r-dark",
DARK,
"Dark surface palette: translucent white over the near-black base.",
),
block(
".r-app.r-light",
LIGHT,
"Light surface palette: translucent black over the off-white base.",
),
".r-app.r-dark {\n color-scheme: dark;\n}",
".r-app.r-light {\n color-scheme: light;\n}",
"",
].join("\n");
async function main() {

View File

@@ -4,33 +4,25 @@
* GENERATED FILE, do not hand-edit. Source: src/tokens/index.ts
* Regenerate with: npm run build:tokens
*
* Scoped under .r-app. Theme palettes live under
* .r-app.r-dark and .r-app.r-light. The classes are toggled on <html>
* by RomM.vue so teleported overlays (RDialog, RMenu, RTooltip), which
* land in <body> outside the app root, still resolve var(--r-color-*).
* Scoped under .r-app. Themed colours are light-dark() pairs; the scheme is
* the OS preference unless RomM.vue sets .r-dark or .r-light on <html>, so
* teleported overlays (RDialog, RMenu, RTooltip), which land in <body>
* outside the app root, still resolve var(--r-color-*).
*/
html.r-app {
color-scheme: light dark;
}
.r-app {
--r-color-brand-primary: #8b74e8;
--r-color-brand-primary-hover: #a18fff;
--r-color-brand-primary-pressed: #6043c8;
--r-color-brand-secondary: #9e8cd6;
--r-color-brand-secondary-hover: #ebe7fa;
--r-color-brand-secondary-pressed: #7a6bb4;
--r-color-brand-accent: #e1a38d;
--r-color-brand-accent-hover: #f0c8b8;
--r-color-fav: #ff4f6b;
--r-color-avatar-gradient: linear-gradient(135deg, #a18fff, #6043c8);
--r-color-romm-red: #da3633;
--r-color-romm-green: #3fb950;
--r-color-romm-blue: #0070f3;
--r-color-romm-gold: #ffd700;
--r-color-success: #4ade80;
--r-color-warning: #fbbf24;
--r-color-danger: #ff5050;
--r-color-info: #93c5fd;
--r-color-warning-fg: #facc15;
--r-color-danger-fg: #f87171;
--r-color-status-base-success: #22c55e;
--r-color-status-base-warning: #eab308;
--r-color-status-base-danger: #ef4444;
@@ -165,6 +157,87 @@
--r-z-menu: 2500;
--r-z-tooltip: 2600;
--r-z-snackbar: 2700;
--r-color-bg: light-dark(#f5f5fa, #07070f);
--r-color-bg-elevated: light-dark(
rgba(0, 0, 0, 0.025),
rgba(255, 255, 255, 0.045)
);
--r-color-surface: light-dark(
rgba(0, 0, 0, 0.035),
rgba(255, 255, 255, 0.07)
);
--r-color-surface-hover: light-dark(
rgba(0, 0, 0, 0.1),
rgba(255, 255, 255, 0.12)
);
--r-color-fg: light-dark(#111117, #ffffff);
--r-color-fg-heading: light-dark(rgba(17, 17, 23, 0.86), #ffffff);
--r-color-fg-secondary: light-dark(
rgba(17, 17, 23, 0.82),
rgba(255, 255, 255, 0.83)
);
--r-color-fg-muted: light-dark(
rgba(17, 17, 23, 0.74),
rgba(255, 255, 255, 0.55)
);
--r-color-fg-faint: light-dark(
rgba(17, 17, 23, 0.58),
rgba(255, 255, 255, 0.45)
);
--r-color-fg-faint-hard: light-dark(
rgba(17, 17, 23, 0.2),
rgba(255, 255, 255, 0.05)
);
--r-color-border: light-dark(rgba(0, 0, 0, 0.1), rgba(255, 255, 255, 0.07));
--r-color-border-strong: light-dark(
rgba(0, 0, 0, 0.22),
rgba(255, 255, 255, 0.15)
);
--r-color-focus: light-dark(rgba(0, 0, 0, 0.45), rgba(255, 255, 255, 0.45));
--r-color-panel: light-dark(
rgba(240, 240, 246, 0.98),
rgba(16, 12, 28, 0.97)
);
--r-color-panel-border: light-dark(
rgba(17, 17, 23, 0.1),
rgba(255, 255, 255, 0.1)
);
--r-color-tooltip-bg: light-dark(
rgba(245, 245, 250, 0.96),
rgba(7, 7, 15, 0.94)
);
--r-color-tooltip-border: light-dark(
rgba(17, 17, 23, 0.08),
rgba(255, 255, 255, 0.09)
);
--r-color-shimmer-sweep: light-dark(
rgba(0, 0, 0, 0.06),
rgba(255, 255, 255, 0.08)
);
--r-color-cover-placeholder: light-dark(#e4e2ef, #1a1a2e);
--r-color-cover-placeholder-bright: light-dark(#efeef7, #252540);
--r-color-auth-glass: light-dark(
rgba(245, 245, 250, 0.85),
rgba(13, 17, 23, 0.65)
);
--r-color-toast-bg: light-dark(
rgba(245, 245, 250, 0.95),
rgba(13, 17, 23, 0.92)
);
--r-color-switch-knob-on: light-dark(rgba(255, 255, 255, 0.95), #111117);
--r-color-title-shadow: light-dark(transparent, rgba(0, 0, 0, 0.5));
--r-color-success: light-dark(#15a34a, #4ade80);
--r-color-warning: light-dark(#ca8a04, #fbbf24);
--r-color-danger: light-dark(#dc2626, #ff5050);
--r-color-info: light-dark(#2563eb, #93c5fd);
--r-color-warning-fg: light-dark(#854d0e, #facc15);
--r-color-danger-fg: light-dark(#b91c1c, #f87171);
--r-color-brand-primary: light-dark(#553e98, #8b74e8);
--r-color-brand-primary-hover: light-dark(#452788, #a18fff);
--r-color-brand-primary-pressed: light-dark(#371f69, #6043c8);
--r-color-brand-secondary: light-dark(#6e5ca8, #9e8cd6);
--r-color-brand-accent: light-dark(#a85530, #e1a38d);
--r-color-romm-gold: light-dark(#b8860b, #ffd700);
}
html[data-input="key"] .r-app {
@@ -174,72 +247,9 @@ html[data-input="pad"] .r-app {
--r-focus-ring-width: 3.5px;
}
/* Dark surface palette: translucent white over the near-black base. */
.r-app.r-dark {
color-scheme: dark;
--r-color-bg: #07070f;
--r-color-bg-elevated: rgba(255, 255, 255, 0.045);
--r-color-surface: rgba(255, 255, 255, 0.07);
--r-color-surface-hover: rgba(255, 255, 255, 0.12);
--r-color-fg: #ffffff;
--r-color-fg-heading: #ffffff;
--r-color-fg-secondary: rgba(255, 255, 255, 0.83);
--r-color-fg-muted: rgba(255, 255, 255, 0.55);
--r-color-fg-faint: rgba(255, 255, 255, 0.45);
--r-color-fg-faint-hard: rgba(255, 255, 255, 0.05);
--r-color-border: rgba(255, 255, 255, 0.07);
--r-color-border-strong: rgba(255, 255, 255, 0.15);
--r-color-focus: rgba(255, 255, 255, 0.45);
--r-color-panel: rgba(16, 12, 28, 0.97);
--r-color-panel-border: rgba(255, 255, 255, 0.1);
--r-color-tooltip-bg: rgba(7, 7, 15, 0.94);
--r-color-tooltip-border: rgba(255, 255, 255, 0.09);
--r-color-shimmer-sweep: rgba(255, 255, 255, 0.08);
--r-color-cover-placeholder: #1a1a2e;
--r-color-cover-placeholder-bright: #252540;
--r-color-auth-glass: rgba(13, 17, 23, 0.65);
--r-color-toast-bg: rgba(13, 17, 23, 0.92);
--r-color-switch-knob-on: #111117;
--r-color-title-shadow: rgba(0, 0, 0, 0.5);
}
/* Light surface palette: translucent black over the off-white base. */
.r-app.r-light {
color-scheme: light;
--r-color-bg: #f5f5fa;
--r-color-bg-elevated: rgba(0, 0, 0, 0.025);
--r-color-surface: rgba(0, 0, 0, 0.035);
--r-color-surface-hover: rgba(0, 0, 0, 0.1);
--r-color-fg: #111117;
--r-color-fg-heading: rgba(17, 17, 23, 0.86);
--r-color-fg-secondary: rgba(17, 17, 23, 0.82);
--r-color-fg-muted: rgba(17, 17, 23, 0.74);
--r-color-fg-faint: rgba(17, 17, 23, 0.58);
--r-color-fg-faint-hard: rgba(17, 17, 23, 0.2);
--r-color-border: rgba(0, 0, 0, 0.1);
--r-color-border-strong: rgba(0, 0, 0, 0.22);
--r-color-focus: rgba(0, 0, 0, 0.45);
--r-color-panel: rgba(240, 240, 246, 0.98);
--r-color-panel-border: rgba(17, 17, 23, 0.1);
--r-color-tooltip-bg: rgba(245, 245, 250, 0.96);
--r-color-tooltip-border: rgba(17, 17, 23, 0.08);
--r-color-shimmer-sweep: rgba(0, 0, 0, 0.06);
--r-color-cover-placeholder: #e4e2ef;
--r-color-cover-placeholder-bright: #efeef7;
--r-color-auth-glass: rgba(245, 245, 250, 0.85);
--r-color-toast-bg: rgba(245, 245, 250, 0.95);
--r-color-switch-knob-on: rgba(255, 255, 255, 0.95);
--r-color-title-shadow: transparent;
--r-color-success: #15a34a;
--r-color-warning: #ca8a04;
--r-color-danger: #dc2626;
--r-color-info: #2563eb;
--r-color-warning-fg: #854d0e;
--r-color-danger-fg: #b91c1c;
--r-color-brand-primary: #553e98;
--r-color-brand-primary-hover: #452788;
--r-color-brand-primary-pressed: #371f69;
--r-color-brand-secondary: #6e5ca8;
--r-color-brand-accent: #a85530;
--r-color-romm-gold: #b8860b;
}

View File

@@ -40,7 +40,7 @@ export const colorBrand = {
// the salmon accent and light-purple links worse), so light mode deepens
// primary, secondary and accent to clear AA for brand-coloured text, links,
// active-nav and chips. These keys share the brand CSS names, so the build
// emits them inside .r-light and they override the shared values there.
// pairs each with its dark value above as one `light-dark()` declaration.
// Hover/pressed deepen further (a light page gains emphasis by going darker,
// the inverse of dark mode where hover goes lighter).
export const colorBrandLight = {
@@ -87,8 +87,7 @@ export const colorStatusBase = {
// yellow and pale blue wash out (a green play-badge or a yellow rating
// star barely registers). Light mode drops them ~one Tailwind tier deeper
// so each still reads as its hue (icons, badges, solid buttons) on white.
// Emitted into .r-light with the same CSS names, so they override the
// shared values there.
// Paired with the canonical values above, by CSS name, in `light-dark()`.
export const colorStatusLight = {
success: "#15A34A",
// One tier brighter than a naive deepening (yellow-600, not -700) so the