Watch
0
0
Fork
You've already forked hyperhive
0

frontend: remove the stylix overlay and add hyperhive and Catppuccin light and dark themes

This commit is contained in:
iris 2026-11-22 16:43:49 +01:00 • committed by mara
commit 91e2d26133
6 changed files with 486 additions and 413 deletions

View file

@ -7,10 +7,9 @@
// sync with the actual build steps.
//
// `colors.css` is the ONE static asset deliberately left UNHASHED, at a
// fixed `dist/static/colors.css`: nix/host-modules/swarm-ui.nix's stylix
// theming swaps that exact path via an `= /static/colors.css` nginx
// location override, no npm/esbuild rebuild involved. Hashing it would
// silently break that swap. See `colors.css`'s own top comment.
// fixed `dist/static/colors.css`: nix/host-modules/swarm-ui.nix serves that
// exact path through an `= /static/colors.css` nginx location that
// revalidates it, and the swagger page links the same file.
//
// `dist/sw.js` keeps its own fixed/unhashed filename too — the browser
// polls this exact URL to detect a SW update, hashing it would make

View file

@ -1,6 +1,6 @@
/* Standalone base16 palette — re-exports the shared base16 slots so
esbuild emits its own `dist/static/colors.css`, linked first by every
swarm-ui page (before theme.css). This is the theme swap contract: a
stylix-generated variant replaces only this file. See
swarm-ui page (before theme.css). This is the theme contract: the
themes' palettes live in this one file. See
@hive/shared/colors.css + docs/4-implementation/web-ui/css-vars.md. */
@import "@hive/shared/colors.css";

View file

@ -10,7 +10,7 @@
// Network-first for the shell, not cache-first: most assets below are
// content-hashed (a fresh URL per deploy, so a normal visit always gets
// the current bundle regardless of this SW), but `colors.css` stays
// deliberately unhashed for the host-side stylix theme swap, so this
// unhashed (the swagger page links it by its fixed name), so this
// SW's own cached copy of it can only ever refresh via a real network
// round-trip. This SW only ever serves its cache on a FAILED fetch
// (offline/flaky network), never in preference to a live response.

View file

@ -3,8 +3,10 @@
// `useApplyMotionOverride`), but that is after the page has painted, so a light
// OS showed a light flash before the dark default took over. This runs from a
// blocking script in the head, as the page's policy allows no inline script. The
// keys and the fallbacks ("dark" for the theme, "system" for motion) are those of
// `shell/Shell.tsx`; `test/theme-boot.test.mjs` keeps them in step.
// keys and the fallbacks (hyperhive dark for the theme, "system" for motion) are
// those of `shell/Shell.tsx`, and the theme ids and the values from before
// themes existed are those of `@hive/shared`'s `settings/themes.ts`;
// `test/theme-boot.test.mjs` keeps them in step.
(function () {
function read(key, fallback) {
try {
@ -15,8 +17,17 @@
}
}
var root = document.documentElement;
var theme = read("swarm-ui:theme-override", "dark");
if (theme === "light" || theme === "dark") root.dataset.theme = theme;
var themes = [
"hyperhive-light",
"hyperhive-dark",
"catppuccin-latte-mauve",
"catppuccin-mocha-mauve",
"catppuccin-mocha-mara",
];
var theme = read("swarm-ui:theme-override", "hyperhive-dark");
if (theme === "light") theme = "hyperhive-light";
if (theme === "dark") theme = "hyperhive-dark";
if (themes.indexOf(theme) >= 0) root.dataset.theme = theme;
var motion = read("swarm-ui:motion-override", "system");
if (motion === "reduce" || motion === "allow") root.dataset.motion = motion;
})();

View file

