@@ -1,10 +1,11 @@
11 import { useCallback, useEffect, useLayoutEffect, useState } from "react";
2+import { nicotine, tuxedo } from "./fruit";
23 import { SKITTLES, isDark, schemeColors, skittlesPalette, toPalette, type Scheme } from "./scheme";
34
45 export type Mode = "light" | "dark";
56
67 // Every color the UI uses. Palettes and user overrides are both just maps of
7-// these; the values live in index.css as CSS variables named "kebab of key".
8+// these; the values land as CSS variables named "kebab of key".
89 export const TOKENS = [
910 "bg",
1011 "surface",
@@ -34,7 +35,7 @@ export const tokenLabels: Record<Token, string> = {
3435 };
3536
3637 // "custom" is not a palette of its own: it layers the user's color overrides on
37-// top of `base`, the built-in palette that was active when custom was chosen.
38+// top of `base`, the built-in theme that was active when custom was chosen.
3839 export const presets = [
3940 { id: "nicotine", label: "80s nicotine" },
4041 { id: "tuxedo", label: "tuxedo" },
@@ -44,6 +45,10 @@ export const presets = [
4445 export type PresetId = (typeof presets)[number]["id"];
4546 export type BasePreset = Exclude<PresetId, "custom">;
4647
48+// The built-in themes are fruit definitions like any terminal scheme, so they
49+// are only ever a source of a palette, never CSS of their own.
50+const builtin: Record<BasePreset, Record<Mode, Scheme>> = { nicotine, tuxedo };
51+
4752 export interface ThemePrefs {
4853 mode: Mode;
4954 preset: PresetId;
@@ -105,25 +110,24 @@ export function loadPrefs(): ThemePrefs {
105110 };
106111 }
107112
108-// basePalette is the built-in palette whose CSS is in effect.
113+// basePalette is the built-in theme the palette comes from.
109114 const basePalette = (p: ThemePrefs): BasePreset => (p.preset === "custom" ? p.base : p.preset);
110115
111-// applyTheme writes the mode class, the preset attribute and the user's
112-// overrides onto <html>. The palettes themselves live in CSS, so switching
113-// preset is a single attribute change.
116+// palette is the scheme→tokens expansion of the built-in theme in effect.
117+const palette = (p: ThemePrefs) => toPalette(builtin[basePalette(p)][p.mode]);
118+
119+// applyTheme writes the mode class and every token onto <html>: the built-in
120+// theme's palette, with the user's overrides on top when the preset is custom.
114121 export function applyTheme(prefs: ThemePrefs) {
115122 const el = document.documentElement;
116123 el.classList.toggle("dark", prefs.mode === "dark");
117- el.dataset.preset = basePalette(prefs);
118- for (const t of TOKENS) {
119- const v = prefs.preset === "custom" ? prefs.overrides[prefs.mode][t] : undefined;
120- if (v) el.style.setProperty(cssVar(t), v);
121- else el.style.removeProperty(cssVar(t));
122- }
124+ const p = palette(prefs);
125+ const over = prefs.preset === "custom" ? prefs.overrides[prefs.mode] : {};
126+ for (const t of TOKENS) el.style.setProperty(cssVar(t), over[t] ?? p[t]);
123127 // Skittles: --user-N are only defined while it is on; userColor() falls back
124- // to the accent otherwise. Needs the resolved bg/fg, hence after the loop.
128+ // to the accent otherwise.
125129 const colors = prefs.skittles
126- ? skittlesPalette(prefs.scheme?.colors, effective("bg"), effective("fg"), basePalette(prefs))
130+ ? skittlesPalette(prefs.scheme?.colors, p.bg, p.fg, basePalette(prefs))
127131 : [];
128132 for (let i = 0; i < SKITTLES; i++) {
129133 if (colors[i]) el.style.setProperty(`--user-${i}`, colors[i]);
@@ -134,19 +138,7 @@ export function applyTheme(prefs: ThemePrefs) {
134138 // effective returns the color a token currently resolves to, so the theme
135139 // editor can seed its color inputs with the palette values.
136140 export function effective(t: Token): string {
137- const v = getComputedStyle(document.documentElement).getPropertyValue(cssVar(t)).trim();
138- if (v.startsWith("#")) return v.length === 4 ? expand(v) : v;
139- return rgbToHex(v);
140-}
141-
142-function expand(hex: string) {
143- return "#" + hex.slice(1).replace(/./g, (c) => c + c);
144-}
145-
146-function rgbToHex(color: string) {
147- const m = color.match(/(\d+)\D+(\d+)\D+(\d+)/);
148- if (!m) return "#000000";
149- return "#" + m.slice(1, 4).map((n) => Number(n).toString(16).padStart(2, "0")).join("");
141+ return getComputedStyle(document.documentElement).getPropertyValue(cssVar(t)).trim();
150142 }
151143
152144 export function useTheme() {