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.
This commit is contained in:
parent
bcb432caf4
commit
868b8743d8
4 changed files with 53 additions and 0 deletions
|
|
@ -306,6 +306,7 @@ hyperhive dark, catppuccin latte mauve, catppuccin mocha mauve and catppuccin
|
||||||
mocha mara. Each theme is light or
|
mocha mara. Each theme is light or
|
||||||
dark, and a pick beats the OS preference. The browser remembers the choice, and
|
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.
|
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 settings menu in the header has a **hide developer details** toggle. It drops
|
||||||
the muted debug rows (coalesced ticks, harness chatter) from every
|
the muted debug rows (coalesced ticks, harness chatter) from every
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
// default").
|
// default").
|
||||||
import { useEffect } from "preact/hooks";
|
import { useEffect } from "preact/hooks";
|
||||||
import { useLocalSetting } from "./settings-storage.js";
|
import { useLocalSetting } from "./settings-storage.js";
|
||||||
|
import { themeColorOf } from "./theme-color.js";
|
||||||
import { AUTO_THEME, DEFAULT_THEME, themeChoice } from "./themes.js";
|
import { AUTO_THEME, DEFAULT_THEME, themeChoice } from "./themes.js";
|
||||||
|
|
||||||
/** `[choice, setChoice]`: `auto` or a theme id from `./themes.ts`. */
|
/** `[choice, setChoice]`: `auto` or a theme id from `./themes.ts`. */
|
||||||
|
|
@ -42,5 +43,21 @@ export function useApplyThemeOverride(
|
||||||
} else {
|
} else {
|
||||||
root.dataset.theme = choice;
|
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<HTMLMetaElement>('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]);
|
}, [choice]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
frontend/packages/shared/src/settings/theme-color.ts
Normal file
13
frontend/packages/shared/src/settings/theme-color.ts
Normal file
|
|
@ -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 `<meta name="theme-color">`. 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;
|
||||||
|
}
|
||||||
22
frontend/packages/swarm-ui/test/theme-color.test.mjs
Normal file
22
frontend/packages/swarm-ui/test/theme-color.test.mjs
Normal file
|
|
@ -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);
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue