@codefast/theme 0.7.0 → 0.8.1
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/CHANGELOG.md +29 -0
- package/README.md +2 -2
- package/dist/appearance-provider.d.ts +8 -6
- package/dist/appearance-provider.d.ts.map +1 -1
- package/dist/appearance-provider.js +9 -3
- package/dist/appearance-provider.js.map +1 -1
- package/dist/appearance-script.d.ts +5 -3
- package/dist/appearance-script.d.ts.map +1 -1
- package/dist/appearance-script.js +4 -2
- package/dist/appearance-script.js.map +1 -1
- package/dist/appearance.d.ts +1 -1
- package/dist/appearance.d.ts.map +1 -1
- package/dist/appearance.js.map +1 -1
- package/dist/dom.d.ts.map +1 -1
- package/dist/dom.js +3 -0
- package/dist/dom.js.map +1 -1
- package/package.json +7 -7
- package/src/appearance-provider.tsx +17 -8
- package/src/appearance-script.tsx +10 -5
- package/src/appearance.ts +1 -1
- package/src/dom.ts +4 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
# @codefast/theme
|
|
2
2
|
|
|
3
|
+
## 0.8.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#748](https://github.com/codefastlabs/codefast/pull/748) [`bde6d1b`](https://github.com/codefastlabs/codefast/commit/bde6d1b46f55f65039f8a3c8e062693fe328952a) Thanks [@thevuong](https://github.com/thevuong)! - Mirror the committed appearance ref in a layout effect so a programmatic `setAppearance` landing before the passive
|
|
8
|
+
flush compares against the just-committed appearance instead of the previous one.
|
|
9
|
+
|
|
10
|
+
## 0.8.0
|
|
11
|
+
|
|
12
|
+
### Minor Changes
|
|
13
|
+
|
|
14
|
+
- [#745](https://github.com/codefastlabs/codefast/pull/745) [`5cbb93a`](https://github.com/codefastlabs/codefast/commit/5cbb93aa42a63f2baa320eb1684267d310018741) Thanks [@thevuong](https://github.com/thevuong)! - fix(theme): harden appearance provider/script and tighten public types
|
|
15
|
+
|
|
16
|
+
Three correctness fixes plus two type-safety tightenings:
|
|
17
|
+
|
|
18
|
+
- Overlapping `setAppearance` calls with `disableTransition` enabled could orphan a transition-suppression `<style>` —
|
|
19
|
+
`enableTransitionsRef` was overwritten before the previous cleanup ran — leaving `*{transition:none}` in `<head>`
|
|
20
|
+
forever and permanently disabling CSS transitions. Each call now flushes the pending suppression before injecting a
|
|
21
|
+
new one.
|
|
22
|
+
- `AppearanceScript` now runtime-validates its `appearance` prop (as `AppearanceProvider` already did), so an invalid
|
|
23
|
+
value can no longer be written to `<html>` before first paint or diverge from the provider's fallback.
|
|
24
|
+
- `applyColorScheme` no-ops during SSR instead of throwing on `window` access, matching its sibling
|
|
25
|
+
`suppressTransitions`.
|
|
26
|
+
- Public props on `AppearanceProvider` and `AppearanceScript` now accept explicit `undefined` (`?: T | undefined`) under
|
|
27
|
+
`exactOptionalPropertyTypes`, so JSX callers can pass possibly-undefined expressions such as `nonce` and `appearance`.
|
|
28
|
+
|
|
29
|
+
Breaking (type surface only, no runtime behavior change): `appearances` is now `ReadonlyArray<Appearance>` (was
|
|
30
|
+
`Array<Appearance>`), so assigning it to a mutable `Appearance[]` is now a compile error.
|
|
31
|
+
|
|
3
32
|
## 0.7.0
|
|
4
33
|
|
|
5
34
|
## 0.6.2
|
package/README.md
CHANGED
|
@@ -24,8 +24,8 @@ pnpm add @codefast/theme
|
|
|
24
24
|
# yarn add @codefast/theme
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
Requires `react` and `react-dom` version 19 or later as peer dependencies. Published on `0.x`
|
|
28
|
-
breaking changes ship as minor versions.
|
|
27
|
+
Requires `react` and `react-dom` version 19 or later as peer dependencies. Published on `0.x` and versioned on its own
|
|
28
|
+
track — breaking changes ship as minor versions.
|
|
29
29
|
|
|
30
30
|
## Quick Start
|
|
31
31
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { JSX, ReactNode } from "react";
|
|
2
2
|
import type { Appearance } from "#/appearance";
|
|
3
3
|
/**
|
|
4
|
+
* Props for {@link AppearanceProvider}.
|
|
5
|
+
*
|
|
4
6
|
* @since 0.5.0-canary.2
|
|
5
7
|
*/
|
|
6
8
|
export type AppearanceProviderProps = {
|
|
@@ -10,24 +12,24 @@ export type AppearanceProviderProps = {
|
|
|
10
12
|
* Prevents jarring color animations when switching appearances.
|
|
11
13
|
* Default: false
|
|
12
14
|
*/
|
|
13
|
-
readonly disableTransition?: boolean;
|
|
15
|
+
readonly disableTransition?: boolean | undefined;
|
|
14
16
|
/**
|
|
15
17
|
* CSP nonce for inline styles when `disableTransition` is enabled.
|
|
16
18
|
*/
|
|
17
|
-
readonly nonce?: string;
|
|
19
|
+
readonly nonce?: string | undefined;
|
|
18
20
|
/**
|
|
19
21
|
* Custom async persistence for appearance changes; replaces the `localStorage` auto-persist.
|
|
20
22
|
*/
|
|
21
|
-
readonly persistAppearance?: (value: Appearance) => Promise<void
|
|
23
|
+
readonly persistAppearance?: ((value: Appearance) => Promise<void>) | undefined;
|
|
22
24
|
/**
|
|
23
25
|
* Callback invoked when `persistAppearance` rejects.
|
|
24
26
|
*/
|
|
25
|
-
readonly onPersistError?: (error: unknown, attemptedAppearance: Appearance) => void;
|
|
27
|
+
readonly onPersistError?: ((error: unknown, attemptedAppearance: Appearance) => void) | undefined;
|
|
26
28
|
/**
|
|
27
29
|
* Fallback preference when storage has no valid entry — also what SSR renders.
|
|
28
30
|
* Defaults to {@link DEFAULT_APPEARANCE}.
|
|
29
31
|
*/
|
|
30
|
-
readonly appearance?: Appearance;
|
|
32
|
+
readonly appearance?: Appearance | undefined;
|
|
31
33
|
/**
|
|
32
34
|
* `localStorage` key the preference is restored from and persisted under.
|
|
33
35
|
* Defaults to {@link STORAGE_KEY}; pair with `<AppearanceScript>` using the **same key**.
|
|
@@ -41,7 +43,7 @@ export type AppearanceProviderProps = {
|
|
|
41
43
|
* prop. For a returning visitor whose stored preference differs, components that render
|
|
42
44
|
* preference-dependent markup hydrate-reconcile once; gate them behind a mounted flag to avoid that.
|
|
43
45
|
*/
|
|
44
|
-
readonly storageKey?: string;
|
|
46
|
+
readonly storageKey?: string | undefined;
|
|
45
47
|
};
|
|
46
48
|
/**
|
|
47
49
|
* Provider component for appearance state management.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"appearance-provider.d.ts","sourceRoot":"","sources":["../src/appearance-provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"appearance-provider.d.ts","sourceRoot":"","sources":["../src/appearance-provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAc5C,OAAO,KAAK,EAAE,UAAU,EAAuC,MAAM,cAAc,CAAC;AAsEpF;;;;GAIG;AACH,MAAM,MAAM,uBAAuB,GAAG;IACpC,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC7B;;;;OAIG;IACH,QAAQ,CAAC,iBAAiB,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACjD;;OAEG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC;;OAEG;IACH,QAAQ,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,UAAU,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,SAAS,CAAC;IAChF;;OAEG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,UAAU,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;IAClG;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC;IAC7C;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1C,CAAC;AAIF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,EACjC,QAAQ,EACR,iBAAyB,EACzB,KAAK,EACL,cAAc,EACd,iBAAiB,EACjB,UAAwB,EACxB,UAAU,EAAE,iBAAsC,GACnD,EAAE,uBAAuB,GAAG,GAAG,CAAC,OAAO,CAmNvC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { startTransition, useCallback, useEffect, useEffectEvent, useMemo, useOptimistic, useRef, useState, useSyncExternalStore, } from "react";
|
|
2
|
+
import { startTransition, useCallback, useEffect, useEffectEvent, useLayoutEffect, useMemo, useOptimistic, useRef, useState, useSyncExternalStore, } from "react";
|
|
3
3
|
import { appearanceSchema } from "#/appearance";
|
|
4
4
|
import { AppearanceContext } from "#/appearance-context";
|
|
5
5
|
import { getSystemColorScheme } from "#/color-scheme";
|
|
@@ -91,7 +91,11 @@ export function AppearanceProvider({ children, disableTransition = false, nonce,
|
|
|
91
91
|
const isPending = optimisticAppearance !== appearance;
|
|
92
92
|
// Stable ref so setAppearance does not re-create every time the committed appearance changes
|
|
93
93
|
const committedAppearanceRef = useRef(appearance);
|
|
94
|
-
|
|
94
|
+
// Layout effect, not passive: a setAppearance call can land before the passive flush and
|
|
95
|
+
// must compare against the just-committed appearance, not the previous one.
|
|
96
|
+
useLayoutEffect(() => {
|
|
97
|
+
committedAppearanceRef.current = appearance;
|
|
98
|
+
}, [appearance]);
|
|
95
99
|
// Shared BroadcastChannel — reused by the setAppearance sender instead of opening a new one per call
|
|
96
100
|
const channelRef = useRef(null);
|
|
97
101
|
// suppressTransitions cleanup stored here and called after applyColorScheme commits to the DOM
|
|
@@ -181,8 +185,10 @@ export function AppearanceProvider({ children, disableTransition = false, nonce,
|
|
|
181
185
|
if (value === committedAppearanceRef.current) {
|
|
182
186
|
return;
|
|
183
187
|
}
|
|
184
|
-
// Store the cleanup; it will be called after applyColorScheme runs in the colorScheme effect
|
|
188
|
+
// Store the cleanup; it will be called after applyColorScheme runs in the colorScheme effect.
|
|
189
|
+
// Flush any pending suppression first so an overlapping call cannot orphan its injected <style>.
|
|
185
190
|
if (disableTransition) {
|
|
191
|
+
enableTransitionsRef.current?.();
|
|
186
192
|
enableTransitionsRef.current = suppressTransitions(nonce);
|
|
187
193
|
}
|
|
188
194
|
startTransition(async () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"appearance-provider.js","sourceRoot":"","sources":["../src/appearance-provider.tsx"],"names":[],"mappings":";AACA,OAAO,EACL,eAAe,EACf,WAAW,EACX,SAAS,EACT,cAAc,EACd,OAAO,EACP,aAAa,EACb,MAAM,EACN,QAAQ,EACR,oBAAoB,GACrB,MAAM,OAAO,CAAC;AAGf,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACzG,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,OAAO,CAAC;AAE9D;;;;;gFAKgF;AAEhF;;;;;GAKG;AACH,SAAS,4BAA4B,CAAC,QAAoB;IACxD,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IAE5C,UAAU,CAAC,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAEhD,OAAO,GAAG,EAAE;QACV,UAAU,CAAC,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IACrD,CAAC,CAAC;AACJ,CAAC;AAED,8FAA8F;AAC9F,SAAS,iBAAiB;IACxB,OAAO,oBAAoB,CAAC;AAC9B,CAAC;AAED;;GAEG;AACH,SAAS,uBAAuB;IAC9B,I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EAAE,CAAC;wBACvB,OAAO,CAAC,KAAK,CAAC,4CAA4C,EAAE,aAAa,CAAC,CAAC;oBAC7E,CAAC;gBACH,CAAC;qBAAM,CAAC;oBACN,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;gBACpD,CAAC;YACH,CAAC;oBAAS,CAAC;gBACT,oFAAoF;gBACpF,yFAAyF;gBACzF,2BAA2B;gBAC3B,MAAM,MAAM,GAAG,oBAAoB,CAAC,OAAO,CAAC;gBAE5C,IAAI,MAAM,EAAE,CAAC;oBACX,oBAAoB,CAAC,OAAO,GAAG,IAAI,CAAC;oBACpC,MAAM,EAAE,CAAC;gBACX,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,EACD,CAAC,iBAAiB,EAAE,KAAK,EAAE,uBAAuB,EAAE,OAAO,EAAE,cAAc,CAAC,CAC7E,CAAC;IAEF,sEAAsE;IACtE,MAAM,KAAK,GAAG,OAAO,CACnB,GAAG,EAAE,CAAC,CAAC;QACL,SAAS;QACT,WAAW;QACX,aAAa;QACb,UAAU,EAAE,oBAAoB;KACjC,CAAC,EACF,CAAC,oBAAoB,EAAE,WAAW,EAAE,aAAa,EAAE,SAAS,CAAC,CAC9D,CAAC;IAEF,OAAO,KAAC,iBAAiB,IAAC,KAAK,EAAE,KAAK,YAAG,QAAQ,GAAqB,CAAC;AACzE,CAAC"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { JSX } from "react";
|
|
2
2
|
import type { Appearance } from "#/appearance";
|
|
3
3
|
/**
|
|
4
|
+
* Props for {@link AppearanceScript}.
|
|
5
|
+
*
|
|
4
6
|
* @since 0.5.0-canary.2
|
|
5
7
|
*/
|
|
6
8
|
export type AppearanceScriptProps = {
|
|
@@ -8,17 +10,17 @@ export type AppearanceScriptProps = {
|
|
|
8
10
|
* Fallback preference when the storage entry is absent or unrecognised.
|
|
9
11
|
* Defaults to {@link DEFAULT_APPEARANCE}.
|
|
10
12
|
*/
|
|
11
|
-
readonly appearance?: Appearance;
|
|
13
|
+
readonly appearance?: Appearance | undefined;
|
|
12
14
|
/**
|
|
13
15
|
* CSP nonce applied to the inline script element.
|
|
14
16
|
*/
|
|
15
|
-
readonly nonce?: string;
|
|
17
|
+
readonly nonce?: string | undefined;
|
|
16
18
|
/**
|
|
17
19
|
* `localStorage` key the script reads before first paint — a recognised value (`"light"`,
|
|
18
20
|
* `"dark"`, or `"automatic"`) wins over `appearance`.
|
|
19
21
|
* Defaults to {@link STORAGE_KEY}; pair with `<AppearanceProvider>` using the **same key**.
|
|
20
22
|
*/
|
|
21
|
-
readonly storageKey?: string;
|
|
23
|
+
readonly storageKey?: string | undefined;
|
|
22
24
|
};
|
|
23
25
|
/**
|
|
24
26
|
* Inline script that prevents Flash of Unstyled Content (FOUC).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"appearance-script.d.ts","sourceRoot":"","sources":["../src/appearance-script.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAGjC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAe/C
|
|
1
|
+
{"version":3,"file":"appearance-script.d.ts","sourceRoot":"","sources":["../src/appearance-script.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAGjC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAe/C;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC;IAC7C;;OAEG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC;;;;OAIG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1C,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,gBAAgB,CAAC,EAC/B,KAAK,EACL,UAAwB,EACxB,UAA+B,GAChC,EAAE,qBAAqB,GAAG,GAAG,CAAC,OAAO,CAWrC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { appearances } from "#/appearance";
|
|
2
|
+
import { appearances, appearanceSchema } from "#/appearance";
|
|
3
3
|
import { DEFAULT_APPEARANCE, MEDIA, STORAGE_KEY } from "#/constants";
|
|
4
4
|
/* JSON.stringify does not escape </script>, so we replace the three characters that
|
|
5
5
|
* can form a closing tag: < > /. Unicode escapes are valid JS and safe in HTML. */
|
|
@@ -39,10 +39,12 @@ const APPEARANCE_VALID_CHECK = appearances.map((s) => `s===${toScriptSafe(s)}`).
|
|
|
39
39
|
* @since 0.5.0-canary.2
|
|
40
40
|
*/
|
|
41
41
|
export function AppearanceScript({ nonce, storageKey = STORAGE_KEY, appearance = DEFAULT_APPEARANCE, }) {
|
|
42
|
+
// Guard against invalid values bypassing TypeScript so the fallback matches AppearanceProvider.
|
|
43
|
+
const safeAppearance = appearanceSchema.safeParse(appearance).success ? appearance : DEFAULT_APPEARANCE;
|
|
42
44
|
// toScriptSafe = JSON.stringify + escape <>/ so </script> cannot break out of the tag.
|
|
43
45
|
// The script reads localStorage before first paint so the stored preference applies
|
|
44
46
|
// without FOUC; an absent or invalid entry falls back to fb.
|
|
45
|
-
const appearanceScript = `(function(){try{var sk=${toScriptSafe(storageKey)},fb=${toScriptSafe(
|
|
47
|
+
const appearanceScript = `(function(){try{var sk=${toScriptSafe(storageKey)},fb=${toScriptSafe(safeAppearance)},s=localStorage.getItem(sk),appearance=(${APPEARANCE_VALID_CHECK})?s:fb,colorScheme=appearance;"automatic"===appearance&&(colorScheme=window.matchMedia(${toScriptSafe(MEDIA)}).matches?"dark":"light"),document.documentElement.classList.remove(${APPEARANCE_REMOVE_ARGS}),document.documentElement.classList.add(colorScheme),document.documentElement.style.colorScheme=colorScheme,document.documentElement.dataset.appearance=appearance}catch(e){}})()`;
|
|
46
48
|
const nonceProps = nonce === undefined ? {} : { nonce };
|
|
47
49
|
return _jsx("script", { dangerouslySetInnerHTML: { __html: appearanceScript }, suppressHydrationWarning: true, ...nonceProps });
|
|
48
50
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"appearance-script.js","sourceRoot":"","sources":["../src/appearance-script.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"appearance-script.js","sourceRoot":"","sources":["../src/appearance-script.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAErE;mFACmF;AACnF,SAAS,YAAY,CAAC,KAAc;IAClC,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;AAChH,CAAC;AAED,yFAAyF;AACzF,MAAM,sBAAsB,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACjF,MAAM,sBAAsB,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AA2B3F,wHAAwH;AAExH;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,gBAAgB,CAAC,EAC/B,KAAK,EACL,UAAU,GAAG,WAAW,EACxB,UAAU,GAAG,kBAAkB,GACT;IACtB,gGAAgG;IAChG,MAAM,cAAc,GAAG,gBAAgB,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,kBAAkB,CAAC;IAExG,wFAAwF;IACxF,oFAAoF;IACpF,6DAA6D;IAC7D,MAAM,gBAAgB,GAAG,0BAA0B,YAAY,CAAC,UAAU,CAAC,OAAO,YAAY,CAAC,cAAc,CAAC,2CAA2C,sBAAsB,0FAA0F,YAAY,CAAC,KAAK,CAAC,uEAAuE,sBAAsB,oLAAoL,CAAC;IAC9iB,MAAM,UAAU,GAAG,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;IAExD,OAAO,iBAAQ,uBAAuB,EAAE,EAAE,MAAM,EAAE,gBAAgB,EAAE,EAAE,wBAAwB,WAAK,UAAU,GAAI,CAAC;AACpH,CAAC"}
|
package/dist/appearance.d.ts
CHANGED
|
@@ -37,7 +37,7 @@ export type ColorScheme = Exclude<Appearance, "automatic">;
|
|
|
37
37
|
*
|
|
38
38
|
* @since 0.5.0-canary.2
|
|
39
39
|
*/
|
|
40
|
-
export declare const appearances:
|
|
40
|
+
export declare const appearances: ReadonlyArray<Appearance>;
|
|
41
41
|
/**
|
|
42
42
|
* Shape of the context value provided by AppearanceProvider.
|
|
43
43
|
*
|
package/dist/appearance.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"appearance.d.ts","sourceRoot":"","sources":["../src/appearance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAWxB;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,EAAE,CAAC,CAAC,OAAO,CAAC;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,WAAW,CAAA;CAAE,CAI/F,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAE1D;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;AAE3D;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,EAAE,
|
|
1
|
+
{"version":3,"file":"appearance.d.ts","sourceRoot":"","sources":["../src/appearance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAWxB;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,EAAE,CAAC,CAAC,OAAO,CAAC;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,WAAW,CAAA;CAAE,CAI/F,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAE1D;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;AAE3D;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,EAAE,aAAa,CAAC,UAAU,CAA4B,CAAC;AAE/E;;;;;;;;;GASG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;IAClC,QAAQ,CAAC,aAAa,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7D,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;CACjC,CAAC"}
|
package/dist/appearance.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"appearance.js","sourceRoot":"","sources":["../src/appearance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;gFAOgF;AAEhF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAwE,CAAC,CAAC,IAAI,CAAC;IAC1G,OAAO;IACP,MAAM;IACN,WAAW;CACZ,CAAC,CAAC;AAsBH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"appearance.js","sourceRoot":"","sources":["../src/appearance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;gFAOgF;AAEhF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAwE,CAAC,CAAC,IAAI,CAAC;IAC1G,OAAO;IACP,MAAM;IACN,WAAW;CACZ,CAAC,CAAC;AAsBH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,WAAW,GAA8B,gBAAgB,CAAC,OAAO,CAAC"}
|
package/dist/dom.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dom.d.ts","sourceRoot":"","sources":["../src/dom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAIhD;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"dom.d.ts","sourceRoot":"","sources":["../src/dom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAIhD;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CAU/D;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,IAAI,CAyC9D"}
|
package/dist/dom.js
CHANGED
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
* @since 0.3.16-canary.0
|
|
10
10
|
*/
|
|
11
11
|
export function applyColorScheme(colorScheme) {
|
|
12
|
+
if (typeof globalThis.window === "undefined") {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
12
15
|
const root = window.document.documentElement;
|
|
13
16
|
root.classList.remove("light", "dark", "automatic");
|
|
14
17
|
root.classList.add(colorScheme);
|
package/dist/dom.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dom.js","sourceRoot":"","sources":["../src/dom.ts"],"names":[],"mappings":"AAEA,wHAAwH;AAExH;;;;;;;;GAQG;AACH,MAAM,UAAU,gBAAgB,CAAC,WAAwB;IACvD,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC;IAE7C,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;IACpD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAChC,IAAI,CAAC,KAAK,CAAC,WAAW,GAAG,WAAW,CAAC;AACvC,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAc;IAChD,IAAI,OAAO,UAAU,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;QAC7C,OAAO,GAAG,EAAE;YACV,UAAU;QACZ,CAAC,CAAC;IACJ,CAAC;IAED,oCAAoC;IACpC,MAAM,oBAAoB,GAAG,UAAU,CAAC,MAAM,CAAC,UAAU,CAAC,kCAAkC,CAAC,CAAC,OAAO,CAAC;IAEtG,IAAI,oBAAoB,EAAE,CAAC;QACzB,OAAO,GAAG,EAAE;YACV,UAAU;QACZ,CAAC,CAAC;IACJ,CAAC;IAED,2CAA2C;IAC3C,MAAM,GAAG,GAAG,QAAQ,CAAC,aAAa,CAAC,OAAO,CA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AAM,KAAK,WAAW,EAAE,CAAC;QAC7C,OAAO,GAAG,EAAE;YACV,UAAU;QACZ,CAAC,CAAC;IACJ,CAAC;IAED,oCAAoC;IACpC,MAAM,oBAAoB,GAAG,UAAU,CAAC,MAAM,CAAC,UAAU,CAAC,kCAAkC,CAAC,CAAC,OAAO,CAAC;IAEtG,IAAI,oBAAoB,EAAE,CAAC;QACzB,OAAO,GAAG,EAAE;YACV,UAAU;QACZ,CAAC,CAAC;IACJ,CAAC;IAED,2CAA2C;IAC3C,MAAM,GAAG,GAAG,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAE5C,IAAI,KAAK,EAAE,CAAC;QACV,GAAG,CAAC,YAAY,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IACnC,CAAC;IAED,0EAA0E;IAC1E,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAC,iDAAiD,CAAC,CAAC,CAAC;IAEvF,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAE1B,OAAO,GAAG,EAAE;QACV,4DAA4D;QAC5D,KAAK,MAAM,CAAC,gBAAgB,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAE5C,+DAA+D;QAC/D,qBAAqB,CAAC,GAAG,EAAE;YACzB,qBAAqB,CAAC,GAAG,EAAE;gBACzB,IAAI,GAAG,CAAC,UAAU,EAAE,CAAC;oBACnB,GAAG,CAAC,MAAM,EAAE,CAAC;gBACf,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codefast/theme",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Appearance management for React 19 with HIG-inspired vocabulary - optimistic updates, cross-tab sync, and FOUC-free SSR",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dark-mode",
|
|
@@ -112,15 +112,15 @@
|
|
|
112
112
|
"@testing-library/dom": "^10.4.1",
|
|
113
113
|
"@testing-library/jest-dom": "^7.0.1",
|
|
114
114
|
"@testing-library/react": "^16.3.2",
|
|
115
|
-
"@testing-library/user-event": "^14.6.
|
|
115
|
+
"@testing-library/user-event": "^14.6.6",
|
|
116
116
|
"@types/node": "^26.2.0",
|
|
117
|
-
"@vitejs/plugin-react": "^6.0
|
|
118
|
-
"@vitest/coverage-v8": "^4.1.
|
|
117
|
+
"@vitejs/plugin-react": "^6.1.0",
|
|
118
|
+
"@vitest/coverage-v8": "^4.1.11",
|
|
119
119
|
"jsdom": "^30.0.1",
|
|
120
120
|
"typescript": "^7.0.2",
|
|
121
|
-
"vite": "^8.2.
|
|
122
|
-
"vitest": "^4.1.
|
|
123
|
-
"@codefast/typescript-config": "0.
|
|
121
|
+
"vite": "^8.2.2",
|
|
122
|
+
"vitest": "^4.1.11",
|
|
123
|
+
"@codefast/typescript-config": "0.8.0"
|
|
124
124
|
},
|
|
125
125
|
"peerDependencies": {
|
|
126
126
|
"@types/react": ">=19.0.0",
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
useCallback,
|
|
5
5
|
useEffect,
|
|
6
6
|
useEffectEvent,
|
|
7
|
+
useLayoutEffect,
|
|
7
8
|
useMemo,
|
|
8
9
|
useOptimistic,
|
|
9
10
|
useRef,
|
|
@@ -82,6 +83,8 @@ function readAppearanceFromStorage(storageKey: string): Appearance | null {
|
|
|
82
83
|
// ── Props ────────────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
83
84
|
|
|
84
85
|
/**
|
|
86
|
+
* Props for {@link AppearanceProvider}.
|
|
87
|
+
*
|
|
85
88
|
* @since 0.5.0-canary.2
|
|
86
89
|
*/
|
|
87
90
|
export type AppearanceProviderProps = {
|
|
@@ -91,24 +94,24 @@ export type AppearanceProviderProps = {
|
|
|
91
94
|
* Prevents jarring color animations when switching appearances.
|
|
92
95
|
* Default: false
|
|
93
96
|
*/
|
|
94
|
-
readonly disableTransition?: boolean;
|
|
97
|
+
readonly disableTransition?: boolean | undefined;
|
|
95
98
|
/**
|
|
96
99
|
* CSP nonce for inline styles when `disableTransition` is enabled.
|
|
97
100
|
*/
|
|
98
|
-
readonly nonce?: string;
|
|
101
|
+
readonly nonce?: string | undefined;
|
|
99
102
|
/**
|
|
100
103
|
* Custom async persistence for appearance changes; replaces the `localStorage` auto-persist.
|
|
101
104
|
*/
|
|
102
|
-
readonly persistAppearance?: (value: Appearance) => Promise<void
|
|
105
|
+
readonly persistAppearance?: ((value: Appearance) => Promise<void>) | undefined;
|
|
103
106
|
/**
|
|
104
107
|
* Callback invoked when `persistAppearance` rejects.
|
|
105
108
|
*/
|
|
106
|
-
readonly onPersistError?: (error: unknown, attemptedAppearance: Appearance) => void;
|
|
109
|
+
readonly onPersistError?: ((error: unknown, attemptedAppearance: Appearance) => void) | undefined;
|
|
107
110
|
/**
|
|
108
111
|
* Fallback preference when storage has no valid entry — also what SSR renders.
|
|
109
112
|
* Defaults to {@link DEFAULT_APPEARANCE}.
|
|
110
113
|
*/
|
|
111
|
-
readonly appearance?: Appearance;
|
|
114
|
+
readonly appearance?: Appearance | undefined;
|
|
112
115
|
/**
|
|
113
116
|
* `localStorage` key the preference is restored from and persisted under.
|
|
114
117
|
* Defaults to {@link STORAGE_KEY}; pair with `<AppearanceScript>` using the **same key**.
|
|
@@ -122,7 +125,7 @@ export type AppearanceProviderProps = {
|
|
|
122
125
|
* prop. For a returning visitor whose stored preference differs, components that render
|
|
123
126
|
* preference-dependent markup hydrate-reconcile once; gate them behind a mounted flag to avoid that.
|
|
124
127
|
*/
|
|
125
|
-
readonly storageKey?: string;
|
|
128
|
+
readonly storageKey?: string | undefined;
|
|
126
129
|
};
|
|
127
130
|
|
|
128
131
|
// ── Component ────────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
@@ -174,7 +177,11 @@ export function AppearanceProvider({
|
|
|
174
177
|
// Stable ref so setAppearance does not re-create every time the committed appearance changes
|
|
175
178
|
const committedAppearanceRef = useRef(appearance);
|
|
176
179
|
|
|
177
|
-
|
|
180
|
+
// Layout effect, not passive: a setAppearance call can land before the passive flush and
|
|
181
|
+
// must compare against the just-committed appearance, not the previous one.
|
|
182
|
+
useLayoutEffect(() => {
|
|
183
|
+
committedAppearanceRef.current = appearance;
|
|
184
|
+
}, [appearance]);
|
|
178
185
|
|
|
179
186
|
// Shared BroadcastChannel — reused by the setAppearance sender instead of opening a new one per call
|
|
180
187
|
const channelRef = useRef<BroadcastChannel | null>(null);
|
|
@@ -294,8 +301,10 @@ export function AppearanceProvider({
|
|
|
294
301
|
return;
|
|
295
302
|
}
|
|
296
303
|
|
|
297
|
-
// Store the cleanup; it will be called after applyColorScheme runs in the colorScheme effect
|
|
304
|
+
// Store the cleanup; it will be called after applyColorScheme runs in the colorScheme effect.
|
|
305
|
+
// Flush any pending suppression first so an overlapping call cannot orphan its injected <style>.
|
|
298
306
|
if (disableTransition) {
|
|
307
|
+
enableTransitionsRef.current?.();
|
|
299
308
|
enableTransitionsRef.current = suppressTransitions(nonce);
|
|
300
309
|
}
|
|
301
310
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { JSX } from "react";
|
|
2
2
|
|
|
3
|
-
import { appearances } from "#/appearance";
|
|
3
|
+
import { appearances, appearanceSchema } from "#/appearance";
|
|
4
4
|
import type { Appearance } from "#/appearance";
|
|
5
5
|
import { DEFAULT_APPEARANCE, MEDIA, STORAGE_KEY } from "#/constants";
|
|
6
6
|
|
|
@@ -17,6 +17,8 @@ const APPEARANCE_VALID_CHECK = appearances.map((s) => `s===${toScriptSafe(s)}`).
|
|
|
17
17
|
// ── Props ────────────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
+
* Props for {@link AppearanceScript}.
|
|
21
|
+
*
|
|
20
22
|
* @since 0.5.0-canary.2
|
|
21
23
|
*/
|
|
22
24
|
export type AppearanceScriptProps = {
|
|
@@ -24,17 +26,17 @@ export type AppearanceScriptProps = {
|
|
|
24
26
|
* Fallback preference when the storage entry is absent or unrecognised.
|
|
25
27
|
* Defaults to {@link DEFAULT_APPEARANCE}.
|
|
26
28
|
*/
|
|
27
|
-
readonly appearance?: Appearance;
|
|
29
|
+
readonly appearance?: Appearance | undefined;
|
|
28
30
|
/**
|
|
29
31
|
* CSP nonce applied to the inline script element.
|
|
30
32
|
*/
|
|
31
|
-
readonly nonce?: string;
|
|
33
|
+
readonly nonce?: string | undefined;
|
|
32
34
|
/**
|
|
33
35
|
* `localStorage` key the script reads before first paint — a recognised value (`"light"`,
|
|
34
36
|
* `"dark"`, or `"automatic"`) wins over `appearance`.
|
|
35
37
|
* Defaults to {@link STORAGE_KEY}; pair with `<AppearanceProvider>` using the **same key**.
|
|
36
38
|
*/
|
|
37
|
-
readonly storageKey?: string;
|
|
39
|
+
readonly storageKey?: string | undefined;
|
|
38
40
|
};
|
|
39
41
|
|
|
40
42
|
// ── Component ────────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
@@ -72,10 +74,13 @@ export function AppearanceScript({
|
|
|
72
74
|
storageKey = STORAGE_KEY,
|
|
73
75
|
appearance = DEFAULT_APPEARANCE,
|
|
74
76
|
}: AppearanceScriptProps): JSX.Element {
|
|
77
|
+
// Guard against invalid values bypassing TypeScript so the fallback matches AppearanceProvider.
|
|
78
|
+
const safeAppearance = appearanceSchema.safeParse(appearance).success ? appearance : DEFAULT_APPEARANCE;
|
|
79
|
+
|
|
75
80
|
// toScriptSafe = JSON.stringify + escape <>/ so </script> cannot break out of the tag.
|
|
76
81
|
// The script reads localStorage before first paint so the stored preference applies
|
|
77
82
|
// without FOUC; an absent or invalid entry falls back to fb.
|
|
78
|
-
const appearanceScript = `(function(){try{var sk=${toScriptSafe(storageKey)},fb=${toScriptSafe(
|
|
83
|
+
const appearanceScript = `(function(){try{var sk=${toScriptSafe(storageKey)},fb=${toScriptSafe(safeAppearance)},s=localStorage.getItem(sk),appearance=(${APPEARANCE_VALID_CHECK})?s:fb,colorScheme=appearance;"automatic"===appearance&&(colorScheme=window.matchMedia(${toScriptSafe(MEDIA)}).matches?"dark":"light"),document.documentElement.classList.remove(${APPEARANCE_REMOVE_ARGS}),document.documentElement.classList.add(colorScheme),document.documentElement.style.colorScheme=colorScheme,document.documentElement.dataset.appearance=appearance}catch(e){}})()`;
|
|
79
84
|
const nonceProps = nonce === undefined ? {} : { nonce };
|
|
80
85
|
|
|
81
86
|
return <script dangerouslySetInnerHTML={{ __html: appearanceScript }} suppressHydrationWarning {...nonceProps} />;
|
package/src/appearance.ts
CHANGED
|
@@ -50,7 +50,7 @@ export type ColorScheme = Exclude<Appearance, "automatic">;
|
|
|
50
50
|
*
|
|
51
51
|
* @since 0.5.0-canary.2
|
|
52
52
|
*/
|
|
53
|
-
export const appearances:
|
|
53
|
+
export const appearances: ReadonlyArray<Appearance> = appearanceSchema.options;
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
56
|
* Shape of the context value provided by AppearanceProvider.
|
package/src/dom.ts
CHANGED
|
@@ -12,6 +12,10 @@ import type { ColorScheme } from "#/appearance";
|
|
|
12
12
|
* @since 0.3.16-canary.0
|
|
13
13
|
*/
|
|
14
14
|
export function applyColorScheme(colorScheme: ColorScheme): void {
|
|
15
|
+
if (typeof globalThis.window === "undefined") {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
15
19
|
const root = window.document.documentElement;
|
|
16
20
|
|
|
17
21
|
root.classList.remove("light", "dark", "automatic");
|