@ -27,25 +27,36 @@ function boot(stored) {
return dataset;
}
test("nothing stored sets the dark default and leaves motion to the system", () => {
assert.deepEqual(boot({}), { theme: "dark" });
test("nothing stored sets the hyperhive dark default and leaves motion to the system", () => {
assert.deepEqual(boot({}), { theme: "hyperhive-dark" });
});
test("a stored light theme and a motion override are set", () => {
test("a stored theme and a motion override are set", () => {
const set = boot({
"swarm-ui:theme-override": '"light"',
"swarm-ui:motion-override": '"reduce"',
});
assert.deepEqual(set, { theme: "light", motion: "reduce" });
assert.deepEqual(set, { theme: "hyperhive-light", motion: "reduce" });
assert.deepEqual(
boot({ "swarm-ui:theme-override": '"catppuccin-mocha-mauve"' }),
{ theme: "catppuccin-mocha-mauve" },
);
});
test("system leaves the attribute off, so the media query decides", () => {
test("a stored dark, from before themes existed, is hyperhive dark", () => {
assert.deepEqual(boot({ "swarm-ui:theme-override": '"dark"' }), {
theme: "hyperhive-dark",
});
});
test("auto, and system from before themes existed, leave the attribute off, so the media query decides", () => {
assert.deepEqual(boot({ "swarm-ui:theme-override": '"auto"' }), {});
assert.deepEqual(boot({ "swarm-ui:theme-override": '"system"' }), {});
});
test("a value that is not JSON, or not one of the choices, is ignored", () => {
assert.deepEqual(boot({ "swarm-ui:theme-override": "light" }), {
theme: "dark",
theme: "hyperhive-dark",
});
assert.deepEqual(boot({ "swarm-ui:theme-override": '"blue"' }), {});
});
@ -60,7 +71,31 @@ test("storage that throws behaves as if nothing were stored", () => {
},
document: { documentElement: { dataset } },
});
assert.deepEqual(dataset, { theme: "dark" });
assert.deepEqual(dataset, { theme: "hyperhive-dark" });
});
test("the theme ids are the registry's", () => {
const registry = readFileSync(
new URL("../../shared/src/settings/themes.ts", import.meta.url),
"utf8",
);
const ids = (text) =>
[...text.matchAll(/"((?:hyperhive|catppuccin)-[a-z-]+)"/g)].map(
(m) => m[1],
);
const listed = script.slice(
script.indexOf("var themes"),
script.indexOf("];"),
);
assert.deepEqual(
ids(listed),
ids(
registry.slice(
registry.indexOf("THEMES"),
registry.indexOf("AUTO_THEME"),
),
).filter((id, i, all) => all.indexOf(id) === i),
);
});
test("the keys are the shell's", () => {

View file

@ -0,0 +1,36 @@
// The theme registry and how a stored choice reads. Run by `npm test`, which
// imports the `.ts` module as it is.
import assert from "node:assert/strict";
import { test } from "node:test";
import {
AUTO_THEME,
DEFAULT_THEME,
THEMES,
themeChoice,
} from "../../shared/src/settings/themes.ts";
test("the default is one of the themes", () => {
assert.ok(THEMES.some((t) => t.id === DEFAULT_THEME));
});
test("every theme is light or dark, and the ids and labels are distinct", () => {
for (const t of THEMES) assert.ok(["light", "dark"].includes(t.scheme));
assert.equal(new Set(THEMES.map((t) => t.id)).size, THEMES.length);
assert.equal(new Set(THEMES.map((t) => t.label)).size, THEMES.length);
});
test("a theme id and auto read as themselves", () => {
assert.equal(themeChoice("catppuccin-mocha-mauve"), "catppuccin-mocha-mauve");
assert.equal(themeChoice(AUTO_THEME), AUTO_THEME);
});
test("the values from before themes existed read as their successors", () => {
assert.equal(themeChoice("system"), AUTO_THEME);
assert.equal(themeChoice("light"), "hyperhive-light");
assert.equal(themeChoice("dark"), "hyperhive-dark");
});
test("an unknown value reads as the fallback", () => {
assert.equal(themeChoice("blue"), DEFAULT_THEME);
assert.equal(themeChoice(undefined, "hyperhive-light"), "hyperhive-light");
});