@codefast/theme 0.5.0-canary.0 → 0.5.0-canary.2
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 +39 -0
- package/README.md +116 -183
- package/dist/appearance-context.d.mts +18 -0
- package/dist/appearance-context.mjs +16 -0
- package/dist/appearance-provider.d.mts +74 -0
- package/dist/appearance-provider.mjs +180 -0
- package/dist/appearance-script.d.mts +59 -0
- package/dist/appearance-script.mjs +46 -0
- package/dist/appearance.d.mts +60 -0
- package/dist/appearance.mjs +25 -0
- package/dist/color-scheme.d.mts +27 -0
- package/dist/color-scheme.mjs +32 -0
- package/dist/constants.d.mts +15 -14
- package/dist/constants.mjs +14 -13
- package/dist/{utils/dom.d.mts → dom.d.mts} +6 -8
- package/dist/{utils/dom.mjs → dom.mjs} +7 -9
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +7 -7
- package/dist/use-appearance.d.mts +34 -0
- package/dist/use-appearance.mjs +38 -0
- package/package.json +39 -59
- package/src/appearance-context.ts +22 -0
- package/src/appearance-provider.tsx +363 -0
- package/src/appearance-script.tsx +86 -0
- package/src/appearance.ts +66 -0
- package/src/color-scheme.ts +42 -0
- package/src/constants.ts +14 -13
- package/src/{utils/dom.ts → dom.ts} +8 -10
- package/src/index.ts +19 -16
- package/src/use-appearance.ts +46 -0
- package/dist/adapters/tanstack-start/server.d.mts +0 -88
- package/dist/adapters/tanstack-start/server.mjs +0 -93
- package/dist/adapters/tanstack-start/vite.d.mts +0 -12
- package/dist/adapters/tanstack-start/vite.mjs +0 -21
- package/dist/core/context.d.mts +0 -18
- package/dist/core/context.mjs +0 -16
- package/dist/core/provider.d.mts +0 -102
- package/dist/core/provider.mjs +0 -183
- package/dist/core/use-theme.d.mts +0 -34
- package/dist/core/use-theme.mjs +0 -38
- package/dist/script/theme-script.d.mts +0 -66
- package/dist/script/theme-script.mjs +0 -43
- package/dist/start.d.mts +0 -2
- package/dist/start.mjs +0 -2
- package/dist/types.d.mts +0 -60
- package/dist/types.mjs +0 -25
- package/dist/utils/system.d.mts +0 -34
- package/dist/utils/system.mjs +0 -40
- package/src/adapters/tanstack-start/server.ts +0 -146
- package/src/adapters/tanstack-start/vite.ts +0 -50
- package/src/core/context.tsx +0 -22
- package/src/core/provider.tsx +0 -364
- package/src/core/use-theme.ts +0 -46
- package/src/script/theme-script.tsx +0 -90
- package/src/start.ts +0 -54
- package/src/types.ts +0 -61
- package/src/utils/system.ts +0 -56
package/dist/core/provider.d.mts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { ColorScheme, ResolvedColorScheme } from "../types.mjs";
|
|
2
|
-
import { JSX, ReactNode } from "react";
|
|
3
|
-
|
|
4
|
-
//#region src/core/provider.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* @since 0.3.16-canary.3
|
|
7
|
-
*/
|
|
8
|
-
type AppearanceProviderProps = {
|
|
9
|
-
readonly children: ReactNode;
|
|
10
|
-
/**
|
|
11
|
-
* When true, temporarily suppresses CSS transitions during color scheme changes.
|
|
12
|
-
* Prevents jarring color animations when switching appearances.
|
|
13
|
-
* Default: false
|
|
14
|
-
*/
|
|
15
|
-
readonly disableTransition?: boolean;
|
|
16
|
-
/**
|
|
17
|
-
* CSP nonce for inline styles when `disableTransition` is enabled.
|
|
18
|
-
*/
|
|
19
|
-
readonly nonce?: string;
|
|
20
|
-
/**
|
|
21
|
-
* Async function to persist color scheme changes to server/storage.
|
|
22
|
-
*
|
|
23
|
-
* For TanStack Start: use `persistColorSchemeCookie` from `@codefast/theme/start`
|
|
24
|
-
* For Next.js: implement a server action
|
|
25
|
-
*/
|
|
26
|
-
readonly persistColorScheme?: (value: ColorScheme) => Promise<void>;
|
|
27
|
-
/**
|
|
28
|
-
* Callback invoked when `persistColorScheme` rejects.
|
|
29
|
-
*/
|
|
30
|
-
readonly onPersistError?: (error: unknown, attemptedColorScheme: ColorScheme) => void;
|
|
31
|
-
/**
|
|
32
|
-
* Initial color scheme preference from the server (cookie, loader, etc.).
|
|
33
|
-
*
|
|
34
|
-
* After mount, `AppearanceProvider` re-syncs whenever this prop changes — for example, when
|
|
35
|
-
* the router re-runs the root loader after navigation. The server value is authoritative:
|
|
36
|
-
* a new prop value will override any local optimistic state.
|
|
37
|
-
*/
|
|
38
|
-
readonly colorScheme: ColorScheme;
|
|
39
|
-
/**
|
|
40
|
-
* OS light/dark guess from the SSR request (e.g. `Sec-CH-Prefers-Color-Scheme`).
|
|
41
|
-
*
|
|
42
|
-
* When the stored preference is `automatic`, {@link useSyncExternalStore} uses this for
|
|
43
|
-
* `getServerSnapshot` so the first client snapshot matches `matchMedia` and avoids a
|
|
44
|
-
* dark → light flip after hydration.
|
|
45
|
-
*/
|
|
46
|
-
readonly ssrColorScheme?: ResolvedColorScheme;
|
|
47
|
-
/**
|
|
48
|
-
* After mount, re-read the canonical color scheme from the server (e.g. httpOnly cookie).
|
|
49
|
-
*
|
|
50
|
-
* Fixes **stale HTML / disk cache** on "Duplicate tab" or back-forward cache where the
|
|
51
|
-
* document was saved with an old `AppearanceScript` / loader value while the cookie was already updated
|
|
52
|
-
* in another tab. Use a stable reference (e.g. `getColorSchemeServerFn` from `@codefast/theme/start`).
|
|
53
|
-
*/
|
|
54
|
-
readonly syncFromServer?: () => Promise<ColorScheme>;
|
|
55
|
-
};
|
|
56
|
-
/**
|
|
57
|
-
* Provider component for color scheme state management.
|
|
58
|
-
*
|
|
59
|
-
* **React 19 Features Used:**
|
|
60
|
-
* - `useOptimistic` - Immediate UI feedback while persisting color scheme
|
|
61
|
-
* - `useSyncExternalStore` - SSR-safe subscription to OS color scheme preference
|
|
62
|
-
* - `useEffectEvent` - Stable callback for cross-tab sync without effect re-runs
|
|
63
|
-
*
|
|
64
|
-
* @param props - Component props
|
|
65
|
-
* @param props.children - React tree to provide context for
|
|
66
|
-
* @param props.colorScheme - Initial preference from the server (cookie, loader, etc.)
|
|
67
|
-
* @param props.ssrColorScheme - Best-effort `light` or `dark` from the SSR request
|
|
68
|
-
* (e.g. `Sec-CH-Prefers-Color-Scheme`). When preference is `automatic`, this value is used as
|
|
69
|
-
* `useSyncExternalStore`'s server snapshot so the first client snapshot matches `matchMedia`
|
|
70
|
-
* and avoids a post-hydration flip. Omit if unavailable; falls back to {@link DEFAULT_RESOLVED_COLOR_SCHEME}.
|
|
71
|
-
* @param props.syncFromServer - Optional one-shot RPC after mount to align with the cookie when SSR/HTML was stale.
|
|
72
|
-
* @param props.persistColorScheme - Optional async handler to persist preference (cookie, server action, …)
|
|
73
|
-
* @param props.onPersistError - Optional callback for persistence failures
|
|
74
|
-
* @param props.disableTransition - Temporarily suppress CSS transitions on color scheme change
|
|
75
|
-
* @param props.nonce - CSP nonce for inline transition-blocking styles when `disableTransition` is set
|
|
76
|
-
* @returns Provider element wrapping `children`
|
|
77
|
-
*
|
|
78
|
-
* @example
|
|
79
|
-
* ```tsx
|
|
80
|
-
* // In your root layout
|
|
81
|
-
* <AppearanceProvider
|
|
82
|
-
* colorScheme={colorScheme}
|
|
83
|
-
* persistColorScheme={(value) => setColorSchemeServerFn({ data: value })}
|
|
84
|
-
* >
|
|
85
|
-
* {children}
|
|
86
|
-
* </AppearanceProvider>
|
|
87
|
-
* ```
|
|
88
|
-
*
|
|
89
|
-
* @since 0.3.16-canary.0
|
|
90
|
-
*/
|
|
91
|
-
declare function AppearanceProvider({
|
|
92
|
-
children,
|
|
93
|
-
disableTransition,
|
|
94
|
-
nonce,
|
|
95
|
-
onPersistError,
|
|
96
|
-
persistColorScheme,
|
|
97
|
-
ssrColorScheme,
|
|
98
|
-
syncFromServer,
|
|
99
|
-
colorScheme: initialColorScheme
|
|
100
|
-
}: AppearanceProviderProps): JSX.Element;
|
|
101
|
-
//#endregion
|
|
102
|
-
export { AppearanceProvider, AppearanceProviderProps };
|
package/dist/core/provider.mjs
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_COLOR_SCHEME, MEDIA, SYNC_CHANNEL } from "../constants.mjs";
|
|
2
|
-
import { colorSchemeSchema } from "../types.mjs";
|
|
3
|
-
import { ColorSchemeContext } from "./context.mjs";
|
|
4
|
-
import { applyColorScheme, suppressTransitions } from "../utils/dom.mjs";
|
|
5
|
-
import { getSystemColorScheme } from "../utils/system.mjs";
|
|
6
|
-
import { startTransition, useCallback, useEffect, useEffectEvent, useMemo, useOptimistic, useRef, useState, useSyncExternalStore } from "react";
|
|
7
|
-
import { jsx } from "react/jsx-runtime";
|
|
8
|
-
//#region src/core/provider.tsx
|
|
9
|
-
/**
|
|
10
|
-
* Subscribe to OS color scheme preference changes.
|
|
11
|
-
*
|
|
12
|
-
* @param callback - Function to call when system color scheme changes
|
|
13
|
-
* @returns Cleanup function to remove the listener
|
|
14
|
-
*/
|
|
15
|
-
function subscribeToSystemColorScheme(callback) {
|
|
16
|
-
const mediaQuery = window.matchMedia(MEDIA);
|
|
17
|
-
mediaQuery.addEventListener("change", callback);
|
|
18
|
-
return () => {
|
|
19
|
-
mediaQuery.removeEventListener("change", callback);
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Safely create a color scheme BroadcastChannel when supported by the environment.
|
|
24
|
-
*/
|
|
25
|
-
function createColorSchemeChannel() {
|
|
26
|
-
if (typeof BroadcastChannel === "undefined") return null;
|
|
27
|
-
try {
|
|
28
|
-
return new BroadcastChannel(SYNC_CHANNEL);
|
|
29
|
-
} catch {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Provider component for color scheme state management.
|
|
35
|
-
*
|
|
36
|
-
* **React 19 Features Used:**
|
|
37
|
-
* - `useOptimistic` - Immediate UI feedback while persisting color scheme
|
|
38
|
-
* - `useSyncExternalStore` - SSR-safe subscription to OS color scheme preference
|
|
39
|
-
* - `useEffectEvent` - Stable callback for cross-tab sync without effect re-runs
|
|
40
|
-
*
|
|
41
|
-
* @param props - Component props
|
|
42
|
-
* @param props.children - React tree to provide context for
|
|
43
|
-
* @param props.colorScheme - Initial preference from the server (cookie, loader, etc.)
|
|
44
|
-
* @param props.ssrColorScheme - Best-effort `light` or `dark` from the SSR request
|
|
45
|
-
* (e.g. `Sec-CH-Prefers-Color-Scheme`). When preference is `automatic`, this value is used as
|
|
46
|
-
* `useSyncExternalStore`'s server snapshot so the first client snapshot matches `matchMedia`
|
|
47
|
-
* and avoids a post-hydration flip. Omit if unavailable; falls back to {@link DEFAULT_RESOLVED_COLOR_SCHEME}.
|
|
48
|
-
* @param props.syncFromServer - Optional one-shot RPC after mount to align with the cookie when SSR/HTML was stale.
|
|
49
|
-
* @param props.persistColorScheme - Optional async handler to persist preference (cookie, server action, …)
|
|
50
|
-
* @param props.onPersistError - Optional callback for persistence failures
|
|
51
|
-
* @param props.disableTransition - Temporarily suppress CSS transitions on color scheme change
|
|
52
|
-
* @param props.nonce - CSP nonce for inline transition-blocking styles when `disableTransition` is set
|
|
53
|
-
* @returns Provider element wrapping `children`
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```tsx
|
|
57
|
-
* // In your root layout
|
|
58
|
-
* <AppearanceProvider
|
|
59
|
-
* colorScheme={colorScheme}
|
|
60
|
-
* persistColorScheme={(value) => setColorSchemeServerFn({ data: value })}
|
|
61
|
-
* >
|
|
62
|
-
* {children}
|
|
63
|
-
* </AppearanceProvider>
|
|
64
|
-
* ```
|
|
65
|
-
*
|
|
66
|
-
* @since 0.3.16-canary.0
|
|
67
|
-
*/
|
|
68
|
-
function AppearanceProvider({ children, disableTransition = false, nonce, onPersistError, persistColorScheme, ssrColorScheme, syncFromServer, colorScheme: initialColorScheme }) {
|
|
69
|
-
const safeInitialColorScheme = colorSchemeSchema.safeParse(initialColorScheme).success ? initialColorScheme : DEFAULT_COLOR_SCHEME;
|
|
70
|
-
const [colorScheme, setColorSchemeState] = useState(safeInitialColorScheme);
|
|
71
|
-
const [optimisticColorScheme, setOptimisticColorScheme] = useOptimistic(colorScheme);
|
|
72
|
-
const isPending = optimisticColorScheme !== colorScheme;
|
|
73
|
-
const committedColorSchemeRef = useRef(colorScheme);
|
|
74
|
-
committedColorSchemeRef.current = colorScheme;
|
|
75
|
-
const channelRef = useRef(null);
|
|
76
|
-
const enableTransitionsRef = useRef(null);
|
|
77
|
-
const intentRef = useRef(null);
|
|
78
|
-
useEffect(() => {
|
|
79
|
-
startTransition(() => {
|
|
80
|
-
setColorSchemeState((prev) => prev === safeInitialColorScheme ? prev : safeInitialColorScheme);
|
|
81
|
-
});
|
|
82
|
-
}, [safeInitialColorScheme]);
|
|
83
|
-
const syncFromServerRef = useRef(syncFromServer);
|
|
84
|
-
syncFromServerRef.current = syncFromServer;
|
|
85
|
-
useEffect(() => {
|
|
86
|
-
const sync = syncFromServerRef.current;
|
|
87
|
-
if (!sync) return;
|
|
88
|
-
let cancelled = false;
|
|
89
|
-
(async () => {
|
|
90
|
-
try {
|
|
91
|
-
const serverColorScheme = await sync();
|
|
92
|
-
if (cancelled) return;
|
|
93
|
-
startTransition(() => {
|
|
94
|
-
setColorSchemeState((prev) => prev === serverColorScheme ? prev : serverColorScheme);
|
|
95
|
-
});
|
|
96
|
-
} catch {}
|
|
97
|
-
})();
|
|
98
|
-
return () => {
|
|
99
|
-
cancelled = true;
|
|
100
|
-
};
|
|
101
|
-
}, []);
|
|
102
|
-
const systemColorScheme = useSyncExternalStore(subscribeToSystemColorScheme, getSystemColorScheme, useCallback(() => {
|
|
103
|
-
return ssrColorScheme ?? "dark";
|
|
104
|
-
}, [ssrColorScheme]));
|
|
105
|
-
const resolvedColorScheme = useMemo(() => optimisticColorScheme === "automatic" ? systemColorScheme : optimisticColorScheme, [optimisticColorScheme, systemColorScheme]);
|
|
106
|
-
useEffect(() => {
|
|
107
|
-
applyColorScheme(resolvedColorScheme);
|
|
108
|
-
const enable = enableTransitionsRef.current;
|
|
109
|
-
if (enable) {
|
|
110
|
-
enableTransitionsRef.current = null;
|
|
111
|
-
enable();
|
|
112
|
-
}
|
|
113
|
-
}, [resolvedColorScheme]);
|
|
114
|
-
const handleCrossTabMessage = useEffectEvent((event) => {
|
|
115
|
-
const result = colorSchemeSchema.safeParse(event.data);
|
|
116
|
-
if (!result.success || result.data === colorScheme) return;
|
|
117
|
-
startTransition(() => {
|
|
118
|
-
setColorSchemeState(result.data);
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
useEffect(() => {
|
|
122
|
-
const channel = createColorSchemeChannel();
|
|
123
|
-
if (!channel) return;
|
|
124
|
-
channelRef.current = channel;
|
|
125
|
-
channel.addEventListener("message", handleCrossTabMessage);
|
|
126
|
-
return () => {
|
|
127
|
-
channelRef.current = null;
|
|
128
|
-
channel.removeEventListener("message", handleCrossTabMessage);
|
|
129
|
-
channel.close();
|
|
130
|
-
};
|
|
131
|
-
}, []);
|
|
132
|
-
const setColorScheme = useCallback(async (value) => {
|
|
133
|
-
await Promise.resolve();
|
|
134
|
-
if (value === committedColorSchemeRef.current) return;
|
|
135
|
-
if (disableTransition) enableTransitionsRef.current = suppressTransitions(nonce);
|
|
136
|
-
startTransition(async () => {
|
|
137
|
-
intentRef.current = value;
|
|
138
|
-
setOptimisticColorScheme(value);
|
|
139
|
-
try {
|
|
140
|
-
if (persistColorScheme) await persistColorScheme(value);
|
|
141
|
-
if (intentRef.current === value) {
|
|
142
|
-
setColorSchemeState(value);
|
|
143
|
-
channelRef.current?.postMessage(value);
|
|
144
|
-
}
|
|
145
|
-
} catch (error) {
|
|
146
|
-
if (onPersistError) try {
|
|
147
|
-
onPersistError(error, value);
|
|
148
|
-
} catch (callbackError) {
|
|
149
|
-
console.error("Failed to handle color scheme persist error:", callbackError);
|
|
150
|
-
}
|
|
151
|
-
else console.error("Failed to set color scheme:", error);
|
|
152
|
-
} finally {
|
|
153
|
-
const enable = enableTransitionsRef.current;
|
|
154
|
-
if (enable) {
|
|
155
|
-
enableTransitionsRef.current = null;
|
|
156
|
-
enable();
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
}, [
|
|
161
|
-
disableTransition,
|
|
162
|
-
nonce,
|
|
163
|
-
setOptimisticColorScheme,
|
|
164
|
-
persistColorScheme,
|
|
165
|
-
onPersistError
|
|
166
|
-
]);
|
|
167
|
-
return /* @__PURE__ */ jsx(ColorSchemeContext, {
|
|
168
|
-
value: useMemo(() => ({
|
|
169
|
-
isPending,
|
|
170
|
-
resolvedColorScheme,
|
|
171
|
-
setColorScheme,
|
|
172
|
-
colorScheme: optimisticColorScheme
|
|
173
|
-
}), [
|
|
174
|
-
optimisticColorScheme,
|
|
175
|
-
resolvedColorScheme,
|
|
176
|
-
setColorScheme,
|
|
177
|
-
isPending
|
|
178
|
-
]),
|
|
179
|
-
children
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
//#endregion
|
|
183
|
-
export { AppearanceProvider };
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { ColorSchemeContextType } from "../types.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/core/use-theme.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Hook to access color scheme context.
|
|
6
|
-
*
|
|
7
|
-
* Uses React 19's `use()` API for context consumption, enabling:
|
|
8
|
-
* - Conditional context reading
|
|
9
|
-
* - Better Suspense boundary integration
|
|
10
|
-
*
|
|
11
|
-
* @returns Color scheme context with `colorScheme`, `resolvedColorScheme`, `setPreferredColorScheme`, and `isPending`
|
|
12
|
-
* @throws Error if called outside of AppearanceProvider
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```tsx
|
|
16
|
-
* function AppearanceToggle() {
|
|
17
|
-
* const { colorScheme, setPreferredColorScheme, isPending } = useColorScheme();
|
|
18
|
-
*
|
|
19
|
-
* return (
|
|
20
|
-
* <button
|
|
21
|
-
* onClick={() => setPreferredColorScheme(colorScheme === 'dark' ? 'light' : 'dark')}
|
|
22
|
-
* disabled={isPending}
|
|
23
|
-
* >
|
|
24
|
-
* {colorScheme}
|
|
25
|
-
* </button>
|
|
26
|
-
* );
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @since 0.3.16-canary.0
|
|
31
|
-
*/
|
|
32
|
-
declare function useColorScheme(): ColorSchemeContextType;
|
|
33
|
-
//#endregion
|
|
34
|
-
export { useColorScheme };
|
package/dist/core/use-theme.mjs
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { ColorSchemeContext } from "./context.mjs";
|
|
2
|
-
import { use } from "react";
|
|
3
|
-
//#region src/core/use-theme.ts
|
|
4
|
-
/**
|
|
5
|
-
* Hook to access color scheme context.
|
|
6
|
-
*
|
|
7
|
-
* Uses React 19's `use()` API for context consumption, enabling:
|
|
8
|
-
* - Conditional context reading
|
|
9
|
-
* - Better Suspense boundary integration
|
|
10
|
-
*
|
|
11
|
-
* @returns Color scheme context with `colorScheme`, `resolvedColorScheme`, `setPreferredColorScheme`, and `isPending`
|
|
12
|
-
* @throws Error if called outside of AppearanceProvider
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```tsx
|
|
16
|
-
* function AppearanceToggle() {
|
|
17
|
-
* const { colorScheme, setPreferredColorScheme, isPending } = useColorScheme();
|
|
18
|
-
*
|
|
19
|
-
* return (
|
|
20
|
-
* <button
|
|
21
|
-
* onClick={() => setPreferredColorScheme(colorScheme === 'dark' ? 'light' : 'dark')}
|
|
22
|
-
* disabled={isPending}
|
|
23
|
-
* >
|
|
24
|
-
* {colorScheme}
|
|
25
|
-
* </button>
|
|
26
|
-
* );
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @since 0.3.16-canary.0
|
|
31
|
-
*/
|
|
32
|
-
function useColorScheme() {
|
|
33
|
-
const value = use(ColorSchemeContext);
|
|
34
|
-
if (!value) throw new Error("useColorScheme must be used within an AppearanceProvider");
|
|
35
|
-
return value;
|
|
36
|
-
}
|
|
37
|
-
//#endregion
|
|
38
|
-
export { useColorScheme };
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { ColorScheme } from "../types.mjs";
|
|
2
|
-
import { JSX } from "react";
|
|
3
|
-
|
|
4
|
-
//#region src/script/theme-script.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* @since 0.3.16-canary.3
|
|
7
|
-
*/
|
|
8
|
-
type AppearanceScriptProps = {
|
|
9
|
-
/**
|
|
10
|
-
* Initial color scheme from server (e.g., from cookie via loader).
|
|
11
|
-
*
|
|
12
|
-
* When `storageKey` is provided, this acts as a fallback if the storage entry is absent
|
|
13
|
-
* or contains an unrecognised value.
|
|
14
|
-
*/
|
|
15
|
-
readonly colorScheme: ColorScheme;
|
|
16
|
-
/**
|
|
17
|
-
* CSP nonce applied to the inline script element.
|
|
18
|
-
*/
|
|
19
|
-
readonly nonce?: string;
|
|
20
|
-
/**
|
|
21
|
-
* When provided, the inline script reads `localStorage.getItem(storageKey)` before
|
|
22
|
-
* first paint and uses that value if it is a recognised color scheme (`"light"`, `"dark"`,
|
|
23
|
-
* or `"automatic"`). Falls back to `colorScheme` when the key is absent or invalid.
|
|
24
|
-
*
|
|
25
|
-
* Use this for client-only React apps that persist the color scheme in `localStorage` instead
|
|
26
|
-
* of an HTTP cookie to prevent FOUC.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```tsx
|
|
30
|
-
* <AppearanceScript colorScheme="automatic" storageKey="my-app-color-scheme" />
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
readonly storageKey?: string;
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Inline script that prevents Flash of Unstyled Content (FOUC).
|
|
37
|
-
*
|
|
38
|
-
* **Why this is needed:**
|
|
39
|
-
* React hydration occurs after the browser has already painted the page.
|
|
40
|
-
* Without this script, users would briefly see the wrong appearance before
|
|
41
|
-
* React takes over and applies the correct one.
|
|
42
|
-
*
|
|
43
|
-
* **How it works:**
|
|
44
|
-
* This script runs synchronously in the `<head>` before first paint:
|
|
45
|
-
* 1. Resolves 'automatic' to 'light' or 'dark' using `matchMedia()`
|
|
46
|
-
* 2. Removes prior `light` / `dark` / `automatic` classes on `<html>` (SSR may
|
|
47
|
-
* have applied the wrong resolved class for `automatic`, e.g. default `dark`)
|
|
48
|
-
* 3. Adds the resolved color scheme class and sets `color-scheme` for native controls
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```tsx
|
|
52
|
-
* // In __root.tsx (TanStack Start)
|
|
53
|
-
* <head>
|
|
54
|
-
* <AppearanceScript colorScheme={colorScheme} />
|
|
55
|
-
* </head>
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
|
-
* @since 0.3.16-canary.0
|
|
59
|
-
*/
|
|
60
|
-
declare function AppearanceScript({
|
|
61
|
-
nonce,
|
|
62
|
-
storageKey,
|
|
63
|
-
colorScheme
|
|
64
|
-
}: AppearanceScriptProps): JSX.Element;
|
|
65
|
-
//#endregion
|
|
66
|
-
export { AppearanceScript, AppearanceScriptProps };
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { MEDIA } from "../constants.mjs";
|
|
2
|
-
import { colorSchemes } from "../types.mjs";
|
|
3
|
-
import { jsx } from "react/jsx-runtime";
|
|
4
|
-
//#region src/script/theme-script.tsx
|
|
5
|
-
function toScriptSafe(value) {
|
|
6
|
-
return JSON.stringify(value).replaceAll("<", "\\u003C").replaceAll(">", "\\u003E").replaceAll("/", "\\u002F");
|
|
7
|
-
}
|
|
8
|
-
const COLOR_SCHEME_REMOVE_ARGS = colorSchemes.map((s) => toScriptSafe(s)).join(",");
|
|
9
|
-
const COLOR_SCHEME_VALID_CHECK = colorSchemes.map((s) => `s===${toScriptSafe(s)}`).join("||");
|
|
10
|
-
/**
|
|
11
|
-
* Inline script that prevents Flash of Unstyled Content (FOUC).
|
|
12
|
-
*
|
|
13
|
-
* **Why this is needed:**
|
|
14
|
-
* React hydration occurs after the browser has already painted the page.
|
|
15
|
-
* Without this script, users would briefly see the wrong appearance before
|
|
16
|
-
* React takes over and applies the correct one.
|
|
17
|
-
*
|
|
18
|
-
* **How it works:**
|
|
19
|
-
* This script runs synchronously in the `<head>` before first paint:
|
|
20
|
-
* 1. Resolves 'automatic' to 'light' or 'dark' using `matchMedia()`
|
|
21
|
-
* 2. Removes prior `light` / `dark` / `automatic` classes on `<html>` (SSR may
|
|
22
|
-
* have applied the wrong resolved class for `automatic`, e.g. default `dark`)
|
|
23
|
-
* 3. Adds the resolved color scheme class and sets `color-scheme` for native controls
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```tsx
|
|
27
|
-
* // In __root.tsx (TanStack Start)
|
|
28
|
-
* <head>
|
|
29
|
-
* <AppearanceScript colorScheme={colorScheme} />
|
|
30
|
-
* </head>
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @since 0.3.16-canary.0
|
|
34
|
-
*/
|
|
35
|
-
function AppearanceScript({ nonce, storageKey, colorScheme }) {
|
|
36
|
-
return /* @__PURE__ */ jsx("script", {
|
|
37
|
-
dangerouslySetInnerHTML: { __html: `(function(){try{var sk=${toScriptSafe(storageKey ?? null)},fbt=${toScriptSafe(colorScheme)},s=sk&&localStorage.getItem(sk),theme=(${COLOR_SCHEME_VALID_CHECK})?s:fbt,resolvedTheme=theme;"automatic"===theme&&(resolvedTheme=window.matchMedia(${toScriptSafe(MEDIA)}).matches?"dark":"light"),document.documentElement.classList.remove(${COLOR_SCHEME_REMOVE_ARGS}),document.documentElement.classList.add(resolvedTheme),document.documentElement.style.colorScheme=resolvedTheme}catch(e){}})()` },
|
|
38
|
-
suppressHydrationWarning: true,
|
|
39
|
-
...nonce === void 0 ? {} : { nonce }
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
//#endregion
|
|
43
|
-
export { AppearanceScript };
|
package/dist/start.d.mts
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { RootColorSchemeLoaderData, getColorSchemeServerFn, getRootColorSchemeServerFn, getSsrColorSchemeServerFn, persistColorSchemeCookie, setColorSchemeServerFn } from "./adapters/tanstack-start/server.mjs";
|
|
2
|
-
export { type RootColorSchemeLoaderData, getColorSchemeServerFn, getRootColorSchemeServerFn, getSsrColorSchemeServerFn, persistColorSchemeCookie, setColorSchemeServerFn };
|
package/dist/start.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { getColorSchemeServerFn, getRootColorSchemeServerFn, getSsrColorSchemeServerFn, persistColorSchemeCookie, setColorSchemeServerFn } from "./adapters/tanstack-start/server.mjs";
|
|
2
|
-
export { getColorSchemeServerFn, getRootColorSchemeServerFn, getSsrColorSchemeServerFn, persistColorSchemeCookie, setColorSchemeServerFn };
|
package/dist/types.d.mts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
|
|
3
|
-
//#region src/types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Zod schema for color scheme validation.
|
|
6
|
-
*
|
|
7
|
-
* Acts as the single source of truth for all color-scheme-related types.
|
|
8
|
-
* Valid values: 'light', 'dark', 'automatic'.
|
|
9
|
-
*
|
|
10
|
-
* @since 0.3.16-canary.0
|
|
11
|
-
*/
|
|
12
|
-
declare const colorSchemeSchema: z.ZodEnum<{
|
|
13
|
-
light: "light";
|
|
14
|
-
dark: "dark";
|
|
15
|
-
automatic: "automatic";
|
|
16
|
-
}>;
|
|
17
|
-
/**
|
|
18
|
-
* User's color scheme preference.
|
|
19
|
-
*
|
|
20
|
-
* - `'light'` - Force light appearance
|
|
21
|
-
* - `'dark'` - Force dark appearance
|
|
22
|
-
* - `'automatic'` - Follow OS preference
|
|
23
|
-
*
|
|
24
|
-
* @since 0.3.16-canary.0
|
|
25
|
-
*/
|
|
26
|
-
type ColorScheme = z.infer<typeof colorSchemeSchema>;
|
|
27
|
-
/**
|
|
28
|
-
* Actual color scheme applied to the UI after resolving 'automatic'.
|
|
29
|
-
*
|
|
30
|
-
* When color scheme is 'automatic', this reflects the OS preference (light/dark).
|
|
31
|
-
*
|
|
32
|
-
* @since 0.3.16-canary.0
|
|
33
|
-
*/
|
|
34
|
-
type ResolvedColorScheme = Exclude<ColorScheme, "automatic">;
|
|
35
|
-
/**
|
|
36
|
-
* Array of all available color scheme options: `['light', 'dark', 'automatic']`.
|
|
37
|
-
*
|
|
38
|
-
* Useful for rendering appearance selectors in UI components.
|
|
39
|
-
*
|
|
40
|
-
* @since 0.3.16-canary.0
|
|
41
|
-
*/
|
|
42
|
-
declare const colorSchemes: ("light" | "dark" | "automatic")[];
|
|
43
|
-
/**
|
|
44
|
-
* Shape of the color scheme context value provided by AppearanceProvider.
|
|
45
|
-
*
|
|
46
|
-
* - `colorScheme` - Current color scheme preference
|
|
47
|
-
* - `resolvedColorScheme` - Actual color scheme applied (automatic resolved to light/dark)
|
|
48
|
-
* - `setPreferredColorScheme` - Async function to update color scheme (triggers optimistic update)
|
|
49
|
-
* - `isPending` - True while color scheme change is being persisted
|
|
50
|
-
*
|
|
51
|
-
* @since 0.3.16-canary.0
|
|
52
|
-
*/
|
|
53
|
-
type ColorSchemeContextType = {
|
|
54
|
-
readonly isPending: boolean;
|
|
55
|
-
readonly resolvedColorScheme: ResolvedColorScheme;
|
|
56
|
-
readonly setColorScheme: (value: ColorScheme) => Promise<void>;
|
|
57
|
-
readonly colorScheme: ColorScheme;
|
|
58
|
-
};
|
|
59
|
-
//#endregion
|
|
60
|
-
export { ColorScheme, ColorSchemeContextType, ResolvedColorScheme, colorSchemeSchema, colorSchemes };
|
package/dist/types.mjs
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
//#region src/types.ts
|
|
3
|
-
/**
|
|
4
|
-
* Zod schema for color scheme validation.
|
|
5
|
-
*
|
|
6
|
-
* Acts as the single source of truth for all color-scheme-related types.
|
|
7
|
-
* Valid values: 'light', 'dark', 'automatic'.
|
|
8
|
-
*
|
|
9
|
-
* @since 0.3.16-canary.0
|
|
10
|
-
*/
|
|
11
|
-
const colorSchemeSchema = z.enum([
|
|
12
|
-
"light",
|
|
13
|
-
"dark",
|
|
14
|
-
"automatic"
|
|
15
|
-
]);
|
|
16
|
-
/**
|
|
17
|
-
* Array of all available color scheme options: `['light', 'dark', 'automatic']`.
|
|
18
|
-
*
|
|
19
|
-
* Useful for rendering appearance selectors in UI components.
|
|
20
|
-
*
|
|
21
|
-
* @since 0.3.16-canary.0
|
|
22
|
-
*/
|
|
23
|
-
const colorSchemes = colorSchemeSchema.options;
|
|
24
|
-
//#endregion
|
|
25
|
-
export { colorSchemeSchema, colorSchemes };
|
package/dist/utils/system.d.mts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { ColorScheme, ResolvedColorScheme } from "../types.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/utils/system.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Detect the user's OS color scheme preference.
|
|
6
|
-
*
|
|
7
|
-
* Uses `matchMedia()` to query the `prefers-color-scheme` media feature.
|
|
8
|
-
* Returns {@link DEFAULT_RESOLVED_COLOR_SCHEME} during SSR since `window` is unavailable.
|
|
9
|
-
*
|
|
10
|
-
* @returns 'light' or 'dark' based on OS preference
|
|
11
|
-
*
|
|
12
|
-
* @since 0.3.16-canary.0
|
|
13
|
-
*/
|
|
14
|
-
declare function getSystemColorScheme(): ResolvedColorScheme;
|
|
15
|
-
/**
|
|
16
|
-
* Resolve color scheme preference to actual light/dark value.
|
|
17
|
-
*
|
|
18
|
-
* - 'light' → 'light'
|
|
19
|
-
* - 'dark' → 'dark'
|
|
20
|
-
* - 'automatic' → on the client, result of {@link getSystemColorScheme}; on the server,
|
|
21
|
-
* `ssrColorScheme` when provided, otherwise {@link DEFAULT_RESOLVED_COLOR_SCHEME}
|
|
22
|
-
*
|
|
23
|
-
* @param colorScheme - User's color scheme preference (`light`, `dark`, or `automatic`)
|
|
24
|
-
* @param ssrColorScheme - When `colorScheme` is `automatic` and this runs during SSR (no `window`),
|
|
25
|
-
* uses this as the resolved appearance—typically from Client Hints
|
|
26
|
-
* (`Sec-CH-Prefers-Color-Scheme`) so `<html class>` matches the real OS preference.
|
|
27
|
-
* Ignored for non-`automatic` schemes and on the client (where {@link getSystemColorScheme} wins).
|
|
28
|
-
* @returns The resolved color scheme to apply (`light` or `dark`)
|
|
29
|
-
*
|
|
30
|
-
* @since 0.3.16-canary.0
|
|
31
|
-
*/
|
|
32
|
-
declare function resolveColorScheme(colorScheme: ColorScheme, ssrColorScheme?: ResolvedColorScheme): ResolvedColorScheme;
|
|
33
|
-
//#endregion
|
|
34
|
-
export { getSystemColorScheme, resolveColorScheme };
|
package/dist/utils/system.mjs
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_RESOLVED_COLOR_SCHEME } from "../constants.mjs";
|
|
2
|
-
//#region src/utils/system.ts
|
|
3
|
-
/**
|
|
4
|
-
* Detect the user's OS color scheme preference.
|
|
5
|
-
*
|
|
6
|
-
* Uses `matchMedia()` to query the `prefers-color-scheme` media feature.
|
|
7
|
-
* Returns {@link DEFAULT_RESOLVED_COLOR_SCHEME} during SSR since `window` is unavailable.
|
|
8
|
-
*
|
|
9
|
-
* @returns 'light' or 'dark' based on OS preference
|
|
10
|
-
*
|
|
11
|
-
* @since 0.3.16-canary.0
|
|
12
|
-
*/
|
|
13
|
-
function getSystemColorScheme() {
|
|
14
|
-
if (typeof globalThis.window === "undefined") return DEFAULT_RESOLVED_COLOR_SCHEME;
|
|
15
|
-
return globalThis.window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Resolve color scheme preference to actual light/dark value.
|
|
19
|
-
*
|
|
20
|
-
* - 'light' → 'light'
|
|
21
|
-
* - 'dark' → 'dark'
|
|
22
|
-
* - 'automatic' → on the client, result of {@link getSystemColorScheme}; on the server,
|
|
23
|
-
* `ssrColorScheme` when provided, otherwise {@link DEFAULT_RESOLVED_COLOR_SCHEME}
|
|
24
|
-
*
|
|
25
|
-
* @param colorScheme - User's color scheme preference (`light`, `dark`, or `automatic`)
|
|
26
|
-
* @param ssrColorScheme - When `colorScheme` is `automatic` and this runs during SSR (no `window`),
|
|
27
|
-
* uses this as the resolved appearance—typically from Client Hints
|
|
28
|
-
* (`Sec-CH-Prefers-Color-Scheme`) so `<html class>` matches the real OS preference.
|
|
29
|
-
* Ignored for non-`automatic` schemes and on the client (where {@link getSystemColorScheme} wins).
|
|
30
|
-
* @returns The resolved color scheme to apply (`light` or `dark`)
|
|
31
|
-
*
|
|
32
|
-
* @since 0.3.16-canary.0
|
|
33
|
-
*/
|
|
34
|
-
function resolveColorScheme(colorScheme, ssrColorScheme) {
|
|
35
|
-
if (colorScheme !== "automatic") return colorScheme;
|
|
36
|
-
if (typeof globalThis.window === "undefined") return ssrColorScheme ?? "dark";
|
|
37
|
-
return getSystemColorScheme();
|
|
38
|
-
}
|
|
39
|
-
//#endregion
|
|
40
|
-
export { getSystemColorScheme, resolveColorScheme };
|