From 868b8743d86d0378f64dc6d3b05376165bc9157c Mon Sep 17 00:00:00 2001 From: iris Date: Sat, 28 Nov 2026 02:45:38 +0100 Subject: [PATCH] swarm-ui: the browser chrome takes the theme's background colour The theme-color meta was a fixed mauve, so a phone's status bar and an installed app's title bar stayed mauve under every theme, light ones included. The theme effect sets it from the computed --bg after each change, and again when the OS scheme changes under auto. --- docs/4-implementation/web-ui/swarm-ui.md | 1 + .../shared/src/settings/theme-apply.ts | 17 ++++++++++++++ .../shared/src/settings/theme-color.ts | 13 +++++++++++ .../swarm-ui/test/theme-color.test.mjs | 22 +++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 frontend/packages/shared/src/settings/theme-color.ts create mode 100644 frontend/packages/swarm-ui/test/theme-color.test.mjs diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index d2f5215f0..c26aa16af 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -306,6 +306,7 @@ hyperhive dark, catppuccin latte mauve, catppuccin mocha mauve and catppuccin mocha mara. Each theme is light or dark, and a pick beats the OS preference. The browser remembers the choice, and hyperhive dark applies until a user picks one. The hive dashboard's menu has the same select. +The page's `theme-color` follows the theme's background, so a phone's status bar and an installed app's title bar change colour with it. The settings menu in the header has a **hide developer details** toggle. It drops the muted debug rows (coalesced ticks, harness chatter) from every diff --git a/frontend/packages/shared/src/settings/theme-apply.ts b/frontend/packages/shared/src/settings/theme-apply.ts index 9564f386d..962a05679 100644 --- a/frontend/packages/shared/src/settings/theme-apply.ts +++ b/frontend/packages/shared/src/settings/theme-apply.ts @@ -15,6 +15,7 @@ // default"). import { useEffect } from "preact/hooks"; import { useLocalSetting } from "./settings-storage.js"; +import { themeColorOf } from "./theme-color.js"; import { AUTO_THEME, DEFAULT_THEME, themeChoice } from "./themes.js"; /** `[choice, setChoice]`: `auto` or a theme id from `./themes.ts`. */ @@ -42,5 +43,21 @@ export function useApplyThemeOverride( } else { root.dataset.theme = choice; } + // The browser's own chrome follows the page background. `auto` can + // change with the OS, so it is read again then. + const sync = () => { + const color = themeColorOf( + getComputedStyle(root).getPropertyValue("--bg"), + ); + if (color === null) return; + document + .querySelector('meta[name="theme-color"]') + ?.setAttribute("content", color); + }; + sync(); + if (choice !== AUTO_THEME) return; + const scheme = window.matchMedia("(prefers-color-scheme: dark)"); + scheme.addEventListener("change", sync); + return () => scheme.removeEventListener("change", sync); }, [choice]); } diff --git a/frontend/packages/shared/src/settings/theme-color.ts b/frontend/packages/shared/src/settings/theme-color.ts new file mode 100644 index 000000000..ec9be6f10 --- /dev/null +++ b/frontend/packages/shared/src/settings/theme-color.ts @@ -0,0 +1,13 @@ +// The colour the browser draws its own chrome in (a phone's status bar, an +// installed app's title bar) comes from ``. It follows +// the page's background, so a theme swap changes it. Kept apart from the effect +// that sets it, with no imports, so a test reaches it without a DOM. + +/** A computed `--bg` as a `theme-color`, or `null` while it isn't a colour (the + * variable isn't defined yet, or holds an unresolved `var()`). */ +export function themeColorOf(computed: string): string | null { + const value = computed.trim(); + return /^(#[0-9a-f]{3,8}|(rgb|hsl|color)a?\([^)]+\))$/i.test(value) + ? value + : null; +} diff --git a/frontend/packages/swarm-ui/test/theme-color.test.mjs b/frontend/packages/swarm-ui/test/theme-color.test.mjs new file mode 100644 index 000000000..2f1c9bcee --- /dev/null +++ b/frontend/packages/swarm-ui/test/theme-color.test.mjs @@ -0,0 +1,22 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { themeColorOf } from "../../shared/src/settings/theme-color.ts"; + +test("a computed hex background is the theme colour", () => { + assert.equal(themeColorOf(" #1e1e2e "), "#1e1e2e"); + assert.equal(themeColorOf("#fff"), "#fff"); +}); + +test("a functional colour is the theme colour", () => { + assert.equal(themeColorOf("rgb(30, 30, 46)"), "rgb(30, 30, 46)"); + assert.equal( + themeColorOf("color(srgb 0.1 0.1 0.2)"), + "color(srgb 0.1 0.1 0.2)", + ); +}); + +test("anything that isn't a colour is skipped", () => { + assert.equal(themeColorOf(""), null); + assert.equal(themeColorOf("var(--base00)"), null); + assert.equal(themeColorOf("not a colour"), null); +});