@celestia-island/hikari 0.55.485 → 0.55.488
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/types/theme/presets.d.ts +31 -17
- package/dist/types/theme/themeBoot.d.ts +43 -0
- package/dist/types/theme/themeVarsCss.d.ts +42 -0
- package/package.json +1 -1
- package/src/styles/theme-boot.js +48 -1
- package/src/styles/theme-vars.css +75 -73
- package/src/theme/presets.ts +38 -89
- package/src/theme/stock-preset.json +1 -0
- package/src/theme/stock-vars.json +1 -0
- package/src/theme/themeBoot.contract.test.ts +115 -5
- package/src/theme/themeBoot.ts +68 -2
- package/src/theme/themeVarsCss.contract.test.ts +35 -1
- package/src/theme/themeVarsCss.ts +25 -9
- package/src/theme/useTheme.test.ts +47 -0
- package/src/theme/useTheme.ts +67 -1
|
@@ -85,29 +85,43 @@ export interface CustomThemePreset {
|
|
|
85
85
|
light: ThemeSchemeTokens;
|
|
86
86
|
groups?: ThemeTokenGroupModes;
|
|
87
87
|
}
|
|
88
|
+
/** The runtime's own memo of a CUSTOM theme's computed cssvars, keyed by
|
|
89
|
+
* theme id then mode, so the PRE-PAINT boot asset can replay the colors
|
|
90
|
+
* without re-implementing (or duplicating) the color math (hikari#1546
|
|
91
|
+
* point 2). Written by applyTheme for non-stock themes only — the stock
|
|
92
|
+
* preset already ships as the static sheet.
|
|
93
|
+
*
|
|
94
|
+
* VERSION DISCIPLINE: the memo bakes THREE inputs, and any of them
|
|
95
|
+
* changing makes a stored payload wrong —
|
|
96
|
+
* 1. `tokensToCSSVars` / its OKLCH-contrast helpers (this repo),
|
|
97
|
+
* 2. the token GROUPS a host registers (their resolved defaults feed the
|
|
98
|
+
* memo through `groupTokensToCSSVars`),
|
|
99
|
+
* 3. a host's own preset table (`window.__celestiaThemes` / in-place
|
|
100
|
+
* preset overrides), whose values are memoized like any custom theme.
|
|
101
|
+
* (1) is ours to police in review; (2) and (3) live in consumers, so the
|
|
102
|
+
* version is also the hatch for a HOST-driven refresh — a host that changes
|
|
103
|
+
* a group default or a preset's colors bumps nothing here, and its users
|
|
104
|
+
* keep one load of the previous palette. That is accepted (the memo is a
|
|
105
|
+
* first-paint hint, `applyTheme` overwrites it immediately) but it is why
|
|
106
|
+
* this is a hint and not a cache of record. */
|
|
107
|
+
export declare const THEME_VARS_MEMO_KEY = "hikari-theme-vars";
|
|
108
|
+
export declare const THEME_VARS_MEMO_VERSION = 1;
|
|
88
109
|
export declare function loadCustomThemes(): CustomThemePreset[];
|
|
89
110
|
export declare function saveCustomThemes(list: CustomThemePreset[]): void;
|
|
90
111
|
export declare function addCustomTheme(theme: CustomThemePreset): void;
|
|
91
112
|
export declare function removeCustomTheme(id: string): void;
|
|
92
113
|
/**
|
|
93
|
-
* The stock preset table holds exactly ONE theme
|
|
94
|
-
*
|
|
95
|
-
* `
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
* that wants a distinct brand registers its own preset (chest's `sc` is the
|
|
101
|
-
* reference), and a user who wants another look edits this one through the
|
|
102
|
-
* theme dialog and keeps it as a custom scheme.
|
|
114
|
+
* The stock preset table holds exactly ONE theme, and its VALUES are no
|
|
115
|
+
* longer written here: they live in the Rust palette crate
|
|
116
|
+
* (`packages/palette/src/stock.rs`, hikari#1546 Phase 2) and arrive as the
|
|
117
|
+
* checked-in JSON artifact above. Editing a color means editing Rust and
|
|
118
|
+
* regenerating (`REGEN_STOCK_JSON=1 cargo test -p hikari-palette --test
|
|
119
|
+
* stock_snapshot`) — the artifact snapshot test in that crate keeps the
|
|
120
|
+
* two sides locked.
|
|
103
121
|
*
|
|
104
|
-
*
|
|
105
|
-
* `
|
|
106
|
-
*
|
|
107
|
-
* Water primary (#88C0D0) and, with it, the near-black on-solid ink slots
|
|
108
|
-
* that pale primary requires (white on it is ~1.9:1). A stored id from the
|
|
109
|
-
* retired set is no longer a known theme, so `storedThemeId()` drops it and
|
|
110
|
-
* the engine resolves this default instead.
|
|
122
|
+
* A consumer that wants a distinct brand still registers its own preset
|
|
123
|
+
* (chest's `sc` is the reference), and a user who wants another look edits
|
|
124
|
+
* the default through the theme dialog and keeps it as a custom scheme.
|
|
111
125
|
*/
|
|
112
126
|
export declare const themePresets: Record<ThemeId, ThemePreset>;
|
|
113
127
|
export type ThemeTokens = ThemeSchemeTokens;
|
|
@@ -1,3 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The PRE-PAINT theme resolver and its shipped boot asset (hikari#1546
|
|
3
|
+
* Phase 1.5, user direction 2026-11-25: 一整套统一 — one resolution
|
|
4
|
+
* executed on BOTH sides of hydration).
|
|
5
|
+
*
|
|
6
|
+
* `resolveSystemModeAt` runs the solar chain's SYNC path exactly as the
|
|
7
|
+
* reactive runtime resolves mode "system": manual config (when the stored
|
|
8
|
+
* strategy selects it and the payload validates) → the timezone estimate
|
|
9
|
+
* (stock default latitude + UTC-offset longitude) → solar altitude →
|
|
10
|
+
* day → light, else dark. Dependency-free by design (safeStorage only —
|
|
11
|
+
* no vue, no cronBus) so it can ride the pre-paint asset.
|
|
12
|
+
*
|
|
13
|
+
* Two documented divergences from the reactive runtime, both narrow:
|
|
14
|
+
* - strategy "ip": the runtime may improve the coords via a
|
|
15
|
+
* granted-browser-geolocation / IP cascade (async, networked). The boot
|
|
16
|
+
* asset cannot — it uses the timezone estimate. The runtime's answer
|
|
17
|
+
* lands with the bundle and self-heals.
|
|
18
|
+
* - stale stored values: the runtime's readers also REMOVE bad payloads;
|
|
19
|
+
* the boot asset is read-only (pre-paint storage writes would be a
|
|
20
|
+
* surprise) and falls back instead. Removal happens at initTheme.
|
|
21
|
+
*
|
|
22
|
+
* It also REPLAYS the runtime's memo for a user's custom theme (see the
|
|
23
|
+
* CUSTOM THEME FIRST PAINT block in the emitted wrapper): the boot never
|
|
24
|
+
* composes a palette itself, so there is no second implementation of the
|
|
25
|
+
* color math to drift. `applyTheme` retires the replayed block on its first
|
|
26
|
+
* run, so a memo can never outlive the paint it was written for.
|
|
27
|
+
*
|
|
28
|
+
* `buildThemeBootJs()` ships the chain as a dependency-free IIFE for
|
|
29
|
+
* consumers to inline in `<head>` (lagrange interpolates it with a `?raw`
|
|
30
|
+
* import). CONSUMERS MUST INLINE IT AFTER THEIR STYLESHEET LINKS AND BEFORE
|
|
31
|
+
* `</head>`: every var-bearing block (the static `html[data-mode]` sheet,
|
|
32
|
+
* this asset's `html:root` replay, the runtime's own `html:root` block) has
|
|
33
|
+
* the same specificity, so HEAD ORDER is the only thing deciding who wins,
|
|
34
|
+
* and the intended chain is sheet -> replay -> runtime. The math is
|
|
35
|
+
* serialized via `Function.prototype.toString` from
|
|
36
|
+
* useSolarTime's OWN definitions — the asset cannot drift from the
|
|
37
|
+
* runtime's solar chain by construction; `themeBoot.contract.test.ts`
|
|
38
|
+
* executes the emitted asset against `resolveSystemModeAt` across a date
|
|
39
|
+
* grid to pin the wrapper (storage reads + attribute writes) instead.
|
|
40
|
+
*/
|
|
41
|
+
/** Marks the boot-injected vars block so the runtime (and tests) can tell
|
|
42
|
+
* it apart from useTheme's own `data-hikari-theme-vars` block. */
|
|
43
|
+
export declare const THEME_BOOT_STYLE_ATTR = "data-hikari-boot-vars";
|
|
1
44
|
/** A read-only storage surface — the seam that lets the contract test feed
|
|
2
45
|
* the resolver the SAME values the emitted asset reads (a test runner may
|
|
3
46
|
* have no `localStorage` at all, and comparing against the environment's
|
|
@@ -1 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The PRE-RENDERED static form of the stock theme table (hikari#1546).
|
|
3
|
+
*
|
|
4
|
+
* `applyTheme()` injects the whole cssvar set from JS, which is why a
|
|
5
|
+
* phone's first paint used to flash the unthemed default: no JS, no vars.
|
|
6
|
+
* This module composes the SAME `tokensToCSSVars` output the runtime uses
|
|
7
|
+
* into plain CSS blocks keyed on the `data-mode` attribute — the SSR
|
|
8
|
+
* template ships it in `<head>`, and a tiny inline bootstrap only sets
|
|
9
|
+
* `data-theme`/`data-mode` (attribute writes, no computation) before body
|
|
10
|
+
* paint. The cascade picks the block instantly.
|
|
11
|
+
*
|
|
12
|
+
* Selectors are mode-keyed and deliberately NOT theme-keyed: a stored
|
|
13
|
+
* CUSTOM theme id cannot be pre-rendered (user-authored, lives in
|
|
14
|
+
* localStorage), so pre-hydration those users see the stock colors in the
|
|
15
|
+
* right MODE — strictly better than the previous unthemed flash — and
|
|
16
|
+
* `applyTheme()` replaces them the moment the bundle lands. Override
|
|
17
|
+
* mechanics: the runtime writes a managed `<style data-hikari-theme-vars>`
|
|
18
|
+
* block whose selector (`html:root`, 0-1-1) TIES with these `html[data-mode]`
|
|
19
|
+
* blocks — the runtime wins by head ORDER (its block is appended after the
|
|
20
|
+
* SSR sheet), so the static sheet must stay in the SSR head; a late/async
|
|
21
|
+
* -loaded copy would re-tie and could snap the theme back.
|
|
22
|
+
*
|
|
23
|
+
* Phase 2 (de-Vue): the table itself moves to the Rust palette crate
|
|
24
|
+
* (tairitsu lane) and this module consumes generated JSON — the CSS form
|
|
25
|
+
* and the bootstrap contract stay exactly as they are here.
|
|
26
|
+
*
|
|
27
|
+
* Group tokens (shape/radius) are intentionally NOT included: every
|
|
28
|
+
* component reads them with its own CSS fallback (e.g.
|
|
29
|
+
* `var(--hk-radius-sm, 4px)` in HkSkeleton, `var(--hk-z-*)`,
|
|
30
|
+
* `var(--hk-picker-*)`), so the pre-hydration window degrades to identical
|
|
31
|
+
* geometry, and the runtime emission covers them anyway.
|
|
32
|
+
*/
|
|
33
|
+
/** The typed view of the Rust artifact: the serde BTreeMap arrives as two
|
|
34
|
+
* string-keyed maps. Declared explicitly because the JSON import's literal
|
|
35
|
+
* type defeats `stockVarsJson[mode]` under the strict build (vue-tsc
|
|
36
|
+
* TS7053 — the release lane caught it, the esbuild test transform did not). */
|
|
37
|
+
export interface StockVarsArtifact {
|
|
38
|
+
dark: Record<string, string>;
|
|
39
|
+
light: Record<string, string>;
|
|
40
|
+
}
|
|
41
|
+
declare const stockVars: StockVarsArtifact;
|
|
1
42
|
export declare function buildThemeVarsCss(): string;
|
|
43
|
+
export { stockVars };
|
package/package.json
CHANGED
package/src/styles/theme-boot.js
CHANGED
|
@@ -99,7 +99,54 @@ try {
|
|
|
99
99
|
if (!(m === "light" || m === "dark")) {
|
|
100
100
|
m = alt > 6 ? "light" : "dark";
|
|
101
101
|
}
|
|
102
|
-
|
|
102
|
+
var themeId = t || (typeof window !== "undefined" && window.__celestiaDefaultTheme) || "default";
|
|
103
|
+
d.setAttribute("data-theme", themeId);
|
|
103
104
|
d.setAttribute("data-mode", m);
|
|
105
|
+
|
|
106
|
+
/* CUSTOM THEME FIRST PAINT (hikari#1546 point 2). The static sheet only
|
|
107
|
+
* carries the stock preset, so a user's own theme used to paint stock
|
|
108
|
+
* colors and snap. applyTheme() now memoizes its computed vars
|
|
109
|
+
* (hikari-theme-vars, versioned); this block REPLAYS that memo — no
|
|
110
|
+
* color math is duplicated here, the runtime stays the single source.
|
|
111
|
+
* The block is injected as the LAST <head> child that exists at this
|
|
112
|
+
* point and uses html:root, so it ties with the runtime's own block
|
|
113
|
+
* (which is appended later and therefore wins once the bundle lands)
|
|
114
|
+
* and outranks the static html[data-mode] sheet by document order. */
|
|
115
|
+
try {
|
|
116
|
+
/* A crafted/edited memo could otherwise smuggle a brace or semicolon into a key
|
|
117
|
+
* and escape the html:root block into arbitrary selectors. localStorage
|
|
118
|
+
* is same-origin (writing it already needs live JS), so this is
|
|
119
|
+
* hardening rather than a trust boundary — but the boot runs before ANY
|
|
120
|
+
* app code, which is the one place we cannot afford to trust it. */
|
|
121
|
+
var SAFE_KEY = /^--[A-Za-z0-9_-]+$/;
|
|
122
|
+
var SAFE_VALUE = /^[A-Za-z0-9 .,%()/#-]*$/;
|
|
123
|
+
{
|
|
124
|
+
var memo = JSON.parse(read("hikari-theme-vars") || "null");
|
|
125
|
+
var memoV = 1;
|
|
126
|
+
if (memo && memo.v === memoV) {
|
|
127
|
+
var entry = memo[themeId];
|
|
128
|
+
/* EXACT mode only: replaying the other mode's palette onto this
|
|
129
|
+
* one's background is worse than the stock colors the static sheet
|
|
130
|
+
* already paints, so a missing entry simply falls through. */
|
|
131
|
+
var vars = entry && entry[m];
|
|
132
|
+
if (vars && typeof vars === "object") {
|
|
133
|
+
var css = "";
|
|
134
|
+
for (var k in vars) {
|
|
135
|
+
if (!Object.prototype.hasOwnProperty.call(vars, k)) continue;
|
|
136
|
+
if (!SAFE_KEY.test(k)) continue;
|
|
137
|
+
var value = "" + vars[k];
|
|
138
|
+
if (!SAFE_VALUE.test(value)) continue;
|
|
139
|
+
css += k + ":" + value + ";";
|
|
140
|
+
}
|
|
141
|
+
if (css) {
|
|
142
|
+
var styleEl = document.createElement("style");
|
|
143
|
+
styleEl.setAttribute("data-hikari-boot-vars", "");
|
|
144
|
+
styleEl.textContent = "html:root{" + css + "}";
|
|
145
|
+
(document.head || d).appendChild(styleEl);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
} catch (e) {}
|
|
104
151
|
} catch (e) {}
|
|
105
152
|
})();
|
|
@@ -1,117 +1,119 @@
|
|
|
1
|
-
/* GENERATED
|
|
2
|
-
*
|
|
3
|
-
*
|
|
1
|
+
/* GENERATED from the Rust palette crate's derivation (packages/palette/
|
|
2
|
+
* src/derivation.rs) via the stock-vars.json artifact — do not edit by
|
|
3
|
+
* hand. The drift contract pins the TypeScript derivation to these
|
|
4
|
+
* values key-for-key; regen is REGEN_STOCK_JSON=1 cargo test
|
|
5
|
+
* -p hikari-palette --test stock_snapshot. */
|
|
4
6
|
html[data-mode="dark"] {
|
|
5
|
-
--color-
|
|
7
|
+
--color-accent: 94 129 172;
|
|
8
|
+
--color-background: 22 27 38;
|
|
9
|
+
--color-border: 128 128 128;
|
|
10
|
+
--color-error: 166 41 55;
|
|
11
|
+
--color-focused-border: 136 192 208;
|
|
12
|
+
--color-info: 0 98 116;
|
|
13
|
+
--color-muted: 180 180 180;
|
|
14
|
+
--color-on-danger: 255 255 255;
|
|
15
|
+
--color-on-info: 255 255 255;
|
|
6
16
|
--color-on-primary: 0 0 0;
|
|
7
|
-
--color-on-solid-text: 46 52 64;
|
|
8
|
-
--color-on-solid-icon: 46 52 64;
|
|
9
17
|
--color-on-solid: 46 52 64;
|
|
10
|
-
--color-on-
|
|
18
|
+
--color-on-solid-icon: 46 52 64;
|
|
19
|
+
--color-on-solid-text: 46 52 64;
|
|
11
20
|
--color-on-success: 255 255 255;
|
|
12
21
|
--color-on-warning: 255 255 255;
|
|
13
|
-
--color-
|
|
22
|
+
--color-primary: 136 192 208;
|
|
14
23
|
--color-secondary: 143 188 187;
|
|
15
|
-
--color-accent: 94 129 172;
|
|
16
|
-
--color-text: 247 249 252;
|
|
17
|
-
--color-text-secondary: 230 232 234;
|
|
18
|
-
--color-text-tertiary: 214 215 216;
|
|
19
|
-
--color-muted: 180 180 180;
|
|
20
|
-
--color-border: 128 128 128;
|
|
21
|
-
--color-focused-border: 136 192 208;
|
|
22
|
-
--color-background: 22 27 38;
|
|
23
|
-
--color-surface: 34 40 54;
|
|
24
24
|
--color-selected-bg: 70 70 85;
|
|
25
25
|
--color-selected-text: 248 248 250;
|
|
26
26
|
--color-status-bar-bg: 34 40 54;
|
|
27
27
|
--color-success: 31 101 48;
|
|
28
|
-
--color-
|
|
28
|
+
--color-surface: 34 40 54;
|
|
29
|
+
--color-text: 247 249 252;
|
|
30
|
+
--color-text-secondary: 230 232 234;
|
|
31
|
+
--color-text-tertiary: 214 215 216;
|
|
29
32
|
--color-warning: 137 72 0;
|
|
30
|
-
--color-info: 0 98 116;
|
|
31
|
-
--hi-color-primary: rgb(var(--color-primary));
|
|
32
|
-
--hi-color-secondary: rgb(var(--color-secondary));
|
|
33
|
-
--hi-color-surface: rgb(var(--color-surface));
|
|
34
33
|
--hi-color-background: rgb(var(--color-background));
|
|
34
|
+
--hi-color-bg-canvas: rgb(var(--color-background));
|
|
35
|
+
--hi-color-bg-container: rgb(var(--color-surface));
|
|
36
|
+
--hi-color-bg-elevated: rgb(var(--color-surface));
|
|
37
|
+
--hi-color-bg-subtle: rgb(var(--color-surface) / 0.5);
|
|
35
38
|
--hi-color-border: rgb(var(--color-border) / 15%);
|
|
36
|
-
--hi-color-
|
|
37
|
-
--hi-color-
|
|
39
|
+
--hi-color-danger-fg: rgb(233 104 109);
|
|
40
|
+
--hi-color-error: rgb(var(--color-error));
|
|
41
|
+
--hi-color-icon-on-solid: rgb(var(--color-on-solid-icon, 255 255 255));
|
|
42
|
+
--hi-color-info: rgb(var(--color-info));
|
|
43
|
+
--hi-color-info-fg: rgb(81 158 177);
|
|
38
44
|
--hi-color-muted: rgb(var(--color-muted));
|
|
39
45
|
--hi-color-on-primary: rgb(var(--color-on-primary));
|
|
40
|
-
--hi-color-
|
|
41
|
-
--hi-color-
|
|
42
|
-
--hi-color-icon-on-solid: rgb(var(--color-on-solid-icon, 255 255 255));
|
|
46
|
+
--hi-color-primary: rgb(var(--color-primary));
|
|
47
|
+
--hi-color-secondary: rgb(var(--color-secondary));
|
|
43
48
|
--hi-color-success: rgb(var(--color-success));
|
|
44
|
-
--hi-color-
|
|
45
|
-
--hi-color-
|
|
46
|
-
--hi-color-info: rgb(var(--color-info));
|
|
49
|
+
--hi-color-success-fg: rgb(94 162 105);
|
|
50
|
+
--hi-color-surface: rgb(var(--color-surface));
|
|
47
51
|
--hi-color-text-on-danger: rgb(var(--color-on-danger));
|
|
52
|
+
--hi-color-text-on-info: rgb(var(--color-on-info));
|
|
53
|
+
--hi-color-text-on-primary: rgb(var(--color-on-primary));
|
|
54
|
+
--hi-color-text-on-solid: rgb(var(--color-on-solid-text, 255 255 255));
|
|
48
55
|
--hi-color-text-on-success: rgb(var(--color-on-success));
|
|
49
56
|
--hi-color-text-on-warning: rgb(var(--color-on-warning));
|
|
50
|
-
--hi-color-text-
|
|
51
|
-
--hi-color-
|
|
52
|
-
--hi-color-
|
|
57
|
+
--hi-color-text-primary: rgb(var(--color-text));
|
|
58
|
+
--hi-color-text-secondary: rgb(var(--color-text-secondary));
|
|
59
|
+
--hi-color-warning: rgb(var(--color-warning));
|
|
53
60
|
--hi-color-warning-fg: rgb(200 130 73);
|
|
54
|
-
--hi-color-info-fg: rgb(81 158 177);
|
|
55
|
-
--hi-color-bg-subtle: rgb(var(--color-surface) / 0.5);
|
|
56
|
-
--hi-color-bg-elevated: rgb(var(--color-surface));
|
|
57
|
-
--hi-color-bg-container: rgb(var(--color-surface));
|
|
58
|
-
--hi-color-bg-canvas: rgb(var(--color-background));
|
|
59
61
|
--hi-secondary-bg: rgb(var(--color-surface) / 0.5);
|
|
60
62
|
}
|
|
61
63
|
html[data-mode="light"] {
|
|
62
|
-
--color-
|
|
64
|
+
--color-accent: 230 167 0;
|
|
65
|
+
--color-background: 245 245 240;
|
|
66
|
+
--color-border: 128 128 128;
|
|
67
|
+
--color-error: 166 41 55;
|
|
68
|
+
--color-focused-border: 214 51 132;
|
|
69
|
+
--color-info: 0 98 116;
|
|
70
|
+
--color-muted: 80 80 80;
|
|
71
|
+
--color-on-danger: 255 255 255;
|
|
72
|
+
--color-on-info: 255 255 255;
|
|
63
73
|
--color-on-primary: 255 255 255;
|
|
64
|
-
--color-on-solid-text: 255 255 255;
|
|
65
|
-
--color-on-solid-icon: 255 255 255;
|
|
66
74
|
--color-on-solid: 255 255 255;
|
|
67
|
-
--color-on-
|
|
75
|
+
--color-on-solid-icon: 255 255 255;
|
|
76
|
+
--color-on-solid-text: 255 255 255;
|
|
68
77
|
--color-on-success: 255 255 255;
|
|
69
78
|
--color-on-warning: 255 255 255;
|
|
70
|
-
--color-
|
|
79
|
+
--color-primary: 214 51 132;
|
|
71
80
|
--color-secondary: 156 106 222;
|
|
72
|
-
--color-accent: 230 167 0;
|
|
73
|
-
--color-text: 30 30 30;
|
|
74
|
-
--color-text-secondary: 43 43 43;
|
|
75
|
-
--color-text-tertiary: 55 55 55;
|
|
76
|
-
--color-muted: 80 80 80;
|
|
77
|
-
--color-border: 128 128 128;
|
|
78
|
-
--color-focused-border: 214 51 132;
|
|
79
|
-
--color-background: 245 245 240;
|
|
80
|
-
--color-surface: 255 255 255;
|
|
81
81
|
--color-selected-bg: 200 200 205;
|
|
82
82
|
--color-selected-text: 40 40 45;
|
|
83
83
|
--color-status-bar-bg: 230 230 225;
|
|
84
84
|
--color-success: 31 101 48;
|
|
85
|
-
--color-
|
|
85
|
+
--color-surface: 255 255 255;
|
|
86
|
+
--color-text: 30 30 30;
|
|
87
|
+
--color-text-secondary: 43 43 43;
|
|
88
|
+
--color-text-tertiary: 55 55 55;
|
|
86
89
|
--color-warning: 137 72 0;
|
|
87
|
-
--color-info: 0 98 116;
|
|
88
|
-
--hi-color-primary: rgb(var(--color-primary));
|
|
89
|
-
--hi-color-secondary: rgb(var(--color-secondary));
|
|
90
|
-
--hi-color-surface: rgb(var(--color-surface));
|
|
91
90
|
--hi-color-background: rgb(var(--color-background));
|
|
91
|
+
--hi-color-bg-canvas: rgb(var(--color-background));
|
|
92
|
+
--hi-color-bg-container: rgb(var(--color-surface));
|
|
93
|
+
--hi-color-bg-elevated: rgb(var(--color-surface));
|
|
94
|
+
--hi-color-bg-subtle: rgb(var(--color-surface) / 0.5);
|
|
92
95
|
--hi-color-border: rgb(var(--color-border) / 15%);
|
|
93
|
-
--hi-color-
|
|
94
|
-
--hi-color-
|
|
96
|
+
--hi-color-danger-fg: rgb(166 41 55);
|
|
97
|
+
--hi-color-error: rgb(var(--color-error));
|
|
98
|
+
--hi-color-icon-on-solid: rgb(var(--color-on-solid-icon, 255 255 255));
|
|
99
|
+
--hi-color-info: rgb(var(--color-info));
|
|
100
|
+
--hi-color-info-fg: rgb(0 98 116);
|
|
95
101
|
--hi-color-muted: rgb(var(--color-muted));
|
|
96
102
|
--hi-color-on-primary: rgb(var(--color-on-primary));
|
|
97
|
-
--hi-color-
|
|
98
|
-
--hi-color-
|
|
99
|
-
--hi-color-icon-on-solid: rgb(var(--color-on-solid-icon, 255 255 255));
|
|
103
|
+
--hi-color-primary: rgb(var(--color-primary));
|
|
104
|
+
--hi-color-secondary: rgb(var(--color-secondary));
|
|
100
105
|
--hi-color-success: rgb(var(--color-success));
|
|
101
|
-
--hi-color-
|
|
102
|
-
--hi-color-
|
|
103
|
-
--hi-color-info: rgb(var(--color-info));
|
|
106
|
+
--hi-color-success-fg: rgb(31 101 48);
|
|
107
|
+
--hi-color-surface: rgb(var(--color-surface));
|
|
104
108
|
--hi-color-text-on-danger: rgb(var(--color-on-danger));
|
|
109
|
+
--hi-color-text-on-info: rgb(var(--color-on-info));
|
|
110
|
+
--hi-color-text-on-primary: rgb(var(--color-on-primary));
|
|
111
|
+
--hi-color-text-on-solid: rgb(var(--color-on-solid-text, 255 255 255));
|
|
105
112
|
--hi-color-text-on-success: rgb(var(--color-on-success));
|
|
106
113
|
--hi-color-text-on-warning: rgb(var(--color-on-warning));
|
|
107
|
-
--hi-color-text-
|
|
108
|
-
--hi-color-
|
|
109
|
-
--hi-color-
|
|
114
|
+
--hi-color-text-primary: rgb(var(--color-text));
|
|
115
|
+
--hi-color-text-secondary: rgb(var(--color-text-secondary));
|
|
116
|
+
--hi-color-warning: rgb(var(--color-warning));
|
|
110
117
|
--hi-color-warning-fg: rgb(137 72 0);
|
|
111
|
-
--hi-color-info-fg: rgb(0 98 116);
|
|
112
|
-
--hi-color-bg-subtle: rgb(var(--color-surface) / 0.5);
|
|
113
|
-
--hi-color-bg-elevated: rgb(var(--color-surface));
|
|
114
|
-
--hi-color-bg-container: rgb(var(--color-surface));
|
|
115
|
-
--hi-color-bg-canvas: rgb(var(--color-background));
|
|
116
118
|
--hi-secondary-bg: rgb(var(--color-surface) / 0.5);
|
|
117
119
|
}
|
package/src/theme/presets.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// The stock `default` theme's VALUES live in the Rust palette crate
|
|
2
|
+
// (packages/palette/src/stock.rs — hikari#1546 Phase 2); this JSON is the
|
|
3
|
+
// serde artifact a snapshot test keeps byte-locked to that crate. This file
|
|
4
|
+
// no longer hand-writes the slot table.
|
|
5
|
+
import stockThemeJson from "./stock-preset.json";
|
|
1
6
|
import { readStorageItem, writeStorageItem } from "./safeStorage";
|
|
2
7
|
import { rgbToOklch, oklchToRgb, contrastRatio } from "../theme/semanticPalette";
|
|
3
8
|
|
|
@@ -98,6 +103,28 @@ export interface CustomThemePreset {
|
|
|
98
103
|
|
|
99
104
|
const CUSTOM_STORAGE_KEY = "hikari-custom-themes";
|
|
100
105
|
|
|
106
|
+
/** The runtime's own memo of a CUSTOM theme's computed cssvars, keyed by
|
|
107
|
+
* theme id then mode, so the PRE-PAINT boot asset can replay the colors
|
|
108
|
+
* without re-implementing (or duplicating) the color math (hikari#1546
|
|
109
|
+
* point 2). Written by applyTheme for non-stock themes only — the stock
|
|
110
|
+
* preset already ships as the static sheet.
|
|
111
|
+
*
|
|
112
|
+
* VERSION DISCIPLINE: the memo bakes THREE inputs, and any of them
|
|
113
|
+
* changing makes a stored payload wrong —
|
|
114
|
+
* 1. `tokensToCSSVars` / its OKLCH-contrast helpers (this repo),
|
|
115
|
+
* 2. the token GROUPS a host registers (their resolved defaults feed the
|
|
116
|
+
* memo through `groupTokensToCSSVars`),
|
|
117
|
+
* 3. a host's own preset table (`window.__celestiaThemes` / in-place
|
|
118
|
+
* preset overrides), whose values are memoized like any custom theme.
|
|
119
|
+
* (1) is ours to police in review; (2) and (3) live in consumers, so the
|
|
120
|
+
* version is also the hatch for a HOST-driven refresh — a host that changes
|
|
121
|
+
* a group default or a preset's colors bumps nothing here, and its users
|
|
122
|
+
* keep one load of the previous palette. That is accepted (the memo is a
|
|
123
|
+
* first-paint hint, `applyTheme` overwrites it immediately) but it is why
|
|
124
|
+
* this is a hint and not a cache of record. */
|
|
125
|
+
export const THEME_VARS_MEMO_KEY = "hikari-theme-vars";
|
|
126
|
+
export const THEME_VARS_MEMO_VERSION = 1;
|
|
127
|
+
|
|
101
128
|
export function loadCustomThemes(): CustomThemePreset[] {
|
|
102
129
|
try {
|
|
103
130
|
const raw = readStorageItem(CUSTOM_STORAGE_KEY);
|
|
@@ -127,79 +154,21 @@ export function removeCustomTheme(id: string) {
|
|
|
127
154
|
saveCustomThemes(list);
|
|
128
155
|
}
|
|
129
156
|
|
|
130
|
-
const rgb = (r: number, g: number, b: number): ThemeTokenRGB => ({ r, g, b });
|
|
131
|
-
|
|
132
157
|
/**
|
|
133
|
-
* The stock preset table holds exactly ONE theme
|
|
158
|
+
* The stock preset table holds exactly ONE theme, and its VALUES are no
|
|
159
|
+
* longer written here: they live in the Rust palette crate
|
|
160
|
+
* (`packages/palette/src/stock.rs`, hikari#1546 Phase 2) and arrive as the
|
|
161
|
+
* checked-in JSON artifact above. Editing a color means editing Rust and
|
|
162
|
+
* regenerating (`REGEN_STOCK_JSON=1 cargo test -p hikari-palette --test
|
|
163
|
+
* stock_snapshot`) — the artifact snapshot test in that crate keeps the
|
|
164
|
+
* two sides locked.
|
|
134
165
|
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
* Gruvbox, Tokyo Night) were collapsed into this single pair on user
|
|
139
|
-
* direction — the library stops shipping competing aesthetics. A consumer
|
|
140
|
-
* that wants a distinct brand registers its own preset (chest's `sc` is the
|
|
141
|
-
* reference), and a user who wants another look edits this one through the
|
|
142
|
-
* theme dialog and keeps it as a custom scheme.
|
|
143
|
-
*
|
|
144
|
-
* Values are carried over VERBATIM from the entries this collapse retired —
|
|
145
|
-
* `synthwave84.light` into the light scheme, `nord.dark` into the dark one.
|
|
146
|
-
* Nothing was restyled: the dark scheme therefore keeps Nord's pale Polar
|
|
147
|
-
* Water primary (#88C0D0) and, with it, the near-black on-solid ink slots
|
|
148
|
-
* that pale primary requires (white on it is ~1.9:1). A stored id from the
|
|
149
|
-
* retired set is no longer a known theme, so `storedThemeId()` drops it and
|
|
150
|
-
* the engine resolves this default instead.
|
|
166
|
+
* A consumer that wants a distinct brand still registers its own preset
|
|
167
|
+
* (chest's `sc` is the reference), and a user who wants another look edits
|
|
168
|
+
* the default through the theme dialog and keeps it as a custom scheme.
|
|
151
169
|
*/
|
|
152
170
|
export const themePresets: Record<ThemeId, ThemePreset> = {
|
|
153
|
-
default:
|
|
154
|
-
id: "default",
|
|
155
|
-
name: "Default",
|
|
156
|
-
dark: {
|
|
157
|
-
primary: rgb(136, 192, 208),
|
|
158
|
-
secondary: rgb(143, 188, 187),
|
|
159
|
-
accent: rgb(94, 129, 172),
|
|
160
|
-
text: rgb(247, 249, 252),
|
|
161
|
-
muted: rgb(180, 180, 180),
|
|
162
|
-
border: rgb(128, 128, 128),
|
|
163
|
-
focusedBorder: rgb(136, 192, 208),
|
|
164
|
-
background: rgb(22, 27, 38),
|
|
165
|
-
surface: rgb(34, 40, 54),
|
|
166
|
-
selectedBackground: rgb(70, 70, 85),
|
|
167
|
-
selectedText: rgb(248, 248, 250),
|
|
168
|
-
statusBarBackground: rgb(34, 40, 54),
|
|
169
|
-
// Same canonical family as light — solid fills ignore the mode.
|
|
170
|
-
success: rgb(31, 101, 48),
|
|
171
|
-
error: rgb(166, 41, 55),
|
|
172
|
-
warning: rgb(137, 72, 0),
|
|
173
|
-
info: rgb(0, 98, 116),
|
|
174
|
-
// The pale primary puts dark ink on solid fills (white is ~1.9:1).
|
|
175
|
-
onSolidText: rgb(46, 52, 64),
|
|
176
|
-
onSolidIcon: rgb(46, 52, 64),
|
|
177
|
-
},
|
|
178
|
-
light: {
|
|
179
|
-
primary: rgb(214, 51, 132),
|
|
180
|
-
secondary: rgb(156, 106, 222),
|
|
181
|
-
accent: rgb(230, 167, 0),
|
|
182
|
-
text: rgb(30, 30, 30),
|
|
183
|
-
muted: rgb(80, 80, 80),
|
|
184
|
-
border: rgb(128, 128, 128),
|
|
185
|
-
focusedBorder: rgb(214, 51, 132),
|
|
186
|
-
background: rgb(245, 245, 240),
|
|
187
|
-
surface: rgb(255, 255, 255),
|
|
188
|
-
selectedBackground: rgb(200, 200, 205),
|
|
189
|
-
selectedText: rgb(40, 40, 45),
|
|
190
|
-
statusBarBackground: rgb(230, 230, 225),
|
|
191
|
-
// Semantic quartet — the SHARED canonical family (solid fills ignore
|
|
192
|
-
// the light/dark config, user direction), lightness derived so the
|
|
193
|
-
// house white ink clears 7:1. See hikari-palette themes.rs for the
|
|
194
|
-
// same trio on the components side.
|
|
195
|
-
success: rgb(31, 101, 48),
|
|
196
|
-
error: rgb(166, 41, 55),
|
|
197
|
-
warning: rgb(137, 72, 0),
|
|
198
|
-
info: rgb(0, 98, 116),
|
|
199
|
-
onSolidText: rgb(255, 255, 255),
|
|
200
|
-
onSolidIcon: rgb(255, 255, 255),
|
|
201
|
-
},
|
|
202
|
-
},
|
|
171
|
+
default: stockThemeJson as unknown as ThemePreset,
|
|
203
172
|
};
|
|
204
173
|
|
|
205
174
|
export type ThemeTokens = ThemeSchemeTokens;
|
|
@@ -224,26 +193,6 @@ export function getThemeTokens(
|
|
|
224
193
|
if (preset) return mode === "dark" ? preset.dark : preset.light;
|
|
225
194
|
return null;
|
|
226
195
|
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* The white-or-black ink for ONE solid fill. Deliberately NOT "whichever
|
|
230
|
-
* side wins the WCAG ratio": on saturated fills the ratio winner is the
|
|
231
|
-
* black ink far more often than design systems ship it (a 32-colour sweep
|
|
232
|
-
* of shipped brand/semantic solids picked white only 19% of the time by
|
|
233
|
-
* ratio, while Radix, Bootstrap and Material all ship white on them).
|
|
234
|
-
* The house rule (user direction, 2026-11-12) is perception-first:
|
|
235
|
-
*
|
|
236
|
-
* solid fill → WHITE by default; BLACK only when the fill is light
|
|
237
|
-
* enough to need it (perceptual luminance above 0.65,
|
|
238
|
-
* which lands on Radix's "designed for dark foreground"
|
|
239
|
-
* set: Sky, Mint, Lime, Yellow, Amber, plus the pale
|
|
240
|
-
* Nord tones; see the threshold note in solidInk);
|
|
241
|
-
* translucent → ride the theme's text channel (never computed here).
|
|
242
|
-
*
|
|
243
|
-
* Per-fill, not per-theme: one global on-solid slot cannot be right for a
|
|
244
|
-
* dark red AND a pale amber at once, which is exactly how the toast
|
|
245
|
-
* copy/close glyphs went near-black on themed bands (2026-11-12 report).
|
|
246
|
-
*/
|
|
247
196
|
/**
|
|
248
197
|
* This colour as TEXT on `bg`: itself when it already clears `target`,
|
|
249
198
|
* otherwise the nearest OKLCH-lightness step that does. Hue is preserved;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"id":"default","name":"Default","dark":{"primary":{"r":136,"g":192,"b":208},"secondary":{"r":143,"g":188,"b":187},"accent":{"r":94,"g":129,"b":172},"text":{"r":247,"g":249,"b":252},"muted":{"r":180,"g":180,"b":180},"border":{"r":128,"g":128,"b":128},"focusedBorder":{"r":136,"g":192,"b":208},"background":{"r":22,"g":27,"b":38},"surface":{"r":34,"g":40,"b":54},"selectedBackground":{"r":70,"g":70,"b":85},"selectedText":{"r":248,"g":248,"b":250},"statusBarBackground":{"r":34,"g":40,"b":54},"success":{"r":31,"g":101,"b":48},"error":{"r":166,"g":41,"b":55},"warning":{"r":137,"g":72,"b":0},"info":{"r":0,"g":98,"b":116},"onSolidText":{"r":46,"g":52,"b":64},"onSolidIcon":{"r":46,"g":52,"b":64}},"light":{"primary":{"r":214,"g":51,"b":132},"secondary":{"r":156,"g":106,"b":222},"accent":{"r":230,"g":167,"b":0},"text":{"r":30,"g":30,"b":30},"muted":{"r":80,"g":80,"b":80},"border":{"r":128,"g":128,"b":128},"focusedBorder":{"r":214,"g":51,"b":132},"background":{"r":245,"g":245,"b":240},"surface":{"r":255,"g":255,"b":255},"selectedBackground":{"r":200,"g":200,"b":205},"selectedText":{"r":40,"g":40,"b":45},"statusBarBackground":{"r":230,"g":230,"b":225},"success":{"r":31,"g":101,"b":48},"error":{"r":166,"g":41,"b":55},"warning":{"r":137,"g":72,"b":0},"info":{"r":0,"g":98,"b":116},"onSolidText":{"r":255,"g":255,"b":255},"onSolidIcon":{"r":255,"g":255,"b":255}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"dark":{"--color-accent":"94 129 172","--color-background":"22 27 38","--color-border":"128 128 128","--color-error":"166 41 55","--color-focused-border":"136 192 208","--color-info":"0 98 116","--color-muted":"180 180 180","--color-on-danger":"255 255 255","--color-on-info":"255 255 255","--color-on-primary":"0 0 0","--color-on-solid":"46 52 64","--color-on-solid-icon":"46 52 64","--color-on-solid-text":"46 52 64","--color-on-success":"255 255 255","--color-on-warning":"255 255 255","--color-primary":"136 192 208","--color-secondary":"143 188 187","--color-selected-bg":"70 70 85","--color-selected-text":"248 248 250","--color-status-bar-bg":"34 40 54","--color-success":"31 101 48","--color-surface":"34 40 54","--color-text":"247 249 252","--color-text-secondary":"230 232 234","--color-text-tertiary":"214 215 216","--color-warning":"137 72 0","--hi-color-background":"rgb(var(--color-background))","--hi-color-bg-canvas":"rgb(var(--color-background))","--hi-color-bg-container":"rgb(var(--color-surface))","--hi-color-bg-elevated":"rgb(var(--color-surface))","--hi-color-bg-subtle":"rgb(var(--color-surface) / 0.5)","--hi-color-border":"rgb(var(--color-border) / 15%)","--hi-color-danger-fg":"rgb(233 104 109)","--hi-color-error":"rgb(var(--color-error))","--hi-color-icon-on-solid":"rgb(var(--color-on-solid-icon, 255 255 255))","--hi-color-info":"rgb(var(--color-info))","--hi-color-info-fg":"rgb(81 158 177)","--hi-color-muted":"rgb(var(--color-muted))","--hi-color-on-primary":"rgb(var(--color-on-primary))","--hi-color-primary":"rgb(var(--color-primary))","--hi-color-secondary":"rgb(var(--color-secondary))","--hi-color-success":"rgb(var(--color-success))","--hi-color-success-fg":"rgb(94 162 105)","--hi-color-surface":"rgb(var(--color-surface))","--hi-color-text-on-danger":"rgb(var(--color-on-danger))","--hi-color-text-on-info":"rgb(var(--color-on-info))","--hi-color-text-on-primary":"rgb(var(--color-on-primary))","--hi-color-text-on-solid":"rgb(var(--color-on-solid-text, 255 255 255))","--hi-color-text-on-success":"rgb(var(--color-on-success))","--hi-color-text-on-warning":"rgb(var(--color-on-warning))","--hi-color-text-primary":"rgb(var(--color-text))","--hi-color-text-secondary":"rgb(var(--color-text-secondary))","--hi-color-warning":"rgb(var(--color-warning))","--hi-color-warning-fg":"rgb(200 130 73)","--hi-secondary-bg":"rgb(var(--color-surface) / 0.5)"},"light":{"--color-accent":"230 167 0","--color-background":"245 245 240","--color-border":"128 128 128","--color-error":"166 41 55","--color-focused-border":"214 51 132","--color-info":"0 98 116","--color-muted":"80 80 80","--color-on-danger":"255 255 255","--color-on-info":"255 255 255","--color-on-primary":"255 255 255","--color-on-solid":"255 255 255","--color-on-solid-icon":"255 255 255","--color-on-solid-text":"255 255 255","--color-on-success":"255 255 255","--color-on-warning":"255 255 255","--color-primary":"214 51 132","--color-secondary":"156 106 222","--color-selected-bg":"200 200 205","--color-selected-text":"40 40 45","--color-status-bar-bg":"230 230 225","--color-success":"31 101 48","--color-surface":"255 255 255","--color-text":"30 30 30","--color-text-secondary":"43 43 43","--color-text-tertiary":"55 55 55","--color-warning":"137 72 0","--hi-color-background":"rgb(var(--color-background))","--hi-color-bg-canvas":"rgb(var(--color-background))","--hi-color-bg-container":"rgb(var(--color-surface))","--hi-color-bg-elevated":"rgb(var(--color-surface))","--hi-color-bg-subtle":"rgb(var(--color-surface) / 0.5)","--hi-color-border":"rgb(var(--color-border) / 15%)","--hi-color-danger-fg":"rgb(166 41 55)","--hi-color-error":"rgb(var(--color-error))","--hi-color-icon-on-solid":"rgb(var(--color-on-solid-icon, 255 255 255))","--hi-color-info":"rgb(var(--color-info))","--hi-color-info-fg":"rgb(0 98 116)","--hi-color-muted":"rgb(var(--color-muted))","--hi-color-on-primary":"rgb(var(--color-on-primary))","--hi-color-primary":"rgb(var(--color-primary))","--hi-color-secondary":"rgb(var(--color-secondary))","--hi-color-success":"rgb(var(--color-success))","--hi-color-success-fg":"rgb(31 101 48)","--hi-color-surface":"rgb(var(--color-surface))","--hi-color-text-on-danger":"rgb(var(--color-on-danger))","--hi-color-text-on-info":"rgb(var(--color-on-info))","--hi-color-text-on-primary":"rgb(var(--color-on-primary))","--hi-color-text-on-solid":"rgb(var(--color-on-solid-text, 255 255 255))","--hi-color-text-on-success":"rgb(var(--color-on-success))","--hi-color-text-on-warning":"rgb(var(--color-on-warning))","--hi-color-text-primary":"rgb(var(--color-text))","--hi-color-text-secondary":"rgb(var(--color-text-secondary))","--hi-color-warning":"rgb(var(--color-warning))","--hi-color-warning-fg":"rgb(137 72 0)","--hi-secondary-bg":"rgb(var(--color-surface) / 0.5)"}}
|
|
@@ -4,6 +4,7 @@ import { fileURLToPath } from "node:url";
|
|
|
4
4
|
import { describe, expect, it } from "vitest";
|
|
5
5
|
|
|
6
6
|
import { buildThemeBootJs, resolveSystemModeAt } from "./themeBoot";
|
|
7
|
+
import { THEME_VARS_MEMO_VERSION } from "./presets";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Drift contract for the SHIPPED BOOT ASSET (hikari#1546 Phase 1.5): the
|
|
@@ -30,9 +31,40 @@ const jsPath = join(here, "..", "styles", "theme-boot.js");
|
|
|
30
31
|
function runBoot(
|
|
31
32
|
js: string,
|
|
32
33
|
opts: { now: Date; storage?: Record<string, string> },
|
|
33
|
-
): {
|
|
34
|
+
): {
|
|
35
|
+
theme: string | null;
|
|
36
|
+
mode: string | null;
|
|
37
|
+
styles: Array<{ tag: string; attr: Record<string, string>; text: string }>;
|
|
38
|
+
} {
|
|
34
39
|
const attrs: Record<string, string> = {};
|
|
40
|
+
const appended: Array<{ tag: string; attr: Record<string, string>; text: string }> = [];
|
|
41
|
+
const fakeHead = {
|
|
42
|
+
appendChild(el: { tag: string; attr: Record<string, string>; text: string }) {
|
|
43
|
+
appended.push(el);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
35
46
|
const fakeDoc = {
|
|
47
|
+
head: fakeHead,
|
|
48
|
+
createElement(tag: string) {
|
|
49
|
+
const el = {
|
|
50
|
+
tag,
|
|
51
|
+
attr: {} as Record<string, string>,
|
|
52
|
+
text: "",
|
|
53
|
+
setAttribute(k: string, v: string) {
|
|
54
|
+
el.attr[k] = v;
|
|
55
|
+
},
|
|
56
|
+
// The emitted code assigns `textContent` (real DOM semantics); the
|
|
57
|
+
// fake mirrors it onto `text` so assertions read what a browser
|
|
58
|
+
// would apply.
|
|
59
|
+
get textContent() {
|
|
60
|
+
return el.text;
|
|
61
|
+
},
|
|
62
|
+
set textContent(v: string) {
|
|
63
|
+
el.text = v;
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
return el;
|
|
67
|
+
},
|
|
36
68
|
documentElement: {
|
|
37
69
|
setAttribute(k: string, v: string) {
|
|
38
70
|
attrs[k] = v;
|
|
@@ -48,12 +80,19 @@ function runBoot(
|
|
|
48
80
|
// `Date` is SHADOWED in the asset's scope with a subclass pinned to the
|
|
49
81
|
// grid instant — the wrapper's bare `new Date()` must observe the grid
|
|
50
82
|
// date, not the wall clock, or the compare is meaningless.
|
|
83
|
+
const pinned = opts.now.getTime();
|
|
51
84
|
const PinnedDate = class extends Date {
|
|
52
|
-
|
|
53
|
-
|
|
85
|
+
// Zero-arg construction is the only call the asset makes; anything else
|
|
86
|
+
// forwards the same arguments (a typed overload avoids the spread-args
|
|
87
|
+
// error a variadic `unknown[]` would raise under vue-tsc).
|
|
88
|
+
constructor();
|
|
89
|
+
constructor(value: number);
|
|
90
|
+
constructor(year: number, month: number, day?: number);
|
|
91
|
+
constructor(a?: number, b?: number, c?: number) {
|
|
92
|
+
super(a === undefined ? pinned : b === undefined ? a : new Date(a, b, c ?? 1).getTime());
|
|
54
93
|
}
|
|
55
94
|
static now() {
|
|
56
|
-
return
|
|
95
|
+
return pinned;
|
|
57
96
|
}
|
|
58
97
|
};
|
|
59
98
|
// eslint-disable-next-line no-new-func -- executing the emitted asset is the point
|
|
@@ -63,7 +102,11 @@ function runBoot(
|
|
|
63
102
|
fakeDoc,
|
|
64
103
|
fakeStorage,
|
|
65
104
|
);
|
|
66
|
-
return {
|
|
105
|
+
return {
|
|
106
|
+
theme: attrs["data-theme"] ?? null,
|
|
107
|
+
mode: attrs["data-mode"] ?? null,
|
|
108
|
+
styles: appended,
|
|
109
|
+
};
|
|
67
110
|
}
|
|
68
111
|
|
|
69
112
|
describe("shipped theme boot asset (hikari#1546 Phase 1.5)", () => {
|
|
@@ -106,6 +149,73 @@ describe("shipped theme boot asset (hikari#1546 Phase 1.5)", () => {
|
|
|
106
149
|
expect(explicitDark.mode).toBe("dark");
|
|
107
150
|
});
|
|
108
151
|
|
|
152
|
+
it("replays the runtime's memoized custom-theme vars, and only for a live version", () => {
|
|
153
|
+
// hikari#1546 point 2: the static sheet carries the STOCK preset, so a
|
|
154
|
+
// user's own theme used to paint stock colors and snap. applyTheme()
|
|
155
|
+
// memoizes what it computed; the asset REPLAYS it — no color math is
|
|
156
|
+
// duplicated in the boot path.
|
|
157
|
+
const at = new Date(Date.UTC(2026, 6, 1, 12, 0, 0));
|
|
158
|
+
const memo = (version: number) =>
|
|
159
|
+
JSON.stringify({
|
|
160
|
+
v: version,
|
|
161
|
+
"my-theme": {
|
|
162
|
+
light: { "--color-primary": "1 2 3", "--color-text": "4 5 6" },
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
const storage = (version: number) => ({
|
|
166
|
+
"hikari-theme": "my-theme",
|
|
167
|
+
"hikari-theme-mode": "light",
|
|
168
|
+
"hikari-theme-vars": memo(version),
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const live = runBoot(js, { now: at, storage: storage(THEME_VARS_MEMO_VERSION) });
|
|
172
|
+
expect(live.theme, "the stored custom id is honored").toBe("my-theme");
|
|
173
|
+
expect(live.styles, "one vars block is injected").toHaveLength(1);
|
|
174
|
+
expect(live.styles[0]!.attr["data-hikari-boot-vars"]).toBe("");
|
|
175
|
+
expect(live.styles[0]!.text, "the memo's vars land in the block").toContain(
|
|
176
|
+
"--color-primary:1 2 3;",
|
|
177
|
+
);
|
|
178
|
+
expect(live.styles[0]!.text, "and its second slot too").toContain("--color-text:4 5 6;");
|
|
179
|
+
|
|
180
|
+
// A memo from an older math version must be IGNORED: replaying stale
|
|
181
|
+
// vars would paint a palette the runtime no longer agrees with.
|
|
182
|
+
const stale = runBoot(js, { now: at, storage: storage(THEME_VARS_MEMO_VERSION - 1) });
|
|
183
|
+
expect(stale.styles, "a stale memo is not replayed").toHaveLength(0);
|
|
184
|
+
|
|
185
|
+
// A hostile/garbled memo must not escape the html:root block: keys and
|
|
186
|
+
// values that are not plain cssvar shapes are dropped, the rest survive.
|
|
187
|
+
const hostile = runBoot(js, {
|
|
188
|
+
now: at,
|
|
189
|
+
storage: {
|
|
190
|
+
"hikari-theme": "my-theme",
|
|
191
|
+
"hikari-theme-mode": "light",
|
|
192
|
+
"hikari-theme-vars": JSON.stringify({
|
|
193
|
+
v: THEME_VARS_MEMO_VERSION,
|
|
194
|
+
"my-theme": {
|
|
195
|
+
light: {
|
|
196
|
+
"--color-primary": "1 2 3",
|
|
197
|
+
"} body { display: none } .x{": "red",
|
|
198
|
+
"--color-text": "4 5 6 } .y{z:1",
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
}),
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
expect(hostile.styles, "the block still injects the safe key").toHaveLength(1);
|
|
205
|
+
expect(hostile.styles[0]!.text, "safe pairs survive").toContain("--color-primary:1 2 3;");
|
|
206
|
+
expect(hostile.styles[0]!.text, "a selector-shaped key is refused").not.toContain("display");
|
|
207
|
+
expect(hostile.styles[0]!.text, "a value carrying a brace is refused").not.toContain(".y{");
|
|
208
|
+
|
|
209
|
+
// A stock theme never has a memo and must not inject anything.
|
|
210
|
+
// MUT:mut-memo-replay — disabling the replay branch REDS the live case
|
|
211
|
+
// above (verified); this one pins the stock exemption.
|
|
212
|
+
const stock = runBoot(js, {
|
|
213
|
+
now: at,
|
|
214
|
+
storage: { "hikari-theme": "default", "hikari-theme-mode": "light", "hikari-theme-vars": memo(THEME_VARS_MEMO_VERSION) },
|
|
215
|
+
});
|
|
216
|
+
expect(stock.styles, "the stock preset rides the static sheet, not a memo").toHaveLength(0);
|
|
217
|
+
});
|
|
218
|
+
|
|
109
219
|
it("routes a stored manual config through the zone meridian, machine-independently", () => {
|
|
110
220
|
// Round-2 review (S1): the first version compared the wrapper (fake
|
|
111
221
|
// storage, manual config) against the MODULE (real localStorage, empty
|
package/src/theme/themeBoot.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
THEME_VARS_MEMO_KEY,
|
|
3
|
+
THEME_VARS_MEMO_VERSION,
|
|
4
|
+
} from "./presets";
|
|
1
5
|
import { readStorageItem } from "./safeStorage";
|
|
2
6
|
import {
|
|
3
7
|
DEFAULT_GEO_LOCATION,
|
|
@@ -35,15 +39,30 @@ import {
|
|
|
35
39
|
* the boot asset is read-only (pre-paint storage writes would be a
|
|
36
40
|
* surprise) and falls back instead. Removal happens at initTheme.
|
|
37
41
|
*
|
|
42
|
+
* It also REPLAYS the runtime's memo for a user's custom theme (see the
|
|
43
|
+
* CUSTOM THEME FIRST PAINT block in the emitted wrapper): the boot never
|
|
44
|
+
* composes a palette itself, so there is no second implementation of the
|
|
45
|
+
* color math to drift. `applyTheme` retires the replayed block on its first
|
|
46
|
+
* run, so a memo can never outlive the paint it was written for.
|
|
47
|
+
*
|
|
38
48
|
* `buildThemeBootJs()` ships the chain as a dependency-free IIFE for
|
|
39
49
|
* consumers to inline in `<head>` (lagrange interpolates it with a `?raw`
|
|
40
|
-
* import).
|
|
50
|
+
* import). CONSUMERS MUST INLINE IT AFTER THEIR STYLESHEET LINKS AND BEFORE
|
|
51
|
+
* `</head>`: every var-bearing block (the static `html[data-mode]` sheet,
|
|
52
|
+
* this asset's `html:root` replay, the runtime's own `html:root` block) has
|
|
53
|
+
* the same specificity, so HEAD ORDER is the only thing deciding who wins,
|
|
54
|
+
* and the intended chain is sheet -> replay -> runtime. The math is
|
|
55
|
+
* serialized via `Function.prototype.toString` from
|
|
41
56
|
* useSolarTime's OWN definitions — the asset cannot drift from the
|
|
42
57
|
* runtime's solar chain by construction; `themeBoot.contract.test.ts`
|
|
43
58
|
* executes the emitted asset against `resolveSystemModeAt` across a date
|
|
44
59
|
* grid to pin the wrapper (storage reads + attribute writes) instead.
|
|
45
60
|
*/
|
|
46
61
|
|
|
62
|
+
/** Marks the boot-injected vars block so the runtime (and tests) can tell
|
|
63
|
+
* it apart from useTheme's own `data-hikari-theme-vars` block. */
|
|
64
|
+
export const THEME_BOOT_STYLE_ATTR = "data-hikari-boot-vars";
|
|
65
|
+
|
|
47
66
|
const DEG = Math.PI / 180;
|
|
48
67
|
const RAD = 180 / Math.PI;
|
|
49
68
|
|
|
@@ -162,8 +181,55 @@ export function buildThemeBootJs(): string {
|
|
|
162
181
|
if (!(m === "light" || m === "dark")) {
|
|
163
182
|
m = alt > 6 ? "light" : "dark";
|
|
164
183
|
}
|
|
165
|
-
|
|
184
|
+
var themeId = t || (typeof window !== "undefined" && window.__celestiaDefaultTheme) || "default";
|
|
185
|
+
d.setAttribute("data-theme", themeId);
|
|
166
186
|
d.setAttribute("data-mode", m);
|
|
187
|
+
|
|
188
|
+
/* CUSTOM THEME FIRST PAINT (hikari#1546 point 2). The static sheet only
|
|
189
|
+
* carries the stock preset, so a user's own theme used to paint stock
|
|
190
|
+
* colors and snap. applyTheme() now memoizes its computed vars
|
|
191
|
+
* (hikari-theme-vars, versioned); this block REPLAYS that memo — no
|
|
192
|
+
* color math is duplicated here, the runtime stays the single source.
|
|
193
|
+
* The block is injected as the LAST <head> child that exists at this
|
|
194
|
+
* point and uses html:root, so it ties with the runtime's own block
|
|
195
|
+
* (which is appended later and therefore wins once the bundle lands)
|
|
196
|
+
* and outranks the static html[data-mode] sheet by document order. */
|
|
197
|
+
try {
|
|
198
|
+
/* A crafted/edited memo could otherwise smuggle a brace or semicolon into a key
|
|
199
|
+
* and escape the html:root block into arbitrary selectors. localStorage
|
|
200
|
+
* is same-origin (writing it already needs live JS), so this is
|
|
201
|
+
* hardening rather than a trust boundary — but the boot runs before ANY
|
|
202
|
+
* app code, which is the one place we cannot afford to trust it. */
|
|
203
|
+
var SAFE_KEY = /^--[A-Za-z0-9_-]+$/;
|
|
204
|
+
var SAFE_VALUE = /^[A-Za-z0-9 .,%()\/#-]*$/;
|
|
205
|
+
{
|
|
206
|
+
var memo = JSON.parse(read("${THEME_VARS_MEMO_KEY}") || "null");
|
|
207
|
+
var memoV = ${THEME_VARS_MEMO_VERSION};
|
|
208
|
+
if (memo && memo.v === memoV) {
|
|
209
|
+
var entry = memo[themeId];
|
|
210
|
+
/* EXACT mode only: replaying the other mode's palette onto this
|
|
211
|
+
* one's background is worse than the stock colors the static sheet
|
|
212
|
+
* already paints, so a missing entry simply falls through. */
|
|
213
|
+
var vars = entry && entry[m];
|
|
214
|
+
if (vars && typeof vars === "object") {
|
|
215
|
+
var css = "";
|
|
216
|
+
for (var k in vars) {
|
|
217
|
+
if (!Object.prototype.hasOwnProperty.call(vars, k)) continue;
|
|
218
|
+
if (!SAFE_KEY.test(k)) continue;
|
|
219
|
+
var value = "" + vars[k];
|
|
220
|
+
if (!SAFE_VALUE.test(value)) continue;
|
|
221
|
+
css += k + ":" + value + ";";
|
|
222
|
+
}
|
|
223
|
+
if (css) {
|
|
224
|
+
var styleEl = document.createElement("style");
|
|
225
|
+
styleEl.setAttribute("${THEME_BOOT_STYLE_ATTR}", "");
|
|
226
|
+
styleEl.textContent = "html:root{" + css + "}";
|
|
227
|
+
(document.head || d).appendChild(styleEl);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
} catch (e) {}
|
|
167
233
|
} catch (e) {}`;
|
|
168
234
|
return `/* GENERATED by packages/vue/src/theme/themeBoot.ts (buildThemeBootJs) —
|
|
169
235
|
* do not edit by hand. The math functions are serialized from useSolarTime's
|
|
@@ -3,7 +3,8 @@ import { dirname, join } from "node:path";
|
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import { describe, expect, it } from "vitest";
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { tokensToCSSVars, getThemeTokens } from "./presets";
|
|
7
|
+
import { buildThemeVarsCss, stockVars } from "./themeVarsCss";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Drift contract for the PRE-RENDERED theme CSS (hikari#1546): the checked
|
|
@@ -46,7 +47,40 @@ describe("pre-rendered theme vars CSS (hikari#1546)", () => {
|
|
|
46
47
|
expect(checkedIn).toBe(css);
|
|
47
48
|
});
|
|
48
49
|
|
|
50
|
+
it("CROSS-LANGUAGE LOCK: the TS derivation equals the Rust derivation, key-for-key", () => {
|
|
51
|
+
// hikari#1546 Phase 2 endgame: the shipped sheet now comes from the
|
|
52
|
+
// Rust palette crate's derivation (via stock-vars.json). The same
|
|
53
|
+
// derivation ALSO lives here for runtime-authored custom themes. Two
|
|
54
|
+
// implementations of one algorithm drift silently — this compare
|
|
55
|
+
// VERIFIED the lock on first run (55 keys x 2 modes byte-identical)
|
|
56
|
+
// and always catches a divergence: any value edit on either side
|
|
57
|
+
// REDS the named key and mode. MUT:mut-derivation-lock. `tokensToCSSVars` is the runtime
|
|
58
|
+
// authority for custom themes; this file's CSS composition is the
|
|
59
|
+
// boot-time authority for the stock preset.
|
|
60
|
+
for (const mode of ["dark", "light"] as const) {
|
|
61
|
+
const tokens = getThemeTokens("default", mode);
|
|
62
|
+
const tsVars = tokensToCSSVars(tokens!);
|
|
63
|
+
const rustVars = stockVars[mode];
|
|
64
|
+
const tsKeys = Object.keys(tsVars).sort();
|
|
65
|
+
const rustKeys = Object.keys(rustVars).sort();
|
|
66
|
+
expect(tsKeys, `TS/Rust key sets must match (${mode})`).toEqual(rustKeys);
|
|
67
|
+
for (const key of tsKeys) {
|
|
68
|
+
expect(
|
|
69
|
+
tsVars[key]?.replace(/\s+/g, " ").trim(),
|
|
70
|
+
`${key} (${mode})`,
|
|
71
|
+
).toBe(rustVars[key]?.replace(/\s+/g, " ").trim());
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
49
76
|
it("covers both modes with the core color tokens", () => {
|
|
77
|
+
expect(css).toContain('html[data-mode="dark"]');
|
|
78
|
+
expect(css).toContain('html[data-mode="light"]');
|
|
79
|
+
// Load-bearing for hikari#1546 Phase 2: the values now originate in the
|
|
80
|
+
// Rust palette crate (stock.rs) and reach this composition through the
|
|
81
|
+
// JSON artifact presets.ts imports. A migration typo would change one
|
|
82
|
+
// of these channel triples — the comparison against the checked-in CSS
|
|
83
|
+
// is what proves the migration changed no color.
|
|
50
84
|
expect(css).toContain('html[data-mode="dark"]');
|
|
51
85
|
expect(css).toContain('html[data-mode="light"]');
|
|
52
86
|
// The slots with no per-component CSS fallback — a missing one leaves
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
// The vars are the RUST DERIVATION's output (packages/palette/src/
|
|
2
|
+
// derivation.rs), shipped as this JSON artifact — hikari#1546 Phase 2
|
|
3
|
+
// endgame. The TypeScript tokensToCSSVars remains ONLY for runtime-authored
|
|
4
|
+
// custom themes, and the drift contract below pins it, key-for-key, to this
|
|
5
|
+
// artifact so the two implementations cannot diverge.
|
|
6
|
+
import stockVarsJson from "./stock-vars.json";
|
|
2
7
|
|
|
3
8
|
/**
|
|
4
9
|
* The PRE-RENDERED static form of the stock theme table (hikari#1546).
|
|
@@ -32,20 +37,29 @@ import { themePresets, tokensToCSSVars, getThemeTokens } from "./presets";
|
|
|
32
37
|
* `var(--hk-picker-*)`), so the pre-hydration window degrades to identical
|
|
33
38
|
* geometry, and the runtime emission covers them anyway.
|
|
34
39
|
*/
|
|
40
|
+
/** The typed view of the Rust artifact: the serde BTreeMap arrives as two
|
|
41
|
+
* string-keyed maps. Declared explicitly because the JSON import's literal
|
|
42
|
+
* type defeats `stockVarsJson[mode]` under the strict build (vue-tsc
|
|
43
|
+
* TS7053 — the release lane caught it, the esbuild test transform did not). */
|
|
44
|
+
export interface StockVarsArtifact {
|
|
45
|
+
dark: Record<string, string>;
|
|
46
|
+
light: Record<string, string>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const stockVars: StockVarsArtifact = stockVarsJson as StockVarsArtifact;
|
|
50
|
+
|
|
35
51
|
const MODES = ["dark", "light"] as const;
|
|
36
52
|
|
|
37
53
|
export function buildThemeVarsCss(): string {
|
|
38
|
-
const preset = themePresets.default;
|
|
39
|
-
if (!preset) return "/* stock preset table is empty — nothing to pre-render */\n";
|
|
40
54
|
const chunks: string[] = [
|
|
41
|
-
"/* GENERATED
|
|
42
|
-
" *
|
|
43
|
-
" *
|
|
55
|
+
"/* GENERATED from the Rust palette crate's derivation (packages/palette/",
|
|
56
|
+
" * src/derivation.rs) via the stock-vars.json artifact — do not edit by",
|
|
57
|
+
" * hand. The drift contract pins the TypeScript derivation to these",
|
|
58
|
+
" * values key-for-key; regen is REGEN_STOCK_JSON=1 cargo test",
|
|
59
|
+
" * -p hikari-palette --test stock_snapshot. */",
|
|
44
60
|
];
|
|
45
61
|
for (const mode of MODES) {
|
|
46
|
-
const
|
|
47
|
-
if (!tokens) continue;
|
|
48
|
-
const vars = tokensToCSSVars(tokens);
|
|
62
|
+
const vars = stockVars[mode];
|
|
49
63
|
chunks.push(`html[data-mode="${mode}"] {`);
|
|
50
64
|
for (const name of Object.keys(vars)) {
|
|
51
65
|
chunks.push(` ${name}: ${vars[name]};`);
|
|
@@ -54,3 +68,5 @@ export function buildThemeVarsCss(): string {
|
|
|
54
68
|
}
|
|
55
69
|
return chunks.join("\n") + "\n";
|
|
56
70
|
}
|
|
71
|
+
|
|
72
|
+
export { stockVars };
|
|
@@ -5,6 +5,53 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
5
5
|
type ThemeModule = typeof import("./useTheme");
|
|
6
6
|
type SolarModule = typeof import("./useSolarTime");
|
|
7
7
|
|
|
8
|
+
describe("custom-theme first-paint memo (hikari#1546 point 2)", () => {
|
|
9
|
+
it("memoizes a custom theme's vars for the boot asset and skips the stock preset", async () => {
|
|
10
|
+
const { THEME_VARS_MEMO_KEY, THEME_VARS_MEMO_VERSION, stockDefaultPreset } =
|
|
11
|
+
await import("./presets");
|
|
12
|
+
const theme = await import("./useTheme");
|
|
13
|
+
const api = theme.useTheme();
|
|
14
|
+
const { nextTick } = await import("vue");
|
|
15
|
+
// The API's own adder updates the live `customThemes` ref (calling the
|
|
16
|
+
// presets-layer helper would only write storage, which the ref read at
|
|
17
|
+
// import time).
|
|
18
|
+
api.addCustomTheme({
|
|
19
|
+
id: "brand-x",
|
|
20
|
+
name: "Brand X",
|
|
21
|
+
light: { ...stockDefaultPreset.light, primary: { r: 1, g: 2, b: 3 } },
|
|
22
|
+
dark: { ...stockDefaultPreset.dark, primary: { r: 4, g: 5, b: 6 } },
|
|
23
|
+
} as never);
|
|
24
|
+
theme.initTheme();
|
|
25
|
+
api.setTheme("brand-x");
|
|
26
|
+
await nextTick();
|
|
27
|
+
// `setTheme` persists the id; applyTheme ran synchronously on the watch
|
|
28
|
+
// flush, so the memo must already be there.
|
|
29
|
+
await Promise.resolve();
|
|
30
|
+
const raw = localStorage.getItem(THEME_VARS_MEMO_KEY);
|
|
31
|
+
expect(raw, "a custom theme writes a memo").toBeTruthy();
|
|
32
|
+
const memo = JSON.parse(raw!) as Record<string, unknown>;
|
|
33
|
+
expect(memo.v).toBe(THEME_VARS_MEMO_VERSION);
|
|
34
|
+
const entry = memo["brand-x"] as Record<string, Record<string, string>>;
|
|
35
|
+
expect(entry, "keyed by theme id then mode").toBeTruthy();
|
|
36
|
+
// Both modes are memoized, and the value is the REAL composition of
|
|
37
|
+
// that mode's tokens (the boot asset replays it verbatim).
|
|
38
|
+
expect(Object.keys(entry).sort()).toEqual(["dark", "light"]);
|
|
39
|
+
expect(entry.light!["--color-primary"]).toContain("1 2 3");
|
|
40
|
+
expect(entry.dark!["--color-primary"]).toContain("4 5 6");
|
|
41
|
+
|
|
42
|
+
// The stock preset never memoizes: it already ships as the static sheet
|
|
43
|
+
// MUT:mut-memo-stock-skip — dropping the stock guard adds a "default"
|
|
44
|
+
// entry and REDS the assertion below (verified).
|
|
45
|
+
api.setTheme("default");
|
|
46
|
+
await nextTick();
|
|
47
|
+
const after = JSON.parse(localStorage.getItem(THEME_VARS_MEMO_KEY)!) as Record<string, unknown>;
|
|
48
|
+
expect(after["default"], "no memo for a stock theme").toBeUndefined();
|
|
49
|
+
// Both modes are composed here (8 × the 400-step OKLCH search), which is
|
|
50
|
+
// slow under the suite's parallel load even though a browser does it in
|
|
51
|
+
// tens of milliseconds — this is a timeout, not a hang.
|
|
52
|
+
}, 30_000);
|
|
53
|
+
});
|
|
54
|
+
|
|
8
55
|
describe("useTheme theme clock", () => {
|
|
9
56
|
let theme: ThemeModule;
|
|
10
57
|
let solar: SolarModule;
|
package/src/theme/useTheme.ts
CHANGED
|
@@ -2,12 +2,13 @@ import { computed, ref, watch } from "vue";
|
|
|
2
2
|
|
|
3
3
|
import { scheduleCronAfter, type CronHandle } from "../runtime/cronBus";
|
|
4
4
|
import { scheduleInterval, type IntervalHandle } from "../runtime/intervalBus";
|
|
5
|
-
import { addCustomTheme as addCustomThemeToStorage, loadCustomThemes, removeCustomTheme as removeCustomThemeFromStorage, themePresets, tokensToCSSVars, type CustomThemePreset, type ThemeId, type ThemeMode, type ThemePreset } from "./presets";
|
|
5
|
+
import { addCustomTheme as addCustomThemeToStorage, loadCustomThemes, removeCustomTheme as removeCustomThemeFromStorage, themePresets, tokensToCSSVars, THEME_VARS_MEMO_KEY, THEME_VARS_MEMO_VERSION, type CustomThemePreset, type ThemeId, type ThemeMode, type ThemePreset } from "./presets";
|
|
6
6
|
import { readStorageItem, removeStorageItem, writeStorageItem } from "./safeStorage";
|
|
7
7
|
import { groupTokensToCSSVars, resolveGroupTokens, setTokenGroupsReapply } from "./tokenGroups";
|
|
8
8
|
import { registerStandardThemeDecor } from "./standardDecor";
|
|
9
9
|
import { registerStandardThemeGroups } from "./standardGroups";
|
|
10
10
|
import { invalidateLuminanceCache } from "./useBackgroundLuminance";
|
|
11
|
+
import { THEME_BOOT_STYLE_ATTR } from "./themeBoot";
|
|
11
12
|
import { getGeolocation, getTimePeriod, hydrateSolarLocationState, timezoneFallback, type GeoLocation, type TimePeriod } from "./useSolarTime";
|
|
12
13
|
|
|
13
14
|
const DEFAULT_THEME = "default";
|
|
@@ -210,6 +211,42 @@ export function pickThemeVarDeltas(
|
|
|
210
211
|
return deltas;
|
|
211
212
|
}
|
|
212
213
|
|
|
214
|
+
/** Memoize a CUSTOM theme's computed vars so the pre-paint boot asset can
|
|
215
|
+
* replay them on the next load (hikari#1546 point 2). The STOCK preset is
|
|
216
|
+
* skipped: it already ships as the static sheet, and rewriting it would
|
|
217
|
+
* duplicate ~50 vars into localStorage for no gain.
|
|
218
|
+
*
|
|
219
|
+
* Failure is silent on purpose — a full/unavailable localStorage must
|
|
220
|
+
* never break theming; it only costs the next load its exact first paint. */
|
|
221
|
+
function rememberVarsForFirstPaint(preset: ThemePreset): void {
|
|
222
|
+
if (preset.id in themePresets) return;
|
|
223
|
+
try {
|
|
224
|
+
const raw = readStorageItem(THEME_VARS_MEMO_KEY);
|
|
225
|
+
const memo: Record<string, unknown> =
|
|
226
|
+
raw ? (JSON.parse(raw) as Record<string, unknown>) : {};
|
|
227
|
+
if (memo.v !== THEME_VARS_MEMO_VERSION) {
|
|
228
|
+
for (const key of Object.keys(memo)) delete memo[key];
|
|
229
|
+
memo.v = THEME_VARS_MEMO_VERSION;
|
|
230
|
+
}
|
|
231
|
+
// BOTH modes, every time: the boot asset resolves the mode from the
|
|
232
|
+
// clock, which can differ from the mode the user is looking at right
|
|
233
|
+
// now. Memoizing only the active one would leave the other mode
|
|
234
|
+
// unpainted (or, worse, tempt the boot into cross-mode fallback).
|
|
235
|
+
const perTheme: Record<string, Record<string, string>> = {};
|
|
236
|
+
for (const mode of ["dark", "light"] as const) {
|
|
237
|
+
const tokens = mode === "dark" ? preset.dark : preset.light;
|
|
238
|
+
perTheme[mode] = tokensToCSSVars(
|
|
239
|
+
tokens,
|
|
240
|
+
groupTokensToCSSVars(resolveGroupTokens(mode, preset.groups?.[mode])),
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
memo[preset.id] = perTheme;
|
|
244
|
+
writeStorageItem(THEME_VARS_MEMO_KEY, JSON.stringify(memo));
|
|
245
|
+
} catch {
|
|
246
|
+
// Storage unavailable or the payload is not ours — drop the memo.
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
213
250
|
function themeVarsStyleElement(): HTMLStyleElement {
|
|
214
251
|
let styleEl = document.head.querySelector<HTMLStyleElement>(
|
|
215
252
|
`style[${THEME_VARS_STYLE_ATTR}]`,
|
|
@@ -222,7 +259,19 @@ function themeVarsStyleElement(): HTMLStyleElement {
|
|
|
222
259
|
return styleEl;
|
|
223
260
|
}
|
|
224
261
|
|
|
262
|
+
/** Retire the pre-paint block once the runtime owns the vars (hikari#1546
|
|
263
|
+
* point 2, review round 1): it exists ONLY to cover the first paint, and
|
|
264
|
+
* leaving it live poisons `injectThemeVars`' baseline measurement — a memo
|
|
265
|
+
* key that the new vars map no longer carries would never be overridden,
|
|
266
|
+
* so a stale first-paint value would survive every later theme switch. */
|
|
267
|
+
function retireBootVars(): void {
|
|
268
|
+
document.head
|
|
269
|
+
.querySelector(`style[${THEME_BOOT_STYLE_ATTR}]`)
|
|
270
|
+
?.remove();
|
|
271
|
+
}
|
|
272
|
+
|
|
225
273
|
function injectThemeVars(el: HTMLElement, vars: Record<string, string>): void {
|
|
274
|
+
retireBootVars();
|
|
226
275
|
const styleEl = themeVarsStyleElement();
|
|
227
276
|
// Measure the cascade WITHOUT our own previous overrides: disable the
|
|
228
277
|
// managed block, read every candidate, re-enable. One recalc serves
|
|
@@ -262,6 +311,8 @@ function applyTheme() {
|
|
|
262
311
|
);
|
|
263
312
|
const vars = tokensToCSSVars(tokens, groupVars);
|
|
264
313
|
|
|
314
|
+
rememberVarsForFirstPaint(preset);
|
|
315
|
+
|
|
265
316
|
invalidateLuminanceCache();
|
|
266
317
|
|
|
267
318
|
injectThemeVars(el, vars);
|
|
@@ -284,6 +335,20 @@ function applyTheme() {
|
|
|
284
335
|
// applications never call setTokenGroupsReapply themselves.
|
|
285
336
|
setTokenGroupsReapply(() => applyTheme());
|
|
286
337
|
|
|
338
|
+
/** Drop a removed theme's memo so deleted ids cannot replay forever. */
|
|
339
|
+
function forgetVarsForFirstPaint(themeId: ThemeId): void {
|
|
340
|
+
try {
|
|
341
|
+
const raw = readStorageItem(THEME_VARS_MEMO_KEY);
|
|
342
|
+
if (!raw) return;
|
|
343
|
+
const memo = JSON.parse(raw) as Record<string, unknown>;
|
|
344
|
+
if (!(themeId in memo)) return;
|
|
345
|
+
delete memo[themeId];
|
|
346
|
+
writeStorageItem(THEME_VARS_MEMO_KEY, JSON.stringify(memo));
|
|
347
|
+
} catch {
|
|
348
|
+
// Unavailable or foreign payload — nothing to prune.
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
287
352
|
function getAllThemePresets(): Record<string, ThemePreset> {
|
|
288
353
|
const result: Record<string, ThemePreset> = { ...themePresets };
|
|
289
354
|
for (const ct of customThemes.value) {
|
|
@@ -371,6 +436,7 @@ export function useTheme() {
|
|
|
371
436
|
|
|
372
437
|
function removeCustomTheme(id: string) {
|
|
373
438
|
removeCustomThemeFromStorage(id);
|
|
439
|
+
forgetVarsForFirstPaint(id);
|
|
374
440
|
customThemes.value = loadCustomThemes();
|
|
375
441
|
if (currentTheme.value === id) {
|
|
376
442
|
// A deleted custom whose id still resolves as a builtin preset falls
|