@adea-ai/ui 0.123.1 → 0.125.0
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/NOTICE +36 -0
- package/dist/components/theme/appearance-panel.js +1 -1
- package/dist/components/theme/index.d.ts +1 -1
- package/dist/components/theme/index.d.ts.map +1 -1
- package/dist/components/theme/index.js +5 -2
- package/dist/components/theme/theme-context.d.ts +13 -0
- package/dist/components/theme/theme-context.d.ts.map +1 -0
- package/dist/components/theme/theme-context.js +41 -0
- package/dist/components/theme/theme-context.js.map +1 -0
- package/dist/components/theme/theme-document.d.ts +82 -0
- package/dist/components/theme/theme-document.d.ts.map +1 -0
- package/dist/components/theme/theme-document.js +98 -0
- package/dist/components/theme/theme-document.js.map +1 -0
- package/dist/components/theme/theme-persistence.d.ts +50 -0
- package/dist/components/theme/theme-persistence.d.ts.map +1 -0
- package/dist/components/theme/theme-persistence.js +87 -0
- package/dist/components/theme/theme-persistence.js.map +1 -0
- package/dist/components/theme/theme-provider.d.ts +52 -75
- package/dist/components/theme/theme-provider.d.ts.map +1 -1
- package/dist/components/theme/theme-provider.js +60 -116
- package/dist/components/theme/theme-provider.js.map +1 -1
- package/dist/components/theme/theme-state.d.ts +167 -0
- package/dist/components/theme/theme-state.d.ts.map +1 -0
- package/dist/components/theme/theme-state.js +122 -0
- package/dist/components/theme/theme-state.js.map +1 -0
- package/dist/components/theme/theme-toggle.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -3
- package/dist/lib/accent-roles.d.ts +124 -0
- package/dist/lib/accent-roles.d.ts.map +1 -0
- package/dist/lib/accent-roles.js +163 -0
- package/dist/lib/accent-roles.js.map +1 -0
- package/dist/lib/appearance-font-settings.d.ts +1 -1
- package/dist/lib/appearance-font-settings.d.ts.map +1 -1
- package/dist/lib/appearance-font-settings.js.map +1 -1
- package/dist/lib/themes.d.ts +71 -1
- package/dist/lib/themes.d.ts.map +1 -1
- package/dist/lib/themes.js +172 -48
- package/dist/lib/themes.js.map +1 -1
- package/dist/r/lib.json +5 -0
- package/dist/r/registry.json +25 -0
- package/dist/r/src/components/theme/index.ts +3 -0
- package/dist/r/src/components/theme/theme-context.ts +43 -0
- package/dist/r/src/components/theme/theme-document.ts +184 -0
- package/dist/r/src/components/theme/theme-persistence.ts +143 -0
- package/dist/r/src/components/theme/theme-provider.tsx +147 -253
- package/dist/r/src/components/theme/theme-state.ts +274 -0
- package/dist/r/src/lib/accent-roles.ts +338 -0
- package/dist/r/src/lib/appearance-font-settings.ts +1 -1
- package/dist/r/src/lib/themes.ts +225 -59
- package/dist/r/theming.json +20 -0
- package/package.json +1 -1
- package/registry.json +25 -0
- package/src/components/theme/index.ts +3 -0
- package/src/components/theme/theme-context.ts +43 -0
- package/src/components/theme/theme-document.ts +184 -0
- package/src/components/theme/theme-persistence.ts +143 -0
- package/src/components/theme/theme-provider.tsx +147 -253
- package/src/components/theme/theme-state.ts +274 -0
- package/src/index.ts +11 -0
- package/src/lib/accent-roles.ts +338 -0
- package/src/lib/appearance-font-settings.ts +1 -1
- package/src/lib/themes.ts +225 -59
package/dist/NOTICE
CHANGED
|
@@ -300,6 +300,42 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
300
300
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
301
301
|
SOFTWARE.
|
|
302
302
|
|
|
303
|
+
## Theme resolution fallback translated from Zeron
|
|
304
|
+
|
|
305
|
+
`resolveVariant` in `packages/ui/src/lib/themes.ts` translates the fallback order
|
|
306
|
+
of Zeron's `ThemeRegistry::resolve` (https://github.com/zeronsh/zeron), as Adea's
|
|
307
|
+
accepted port of it in `packages/ui/src/components/appearance.ts` expresses it:
|
|
308
|
+
an id the registry does not hold resolves to the default variant of the same
|
|
309
|
+
appearance, then to any variant of that appearance, and never to a variant of the
|
|
310
|
+
other one. The provider's use of it, which leaves the stored id untouched, is the
|
|
311
|
+
Adea behaviour that port retains.
|
|
312
|
+
|
|
313
|
+
Copyright (c) 2026 Wing
|
|
314
|
+
Licensed under the MIT License.
|
|
315
|
+
|
|
316
|
+
Zeron's MIT notice, reproduced in full under "Appearance editor translated from
|
|
317
|
+
Zeron" above, applies to the translated portion. The ThemeProvider's preview,
|
|
318
|
+
persistence and document-application seams are written against Adea's provider
|
|
319
|
+
contract and introduce no further upstream source.
|
|
320
|
+
|
|
321
|
+
## Accent roles and colour helpers translated from Zeron
|
|
322
|
+
|
|
323
|
+
`packages/ui/src/lib/accent-roles.ts` translates Zeron's `Color` helpers and
|
|
324
|
+
`AccentRoles` derivation (https://github.com/zeronsh/zeron), as Adea's accepted
|
|
325
|
+
port of them in `packages/ui/src/components/appearance.ts` expresses it: the hex
|
|
326
|
+
parser and serialiser, the alpha-compositing contrast measure, the contrast
|
|
327
|
+
repair that raises a colour to a floor, the 3:1 interaction
|
|
328
|
+
gate applied to a custom accent, and the per-element paint of an accent's roles.
|
|
329
|
+
The colour engine, the preset values and the role derivation they feed remain
|
|
330
|
+
`@adea-ai/themes`'; the `#rrggbb` selection grammar and the use of the gate in the
|
|
331
|
+
provider are Adea behaviour that port retains.
|
|
332
|
+
|
|
333
|
+
Copyright (c) 2026 Wing
|
|
334
|
+
Licensed under the MIT License.
|
|
335
|
+
|
|
336
|
+
Zeron's MIT notice, reproduced in full under "Appearance editor translated from
|
|
337
|
+
Zeron" above, applies to the translated portion.
|
|
338
|
+
|
|
303
339
|
## Strict binary layout extracted from the accepted Adea donor port
|
|
304
340
|
|
|
305
341
|
`packages/ui/src/components/layout/split-layout/model.ts` is extracted from
|
|
@@ -7,7 +7,7 @@ import { DEFAULT_APPEARANCE_EDITOR_FONT_SETTINGS } from "../../lib/appearance-fo
|
|
|
7
7
|
import { AccentSwatchGroups } from "../composites/appearance-editor/accent-swatch-groups.js";
|
|
8
8
|
import { AppearanceFontSettingsGroup } from "../composites/appearance-editor/font-settings-group.js";
|
|
9
9
|
import { ThemePicker } from "./theme-picker.js";
|
|
10
|
-
import { useTheme } from "./theme-
|
|
10
|
+
import { useTheme } from "./theme-context.js";
|
|
11
11
|
import { createComponent, insert, mergeProps, spread, template } from "solid-js/web";
|
|
12
12
|
import { Show, splitProps } from "solid-js";
|
|
13
13
|
import { RadioGroup } from "@kobalte/core/radio-group";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { AppearancePanel, type AppearancePanelProps } from './appearance-panel';
|
|
2
2
|
export { ThemePicker, ThemePreview, ThemeSwatch, type ThemePickerProps, type ThemePreviewProps, type ThemeSwatchProps, } from './theme-picker';
|
|
3
|
-
export { ThemeProvider, defaultThemeSelection, inlineScriptLiteral, resolvedDensity, themeScript, useTheme, type ThemeContextValue, type ThemeDensity, type ThemeProviderProps, type ThemeSelection, } from './theme-provider';
|
|
3
|
+
export { ThemeProvider, defaultThemeSelection, inlineScriptLiteral, localStoragePersistence, resolvedDensity, themeScript, useOptionalTheme, useTheme, type ThemeContextValue, type ThemeDensity, type ThemePersistence, type ThemeProviderProps, type ThemeSelection, } from './theme-provider';
|
|
4
4
|
export { ThemeToggle, type ThemeToggleProps } from './theme-toggle';
|
|
5
5
|
export { ThemeModeToggle, type ThemeModeToggleProps } from './theme-mode-toggle';
|
|
6
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/theme/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,KAAK,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAC/E,OAAO,EACL,WAAW,EACX,YAAY,EACZ,WAAW,EACX,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,GACtB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,mBAAmB,EACnB,eAAe,EACf,WAAW,EACX,QAAQ,EACR,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,cAAc,GACpB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,WAAW,EAAE,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AACnE,OAAO,EAAE,eAAe,EAAE,KAAK,oBAAoB,EAAE,MAAM,qBAAqB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/theme/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,KAAK,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAC/E,OAAO,EACL,WAAW,EACX,YAAY,EACZ,WAAW,EACX,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,GACtB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,mBAAmB,EACnB,uBAAuB,EACvB,eAAe,EACf,WAAW,EACX,gBAAgB,EAChB,QAAQ,EACR,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,KAAK,cAAc,GACpB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,WAAW,EAAE,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AACnE,OAAO,EAAE,eAAe,EAAE,KAAK,oBAAoB,EAAE,MAAM,qBAAqB,CAAA"}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { ThemePicker, ThemePreview, ThemeSwatch } from "./theme-picker.js";
|
|
2
|
-
import {
|
|
2
|
+
import { useOptionalTheme, useTheme } from "./theme-context.js";
|
|
3
|
+
import { defaultThemeSelection, resolvedDensity } from "./theme-state.js";
|
|
4
|
+
import { localStoragePersistence } from "./theme-persistence.js";
|
|
5
|
+
import { ThemeProvider, inlineScriptLiteral, themeScript } from "./theme-provider.js";
|
|
3
6
|
import { AppearancePanel } from "./appearance-panel.js";
|
|
4
7
|
import { ThemeToggle } from "./theme-toggle.js";
|
|
5
8
|
import { ThemeModeToggle } from "./theme-mode-toggle.js";
|
|
6
|
-
export { AppearancePanel, ThemeModeToggle, ThemePicker, ThemePreview, ThemeProvider, ThemeSwatch, ThemeToggle, defaultThemeSelection, inlineScriptLiteral, resolvedDensity, themeScript, useTheme };
|
|
9
|
+
export { AppearancePanel, ThemeModeToggle, ThemePicker, ThemePreview, ThemeProvider, ThemeSwatch, ThemeToggle, defaultThemeSelection, inlineScriptLiteral, localStoragePersistence, resolvedDensity, themeScript, useOptionalTheme, useTheme };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Context } from 'solid-js';
|
|
2
|
+
import type { ThemeContextValue } from './theme-state';
|
|
3
|
+
export declare function themeContext(): Context<ThemeContextValue | undefined>;
|
|
4
|
+
export declare function useTheme(): ThemeContextValue;
|
|
5
|
+
/**
|
|
6
|
+
* The theme context, or `undefined` outside a `ThemeProvider`.
|
|
7
|
+
*
|
|
8
|
+
* For a component that can run both ways — a shared control rendered in a harness,
|
|
9
|
+
* a story, or a host that has not mounted a provider — and would rather degrade
|
|
10
|
+
* than throw.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useOptionalTheme(): ThemeContextValue | undefined;
|
|
13
|
+
//# sourceMappingURL=theme-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme-context.d.ts","sourceRoot":"","sources":["../../../src/components/theme/theme-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,KAAK,OAAO,EAAE,MAAM,UAAU,CAAA;AAClE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AAqBtD,wBAAgB,YAAY,IAAI,OAAO,CAAC,iBAAiB,GAAG,SAAS,CAAC,CAGrE;AAED,wBAAgB,QAAQ,IAAI,iBAAiB,CAI5C;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,IAAI,iBAAiB,GAAG,SAAS,CAEhE"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createContext, useContext } from "solid-js";
|
|
2
|
+
//#region src/components/theme/theme-context.ts
|
|
3
|
+
/**
|
|
4
|
+
* Where the context object lives: on the global, under a registered symbol.
|
|
5
|
+
*
|
|
6
|
+
* A context is an object identity, and a bundle can legitimately load this module
|
|
7
|
+
* twice — a dev graph where a relative import and a package subpath are distinct
|
|
8
|
+
* module records, or two copies of the package at different versions. Each copy
|
|
9
|
+
* would then call `createContext` and own its own object; a provider from one and a
|
|
10
|
+
* `useOptionalTheme()` from the other would never meet, and the second would return
|
|
11
|
+
* `undefined` as though no provider were mounted. `Symbol.for` is shared across
|
|
12
|
+
* every copy in a realm, so they all find the one context the first created.
|
|
13
|
+
*
|
|
14
|
+
* Both copies must agree on its shape. The context value only ever grows, which is
|
|
15
|
+
* why a newer provider under an older reader is safe and the reverse is a missing
|
|
16
|
+
* member, not a mismatch.
|
|
17
|
+
*/
|
|
18
|
+
var CONTEXT_KEY = Symbol.for("adea.ui.theme");
|
|
19
|
+
function themeContext() {
|
|
20
|
+
const host = globalThis;
|
|
21
|
+
return host[CONTEXT_KEY] ??= createContext();
|
|
22
|
+
}
|
|
23
|
+
function useTheme() {
|
|
24
|
+
const value = useContext(themeContext());
|
|
25
|
+
if (!value) throw new Error("useTheme must be called inside a <ThemeProvider>.");
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The theme context, or `undefined` outside a `ThemeProvider`.
|
|
30
|
+
*
|
|
31
|
+
* For a component that can run both ways — a shared control rendered in a harness,
|
|
32
|
+
* a story, or a host that has not mounted a provider — and would rather degrade
|
|
33
|
+
* than throw.
|
|
34
|
+
*/
|
|
35
|
+
function useOptionalTheme() {
|
|
36
|
+
return useContext(themeContext());
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { themeContext, useOptionalTheme, useTheme };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=theme-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme-context.js","names":[],"sources":["../../../src/components/theme/theme-context.ts"],"sourcesContent":["import { createContext, useContext, type Context } from 'solid-js'\nimport type { ThemeContextValue } from './theme-state'\n\n/**\n * Where the context object lives: on the global, under a registered symbol.\n *\n * A context is an object identity, and a bundle can legitimately load this module\n * twice — a dev graph where a relative import and a package subpath are distinct\n * module records, or two copies of the package at different versions. Each copy\n * would then call `createContext` and own its own object; a provider from one and a\n * `useOptionalTheme()` from the other would never meet, and the second would return\n * `undefined` as though no provider were mounted. `Symbol.for` is shared across\n * every copy in a realm, so they all find the one context the first created.\n *\n * Both copies must agree on its shape. The context value only ever grows, which is\n * why a newer provider under an older reader is safe and the reverse is a missing\n * member, not a mismatch.\n */\nconst CONTEXT_KEY = Symbol.for('adea.ui.theme')\n\ntype ContextHost = { [CONTEXT_KEY]?: Context<ThemeContextValue | undefined> }\n\nexport function themeContext(): Context<ThemeContextValue | undefined> {\n const host = globalThis as ContextHost\n return (host[CONTEXT_KEY] ??= createContext<ThemeContextValue>())\n}\n\nexport function useTheme(): ThemeContextValue {\n const value = useContext(themeContext())\n if (!value) throw new Error('useTheme must be called inside a <ThemeProvider>.')\n return value\n}\n\n/**\n * The theme context, or `undefined` outside a `ThemeProvider`.\n *\n * For a component that can run both ways — a shared control rendered in a harness,\n * a story, or a host that has not mounted a provider — and would rather degrade\n * than throw.\n */\nexport function useOptionalTheme(): ThemeContextValue | undefined {\n return useContext(themeContext())\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkBA,IAAM,cAAc,OAAO,IAAI,eAAe;AAI9C,SAAgB,eAAuD;CACrE,MAAM,OAAO;CACb,OAAQ,KAAK,iBAAiB,cAAiC;AACjE;AAEA,SAAgB,WAA8B;CAC5C,MAAM,QAAQ,WAAW,aAAa,CAAC;CACvC,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM,mDAAmD;CAC/E,OAAO;AACT;;;;;;;;AASA,SAAgB,mBAAkD;CAChE,OAAO,WAAW,aAAa,CAAC;AAClC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type ThemeAppearance, type ThemeVariant } from '#lib/themes';
|
|
2
|
+
import { type ThemeSelection } from './theme-state';
|
|
3
|
+
/**
|
|
4
|
+
* The document half of the theme provider: the single writer of `<html>`.
|
|
5
|
+
*
|
|
6
|
+
* Written against the few members of an element it uses, so the rules can be tested
|
|
7
|
+
* on a stand-in and the provider's effect stays a one-line call.
|
|
8
|
+
*/
|
|
9
|
+
export type ThemeRoot = Pick<HTMLElement, 'classList' | 'dataset' | 'style' | 'removeAttribute' | 'setAttribute'>;
|
|
10
|
+
export type ThemeDocumentState = {
|
|
11
|
+
/** What the document shows: the committed selection, or a preview laid over it. */
|
|
12
|
+
selection: ThemeSelection;
|
|
13
|
+
appearance: ThemeAppearance;
|
|
14
|
+
variant: ThemeVariant;
|
|
15
|
+
};
|
|
16
|
+
export type ThemeDocumentOptions = {
|
|
17
|
+
/**
|
|
18
|
+
* Rewrites the custom properties just before they are written: the variant's
|
|
19
|
+
* roles with the accent laid over them. It sees every property of the pass and
|
|
20
|
+
* must return the full map; a name it drops is not written.
|
|
21
|
+
*/
|
|
22
|
+
transformTokens?: (tokens: Record<string, string>) => Record<string, string>;
|
|
23
|
+
/**
|
|
24
|
+
* The contrast an accent is held to against the variant's background. Absent, a
|
|
25
|
+
* preset is painted verbatim (the catalogue measured it for its own themes) and
|
|
26
|
+
* only a custom `#rrggbb` is held to the default floor of 3:1.
|
|
27
|
+
*/
|
|
28
|
+
accentContrastFloor?: number;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The custom properties a state paints: the variant's roles, with the accent
|
|
32
|
+
* selection laid over them, then `transformTokens`.
|
|
33
|
+
*
|
|
34
|
+
* The accent is applied *here* rather than left to the `[data-accent]` blocks in
|
|
35
|
+
* `theme.css`, because the properties are written inline and an inline declaration
|
|
36
|
+
* outranks every stylesheet selector however specific it is. The blocks still exist
|
|
37
|
+
* and are still correct — they are what styles the window before this runs — but
|
|
38
|
+
* they cannot win from here, so leaving the accent to them meant the attribute was
|
|
39
|
+
* set, the rules were present, and nothing changed on screen.
|
|
40
|
+
*/
|
|
41
|
+
export declare function themeDocumentTokens(state: ThemeDocumentState, options?: ThemeDocumentOptions): {
|
|
42
|
+
tokens: Record<string, string>;
|
|
43
|
+
accented: boolean;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Write a state onto a root and report which custom properties it owns.
|
|
47
|
+
*
|
|
48
|
+
* Pass the previous call's return as `previous` and any palette property that pass
|
|
49
|
+
* wrote and this one does not is removed. For the catalogue the two sets are the
|
|
50
|
+
* same, so nothing is ever removed; it matters for a host variant that carries
|
|
51
|
+
* `tokens` the catalogue does not, which would otherwise stay inline, beating the
|
|
52
|
+
* stylesheet, after the user switched away from it.
|
|
53
|
+
*/
|
|
54
|
+
export declare function applyThemeToRoot(root: ThemeRoot, state: ThemeDocumentState, options?: ThemeDocumentOptions, previous?: ReadonlySet<string>): ReadonlySet<string>;
|
|
55
|
+
/** What {@link applyWithoutTransitions} needs of a document. */
|
|
56
|
+
export type TransitionDocument = {
|
|
57
|
+
head: {
|
|
58
|
+
appendChild(node: unknown): unknown;
|
|
59
|
+
};
|
|
60
|
+
createElement(tag: 'style'): {
|
|
61
|
+
textContent: string | null;
|
|
62
|
+
offsetHeight?: number;
|
|
63
|
+
remove(): void;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Run `apply` with every CSS transition switched off for the frame that carries
|
|
68
|
+
* it, so a theme switch cannot animate page-wide.
|
|
69
|
+
*
|
|
70
|
+
* Without this, flipping the custom properties on `<html>` starts a transition on
|
|
71
|
+
* every element that declares one, and each fades from the old palette to the new
|
|
72
|
+
* over its own duration: a theme change that looks like a rendering fault. The
|
|
73
|
+
* rule is removed two frames later, after the browser has committed the new styles
|
|
74
|
+
* with transitions off, and `restore` runs it early (the provider calls it when it
|
|
75
|
+
* is disposed mid-switch).
|
|
76
|
+
*
|
|
77
|
+
* `schedule` is the frame source; it defaults to `requestAnimationFrame` and falls
|
|
78
|
+
* back to running at once where there is none, which leaves transitions on rather
|
|
79
|
+
* than off.
|
|
80
|
+
*/
|
|
81
|
+
export declare function applyWithoutTransitions(doc: TransitionDocument, apply: () => void, schedule?: (callback: () => void) => void): () => void;
|
|
82
|
+
//# sourceMappingURL=theme-document.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme-document.d.ts","sourceRoot":"","sources":["../../../src/components/theme/theme-document.ts"],"names":[],"mappings":"AAIA,OAAO,EAGL,KAAK,eAAe,EACpB,KAAK,YAAY,EAClB,MAAM,aAAa,CAAA;AAEpB,OAAO,EAAmB,KAAK,cAAc,EAAE,MAAM,eAAe,CAAA;AAEpE;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG,IAAI,CAC1B,WAAW,EACX,WAAW,GAAG,SAAS,GAAG,OAAO,GAAG,iBAAiB,GAAG,cAAc,CACvE,CAAA;AAED,MAAM,MAAM,kBAAkB,GAAG;IAC/B,mFAAmF;IACnF,SAAS,EAAE,cAAc,CAAA;IACzB,UAAU,EAAE,eAAe,CAAA;IAC3B,OAAO,EAAE,YAAY,CAAA;CACtB,CAAA;AAED,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;;OAIG;IACH,eAAe,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC5E;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC7B,CAAA;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,kBAAkB,EACzB,OAAO,GAAE,oBAAyB,GACjC;IAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAE,CAYvD;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,kBAAkB,EACzB,OAAO,GAAE,oBAAyB,EAClC,QAAQ,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAC7B,WAAW,CAAC,MAAM,CAAC,CAsCrB;AAED,gEAAgE;AAChE,MAAM,MAAM,kBAAkB,GAAG;IAC/B,IAAI,EAAE;QAAE,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAAA;KAAE,CAAA;IAC7C,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG;QAC3B,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;QAC1B,YAAY,CAAC,EAAE,MAAM,CAAA;QACrB,MAAM,IAAI,IAAI,CAAA;KACf,CAAA;CACF,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,uBAAuB,CACrC,GAAG,EAAE,kBAAkB,EACvB,KAAK,EAAE,MAAM,IAAI,EACjB,QAAQ,GAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAsB,GACzD,MAAM,IAAI,CAqBZ"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { isCustomAccent } from "../../lib/accent-roles.js";
|
|
2
|
+
import { accentVariables, themeCssVariables } from "../../lib/themes.js";
|
|
3
|
+
import { applyAppearanceFontSettings, normalizeAppearanceEditorFontSettings } from "../../lib/appearance-font-settings.js";
|
|
4
|
+
import { resolvedDensity } from "./theme-state.js";
|
|
5
|
+
//#region src/components/theme/theme-document.ts
|
|
6
|
+
/**
|
|
7
|
+
* The custom properties a state paints: the variant's roles, with the accent
|
|
8
|
+
* selection laid over them, then `transformTokens`.
|
|
9
|
+
*
|
|
10
|
+
* The accent is applied *here* rather than left to the `[data-accent]` blocks in
|
|
11
|
+
* `theme.css`, because the properties are written inline and an inline declaration
|
|
12
|
+
* outranks every stylesheet selector however specific it is. The blocks still exist
|
|
13
|
+
* and are still correct — they are what styles the window before this runs — but
|
|
14
|
+
* they cannot win from here, so leaving the accent to them meant the attribute was
|
|
15
|
+
* set, the rules were present, and nothing changed on screen.
|
|
16
|
+
*/
|
|
17
|
+
function themeDocumentTokens(state, options = {}) {
|
|
18
|
+
const properties = { ...themeCssVariables(state.variant) };
|
|
19
|
+
const accent = accentVariables(state.selection.accent, state.appearance, state.selection, {
|
|
20
|
+
background: state.variant.colors.background,
|
|
21
|
+
floor: options.accentContrastFloor,
|
|
22
|
+
variantId: state.variant.id
|
|
23
|
+
});
|
|
24
|
+
if (accent) Object.assign(properties, accent);
|
|
25
|
+
return {
|
|
26
|
+
tokens: options.transformTokens ? options.transformTokens(properties) : properties,
|
|
27
|
+
accented: accent !== void 0
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Write a state onto a root and report which custom properties it owns.
|
|
32
|
+
*
|
|
33
|
+
* Pass the previous call's return as `previous` and any palette property that pass
|
|
34
|
+
* wrote and this one does not is removed. For the catalogue the two sets are the
|
|
35
|
+
* same, so nothing is ever removed; it matters for a host variant that carries
|
|
36
|
+
* `tokens` the catalogue does not, which would otherwise stay inline, beating the
|
|
37
|
+
* stylesheet, after the user switched away from it.
|
|
38
|
+
*/
|
|
39
|
+
function applyThemeToRoot(root, state, options = {}, previous) {
|
|
40
|
+
const { tokens, accented } = themeDocumentTokens(state, options);
|
|
41
|
+
for (const [name, value] of Object.entries(tokens)) root.style.setProperty(name, value);
|
|
42
|
+
if (previous) {
|
|
43
|
+
for (const name of previous) if (!(name in tokens)) root.style.removeProperty(name);
|
|
44
|
+
}
|
|
45
|
+
root.classList.toggle("dark", state.appearance === "dark");
|
|
46
|
+
root.dataset["theme"] = state.variant.id;
|
|
47
|
+
root.dataset["appearance"] = state.appearance;
|
|
48
|
+
root.style.colorScheme = state.appearance;
|
|
49
|
+
if (!accented) root.removeAttribute("data-accent");
|
|
50
|
+
else root.dataset["accent"] = isCustomAccent(state.selection.accent) ? "custom" : state.selection.accent;
|
|
51
|
+
applyAppearanceFontSettings(root, normalizeAppearanceEditorFontSettings(state.selection.fonts, state.selection.font).settings);
|
|
52
|
+
if (resolvedDensity(state.selection) === "comfortable") root.removeAttribute("data-density");
|
|
53
|
+
else root.dataset["density"] = resolvedDensity(state.selection);
|
|
54
|
+
return new Set(Object.keys(tokens));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Run `apply` with every CSS transition switched off for the frame that carries
|
|
58
|
+
* it, so a theme switch cannot animate page-wide.
|
|
59
|
+
*
|
|
60
|
+
* Without this, flipping the custom properties on `<html>` starts a transition on
|
|
61
|
+
* every element that declares one, and each fades from the old palette to the new
|
|
62
|
+
* over its own duration: a theme change that looks like a rendering fault. The
|
|
63
|
+
* rule is removed two frames later, after the browser has committed the new styles
|
|
64
|
+
* with transitions off, and `restore` runs it early (the provider calls it when it
|
|
65
|
+
* is disposed mid-switch).
|
|
66
|
+
*
|
|
67
|
+
* `schedule` is the frame source; it defaults to `requestAnimationFrame` and falls
|
|
68
|
+
* back to running at once where there is none, which leaves transitions on rather
|
|
69
|
+
* than off.
|
|
70
|
+
*/
|
|
71
|
+
function applyWithoutTransitions(doc, apply, schedule = defaultSchedule) {
|
|
72
|
+
const style = doc.createElement("style");
|
|
73
|
+
style.textContent = "*{transition:none !important}";
|
|
74
|
+
doc.head.appendChild(style);
|
|
75
|
+
style.offsetHeight;
|
|
76
|
+
let removed = false;
|
|
77
|
+
const restore = () => {
|
|
78
|
+
if (removed) return;
|
|
79
|
+
removed = true;
|
|
80
|
+
style.remove();
|
|
81
|
+
};
|
|
82
|
+
try {
|
|
83
|
+
apply();
|
|
84
|
+
} catch (error) {
|
|
85
|
+
restore();
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
schedule(() => schedule(restore));
|
|
89
|
+
return restore;
|
|
90
|
+
}
|
|
91
|
+
function defaultSchedule(callback) {
|
|
92
|
+
if (typeof requestAnimationFrame === "function") requestAnimationFrame(callback);
|
|
93
|
+
else callback();
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { applyThemeToRoot, applyWithoutTransitions, themeDocumentTokens };
|
|
97
|
+
|
|
98
|
+
//# sourceMappingURL=theme-document.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme-document.js","names":[],"sources":["../../../src/components/theme/theme-document.ts"],"sourcesContent":["import {\n applyAppearanceFontSettings,\n normalizeAppearanceEditorFontSettings,\n} from '#lib/appearance-font-settings'\nimport {\n accentVariables,\n themeCssVariables,\n type ThemeAppearance,\n type ThemeVariant,\n} from '#lib/themes'\nimport { isCustomAccent } from '#lib/accent-roles'\nimport { resolvedDensity, type ThemeSelection } from './theme-state'\n\n/**\n * The document half of the theme provider: the single writer of `<html>`.\n *\n * Written against the few members of an element it uses, so the rules can be tested\n * on a stand-in and the provider's effect stays a one-line call.\n */\nexport type ThemeRoot = Pick<\n HTMLElement,\n 'classList' | 'dataset' | 'style' | 'removeAttribute' | 'setAttribute'\n>\n\nexport type ThemeDocumentState = {\n /** What the document shows: the committed selection, or a preview laid over it. */\n selection: ThemeSelection\n appearance: ThemeAppearance\n variant: ThemeVariant\n}\n\nexport type ThemeDocumentOptions = {\n /**\n * Rewrites the custom properties just before they are written: the variant's\n * roles with the accent laid over them. It sees every property of the pass and\n * must return the full map; a name it drops is not written.\n */\n transformTokens?: (tokens: Record<string, string>) => Record<string, string>\n /**\n * The contrast an accent is held to against the variant's background. Absent, a\n * preset is painted verbatim (the catalogue measured it for its own themes) and\n * only a custom `#rrggbb` is held to the default floor of 3:1.\n */\n accentContrastFloor?: number\n}\n\n/**\n * The custom properties a state paints: the variant's roles, with the accent\n * selection laid over them, then `transformTokens`.\n *\n * The accent is applied *here* rather than left to the `[data-accent]` blocks in\n * `theme.css`, because the properties are written inline and an inline declaration\n * outranks every stylesheet selector however specific it is. The blocks still exist\n * and are still correct — they are what styles the window before this runs — but\n * they cannot win from here, so leaving the accent to them meant the attribute was\n * set, the rules were present, and nothing changed on screen.\n */\nexport function themeDocumentTokens(\n state: ThemeDocumentState,\n options: ThemeDocumentOptions = {}\n): { tokens: Record<string, string>; accented: boolean } {\n const properties = { ...themeCssVariables(state.variant) }\n const accent = accentVariables(state.selection.accent, state.appearance, state.selection, {\n background: state.variant.colors.background,\n floor: options.accentContrastFloor,\n variantId: state.variant.id,\n })\n if (accent) Object.assign(properties, accent)\n return {\n tokens: options.transformTokens ? options.transformTokens(properties) : properties,\n accented: accent !== undefined,\n }\n}\n\n/**\n * Write a state onto a root and report which custom properties it owns.\n *\n * Pass the previous call's return as `previous` and any palette property that pass\n * wrote and this one does not is removed. For the catalogue the two sets are the\n * same, so nothing is ever removed; it matters for a host variant that carries\n * `tokens` the catalogue does not, which would otherwise stay inline, beating the\n * stylesheet, after the user switched away from it.\n */\nexport function applyThemeToRoot(\n root: ThemeRoot,\n state: ThemeDocumentState,\n options: ThemeDocumentOptions = {},\n previous?: ReadonlySet<string>\n): ReadonlySet<string> {\n const { tokens, accented } = themeDocumentTokens(state, options)\n\n for (const [name, value] of Object.entries(tokens)) {\n root.style.setProperty(name, value)\n }\n if (previous) {\n for (const name of previous) {\n if (!(name in tokens)) root.style.removeProperty(name)\n }\n }\n\n root.classList.toggle('dark', state.appearance === 'dark')\n root.dataset['theme'] = state.variant.id\n root.dataset['appearance'] = state.appearance\n root.style.colorScheme = state.appearance\n\n // A custom colour has no stylesheet block to select, and its value is the\n // attribute's worst possible content, so it is reported as `custom`.\n if (!accented) root.removeAttribute('data-accent')\n else\n root.dataset['accent'] = isCustomAccent(state.selection.accent)\n ? 'custom'\n : state.selection.accent\n\n applyAppearanceFontSettings(\n root,\n normalizeAppearanceEditorFontSettings(state.selection.fonts, state.selection.font).settings\n )\n\n // Density is a pure token shift, so unlike the colour axes there is nothing to\n // compute: the attribute selects a block in `theme.css`. It is applied here for\n // the same reason as the others — this is the one place that knows what the\n // current selection is, and it is the only writer of these attributes.\n if (resolvedDensity(state.selection) === 'comfortable') root.removeAttribute('data-density')\n else root.dataset['density'] = resolvedDensity(state.selection)\n\n return new Set(Object.keys(tokens))\n}\n\n/** What {@link applyWithoutTransitions} needs of a document. */\nexport type TransitionDocument = {\n head: { appendChild(node: unknown): unknown }\n createElement(tag: 'style'): {\n textContent: string | null\n offsetHeight?: number\n remove(): void\n }\n}\n\n/**\n * Run `apply` with every CSS transition switched off for the frame that carries\n * it, so a theme switch cannot animate page-wide.\n *\n * Without this, flipping the custom properties on `<html>` starts a transition on\n * every element that declares one, and each fades from the old palette to the new\n * over its own duration: a theme change that looks like a rendering fault. The\n * rule is removed two frames later, after the browser has committed the new styles\n * with transitions off, and `restore` runs it early (the provider calls it when it\n * is disposed mid-switch).\n *\n * `schedule` is the frame source; it defaults to `requestAnimationFrame` and falls\n * back to running at once where there is none, which leaves transitions on rather\n * than off.\n */\nexport function applyWithoutTransitions(\n doc: TransitionDocument,\n apply: () => void,\n schedule: (callback: () => void) => void = defaultSchedule\n): () => void {\n const style = doc.createElement('style')\n style.textContent = '*{transition:none !important}'\n doc.head.appendChild(style)\n // Reading a layout property forces the browser to commit the rule before the\n // properties change; without it the two can be batched into one style recalc.\n void style.offsetHeight\n let removed = false\n const restore = () => {\n if (removed) return\n removed = true\n style.remove()\n }\n try {\n apply()\n } catch (error) {\n restore()\n throw error\n }\n schedule(() => schedule(restore))\n return restore\n}\n\nfunction defaultSchedule(callback: () => void): void {\n if (typeof requestAnimationFrame === 'function') requestAnimationFrame(callback)\n else callback()\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAyDA,SAAgB,oBACd,OACA,UAAgC,CAAC,GACsB;CACvD,MAAM,aAAa,EAAE,GAAG,kBAAkB,MAAM,OAAO,EAAE;CACzD,MAAM,SAAS,gBAAgB,MAAM,UAAU,QAAQ,MAAM,YAAY,MAAM,WAAW;EACxF,YAAY,MAAM,QAAQ,OAAO;EACjC,OAAO,QAAQ;EACf,WAAW,MAAM,QAAQ;CAC3B,CAAC;CACD,IAAI,QAAQ,OAAO,OAAO,YAAY,MAAM;CAC5C,OAAO;EACL,QAAQ,QAAQ,kBAAkB,QAAQ,gBAAgB,UAAU,IAAI;EACxE,UAAU,WAAW,KAAA;CACvB;AACF;;;;;;;;;;AAWA,SAAgB,iBACd,MACA,OACA,UAAgC,CAAC,GACjC,UACqB;CACrB,MAAM,EAAE,QAAQ,aAAa,oBAAoB,OAAO,OAAO;CAE/D,KAAK,MAAM,CAAC,MAAM,UAAU,OAAO,QAAQ,MAAM,GAC/C,KAAK,MAAM,YAAY,MAAM,KAAK;CAEpC,IAAI,UACG;OAAA,MAAM,QAAQ,UACjB,IAAI,EAAE,QAAQ,SAAS,KAAK,MAAM,eAAe,IAAI;CAAA;CAIzD,KAAK,UAAU,OAAO,QAAQ,MAAM,eAAe,MAAM;CACzD,KAAK,QAAQ,WAAW,MAAM,QAAQ;CACtC,KAAK,QAAQ,gBAAgB,MAAM;CACnC,KAAK,MAAM,cAAc,MAAM;CAI/B,IAAI,CAAC,UAAU,KAAK,gBAAgB,aAAa;MAE/C,KAAK,QAAQ,YAAY,eAAe,MAAM,UAAU,MAAM,IAC1D,WACA,MAAM,UAAU;CAEtB,4BACE,MACA,sCAAsC,MAAM,UAAU,OAAO,MAAM,UAAU,IAAI,CAAC,CAAC,QACrF;CAMA,IAAI,gBAAgB,MAAM,SAAS,MAAM,eAAe,KAAK,gBAAgB,cAAc;MACtF,KAAK,QAAQ,aAAa,gBAAgB,MAAM,SAAS;CAE9D,OAAO,IAAI,IAAI,OAAO,KAAK,MAAM,CAAC;AACpC;;;;;;;;;;;;;;;;AA2BA,SAAgB,wBACd,KACA,OACA,WAA2C,iBAC/B;CACZ,MAAM,QAAQ,IAAI,cAAc,OAAO;CACvC,MAAM,cAAc;CACpB,IAAI,KAAK,YAAY,KAAK;CAG1B,MAAW;CACX,IAAI,UAAU;CACd,MAAM,gBAAgB;EACpB,IAAI,SAAS;EACb,UAAU;EACV,MAAM,OAAO;CACf;CACA,IAAI;EACF,MAAM;CACR,SAAS,OAAO;EACd,QAAQ;EACR,MAAM;CACR;CACA,eAAe,SAAS,OAAO,CAAC;CAChC,OAAO;AACT;AAEA,SAAS,gBAAgB,UAA4B;CACnD,IAAI,OAAO,0BAA0B,YAAY,sBAAsB,QAAQ;MAC1E,SAAS;AAChB"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type ThemeSelection } from './theme-state';
|
|
2
|
+
/**
|
|
3
|
+
* Where a selection is kept between sessions.
|
|
4
|
+
*
|
|
5
|
+
* The provider never touches storage itself; it asks a `ThemePersistence`. The
|
|
6
|
+
* default is {@link localStoragePersistence}, which is what the provider always did.
|
|
7
|
+
* A host that stores the preference in a different shape, under a versioned
|
|
8
|
+
* envelope, or somewhere that is not `localStorage` supplies its own and the
|
|
9
|
+
* resolution and application rules stay the shared ones.
|
|
10
|
+
*
|
|
11
|
+
* The contract is deliberately narrow, so that an implementation cannot make the
|
|
12
|
+
* provider less safe than it was:
|
|
13
|
+
*
|
|
14
|
+
* - `read` is synchronous and runs once, when the provider is created. It may
|
|
15
|
+
* repair or quarantine what it finds, and it must not throw: the provider
|
|
16
|
+
* treats a throw as "nothing stored" and starts from the defaults.
|
|
17
|
+
* - `write` runs only from `setSelection`. Never on mount, never from `preview`,
|
|
18
|
+
* never as a side effect of reading, so the first write to a store is always a
|
|
19
|
+
* user's decision. A throw is swallowed, because a full or blocked store must
|
|
20
|
+
* not break the switch.
|
|
21
|
+
*/
|
|
22
|
+
export type ThemePersistence = {
|
|
23
|
+
read(): Partial<ThemeSelection> | undefined;
|
|
24
|
+
write(next: ThemeSelection): void;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* The default persistence: one JSON document in `localStorage` under `key`.
|
|
28
|
+
*
|
|
29
|
+
* `read` is the provider's original `readSelection`, moved here unchanged — every
|
|
30
|
+
* stored id is validated against the catalogue, so a stale preference falls back to
|
|
31
|
+
* a default rather than reaching the document. `key` may be a function so a
|
|
32
|
+
* provider whose `storageKey` prop changes writes to the key it is now given.
|
|
33
|
+
*
|
|
34
|
+
* Blocked, full or absent storage is survivable in both directions: a read answers
|
|
35
|
+
* with the defaults and a write is dropped, so the preference simply lasts the
|
|
36
|
+
* session.
|
|
37
|
+
*/
|
|
38
|
+
export declare function localStoragePersistence(key: string | (() => string)): ThemePersistence;
|
|
39
|
+
/**
|
|
40
|
+
* What a persistence knows, or nothing.
|
|
41
|
+
*
|
|
42
|
+
* The seam's `read` must not throw, and the provider does not rely on that: a host
|
|
43
|
+
* persistence is code the design system has not seen, and a theme provider that
|
|
44
|
+
* fails to mount because a store misbehaved has turned a lost preference into a
|
|
45
|
+
* blank application.
|
|
46
|
+
*/
|
|
47
|
+
export declare function readPersisted(persistence: ThemePersistence): Partial<ThemeSelection> | undefined;
|
|
48
|
+
/** Hand a selection to the store; a store that throws loses the preference, not the switch. */
|
|
49
|
+
export declare function writePersisted(persistence: ThemePersistence, next: ThemeSelection): void;
|
|
50
|
+
//# sourceMappingURL=theme-persistence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme-persistence.d.ts","sourceRoot":"","sources":["../../../src/components/theme/theme-persistence.ts"],"names":[],"mappings":"AAIA,OAAO,EAA0C,KAAK,cAAc,EAAE,MAAM,eAAe,CAAA;AAE3F;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,IAAI,OAAO,CAAC,cAAc,CAAC,GAAG,SAAS,CAAA;IAC3C,KAAK,CAAC,IAAI,EAAE,cAAc,GAAG,IAAI,CAAA;CAClC,CAAA;AA8DD;;;;;;;;;;;GAWG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,GAAG,gBAAgB,CAatF;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,WAAW,EAAE,gBAAgB,GAAG,OAAO,CAAC,cAAc,CAAC,GAAG,SAAS,CAMhG;AAED,+FAA+F;AAC/F,wBAAgB,cAAc,CAAC,WAAW,EAAE,gBAAgB,EAAE,IAAI,EAAE,cAAc,GAAG,IAAI,CAOxF"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { accentPresets } from "../../lib/tokens.js";
|
|
2
|
+
import { normalizeCustomAccent } from "../../lib/accent-roles.js";
|
|
3
|
+
import { isThemeAccentId } from "../../lib/theme-accents.js";
|
|
4
|
+
import { defaultDarkThemeId, defaultLightThemeId, themeById } from "../../lib/themes.js";
|
|
5
|
+
import { normalizeAppearanceEditorFontSettings } from "../../lib/appearance-font-settings.js";
|
|
6
|
+
import { defaultThemeSelection, resolvedDensity } from "./theme-state.js";
|
|
7
|
+
//#region src/components/theme/theme-persistence.ts
|
|
8
|
+
/**
|
|
9
|
+
* The preference store, or nothing.
|
|
10
|
+
*
|
|
11
|
+
* Only ever called inside a `try`: where storage is blocked — a sandboxed iframe, a
|
|
12
|
+
* privacy mode, a denied site-data permission — merely *reading* the `localStorage`
|
|
13
|
+
* global throws ("Access is denied", `SecurityError`), so even the `typeof` guard
|
|
14
|
+
* has to sit inside the `try` rather than in front of it. Without a store the
|
|
15
|
+
* preference lives only in the provider's signal for the session.
|
|
16
|
+
*/
|
|
17
|
+
function preferenceStore() {
|
|
18
|
+
return typeof localStorage === "undefined" ? void 0 : localStorage;
|
|
19
|
+
}
|
|
20
|
+
/** The stored preference, validated against the catalogue. */
|
|
21
|
+
function readSelection(storageKey) {
|
|
22
|
+
try {
|
|
23
|
+
const raw = preferenceStore()?.getItem(storageKey);
|
|
24
|
+
if (!raw) return defaultThemeSelection;
|
|
25
|
+
const parsed = JSON.parse(raw);
|
|
26
|
+
const fonts = normalizeAppearanceEditorFontSettings(parsed.fonts, parsed.font).settings;
|
|
27
|
+
return {
|
|
28
|
+
appearance: parsed.appearance === "light" || parsed.appearance === "dark" || parsed.appearance === "system" ? parsed.appearance : defaultThemeSelection.appearance,
|
|
29
|
+
lightThemeId: themeById(parsed.lightThemeId ?? "")?.appearance === "light" ? parsed.lightThemeId : defaultLightThemeId,
|
|
30
|
+
darkThemeId: themeById(parsed.darkThemeId ?? "")?.appearance === "dark" ? parsed.darkThemeId : defaultDarkThemeId,
|
|
31
|
+
accent: typeof parsed.accent === "string" && (accentPresets.some((preset) => preset.id === parsed.accent) || isThemeAccentId(parsed.accent)) ? parsed.accent : normalizeCustomAccent(parsed.accent) ?? defaultThemeSelection.accent,
|
|
32
|
+
font: fonts.ui.family,
|
|
33
|
+
fonts,
|
|
34
|
+
density: resolvedDensity(parsed)
|
|
35
|
+
};
|
|
36
|
+
} catch {
|
|
37
|
+
return defaultThemeSelection;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The default persistence: one JSON document in `localStorage` under `key`.
|
|
42
|
+
*
|
|
43
|
+
* `read` is the provider's original `readSelection`, moved here unchanged — every
|
|
44
|
+
* stored id is validated against the catalogue, so a stale preference falls back to
|
|
45
|
+
* a default rather than reaching the document. `key` may be a function so a
|
|
46
|
+
* provider whose `storageKey` prop changes writes to the key it is now given.
|
|
47
|
+
*
|
|
48
|
+
* Blocked, full or absent storage is survivable in both directions: a read answers
|
|
49
|
+
* with the defaults and a write is dropped, so the preference simply lasts the
|
|
50
|
+
* session.
|
|
51
|
+
*/
|
|
52
|
+
function localStoragePersistence(key) {
|
|
53
|
+
const keyOf = () => typeof key === "function" ? key() : key;
|
|
54
|
+
return {
|
|
55
|
+
read: () => readSelection(keyOf()),
|
|
56
|
+
write(next) {
|
|
57
|
+
try {
|
|
58
|
+
preferenceStore()?.setItem(keyOf(), JSON.stringify(next));
|
|
59
|
+
} catch {}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* What a persistence knows, or nothing.
|
|
65
|
+
*
|
|
66
|
+
* The seam's `read` must not throw, and the provider does not rely on that: a host
|
|
67
|
+
* persistence is code the design system has not seen, and a theme provider that
|
|
68
|
+
* fails to mount because a store misbehaved has turned a lost preference into a
|
|
69
|
+
* blank application.
|
|
70
|
+
*/
|
|
71
|
+
function readPersisted(persistence) {
|
|
72
|
+
try {
|
|
73
|
+
return persistence.read();
|
|
74
|
+
} catch {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Hand a selection to the store; a store that throws loses the preference, not the switch. */
|
|
79
|
+
function writePersisted(persistence, next) {
|
|
80
|
+
try {
|
|
81
|
+
persistence.write(next);
|
|
82
|
+
} catch {}
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
export { localStoragePersistence, readPersisted, writePersisted };
|
|
86
|
+
|
|
87
|
+
//# sourceMappingURL=theme-persistence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme-persistence.js","names":[],"sources":["../../../src/components/theme/theme-persistence.ts"],"sourcesContent":["import { normalizeCustomAccent } from '#lib/accent-roles'\nimport { accentPresets } from '#lib/tokens'\nimport { normalizeAppearanceEditorFontSettings } from '#lib/appearance-font-settings'\nimport { defaultDarkThemeId, defaultLightThemeId, isThemeAccentId, themeById } from '#lib/themes'\nimport { defaultThemeSelection, resolvedDensity, type ThemeSelection } from './theme-state'\n\n/**\n * Where a selection is kept between sessions.\n *\n * The provider never touches storage itself; it asks a `ThemePersistence`. The\n * default is {@link localStoragePersistence}, which is what the provider always did.\n * A host that stores the preference in a different shape, under a versioned\n * envelope, or somewhere that is not `localStorage` supplies its own and the\n * resolution and application rules stay the shared ones.\n *\n * The contract is deliberately narrow, so that an implementation cannot make the\n * provider less safe than it was:\n *\n * - `read` is synchronous and runs once, when the provider is created. It may\n * repair or quarantine what it finds, and it must not throw: the provider\n * treats a throw as \"nothing stored\" and starts from the defaults.\n * - `write` runs only from `setSelection`. Never on mount, never from `preview`,\n * never as a side effect of reading, so the first write to a store is always a\n * user's decision. A throw is swallowed, because a full or blocked store must\n * not break the switch.\n */\nexport type ThemePersistence = {\n read(): Partial<ThemeSelection> | undefined\n write(next: ThemeSelection): void\n}\n\n/**\n * The preference store, or nothing.\n *\n * Only ever called inside a `try`: where storage is blocked — a sandboxed iframe, a\n * privacy mode, a denied site-data permission — merely *reading* the `localStorage`\n * global throws (\"Access is denied\", `SecurityError`), so even the `typeof` guard\n * has to sit inside the `try` rather than in front of it. Without a store the\n * preference lives only in the provider's signal for the session.\n */\nfunction preferenceStore(): Storage | undefined {\n return typeof localStorage === 'undefined' ? undefined : localStorage\n}\n\n/** The stored preference, validated against the catalogue. */\nfunction readSelection(storageKey: string): ThemeSelection {\n try {\n const raw = preferenceStore()?.getItem(storageKey)\n if (!raw) return defaultThemeSelection\n const parsed = JSON.parse(raw) as Partial<ThemeSelection>\n const fonts = normalizeAppearanceEditorFontSettings(parsed.fonts, parsed.font).settings\n return {\n appearance:\n parsed.appearance === 'light' ||\n parsed.appearance === 'dark' ||\n parsed.appearance === 'system'\n ? parsed.appearance\n : defaultThemeSelection.appearance,\n // An unknown id would leave the document with no theme at all, so a stale\n // preference from an older catalogue falls back rather than applying nothing.\n lightThemeId:\n themeById(parsed.lightThemeId ?? '')?.appearance === 'light'\n ? (parsed.lightThemeId as string)\n : defaultLightThemeId,\n darkThemeId:\n themeById(parsed.darkThemeId ?? '')?.appearance === 'dark'\n ? (parsed.darkThemeId as string)\n : defaultDarkThemeId,\n // A theme accent is kept whenever it names a slot, even one the stored pair\n // cannot offer: the id is role-shaped so it survives a theme switch, and an\n // unoffered slot already resolves to the theme's own primary when applied. A\n // custom `#rrggbb` is kept in its one stored spelling; its readability is\n // settled when it is painted, not when it is read.\n accent:\n typeof parsed.accent === 'string' &&\n (accentPresets.some((preset) => preset.id === parsed.accent) ||\n isThemeAccentId(parsed.accent))\n ? parsed.accent\n : (normalizeCustomAccent(parsed.accent) ?? defaultThemeSelection.accent),\n font: fonts.ui.family,\n fonts,\n // Every axis has to be read back here, or it is lost twice: the provider's\n // document effect removes the `data-density` that `themeScript` set for the first paint, and\n // the next `setSelection` rewrites the store without it.\n density: resolvedDensity(parsed as ThemeSelection),\n }\n } catch {\n return defaultThemeSelection\n }\n}\n\n/**\n * The default persistence: one JSON document in `localStorage` under `key`.\n *\n * `read` is the provider's original `readSelection`, moved here unchanged — every\n * stored id is validated against the catalogue, so a stale preference falls back to\n * a default rather than reaching the document. `key` may be a function so a\n * provider whose `storageKey` prop changes writes to the key it is now given.\n *\n * Blocked, full or absent storage is survivable in both directions: a read answers\n * with the defaults and a write is dropped, so the preference simply lasts the\n * session.\n */\nexport function localStoragePersistence(key: string | (() => string)): ThemePersistence {\n const keyOf = () => (typeof key === 'function' ? key() : key)\n return {\n read: () => readSelection(keyOf()),\n write(next) {\n try {\n preferenceStore()?.setItem(keyOf(), JSON.stringify(next))\n } catch {\n // A full, blocked or unavailable store must not break the switch: the\n // preference simply does not survive the reload.\n }\n },\n }\n}\n\n/**\n * What a persistence knows, or nothing.\n *\n * The seam's `read` must not throw, and the provider does not rely on that: a host\n * persistence is code the design system has not seen, and a theme provider that\n * fails to mount because a store misbehaved has turned a lost preference into a\n * blank application.\n */\nexport function readPersisted(persistence: ThemePersistence): Partial<ThemeSelection> | undefined {\n try {\n return persistence.read()\n } catch {\n return undefined\n }\n}\n\n/** Hand a selection to the store; a store that throws loses the preference, not the switch. */\nexport function writePersisted(persistence: ThemePersistence, next: ThemeSelection): void {\n try {\n persistence.write(next)\n } catch {\n // Same contract as a full or blocked `localStorage`: the choice holds for the\n // session and does not survive the reload.\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAwCA,SAAS,kBAAuC;CAC9C,OAAO,OAAO,iBAAiB,cAAc,KAAA,IAAY;AAC3D;;AAGA,SAAS,cAAc,YAAoC;CACzD,IAAI;EACF,MAAM,MAAM,gBAAgB,CAAC,EAAE,QAAQ,UAAU;EACjD,IAAI,CAAC,KAAK,OAAO;EACjB,MAAM,SAAS,KAAK,MAAM,GAAG;EAC7B,MAAM,QAAQ,sCAAsC,OAAO,OAAO,OAAO,IAAI,CAAC,CAAC;EAC/E,OAAO;GACL,YACE,OAAO,eAAe,WACtB,OAAO,eAAe,UACtB,OAAO,eAAe,WAClB,OAAO,aACP,sBAAsB;GAG5B,cACE,UAAU,OAAO,gBAAgB,EAAE,CAAC,EAAE,eAAe,UAChD,OAAO,eACR;GACN,aACE,UAAU,OAAO,eAAe,EAAE,CAAC,EAAE,eAAe,SAC/C,OAAO,cACR;GAMN,QACE,OAAO,OAAO,WAAW,aACxB,cAAc,MAAM,WAAW,OAAO,OAAO,OAAO,MAAM,KACzD,gBAAgB,OAAO,MAAM,KAC3B,OAAO,SACN,sBAAsB,OAAO,MAAM,KAAK,sBAAsB;GACrE,MAAM,MAAM,GAAG;GACf;GAIA,SAAS,gBAAgB,MAAwB;EACnD;CACF,QAAQ;EACN,OAAO;CACT;AACF;;;;;;;;;;;;;AAcA,SAAgB,wBAAwB,KAAgD;CACtF,MAAM,cAAe,OAAO,QAAQ,aAAa,IAAI,IAAI;CACzD,OAAO;EACL,YAAY,cAAc,MAAM,CAAC;EACjC,MAAM,MAAM;GACV,IAAI;IACF,gBAAgB,CAAC,EAAE,QAAQ,MAAM,GAAG,KAAK,UAAU,IAAI,CAAC;GAC1D,QAAQ,CAGR;EACF;CACF;AACF;;;;;;;;;AAUA,SAAgB,cAAc,aAAoE;CAChG,IAAI;EACF,OAAO,YAAY,KAAK;CAC1B,QAAQ;EACN;CACF;AACF;;AAGA,SAAgB,eAAe,aAA+B,MAA4B;CACxF,IAAI;EACF,YAAY,MAAM,IAAI;CACxB,QAAQ,CAGR;AACF"}
|