Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

fix(core): 导入的深色主题不再留白色色带,整套灰阶跟着主题走

lmlihengeb1f11471268
diff --git a/packages/core/src/internal/external-theme.ts b/packages/core/src/internal/external-theme.ts
index 463a510b..3ac4823b 100644
--- a/packages/core/src/internal/external-theme.ts
+++ b/packages/core/src/internal/external-theme.ts
@@ -23,6 +23,13 @@
* base rule is the bare `:root` — which is why a half-filled theme warns rather than fails: it
* paints, it is just more Primer than its author meant.
*
+ * AND IT CARRIES THE PALETTE BRIDGE. Most of the app still paints with Tailwind palette classes
+ * (`bg-white`, `text-gray-500`, `border-gray-200`) rather than the token utilities the contract
+ * names, and a shipped theme keeps those following it by re-pointing Tailwind's `--color-gray-*`
+ * from its own values — the "gray bridge" at the top of `github.css` and `geek.css`. An imported
+ * theme cannot do that for itself (its names are the contract's own), so every theme this module
+ * writes gets the same bridge, out of its own tokens: see {@link PALETTE_BRIDGE_LIGHT}.
+ *
* THE LAYERS ARE NAMED (`@layer ui-theme`, `@layer ui-accent`) for the same reason: an unlayered
* rule beats every layered one, so an imported theme that declared `--ui-font-sans` unlayered
* would override the user's own font pairing, which lives in `ui-font`, last of the three.
@@ -236,6 +243,88 @@ export function readExternalTheme(value: unknown): ExternalThemeRead {
};
}
+/**
+ * The palette bridge, light mode: every Tailwind gray rung the app still paints with, and the
+ * contract token whose meaning it carries. `github.css` declares the same correspondence for
+ * itself (`--ui-surface-muted` is its `--color-gray-50`, `--ui-line-muted` its `--color-gray-100`,
+ * `--ui-line` its `--color-gray-200`, and so on into the ink), and a shipped theme writes the table
+ * out by hand because its file IS css. An imported theme cannot: its values are confined to the
+ * contract's `--ui-*` names, so a file that arrived as data would leave every `bg-white`,
+ * `bg-gray-50` and `text-gray-500` in the app on the default theme's ramp — which is why a dark
+ * imported theme showed dark `bg-white` cards (that one rung IS bridged by the default theme, to
+ * `var(--ui-surface)`) with white `bg-gray-50` bands, light `border-gray-200` borders and unreadable
+ * `text-gray-700` ink beside them. This module writes the bridge for it, out of its own tokens.
+ *
+ * WHY EVERY VALUE IS A REFERENCE AND NOT A COLOUR. `var(--ui-surface-muted)` is not this theme's
+ * `--color-gray-50` — it is whatever that token means in the mode the rule matches, so ONE table
+ * serves a light and a dark theme, and a theme that changes a token in `dark` moves the rung with
+ * it. A literal would pin the rung to a single mode, which is exactly the failure being fixed.
+ */
+const PALETTE_BRIDGE_LIGHT: ReadonlyArray<readonly [string, string]> = [
+ ["--color-gray-50", "--ui-surface-muted"],
+ ["--color-gray-100", "--ui-line-muted"],
+ ["--color-gray-200", "--ui-line"],
+ ["--color-gray-300", "--ui-line-emphasis"],
+ ["--color-gray-400", "--ui-fg-subtle"],
+ ["--color-gray-500", "--ui-fg-muted"],
+ ["--color-gray-600", "--ui-fg-muted"],
+ ["--color-gray-700", "--ui-fg"],
+ ["--color-gray-800", "--ui-fg"],
+ ["--color-gray-900", "--ui-fg-emphasis"],
+ ["--color-gray-950", "--ui-fg-emphasis"],
+];
+
+/**
+ * The palette bridge, dark mode. The ramp inverts its roles: `dark:` pairs spell the washes, lines
+ * and fills on the rungs the light side keeps for ink, and the ink on the rungs it keeps for
+ * borders. The rungs follow `geek.css`, the one shipped theme that writes its dark bridge out with
+ * its reasons — 100 carries the body ink, 300 and 400 the muted ink, 500 the subtle, 700 and 800
+ * the lines, 900 the raised fill a panel sits on and 950 the recessed one.
+ *
+ * THREE RUNGS DEPART FROM THAT SHAPE, because the app's own usage says its dark ramp is not a pure
+ * inversion: 50 stays a wash (nothing in the app spells `dark:*-50`, while `bg-gray-50` is a wash
+ * in both modes), 200 follows the lines rather than the ink (its unpaired use is `border-gray-200`,
+ * a border either way), and `--color-white` is absent on purpose — the default theme makes it
+ * `var(--ui-surface)` in every mode, which is what a card wants and what a text on a fill does not
+ * need since those fills are accents.
+ */
+const PALETTE_BRIDGE_DARK: ReadonlyArray<readonly [string, string]> = [
+ ["--color-gray-50", "--ui-surface-muted"],
+ ["--color-gray-100", "--ui-fg"],
+ ["--color-gray-200", "--ui-line"],
+ ["--color-gray-300", "--ui-fg-muted"],
+ ["--color-gray-400", "--ui-fg-muted"],
+ ["--color-gray-500", "--ui-fg-subtle"],
+ ["--color-gray-600", "--ui-fg-subtle"],
+ ["--color-gray-700", "--ui-line"],
+ ["--color-gray-800", "--ui-line"],
+ ["--color-gray-900", "--ui-surface"],
+ ["--color-gray-950", "--ui-canvas"],
+];
+
+/**
+ * One bridge rule's declarations, for the rungs whose token this theme actually declares.
+ *
+ * A rung whose token the theme leaves out is SKIPPED rather than defaulted: the default theme's
+ * own value for that token is often an alias back to the rung being set (`--ui-fg` is
+ * `var(--color-gray-900)` there), and a bridge entry pointing at it would make the two names
+ * reference each other — a cycle, which css resolves by throwing away BOTH values. The theme's own
+ * value is skipped for the same reason: a file that spells `--ui-line: var(--color-gray-200)` wants
+ * the default ramp it inherited, not a loop through its own name.
+ */
+function paletteBridge(
+ rungs: ReadonlyArray<readonly [string, string]>,
+ declared: Readonly<Record<string, string>>,
+): Record<string, string> {
+ const out: Record<string, string> = {};
+ for (const [name, token] of rungs) {
+ const value = declared[token];
+ if (value === undefined || value.includes("--color-")) continue;
+ out[name] = `var(${token})`;
+ }
+ return out;
+}
+
function declarations(tokens: Readonly<Record<string, string>>): string {
return Object.entries(tokens)
.map(([name, value]) => `${name}:${value}`)
@@ -257,8 +346,21 @@ export function externalThemeCss(theme: ExternalTheme): string {
const [base, baseDark] = roots(theme.id, null);
out.push("@layer ui-theme{");
out.push(`${base},${baseDark}{${declarations(theme.light)}}`);
- if (theme.dark !== undefined && Object.keys(theme.dark).length > 0) {
- out.push(`${baseDark}{${declarations(theme.dark)}}`);
+ // The palette bridge follows the rule it bridges (and the dark one follows the dark rule, which
+ // is what lets it win that selector: both carry `.dark`, so the later rule is the one that holds).
+ const lightBridge = paletteBridge(PALETTE_BRIDGE_LIGHT, theme.light);
+ if (Object.keys(lightBridge).length > 0) {
+ out.push(`${base},${baseDark}{${declarations(lightBridge)}}`);
+ }
+ const dark = theme.dark;
+ if (dark !== undefined && Object.keys(dark).length > 0) {
+ out.push(`${baseDark}{${declarations(dark)}}`);
+ // A theme with no dark rule keeps its own values in dark mode, and this bridge keeps its light
+ // mapping with them; one is emitted only where the theme says what dark changes.
+ const darkBridge = paletteBridge(PALETTE_BRIDGE_DARK, { ...theme.light, ...dark });
+ if (Object.keys(darkBridge).length > 0) {
+ out.push(`${baseDark}{${declarations(darkBridge)}}`);
+ }
}
out.push("}");
const accents = theme.accents ?? [];
diff --git a/packages/core/test/external-theme.test.ts b/packages/core/test/external-theme.test.ts
index db0b966d..751c921d 100644
--- a/packages/core/test/external-theme.test.ts
+++ b/packages/core/test/external-theme.test.ts
@@ -165,6 +165,105 @@ describe("externalThemeCss", () => {
expect(externalThemesCss([])).toContain("no imported themes");
expect(externalThemesCss([read(VALID)])).toContain('data-theme="warm-paper"');
});
+
+ /**
+ * The palette bridge: most of the app still spells `bg-white` / `text-gray-500` /
+ * `border-gray-200`, and a shipped theme keeps those following it by re-pointing Tailwind's
+ * `--color-gray-*` from its own values. An imported theme cannot declare those names itself, so
+ * without this it paints its own tokens dark and leaves every palette-class rung on the default
+ * theme's ramp — dark cards with white header bands, light borders and unreadable ink.
+ */
+ const FULL = {
+ id: "full",
+ name: "Full",
+ light: {
+ "--ui-canvas": "#ffffff",
+ "--ui-surface": "#ffffff",
+ "--ui-surface-muted": "#f1f0ee",
+ "--ui-line-muted": "#eeeeee",
+ "--ui-line": "#dddddd",
+ "--ui-line-emphasis": "#cccccc",
+ "--ui-fg-subtle": "#999999",
+ "--ui-fg-muted": "#777777",
+ "--ui-fg": "#222222",
+ "--ui-fg-emphasis": "#000000",
+ },
+ dark: {
+ "--ui-canvas": "#111111",
+ "--ui-surface": "#1a1a1a",
+ "--ui-surface-muted": "#222222",
+ "--ui-line": "#333333",
+ "--ui-fg": "#eeeeee",
+ "--ui-fg-emphasis": "#ffffff",
+ },
+ };
+
+ it("re-points Tailwind's gray ramp onto the theme's own tokens, in both modes", () => {
+ const css = externalThemeCss(read(FULL));
+ // Light: the correspondence the default theme declares for itself, expressed in this theme's
+ // tokens instead of its literals — so a dark imported theme moves the whole ramp with it.
+ expect(css).toContain(
+ ':root[data-theme="full"],:root[data-theme="full"].dark{--color-gray-50:var(--ui-surface-muted);' +
+ "--color-gray-100:var(--ui-line-muted);--color-gray-200:var(--ui-line);" +
+ "--color-gray-300:var(--ui-line-emphasis);--color-gray-400:var(--ui-fg-subtle);" +
+ "--color-gray-500:var(--ui-fg-muted);--color-gray-600:var(--ui-fg-muted);" +
+ "--color-gray-700:var(--ui-fg);--color-gray-800:var(--ui-fg);" +
+ "--color-gray-900:var(--ui-fg-emphasis);--color-gray-950:var(--ui-fg-emphasis)}",
+ );
+ // Dark: the ramp inverts — the low rungs carry the ink, the high ones the fills. Emitted after
+ // the light bridge, which is what lets it win the `.dark` selector the two share.
+ expect(css).toContain(
+ ':root[data-theme="full"].dark{--color-gray-50:var(--ui-surface-muted);' +
+ "--color-gray-100:var(--ui-fg);--color-gray-200:var(--ui-line);" +
+ "--color-gray-300:var(--ui-fg-muted);--color-gray-400:var(--ui-fg-muted);" +
+ "--color-gray-500:var(--ui-fg-subtle);--color-gray-600:var(--ui-fg-subtle);" +
+ "--color-gray-700:var(--ui-line);--color-gray-800:var(--ui-line);" +
+ "--color-gray-900:var(--ui-surface);--color-gray-950:var(--ui-canvas)}",
+ );
+ // `--color-white` is left to the default theme: it is `var(--ui-surface)` there in every mode.
+ expect(css).not.toContain("--color-white");
+ // The dark bridge is the LATER of the two, which is what makes it win the `.dark` selector they
+ // share (same specificity, so the cascade decides by position).
+ expect(css.indexOf("--color-gray-100:var(--ui-fg)")).toBeGreaterThan(
+ css.indexOf("--color-gray-100:var(--ui-line-muted)"),
+ );
+ });
+
+ it("bridges only the rungs whose token this theme declares", () => {
+ // `warm-paper` declares ink and a canvas and nothing else: no line, wash or subtle rung is
+ // bridged, because pointing one at a token the default theme aliases back onto that same rung
+ // would leave css with a cycle — which it resolves by discarding both values.
+ const css = externalThemeCss(read(VALID));
+ expect(css).toContain(
+ ':root[data-theme="warm-paper"],:root[data-theme="warm-paper"].dark' +
+ "{--color-gray-700:var(--ui-fg);--color-gray-800:var(--ui-fg)}",
+ );
+ expect(css).not.toContain("--color-gray-200");
+ expect(css).not.toContain("--color-gray-500");
+ expect(css).not.toContain("--color-gray-50:");
+ });
+
+ it("leaves a rung alone when the theme's own value is an alias back into the palette", () => {
+ const css = externalThemeCss(
+ read({
+ id: "aliased",
+ name: "Aliased",
+ light: { "--ui-line": "var(--color-gray-200)", "--ui-fg": "#222222" },
+ }),
+ );
+ expect(css).toContain("--ui-line:var(--color-gray-200)");
+ expect(css).not.toContain("--color-gray-200:var(--ui-line)");
+ // The rung beside it, whose token is a plain colour, is still bridged.
+ expect(css).toContain("--color-gray-700:var(--ui-fg)");
+ });
+
+ it("writes no dark bridge for a theme that only sets light values", () => {
+ const css = externalThemeCss(
+ read({ id: "bare", name: "Bare", light: { "--ui-fg": "#111111", "--ui-canvas": "#ffffff" } }),
+ );
+ expect(css).toContain("--color-gray-700:var(--ui-fg)");
+ expect(css).not.toContain("--color-gray-950");
+ });
});
describe("the id pattern", () => {
diff --git a/packages/server/test/themes.test.ts b/packages/server/test/themes.test.ts
index 71b295d4..6861f5a4 100644
--- a/packages/server/test/themes.test.ts
+++ b/packages/server/test/themes.test.ts
@@ -151,9 +151,13 @@ describe("imported themes", () => {
const css = await (await t.app.request("/api/themes.css")).text();
// Light first and both modes in one base rule, dark only what it changes — so the sheet needs
- // no promise about which stylesheet the browser read first.
+ // no promise about which stylesheet the browser read first. The bridge rule after the dark one
+ // re-points Tailwind's gray rungs onto the theme's own tokens, which is how a theme makes the
+ // app's `bg-white` / `text-gray-500` follow it; this theme declares a canvas and a surface and
+ // no ink, so exactly those two rungs are bridged and only under `.dark` — the one mode it
+ // changes. (core's external-theme.test.ts pins the mapping in full.)
expect(css).toContain(
- '@layer ui-theme{\n:root[data-theme="warm-paper"],:root[data-theme="warm-paper"].dark{--ui-canvas:#f7f3ec;--ui-surface:#fffdf8}\n:root[data-theme="warm-paper"].dark{--ui-canvas:#16130f}\n}',
+ '@layer ui-theme{\n:root[data-theme="warm-paper"],:root[data-theme="warm-paper"].dark{--ui-canvas:#f7f3ec;--ui-surface:#fffdf8}\n:root[data-theme="warm-paper"].dark{--ui-canvas:#16130f}\n:root[data-theme="warm-paper"].dark{--color-gray-900:var(--ui-surface);--color-gray-950:var(--ui-canvas)}\n}',
);
// A preset is scoped to its own theme as well: it paints only under the theme that lists it.
expect(css).toContain(