@codefast/theme 0.3.10

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE +21 -0
  3. package/README.md +102 -0
  4. package/dist/adapters/tanstack-start/index.cjs +42 -0
  5. package/dist/adapters/tanstack-start/index.d.ts +39 -0
  6. package/dist/adapters/tanstack-start/index.js +2 -0
  7. package/dist/adapters/tanstack-start/server.cjs +70 -0
  8. package/dist/adapters/tanstack-start/server.d.ts +37 -0
  9. package/dist/adapters/tanstack-start/server.js +30 -0
  10. package/dist/constants.cjs +52 -0
  11. package/dist/constants.d.ts +31 -0
  12. package/dist/constants.js +6 -0
  13. package/dist/core/context.cjs +37 -0
  14. package/dist/core/context.d.ts +13 -0
  15. package/dist/core/context.js +3 -0
  16. package/dist/core/index.cjs +44 -0
  17. package/dist/core/index.d.ts +3 -0
  18. package/dist/core/index.js +4 -0
  19. package/dist/core/provider.cjs +123 -0
  20. package/dist/core/provider.d.ts +47 -0
  21. package/dist/core/provider.js +89 -0
  22. package/dist/core/use-theme.cjs +42 -0
  23. package/dist/core/use-theme.d.ts +28 -0
  24. package/dist/core/use-theme.js +8 -0
  25. package/dist/index.cjs +76 -0
  26. package/dist/index.d.ts +16 -0
  27. package/dist/index.js +6 -0
  28. package/dist/script/index.cjs +36 -0
  29. package/dist/script/index.d.ts +1 -0
  30. package/dist/script/index.js +2 -0
  31. package/dist/script/theme-script.cjs +44 -0
  32. package/dist/script/theme-script.d.ts +32 -0
  33. package/dist/script/theme-script.js +10 -0
  34. package/dist/types.cjs +45 -0
  35. package/dist/types.d.ts +46 -0
  36. package/dist/types.js +8 -0
  37. package/dist/utils/dom.cjs +61 -0
  38. package/dist/utils/dom.d.ts +28 -0
  39. package/dist/utils/dom.js +24 -0
  40. package/dist/utils/index.cjs +46 -0
  41. package/dist/utils/index.d.ts +2 -0
  42. package/dist/utils/index.js +3 -0
  43. package/dist/utils/system.cjs +46 -0
  44. package/dist/utils/system.d.ts +21 -0
  45. package/dist/utils/system.js +9 -0
  46. package/package.json +164 -0
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ ThemeProvider: ()=>ThemeProvider
28
+ });
29
+ const jsx_runtime_namespaceObject = require("react/jsx-runtime");
30
+ const external_react_namespaceObject = require("react");
31
+ const external_constants_cjs_namespaceObject = require("../constants.cjs");
32
+ const external_context_cjs_namespaceObject = require("./context.cjs");
33
+ const index_cjs_namespaceObject = require("../utils/index.cjs");
34
+ function subscribeToSystemTheme(callback) {
35
+ const mediaQuery = window.matchMedia(external_constants_cjs_namespaceObject.MEDIA);
36
+ mediaQuery.addEventListener('change', callback);
37
+ return ()=>{
38
+ mediaQuery.removeEventListener('change', callback);
39
+ };
40
+ }
41
+ function getSystemThemeSnapshot() {
42
+ return (0, index_cjs_namespaceObject.getSystemTheme)();
43
+ }
44
+ function getServerSnapshot() {
45
+ return external_constants_cjs_namespaceObject.DEFAULT_RESOLVED_THEME;
46
+ }
47
+ function ThemeProvider({ children, disableTransitionOnChange = false, nonce, persistTheme, theme: initialTheme }) {
48
+ const [theme, setThemeState] = (0, external_react_namespaceObject.useState)(initialTheme);
49
+ const [optimisticTheme, setOptimisticTheme] = (0, external_react_namespaceObject.useOptimistic)(theme);
50
+ const isPending = optimisticTheme !== theme;
51
+ const systemTheme = (0, external_react_namespaceObject.useSyncExternalStore)(subscribeToSystemTheme, getSystemThemeSnapshot, getServerSnapshot);
52
+ const resolvedTheme = (0, external_react_namespaceObject.useMemo)(()=>'system' === optimisticTheme ? systemTheme : optimisticTheme, [
53
+ optimisticTheme,
54
+ systemTheme
55
+ ]);
56
+ (0, external_react_namespaceObject.useEffect)(()=>{
57
+ (0, index_cjs_namespaceObject.applyTheme)(resolvedTheme);
58
+ }, [
59
+ resolvedTheme
60
+ ]);
61
+ const handleCrossTabMessage = (0, external_react_namespaceObject.useEffectEvent)((event)=>{
62
+ const newTheme = event.data;
63
+ if (newTheme === theme) return;
64
+ (0, external_react_namespaceObject.startTransition)(()=>{
65
+ setThemeState(newTheme);
66
+ });
67
+ });
68
+ (0, external_react_namespaceObject.useEffect)(()=>{
69
+ const channel = new BroadcastChannel(external_constants_cjs_namespaceObject.THEME_CHANNEL);
70
+ channel.addEventListener('message', handleCrossTabMessage);
71
+ return ()=>{
72
+ channel.removeEventListener('message', handleCrossTabMessage);
73
+ channel.close();
74
+ };
75
+ }, []);
76
+ const setTheme = (0, external_react_namespaceObject.useCallback)(async (value)=>{
77
+ await Promise.resolve();
78
+ if (value === theme) return;
79
+ const enable = disableTransitionOnChange ? (0, index_cjs_namespaceObject.disableAnimation)(nonce) : null;
80
+ (0, external_react_namespaceObject.startTransition)(async ()=>{
81
+ setOptimisticTheme(value);
82
+ try {
83
+ if (persistTheme) await persistTheme(value);
84
+ setThemeState(value);
85
+ const channel = new BroadcastChannel(external_constants_cjs_namespaceObject.THEME_CHANNEL);
86
+ channel.postMessage(value);
87
+ channel.close();
88
+ } catch (error) {
89
+ console.error('Failed to set theme:', error);
90
+ } finally{
91
+ enable?.();
92
+ }
93
+ });
94
+ }, [
95
+ theme,
96
+ disableTransitionOnChange,
97
+ nonce,
98
+ setOptimisticTheme,
99
+ persistTheme
100
+ ]);
101
+ const value = (0, external_react_namespaceObject.useMemo)(()=>({
102
+ isPending,
103
+ resolvedTheme,
104
+ setTheme,
105
+ theme: optimisticTheme
106
+ }), [
107
+ optimisticTheme,
108
+ resolvedTheme,
109
+ setTheme,
110
+ isPending
111
+ ]);
112
+ return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_context_cjs_namespaceObject.ThemeContext, {
113
+ value: value,
114
+ children: children
115
+ });
116
+ }
117
+ exports.ThemeProvider = __webpack_exports__.ThemeProvider;
118
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
119
+ "ThemeProvider"
120
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
121
+ Object.defineProperty(exports, '__esModule', {
122
+ value: true
123
+ });
@@ -0,0 +1,47 @@
1
+ import type { JSX, ReactNode } from 'react';
2
+ import type { Theme } from '../types';
3
+ interface ThemeProviderProps {
4
+ children: ReactNode;
5
+ /**
6
+ * When true, temporarily disables CSS transitions during theme changes.
7
+ * Prevents jarring color animations when switching themes.
8
+ * Default: false
9
+ */
10
+ disableTransitionOnChange?: boolean;
11
+ /**
12
+ * CSP nonce for inline styles when `disableTransitionOnChange` is enabled.
13
+ */
14
+ nonce?: string;
15
+ /**
16
+ * Async function to persist theme changes to server/storage.
17
+ *
18
+ * For TanStack Start: use `setThemeServerFn` from `@codefast/theme/tanstack-start`
19
+ * For Next.js: implement a server action
20
+ */
21
+ persistTheme?: (value: Theme) => Promise<void>;
22
+ /**
23
+ * Initial theme from server (typically from cookie via loader).
24
+ */
25
+ theme: Theme;
26
+ }
27
+ /**
28
+ * Provider component for theme state management.
29
+ *
30
+ * **React 19 Features Used:**
31
+ * - `useOptimistic` - Immediate UI feedback while persisting theme
32
+ * - `useSyncExternalStore` - SSR-safe subscription to OS theme preference
33
+ * - `useEffectEvent` - Stable callback for cross-tab sync without effect re-runs
34
+ *
35
+ * @example
36
+ * ```tsx
37
+ * // In your root layout
38
+ * <ThemeProvider
39
+ * theme={theme}
40
+ * persistTheme={(value) => setThemeServerFn({ data: value })}
41
+ * >
42
+ * {children}
43
+ * </ThemeProvider>
44
+ * ```
45
+ */
46
+ export declare function ThemeProvider({ children, disableTransitionOnChange, nonce, persistTheme, theme: initialTheme, }: ThemeProviderProps): JSX.Element;
47
+ export {};
@@ -0,0 +1,89 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { startTransition, useCallback, useEffect, useEffectEvent, useMemo, useOptimistic, useState, useSyncExternalStore } from "react";
3
+ import { DEFAULT_RESOLVED_THEME, MEDIA, THEME_CHANNEL } from "../constants.js";
4
+ import { ThemeContext } from "./context.js";
5
+ import { applyTheme, disableAnimation, getSystemTheme } from "../utils/index.js";
6
+ function subscribeToSystemTheme(callback) {
7
+ const mediaQuery = window.matchMedia(MEDIA);
8
+ mediaQuery.addEventListener('change', callback);
9
+ return ()=>{
10
+ mediaQuery.removeEventListener('change', callback);
11
+ };
12
+ }
13
+ function getSystemThemeSnapshot() {
14
+ return getSystemTheme();
15
+ }
16
+ function getServerSnapshot() {
17
+ return DEFAULT_RESOLVED_THEME;
18
+ }
19
+ function ThemeProvider({ children, disableTransitionOnChange = false, nonce, persistTheme, theme: initialTheme }) {
20
+ const [theme, setThemeState] = useState(initialTheme);
21
+ const [optimisticTheme, setOptimisticTheme] = useOptimistic(theme);
22
+ const isPending = optimisticTheme !== theme;
23
+ const systemTheme = useSyncExternalStore(subscribeToSystemTheme, getSystemThemeSnapshot, getServerSnapshot);
24
+ const resolvedTheme = useMemo(()=>'system' === optimisticTheme ? systemTheme : optimisticTheme, [
25
+ optimisticTheme,
26
+ systemTheme
27
+ ]);
28
+ useEffect(()=>{
29
+ applyTheme(resolvedTheme);
30
+ }, [
31
+ resolvedTheme
32
+ ]);
33
+ const handleCrossTabMessage = useEffectEvent((event)=>{
34
+ const newTheme = event.data;
35
+ if (newTheme === theme) return;
36
+ startTransition(()=>{
37
+ setThemeState(newTheme);
38
+ });
39
+ });
40
+ useEffect(()=>{
41
+ const channel = new BroadcastChannel(THEME_CHANNEL);
42
+ channel.addEventListener('message', handleCrossTabMessage);
43
+ return ()=>{
44
+ channel.removeEventListener('message', handleCrossTabMessage);
45
+ channel.close();
46
+ };
47
+ }, []);
48
+ const setTheme = useCallback(async (value)=>{
49
+ await Promise.resolve();
50
+ if (value === theme) return;
51
+ const enable = disableTransitionOnChange ? disableAnimation(nonce) : null;
52
+ startTransition(async ()=>{
53
+ setOptimisticTheme(value);
54
+ try {
55
+ if (persistTheme) await persistTheme(value);
56
+ setThemeState(value);
57
+ const channel = new BroadcastChannel(THEME_CHANNEL);
58
+ channel.postMessage(value);
59
+ channel.close();
60
+ } catch (error) {
61
+ console.error('Failed to set theme:', error);
62
+ } finally{
63
+ enable?.();
64
+ }
65
+ });
66
+ }, [
67
+ theme,
68
+ disableTransitionOnChange,
69
+ nonce,
70
+ setOptimisticTheme,
71
+ persistTheme
72
+ ]);
73
+ const value = useMemo(()=>({
74
+ isPending,
75
+ resolvedTheme,
76
+ setTheme,
77
+ theme: optimisticTheme
78
+ }), [
79
+ optimisticTheme,
80
+ resolvedTheme,
81
+ setTheme,
82
+ isPending
83
+ ]);
84
+ return /*#__PURE__*/ jsx(ThemeContext, {
85
+ value: value,
86
+ children: children
87
+ });
88
+ }
89
+ export { ThemeProvider };
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ useTheme: ()=>useTheme
28
+ });
29
+ const external_react_namespaceObject = require("react");
30
+ const external_context_cjs_namespaceObject = require("./context.cjs");
31
+ function useTheme() {
32
+ const value = (0, external_react_namespaceObject.use)(external_context_cjs_namespaceObject.ThemeContext);
33
+ if (!value) throw new Error('useTheme must be used within a ThemeProvider');
34
+ return value;
35
+ }
36
+ exports.useTheme = __webpack_exports__.useTheme;
37
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
38
+ "useTheme"
39
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
40
+ Object.defineProperty(exports, '__esModule', {
41
+ value: true
42
+ });
@@ -0,0 +1,28 @@
1
+ import type { ThemeContextType } from '../types';
2
+ /**
3
+ * Hook to access theme context.
4
+ *
5
+ * Uses React 19's `use()` API for context consumption, enabling:
6
+ * - Conditional context reading
7
+ * - Better Suspense boundary integration
8
+ *
9
+ * @returns Theme context with `theme`, `resolvedTheme`, `setTheme`, and `isPending`
10
+ * @throws Error if called outside of ThemeProvider
11
+ *
12
+ * @example
13
+ * ```tsx
14
+ * function ThemeToggle() {
15
+ * const { theme, setTheme, isPending } = useTheme();
16
+ *
17
+ * return (
18
+ * <button
19
+ * onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
20
+ * disabled={isPending}
21
+ * >
22
+ * {theme}
23
+ * </button>
24
+ * );
25
+ * }
26
+ * ```
27
+ */
28
+ export declare function useTheme(): ThemeContextType;
@@ -0,0 +1,8 @@
1
+ import { use } from "react";
2
+ import { ThemeContext } from "./context.js";
3
+ function useTheme() {
4
+ const value = use(ThemeContext);
5
+ if (!value) throw new Error('useTheme must be used within a ThemeProvider');
6
+ return value;
7
+ }
8
+ export { useTheme };
package/dist/index.cjs ADDED
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ ThemeProvider: ()=>index_cjs_namespaceObject.ThemeProvider,
28
+ resolveTheme: ()=>external_utils_index_cjs_namespaceObject.resolveTheme,
29
+ DEFAULT_THEME: ()=>external_constants_cjs_namespaceObject.DEFAULT_THEME,
30
+ disableAnimation: ()=>external_utils_index_cjs_namespaceObject.disableAnimation,
31
+ themes: ()=>external_types_cjs_namespaceObject.themes,
32
+ useTheme: ()=>index_cjs_namespaceObject.useTheme,
33
+ applyTheme: ()=>external_utils_index_cjs_namespaceObject.applyTheme,
34
+ ThemeContext: ()=>index_cjs_namespaceObject.ThemeContext,
35
+ getSystemTheme: ()=>external_utils_index_cjs_namespaceObject.getSystemTheme,
36
+ DEFAULT_RESOLVED_THEME: ()=>external_constants_cjs_namespaceObject.DEFAULT_RESOLVED_THEME,
37
+ THEME_STORAGE_KEY: ()=>external_constants_cjs_namespaceObject.THEME_STORAGE_KEY,
38
+ ThemeScript: ()=>external_script_index_cjs_namespaceObject.ThemeScript,
39
+ themeSchema: ()=>external_types_cjs_namespaceObject.themeSchema
40
+ });
41
+ const external_types_cjs_namespaceObject = require("./types.cjs");
42
+ const external_constants_cjs_namespaceObject = require("./constants.cjs");
43
+ const index_cjs_namespaceObject = require("./core/index.cjs");
44
+ const external_script_index_cjs_namespaceObject = require("./script/index.cjs");
45
+ const external_utils_index_cjs_namespaceObject = require("./utils/index.cjs");
46
+ exports.DEFAULT_RESOLVED_THEME = __webpack_exports__.DEFAULT_RESOLVED_THEME;
47
+ exports.DEFAULT_THEME = __webpack_exports__.DEFAULT_THEME;
48
+ exports.THEME_STORAGE_KEY = __webpack_exports__.THEME_STORAGE_KEY;
49
+ exports.ThemeContext = __webpack_exports__.ThemeContext;
50
+ exports.ThemeProvider = __webpack_exports__.ThemeProvider;
51
+ exports.ThemeScript = __webpack_exports__.ThemeScript;
52
+ exports.applyTheme = __webpack_exports__.applyTheme;
53
+ exports.disableAnimation = __webpack_exports__.disableAnimation;
54
+ exports.getSystemTheme = __webpack_exports__.getSystemTheme;
55
+ exports.resolveTheme = __webpack_exports__.resolveTheme;
56
+ exports.themeSchema = __webpack_exports__.themeSchema;
57
+ exports.themes = __webpack_exports__.themes;
58
+ exports.useTheme = __webpack_exports__.useTheme;
59
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
60
+ "DEFAULT_RESOLVED_THEME",
61
+ "DEFAULT_THEME",
62
+ "THEME_STORAGE_KEY",
63
+ "ThemeContext",
64
+ "ThemeProvider",
65
+ "ThemeScript",
66
+ "applyTheme",
67
+ "disableAnimation",
68
+ "getSystemTheme",
69
+ "resolveTheme",
70
+ "themeSchema",
71
+ "themes",
72
+ "useTheme"
73
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
74
+ Object.defineProperty(exports, '__esModule', {
75
+ value: true
76
+ });
@@ -0,0 +1,16 @@
1
+ /**
2
+ * \@codefast/theme
3
+ *
4
+ * React 19 theme management library with SSR support, cross-tab synchronization,
5
+ * and optimistic UI updates. Supports light, dark, and system theme preferences.
6
+ *
7
+ * @see {@link ThemeProvider} - Main provider component
8
+ * @see {@link useTheme} - Hook for consuming theme context
9
+ * @see {@link ThemeScript} - FOUC prevention script
10
+ */
11
+ export type { ResolvedTheme, Theme, ThemeContextType } from './types';
12
+ export { themes, themeSchema } from './types';
13
+ export { DEFAULT_RESOLVED_THEME, DEFAULT_THEME, THEME_STORAGE_KEY } from './constants';
14
+ export { ThemeContext, ThemeProvider, useTheme } from './core';
15
+ export { ThemeScript } from './script';
16
+ export { applyTheme, disableAnimation, getSystemTheme, resolveTheme } from './utils';
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import { themeSchema, themes } from "./types.js";
2
+ import { DEFAULT_RESOLVED_THEME, DEFAULT_THEME, THEME_STORAGE_KEY } from "./constants.js";
3
+ import { ThemeContext, ThemeProvider, useTheme } from "./core/index.js";
4
+ import { ThemeScript } from "./script/index.js";
5
+ import { applyTheme, disableAnimation, getSystemTheme, resolveTheme } from "./utils/index.js";
6
+ export { DEFAULT_RESOLVED_THEME, DEFAULT_THEME, THEME_STORAGE_KEY, ThemeContext, ThemeProvider, ThemeScript, applyTheme, disableAnimation, getSystemTheme, resolveTheme, themeSchema, themes, useTheme };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ ThemeScript: ()=>external_theme_script_cjs_namespaceObject.ThemeScript
28
+ });
29
+ const external_theme_script_cjs_namespaceObject = require("./theme-script.cjs");
30
+ exports.ThemeScript = __webpack_exports__.ThemeScript;
31
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
32
+ "ThemeScript"
33
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
34
+ Object.defineProperty(exports, '__esModule', {
35
+ value: true
36
+ });
@@ -0,0 +1 @@
1
+ export { ThemeScript } from './theme-script';
@@ -0,0 +1,2 @@
1
+ import { ThemeScript } from "./theme-script.js";
2
+ export { ThemeScript };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ ThemeScript: ()=>ThemeScript
28
+ });
29
+ const jsx_runtime_namespaceObject = require("react/jsx-runtime");
30
+ function ThemeScript({ theme }) {
31
+ const themeScript = `(function(){try{var theme="${theme}",resolvedTheme=theme;"system"===theme&&(resolvedTheme=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"),document.documentElement.classList.add(resolvedTheme),document.documentElement.style.colorScheme=resolvedTheme}catch(error){}})()`;
32
+ return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("script", {
33
+ dangerouslySetInnerHTML: {
34
+ __html: themeScript
35
+ }
36
+ });
37
+ }
38
+ exports.ThemeScript = __webpack_exports__.ThemeScript;
39
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
40
+ "ThemeScript"
41
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
42
+ Object.defineProperty(exports, '__esModule', {
43
+ value: true
44
+ });
@@ -0,0 +1,32 @@
1
+ import type { JSX } from 'react';
2
+ import type { Theme } from '../types';
3
+ interface ThemeScriptProps {
4
+ /**
5
+ * Initial theme from server (e.g., from cookie via loader).
6
+ */
7
+ theme: Theme;
8
+ }
9
+ /**
10
+ * Inline script that prevents Flash of Unstyled Content (FOUC).
11
+ *
12
+ * **Why this is needed:**
13
+ * React hydration occurs after the browser has already painted the page.
14
+ * Without this script, users would briefly see the wrong theme before
15
+ * React takes over and applies the correct one.
16
+ *
17
+ * **How it works:**
18
+ * This script runs synchronously in the `<head>` before first paint:
19
+ * 1. Resolves 'system' to 'light' or 'dark' using `matchMedia()`
20
+ * 2. Adds the theme class to `<html>` immediately
21
+ * 3. Sets `color-scheme` for native form controls and scrollbars
22
+ *
23
+ * @example
24
+ * ```tsx
25
+ * // In __root.tsx (TanStack Start)
26
+ * <head>
27
+ * <ThemeScript theme={theme} />
28
+ * </head>
29
+ * ```
30
+ */
31
+ export declare function ThemeScript({ theme }: ThemeScriptProps): JSX.Element;
32
+ export {};
@@ -0,0 +1,10 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ function ThemeScript({ theme }) {
3
+ const themeScript = `(function(){try{var theme="${theme}",resolvedTheme=theme;"system"===theme&&(resolvedTheme=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"),document.documentElement.classList.add(resolvedTheme),document.documentElement.style.colorScheme=resolvedTheme}catch(error){}})()`;
4
+ return /*#__PURE__*/ jsx("script", {
5
+ dangerouslySetInnerHTML: {
6
+ __html: themeScript
7
+ }
8
+ });
9
+ }
10
+ export { ThemeScript };
package/dist/types.cjs ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ themeSchema: ()=>themeSchema,
28
+ themes: ()=>themes
29
+ });
30
+ const external_zod_namespaceObject = require("zod");
31
+ const themeSchema = external_zod_namespaceObject.z["enum"]([
32
+ 'light',
33
+ 'dark',
34
+ 'system'
35
+ ]);
36
+ const themes = themeSchema.options;
37
+ exports.themeSchema = __webpack_exports__.themeSchema;
38
+ exports.themes = __webpack_exports__.themes;
39
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
40
+ "themeSchema",
41
+ "themes"
42
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
43
+ Object.defineProperty(exports, '__esModule', {
44
+ value: true
45
+ });
@@ -0,0 +1,46 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Zod schema for theme validation.
4
+ *
5
+ * Acts as the single source of truth for all theme-related types.
6
+ * Valid values: 'light', 'dark', 'system'.
7
+ */
8
+ export declare const themeSchema: z.ZodEnum<{
9
+ light: "light";
10
+ dark: "dark";
11
+ system: "system";
12
+ }>;
13
+ /**
14
+ * User's theme preference.
15
+ *
16
+ * - `'light'` - Force light mode
17
+ * - `'dark'` - Force dark mode
18
+ * - `'system'` - Follow OS preference
19
+ */
20
+ export type Theme = z.infer<typeof themeSchema>;
21
+ /**
22
+ * Actual theme applied to the UI after resolving 'system'.
23
+ *
24
+ * When theme is 'system', this reflects the OS preference (light/dark).
25
+ */
26
+ export type ResolvedTheme = Exclude<Theme, 'system'>;
27
+ /**
28
+ * Array of all available theme options: `['light', 'dark', 'system']`.
29
+ *
30
+ * Useful for rendering theme selectors in UI components.
31
+ */
32
+ export declare const themes: ("light" | "dark" | "system")[];
33
+ /**
34
+ * Shape of the theme context value provided by ThemeProvider.
35
+ *
36
+ * - `theme` - Current theme preference
37
+ * - `resolvedTheme` - Actual theme applied (system resolved to light/dark)
38
+ * - `setTheme` - Async function to update theme (triggers optimistic update)
39
+ * - `isPending` - True while theme change is being persisted
40
+ */
41
+ export interface ThemeContextType {
42
+ isPending: boolean;
43
+ resolvedTheme: ResolvedTheme;
44
+ setTheme: (value: Theme) => Promise<void>;
45
+ theme: Theme;
46
+ }