Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Write the light palette once; data-theme always names the resolved theme

Bittie943a84562ca
diff --git a/public/theme-boot.js b/public/theme-boot.js
index 3181589..e561c64 100644
--- a/public/theme-boot.js
+++ b/public/theme-boot.js
@@ -10,7 +10,9 @@
* which is after the first paint. A plain <script src> in <head> blocks the
* parser, so this runs before anything is on screen.
*
- * It writes the same attribute and ground colour that src/lib/theme.ts writes,
+ * It writes the same attribute and ground colour that src/lib/theme.ts writes
+ * (data-theme is always the RESOLVED theme, "system" included: tokens.css
+ * only has an attribute-keyed light palette),
* and deliberately duplicates the storage key and the two --bg values, because
* nothing here may depend on the bundle having loaded. theme.test.ts pins both
* copies against tokens.css so they cannot drift apart in silence.
@@ -38,9 +40,6 @@
var root = document.documentElement;
var mica = window.__SLOPUS_BACKDROP__ === "mica";
if (mica) root.setAttribute("data-backdrop", "mica");
- if (choice === "system") root.removeAttribute("data-theme");
- else root.setAttribute("data-theme", choice);
-
var resolved = choice;
if (choice === "system") {
resolved =
@@ -48,5 +47,6 @@
? "light"
: "dark";
}
+ root.setAttribute("data-theme", resolved);
root.style.backgroundColor = mica ? "transparent" : GROUND[resolved];
})();
diff --git a/src/lib/nativeShell.test.ts b/src/lib/nativeShell.test.ts
index f3f13dc..c2dac13 100644
--- a/src/lib/nativeShell.test.ts
+++ b/src/lib/nativeShell.test.ts
@@ -192,11 +192,11 @@ describe("accent tokens", () => {
expect(inkOn("#ffb900")).toBe("#000000");
});
- it("mirrors the three theme blocks of tokens.css so a theme switch needs no JavaScript", () => {
+ it("mirrors the two theme blocks of tokens.css so a theme switch is a CSS repaint", () => {
const css = accentStylesheet(BLUE);
expect(css).toContain(":root:root {");
- expect(css).toContain('@media (prefers-color-scheme: light) {\n:root:root:not([data-theme="dark"]) {');
expect(css).toContain(':root:root[data-theme="light"] {');
+ expect(css).not.toContain("prefers-color-scheme");
});
it("removes itself for null or a malformed payload, putting the palette's blue back", () => {
diff --git a/src/lib/nativeShell.ts b/src/lib/nativeShell.ts
index d446873..f55b388 100644
--- a/src/lib/nativeShell.ts
+++ b/src/lib/nativeShell.ts
@@ -319,17 +319,15 @@ export function accentTokens(accent: SystemAccent, theme: ResolvedTheme): Record
const block = (selector: string, tokens: Record<string, string>) =>
`${selector} {\n${Object.entries(tokens).map(([name, value]) => ` ${name}: ${value};`).join("\n")}\n}`;
-/** The stylesheet text for one accent. It mirrors the three blocks of
- * tokens.css — dark on :root, light twice (the OS media query guarded by
- * :not([data-theme="dark"]), and the explicit attribute) — so a theme switch
- * repaints through CSS alone. `:root:root` outranks tokens.css's own
+/** The stylesheet text for one accent. It mirrors the two blocks of
+ * tokens.css — dark on :root, light on [data-theme="light"] — so a theme
+ * switch repaints through CSS alone. `:root:root` outranks tokens.css's own
* selectors by one class, whatever order the two sheets end up in. */
export function accentStylesheet(accent: SystemAccent): string {
const dark = accentTokens(accent, "dark");
const light = accentTokens(accent, "light");
return [
block(":root:root", dark),
- `@media (prefers-color-scheme: light) {\n${block(':root:root:not([data-theme="dark"])', light)}\n}`,
block(':root:root[data-theme="light"]', light),
].join("\n");
}
diff --git a/src/lib/theme.test.ts b/src/lib/theme.test.ts
index 8627760..833ce80 100644
--- a/src/lib/theme.test.ts
+++ b/src/lib/theme.test.ts
@@ -30,7 +30,7 @@ const readCss = (fromProjectRoot: string) =>
/* jsdom's matchMedia is not implemented at all, so every test that cares about
the OS setting installs its own. `light` is what the query asks for, mirroring
- `(prefers-color-scheme: light)` in tokens.css. */
+ `(prefers-color-scheme: light)` in theme.ts. */
function stubMatchMedia(light: boolean) {
const listeners = new Set<() => void>();
const query = {
@@ -69,17 +69,21 @@ describe("theme preference", () => {
expect(loadTheme()).toBe("light");
});
- it("writes an attribute for an explicit choice and none at all for system", () => {
+ it("always writes the resolved theme, system included", () => {
expect(applyTheme("light")).toBe("light");
expect(document.documentElement.getAttribute("data-theme")).toBe("light");
expect(applyTheme("dark")).toBe("dark");
expect(document.documentElement.getAttribute("data-theme")).toBe("dark");
- /* No attribute is the point: it hands the decision back to the media query
- so the OS can flip it live without JavaScript. */
+ /* tokens.css has no media-query palette: "system" has to name the OS's
+ theme on the attribute, or a light computer would paint dark. */
+ stubMatchMedia(true);
+ applyTheme("system");
+ expect(document.documentElement.getAttribute("data-theme")).toBe("light");
+ stubMatchMedia(false);
applyTheme("system");
- expect(document.documentElement.hasAttribute("data-theme")).toBe(false);
+ expect(document.documentElement.getAttribute("data-theme")).toBe("dark");
});
it("resolves system against the computer, in both directions", () => {
@@ -159,14 +163,14 @@ const declarations = (css: string): Map<string, string> => {
describe("tokens.css", () => {
const css = readCss("src/styles/tokens.css");
const dark = declarations(block(css, "\n:root {"));
- const mediaLight = declarations(block(css, ':root:not([data-theme="dark"]) {'));
- const explicitLight = declarations(block(css, ':root[data-theme="light"] {'));
-
- it("guards the media block so an explicit dark choice beats a light computer", () => {
- /* Drop this guard and picking Dark on a light-theme computer silently
- repaints light: same specificity, and the media block wins on order. */
- expect(css).toContain("@media (prefers-color-scheme: light)");
- expect(css).toContain(':root:not([data-theme="dark"])');
+ const light = declarations(block(css, ':root[data-theme="light"] {'));
+
+ it("writes the light palette exactly once, keyed on the attribute", () => {
+ /* theme.ts always stamps the resolved theme, so a prefers-color-scheme
+ copy would be a second palette to keep in step by hand, and one an
+ explicit Dark on a light computer would have to be guarded against. */
+ expect(css).not.toContain("prefers-color-scheme");
+ expect(css.split(':root[data-theme="light"] {').length).toBe(2);
});
it("never introduces a colour for the first time in a media or attribute block", () => {
@@ -174,19 +178,14 @@ describe("tokens.css", () => {
only under light leaves dark painting with an unresolved var(), which
falls back to inherit/initial and puts one theme's ink on the other
theme's ground. */
- const orphans = [...mediaLight.keys(), ...explicitLight.keys()].filter((name) => !dark.has(name));
+ const orphans = [...light.keys()].filter((name) => !dark.has(name));
expect(orphans).toEqual([]);
});
- it("keeps the two light blocks identical", () => {
- expect([...explicitLight.entries()].sort()).toEqual([...mediaLight.entries()].sort());
- });
-
it("flips color-scheme with the palette so platform menus follow", () => {
/* Without this the <select> dropdown, the scrollbars and the caret stay
dark on a light page — chrome the stylesheet cannot reach. */
expect(block(css, "\n:root {")).toContain("color-scheme: dark");
- expect(block(css, ':root:not([data-theme="dark"]) {')).toContain("color-scheme: light");
expect(block(css, ':root[data-theme="light"] {')).toContain("color-scheme: light");
});
@@ -196,7 +195,7 @@ describe("tokens.css", () => {
flash of a colour that is in no palette, which nobody would ever notice
by looking. */
expect(dark.get("--bg")).toBe(THEME_BACKGROUND.dark);
- expect(mediaLight.get("--bg")).toBe(THEME_BACKGROUND.light);
+ expect(light.get("--bg")).toBe(THEME_BACKGROUND.light);
const boot = read("public/theme-boot.js");
expect(boot).toContain(`dark: "${THEME_BACKGROUND.dark}"`);
diff --git a/src/lib/theme.ts b/src/lib/theme.ts
index d299357..9975bdb 100644
--- a/src/lib/theme.ts
+++ b/src/lib/theme.ts
@@ -6,19 +6,18 @@
* never go into slopus.json), and a project folder copied here from another
* machine does not bring somebody else's appearance with it.
*
- * THREE states, not two:
+ * THREE choices, not two:
*
- * "system" follow the computer, LIVE. No attribute is written, so
- * `@media (prefers-color-scheme: light)` in tokens.css does the
- * work and an OS flip while the app is open repaints with no JS
- * involved at all.
+ * "system" follow the computer, LIVE: data-theme is the OS's theme, and
+ * main.tsx re-applies it whenever the OS flips (watchSystemTheme)
* "light" <html data-theme="light">
* "dark" <html data-theme="dark">
*
- * An explicit choice beats the OS in BOTH directions. That is a property of
- * the CSS, not of this file: the light rules are guarded by
- * `:root:not([data-theme="dark"])`, so "dark" on a light computer really does
- * fall back to the bare `:root` dark palette. Do not "simplify" that guard.
+ * The attribute always names the RESOLVED theme, never the choice. That is
+ * what lets tokens.css write its light palette once, under
+ * :root[data-theme="light"], instead of twice (a media-query copy for
+ * "system" plus the attribute copy) kept identical by hand. The choice itself
+ * lives in localStorage; read it with loadTheme(), never from the attribute.
*/
import { setWindowTheme } from "./nativeShell";
@@ -98,7 +97,7 @@ export function hasMicaBackdrop(): boolean {
return native;
}
-/** Writes the choice onto <html> and repaints the document ground.
+/** Writes the resolved theme onto <html> and repaints the document ground.
*
* The ground is set inline as well as by CSS because theme-boot.js sets it
* that way before the stylesheet has loaded; leaving a stale inline colour
@@ -111,10 +110,9 @@ export function hasMicaBackdrop(): boolean {
* the system menus and message boxes. */
export function applyTheme(choice: ThemeChoice): ResolvedTheme {
const root = document.documentElement;
- if (choice === "system") root.removeAttribute("data-theme");
- else root.setAttribute("data-theme", choice);
-
const resolved = resolveTheme(choice);
+ root.setAttribute("data-theme", resolved);
+
root.style.backgroundColor = hasMicaBackdrop() ? "transparent" : THEME_BACKGROUND[resolved];
void setWindowTheme(choice);
@@ -122,9 +120,9 @@ export function applyTheme(choice: ThemeChoice): ResolvedTheme {
}
/** Calls back whenever the COMPUTER's setting changes. Returns an unsubscribe.
- * Only "system" needs it — the CSS repaints on its own, but the settings
- * screen has to keep saying which one the computer is currently on, and the
- * inline ground colour above has to be rewritten. */
+ * Only "system" needs it: the attribute (and with it the whole palette) and
+ * the inline ground colour above have to be rewritten, and the settings
+ * screen has to keep saying which one the computer is currently on. */
export function watchSystemTheme(onChange: (theme: ResolvedTheme) => void): () => void {
if (typeof matchMedia !== "function") return () => {};
const query = matchMedia("(prefers-color-scheme: light)");
diff --git a/src/main.tsx b/src/main.tsx
index df52799..0eb372b 100644
--- a/src/main.tsx
+++ b/src/main.tsx
@@ -13,10 +13,10 @@ import "./styles/index.css";
applyTheme(loadTheme());
installGlobalDiagnostics();
-/* Only "system" needs watching, and only for the inline ground colour —
- tokens.css repaints the app itself through
- `@media (prefers-color-scheme)` with no JavaScript involved. The listener is
- never torn down because it lives exactly as long as the window does. */
+/* Only "system" needs watching: data-theme names the resolved theme, so an
+ OS flip has to rewrite it (and the inline ground colour) for tokens.css to
+ repaint. The listener is never torn down because it lives exactly as long
+ as the window does. */
watchSystemTheme(() => {
if (loadTheme() === "system") applyTheme("system");
});
diff --git a/src/styles/tokens.css b/src/styles/tokens.css
index 17001df..8f9de30 100644
--- a/src/styles/tokens.css
+++ b/src/styles/tokens.css
@@ -46,31 +46,27 @@
THEMING
----------------------------------------------------------------------------
- Three states, one attribute:
+ Three choices, one attribute that always names the RESOLVED theme:
- system (default) no data-theme attribute; the media query below decides
+ system (default) data-theme follows the OS, live (theme.ts listens to
+ prefers-color-scheme and rewrites it)
light <html data-theme="light">
dark <html data-theme="dark">
The COMPLETE palette is defined once, on a bare `:root`, in DARK. Light only
ever REDEFINES those same names — it never introduces a colour of its own, so
a token can never be missing in one theme and paint one theme's ink on the
- other theme's ground. Light appears twice, and the two blocks must stay
- identical:
-
- @media (prefers-color-scheme: light) { :root:not([data-theme="dark"]) }
- the OS asked for light and the user has not overridden it
- :root[data-theme="light"]
- the user asked for light, whatever the OS says
-
- The `:not([data-theme="dark"])` guard is what makes an explicit choice beat
- the OS in BOTH directions: without it, "dark" chosen on a light computer
- would still get the light block (same specificity, later in the file wins).
+ other theme's ground. Light is written ONCE, under :root[data-theme="light"].
+ There is no `@media (prefers-color-scheme)` block: resolving "system"
+ against the OS is JavaScript's job (public/theme-boot.js before the first
+ paint, src/lib/theme.ts after), so the palette never has to be copied into
+ a second selector and kept in step by hand.
A token written as `var(--other)` on :root is an ALIAS: it resolves against
whichever theme is active, so light does not (and should not) restate it.
- src/lib/theme.ts owns the attribute; index.html sets it before first paint.
+ src/lib/theme.ts owns the attribute; public/theme-boot.js sets it before
+ the first paint.
========================================================================== */
:root {
@@ -400,7 +396,8 @@
}
/* ============================================================================
- LIGHT — the OS asked for it and the user has not overridden it.
+ LIGHT — <html data-theme="light">, which theme.ts writes whether the user
+ picked Light or picked System on a light computer.
Designed, not inverted. The ground is a cool grey (#eef1f6) and panels are
WHITE, so a card still reads as raised; a straight inversion would have put
@@ -415,118 +412,6 @@
body over a grey button row, where dark puts the lighter step on top).
========================================================================== */
-@media (prefers-color-scheme: light) {
- :root:not([data-theme="dark"]) {
- color-scheme: light;
-
- --bg: #eef1f6;
- --panel: #ffffff;
- --panel-2: #f5f7fa;
- --panel-3: #e9ecf3;
-
- --surface-chrome: #f5f7fa;
- --surface-well: #e4e8ef;
- --surface-well-alt: #eaedf3;
- --stroke-divider: rgba(15, 23, 42, 0.18);
-
- --card: rgba(255, 255, 255, 0.7);
- --card-hover: rgba(249, 249, 249, 0.5);
- --card-pressed: rgba(249, 249, 249, 0.3);
- --stroke-card: rgba(0, 0, 0, 0.0578);
- --stroke-surface: rgba(0, 0, 0, 0.0578);
- --stroke-control: rgba(0, 0, 0, 0.0578);
- --stroke-control-bottom: rgba(0, 0, 0, 0.1622);
- --stroke-strong: rgba(0, 0, 0, 0.4458);
- --fill-control: rgba(255, 255, 255, 0.7);
- --fill-control-hover: rgba(249, 249, 249, 0.5);
- --fill-control-pressed: rgba(249, 249, 249, 0.3);
- --fill-control-disabled: rgba(249, 249, 249, 0.3);
- --fill-control-alt: rgba(0, 0, 0, 0.0241);
- --fill-input-focus: #ffffff;
- --fill-subtle-hover: rgba(0, 0, 0, 0.0373);
- --fill-subtle-pressed: rgba(0, 0, 0, 0.0241);
- --thumb-bg: #ffffff;
- --acrylic: rgba(252, 252, 252, 0.85);
- --dialog-bg: var(--panel);
- --dialog-footer-bg: var(--panel-2);
- --tooltip-bg: #f9f9f9;
- --tooltip-stroke: rgba(0, 0, 0, 0.0578);
-
- --line: rgba(15, 23, 42, 0.1);
- --line-solid: #dde1ea;
-
- --wash-1: rgba(15, 23, 42, 0.035);
- --wash-2: rgba(15, 23, 42, 0.055);
- --grid-line: rgba(15, 23, 42, 0.16);
-
- --scrim: rgba(0, 0, 0, 0.3); /* lighter than dark's: a white dialog needs less */
-
- --text-primary: #101420; /* 18.4:1 on --panel */
- --text-secondary: #333c4e; /* 11.1:1 */
- --text-muted: #4a5365; /* 7.7:1 */
- --text-faint: #5a6377; /* 6.0:1 on --panel, 5.1:1 on --panel-3 */
- --text-disabled: rgba(0, 0, 0, 0.361);
-
- --accent: #2d5bd7;
- --accent-bright: #1e4fd0; /* 6.8:1 — the readable accent */
- --accent-fill: #2d5bd7; /* 5.9:1 under white */
- --accent-dark: #1e40af;
- --accent-soft: rgba(45, 91, 215, 0.1);
- --accent-soft-strong: rgba(45, 91, 215, 0.16);
- --accent-on-soft: #1e40af;
- --accent-line: rgba(45, 91, 215, 0.3);
- --accent-line-strong: rgba(30, 79, 208, 0.6);
- --accent-disabled: rgba(0, 0, 0, 0.2169);
-
- --on-accent-disabled: #ffffff;
-
- --button-primary-bg: #2d5bd7; /* white on it is 5.9:1 */
- --button-primary-hover: #2450c4; /* 7.0:1 — darker in light, so not the --accent-fill alias */
- --button-primary-active: #1e40af; /* 8.7:1 */
- --button-primary-stroke: rgba(255, 255, 255, 0.08);
- --button-primary-stroke-bottom: rgba(0, 0, 0, 0.4);
-
- --button-fill-bg: #e2e6ee; /* --text-primary on it is 14.7:1 */
- --button-fill-hover: #d6dbe6;
-
- --track: #dfe3ec;
- --progress-fill: #4a5365; /* 6.0:1 on --track */
-
- --success: #0f7a45; /* 5.4:1 */
- --success-soft: rgba(15, 122, 69, 0.08);
-
- --warning: #8a5a09; /* 5.9:1 */
- --warning-soft: rgba(138, 90, 9, 0.08);
-
- --danger: #c02c3a; /* 5.7:1 */
- --danger-line: rgba(192, 44, 58, 0.3);
- --danger-line-strong: rgba(192, 44, 58, 0.55);
- --danger-soft: #fdf1f2;
- --danger-soft-strong: #fbe4e6;
-
- --audio: #10706a; /* 5.9:1 on --panel */
-
- --clip-video: #4a68c8; /* the kind colours: unchanged */
- --clip-generated: #7d58c9;
- --clip-image: #3f8a5c;
- --clip-audio: #2f7d77;
- --clip-caption: #9c6a1c;
- --snap: #a27800;
-
- --shadow: 0 32px 64px rgba(0, 0, 0, 0.1876), 0 2px 21px rgba(0, 0, 0, 0.1474);
- --shadow-sm: 0 8px 16px rgba(0, 0, 0, 0.14);
- --shadow-tooltip: 0 4px 8px rgba(0, 0, 0, 0.14);
-
- --focus-outer: rgba(0, 0, 0, 0.8956);
- --focus-inner: rgba(255, 255, 255, 0.7);
- }
-}
-
-/* ============================================================================
- LIGHT — the user chose it explicitly, whatever the OS says.
- Must stay identical to the block above apart from the selector.
- ========================================================================== */
-
:root[data-theme="light"] {
color-scheme: light;
@@ -592,7 +477,7 @@
--on-accent-disabled: #ffffff;
--button-primary-bg: #2d5bd7; /* white on it is 5.9:1 */
- --button-primary-hover: #2450c4; /* 7.0:1 */
+ --button-primary-hover: #2450c4; /* 7.0:1 — darker in light, so not the --accent-fill alias */
--button-primary-active: #1e40af; /* 8.7:1 */
--button-primary-stroke: rgba(255, 255, 255, 0.08);
--button-primary-stroke-bottom: rgba(0, 0, 0, 0.4);
diff --git a/src/styles/window.css b/src/styles/window.css
index 54ddaa1..106f697 100644
--- a/src/styles/window.css
+++ b/src/styles/window.css
@@ -9,9 +9,8 @@
--panel on top. Without the attribute — Windows 10, a browser — nothing in
this block applies and the solid --bg from tokens.css stays.
- `:root:root` outranks the three --bg declarations in tokens.css (bare
- :root, the media-guarded light block and [data-theme="light"]) by one
- class, so this holds in both themes whatever order the sheets load in.
+ `:root:root` outranks the two --bg declarations in tokens.css (bare
+ :root and [data-theme="light"]) by one class, so this holds in both themes whatever order the sheets load in.
The inline ground theme.ts writes is `transparent` under Mica too.
Anything that painted --bg as an OPAQUE plate — a cut-out, a mask edge, a