@space-man/react-theme-animation 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,81 @@
1
+ import React, { ReactNode } from 'react';
2
+ import { Theme, ColorTheme, ThemeAnimationType } from '../types';
3
+ /**
4
+ * Context type for the Next Theme Provider
5
+ */
6
+ interface NextThemeContextType {
7
+ ref: React.RefObject<HTMLButtonElement | null>;
8
+ theme: Theme;
9
+ colorTheme: ColorTheme;
10
+ resolvedTheme: 'light' | 'dark';
11
+ systemTheme: 'light' | 'dark';
12
+ setTheme: (theme: Theme) => void;
13
+ setColorTheme: (colorTheme: ColorTheme) => void;
14
+ switchTheme: (theme: Theme, animationOff?: boolean) => Promise<void>;
15
+ switchColorTheme: (colorTheme: string) => void;
16
+ toggleTheme: (animationOff?: boolean) => Promise<void>;
17
+ toggleLightTheme: (animationOff?: boolean) => Promise<void>;
18
+ toggleDarkTheme: (animationOff?: boolean) => Promise<void>;
19
+ toggleColorTheme: () => void;
20
+ createColorThemeToggle: (targetColorTheme: string) => () => void;
21
+ isColorThemeActive: (targetColorTheme: string) => boolean;
22
+ switchThemeFromElement: (theme: Theme, element: HTMLButtonElement) => Promise<void>;
23
+ }
24
+ /**
25
+ * Props for the Next Theme Provider
26
+ */
27
+ interface NextThemeProviderProps {
28
+ children: ReactNode;
29
+ themes?: Theme[];
30
+ colorThemes?: ColorTheme[];
31
+ defaultTheme?: Theme;
32
+ defaultColorTheme?: ColorTheme;
33
+ animationType?: ThemeAnimationType;
34
+ duration?: number;
35
+ storageKey?: string;
36
+ colorStorageKey?: string;
37
+ attribute?: 'class' | 'data-theme';
38
+ globalClassName?: string;
39
+ colorThemePrefix?: string;
40
+ nonce?: string;
41
+ disableAnimationOnInit?: boolean;
42
+ disablePreHydrationScript?: boolean;
43
+ }
44
+ /**
45
+ * Next Theme Provider - Next.js optimized theme management with animations
46
+ *
47
+ * This provider is specifically optimized for Next.js and SSR frameworks with
48
+ * pre-hydration script injection to prevent flash of wrong theme.
49
+ *
50
+ * Features:
51
+ * - Pre-hydration script for flash prevention (SSR apps)
52
+ * - Animated theme transitions via View Transitions API
53
+ * - Multi-dimensional theming (theme + colorTheme)
54
+ * - System theme detection
55
+ * - CSP support via nonce prop
56
+ *
57
+ * @param children - React children to wrap with theme context
58
+ * @param themes - Available theme options
59
+ * @param colorThemes - Available color theme options
60
+ * @param defaultTheme - Default theme to use
61
+ * @param defaultColorTheme - Default color theme to use
62
+ * @param animationType - Animation type for theme transitions
63
+ * @param duration - Animation duration in milliseconds
64
+ * @param storageKey - LocalStorage key for theme preference
65
+ * @param colorStorageKey - LocalStorage key for color theme preference
66
+ * @param attribute - HTML attribute to use for theme ('class' or 'data-theme')
67
+ * @param globalClassName - Class name for dark theme (when using 'class' attribute)
68
+ * @param colorThemePrefix - Prefix for color theme classes
69
+ * @param nonce - CSP nonce for inline script (SSR apps)
70
+ * @param disableAnimationOnInit - Skip animation during initial hydration
71
+ * @param disablePreHydrationScript - Disable flash prevention script (for SPAs without SSR)
72
+ */
73
+ export declare const NextThemeProvider: React.FC<NextThemeProviderProps>;
74
+ /**
75
+ * Hook to consume the Next Theme context
76
+ *
77
+ * @returns Theme context value with all theme state and methods
78
+ * @throws Error if used outside of NextThemeProvider
79
+ */
80
+ export declare const useNextTheme: () => NextThemeContextType;
81
+ export {};
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.useNextTheme = exports.NextThemeProvider = void 0;
37
+ const jsx_runtime_1 = require("react/jsx-runtime");
38
+ const react_1 = __importStar(require("react"));
39
+ const use_theme_animation_1 = require("../hooks/use-theme-animation");
40
+ const types_1 = require("../types");
41
+ const NextThemeContext = (0, react_1.createContext)(undefined);
42
+ /**
43
+ * Pre-hydration script content generator
44
+ * This script runs BEFORE React hydrates to prevent flash of wrong theme
45
+ */
46
+ const generatePreHydrationScript = (storageKey, colorStorageKey, defaultTheme, defaultColorTheme, attribute, globalClassName, colorThemePrefix) => {
47
+ return `
48
+ (function() {
49
+ try {
50
+ var theme = localStorage.getItem('${storageKey}') || '${defaultTheme}';
51
+ var colorTheme = localStorage.getItem('${colorStorageKey}') || '${defaultColorTheme}';
52
+ var resolved = theme === 'system'
53
+ ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
54
+ : theme;
55
+
56
+ if ('${attribute}' === 'class') {
57
+ if (resolved === 'dark') {
58
+ document.documentElement.classList.add('${globalClassName}');
59
+ } else {
60
+ document.documentElement.classList.remove('${globalClassName}');
61
+ }
62
+ } else {
63
+ document.documentElement.setAttribute('data-theme', resolved);
64
+ }
65
+
66
+ document.documentElement.style.colorScheme = resolved;
67
+ document.documentElement.classList.add('${colorThemePrefix}' + colorTheme);
68
+ } catch (e) {
69
+ console.warn('Theme pre-hydration script failed:', e);
70
+ }
71
+ })();
72
+ `;
73
+ };
74
+ /**
75
+ * Pre-hydration script component
76
+ * Injects inline script before React hydration to prevent theme flash
77
+ */
78
+ const ThemePreHydrationScript = react_1.default.memo(({ storageKey, colorStorageKey, defaultTheme, defaultColorTheme, attribute, globalClassName, colorThemePrefix, nonce, }) => {
79
+ const scriptContent = generatePreHydrationScript(storageKey, colorStorageKey, defaultTheme, defaultColorTheme, attribute, globalClassName, colorThemePrefix);
80
+ return ((0, jsx_runtime_1.jsx)("script", { nonce: nonce, suppressHydrationWarning: true, dangerouslySetInnerHTML: { __html: scriptContent } }));
81
+ });
82
+ ThemePreHydrationScript.displayName = 'ThemePreHydrationScript';
83
+ /**
84
+ * Next Theme Provider - Next.js optimized theme management with animations
85
+ *
86
+ * This provider is specifically optimized for Next.js and SSR frameworks with
87
+ * pre-hydration script injection to prevent flash of wrong theme.
88
+ *
89
+ * Features:
90
+ * - Pre-hydration script for flash prevention (SSR apps)
91
+ * - Animated theme transitions via View Transitions API
92
+ * - Multi-dimensional theming (theme + colorTheme)
93
+ * - System theme detection
94
+ * - CSP support via nonce prop
95
+ *
96
+ * @param children - React children to wrap with theme context
97
+ * @param themes - Available theme options
98
+ * @param colorThemes - Available color theme options
99
+ * @param defaultTheme - Default theme to use
100
+ * @param defaultColorTheme - Default color theme to use
101
+ * @param animationType - Animation type for theme transitions
102
+ * @param duration - Animation duration in milliseconds
103
+ * @param storageKey - LocalStorage key for theme preference
104
+ * @param colorStorageKey - LocalStorage key for color theme preference
105
+ * @param attribute - HTML attribute to use for theme ('class' or 'data-theme')
106
+ * @param globalClassName - Class name for dark theme (when using 'class' attribute)
107
+ * @param colorThemePrefix - Prefix for color theme classes
108
+ * @param nonce - CSP nonce for inline script (SSR apps)
109
+ * @param disableAnimationOnInit - Skip animation during initial hydration
110
+ * @param disablePreHydrationScript - Disable flash prevention script (for SPAs without SSR)
111
+ */
112
+ const NextThemeProvider = ({ children, themes = ['light', 'dark', 'system'], colorThemes = ['default'], defaultTheme = 'system', defaultColorTheme = 'default', animationType = types_1.ThemeAnimationType.CIRCLE, duration = 750, storageKey = 'theme', colorStorageKey = 'color-theme', attribute = 'class', globalClassName = 'dark', colorThemePrefix = 'theme-', nonce, disableAnimationOnInit = true, disablePreHydrationScript = false, }) => {
113
+ const [mounted, setMounted] = (0, react_1.useState)(false);
114
+ const themeState = (0, use_theme_animation_1.useThemeAnimation)({
115
+ themes,
116
+ colorThemes,
117
+ defaultTheme,
118
+ defaultColorTheme,
119
+ animationType,
120
+ duration,
121
+ storageKey,
122
+ colorStorageKey,
123
+ globalClassName: attribute === 'class' ? globalClassName : undefined,
124
+ colorThemePrefix,
125
+ });
126
+ (0, react_1.useEffect)(() => {
127
+ setMounted(true);
128
+ }, []);
129
+ const switchThemeWithHydrationAwareness = (0, react_1.useCallback)(async (theme, animationOff = false) => {
130
+ if (!mounted && disableAnimationOnInit) {
131
+ themeState.setTheme(theme);
132
+ }
133
+ else {
134
+ await themeState.switchTheme(theme, animationOff);
135
+ }
136
+ }, [mounted, disableAnimationOnInit, themeState]);
137
+ const switchThemeFromElement = (0, react_1.useCallback)(async (theme, element) => {
138
+ if (!mounted && disableAnimationOnInit) {
139
+ themeState.setTheme(theme);
140
+ return;
141
+ }
142
+ if (themeState.ref.current) {
143
+ const originalRef = themeState.ref.current;
144
+ Object.defineProperty(themeState.ref, 'current', {
145
+ value: element,
146
+ writable: true,
147
+ configurable: true,
148
+ });
149
+ await themeState.switchTheme(theme);
150
+ Object.defineProperty(themeState.ref, 'current', {
151
+ value: originalRef,
152
+ writable: true,
153
+ configurable: true,
154
+ });
155
+ }
156
+ else {
157
+ Object.defineProperty(themeState.ref, 'current', {
158
+ value: element,
159
+ writable: true,
160
+ configurable: true,
161
+ });
162
+ await themeState.switchTheme(theme);
163
+ }
164
+ }, [mounted, disableAnimationOnInit, themeState]);
165
+ const systemTheme = typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches
166
+ ? 'dark'
167
+ : 'light';
168
+ if (!mounted) {
169
+ const loadingContextValue = {
170
+ ref: { current: null },
171
+ theme: defaultTheme,
172
+ colorTheme: defaultColorTheme,
173
+ resolvedTheme: defaultTheme === 'dark' ? 'dark' : 'light',
174
+ systemTheme: 'light',
175
+ setTheme: () => { },
176
+ setColorTheme: () => { },
177
+ switchTheme: async () => { },
178
+ switchThemeFromElement: async () => { },
179
+ switchColorTheme: () => { },
180
+ toggleTheme: async () => { },
181
+ toggleLightTheme: async () => { },
182
+ toggleDarkTheme: async () => { },
183
+ toggleColorTheme: () => { },
184
+ createColorThemeToggle: () => () => { },
185
+ isColorThemeActive: () => false,
186
+ };
187
+ return ((0, jsx_runtime_1.jsxs)(NextThemeContext.Provider, { value: loadingContextValue, children: [!disablePreHydrationScript && ((0, jsx_runtime_1.jsx)(ThemePreHydrationScript, { storageKey: storageKey, colorStorageKey: colorStorageKey, defaultTheme: defaultTheme, defaultColorTheme: defaultColorTheme, attribute: attribute, globalClassName: globalClassName, colorThemePrefix: colorThemePrefix, nonce: nonce })), children] }));
188
+ }
189
+ const contextValue = {
190
+ ref: themeState.ref,
191
+ theme: themeState.theme,
192
+ colorTheme: themeState.colorTheme,
193
+ resolvedTheme: themeState.resolvedTheme,
194
+ systemTheme,
195
+ setTheme: themeState.setTheme,
196
+ setColorTheme: themeState.setColorTheme,
197
+ switchTheme: switchThemeWithHydrationAwareness,
198
+ switchThemeFromElement,
199
+ switchColorTheme: themeState.switchColorTheme,
200
+ toggleTheme: themeState.toggleTheme,
201
+ toggleLightTheme: themeState.toggleLightTheme,
202
+ toggleDarkTheme: themeState.toggleDarkTheme,
203
+ toggleColorTheme: themeState.toggleColorTheme,
204
+ createColorThemeToggle: themeState.createColorThemeToggle,
205
+ isColorThemeActive: themeState.isColorThemeActive,
206
+ };
207
+ return ((0, jsx_runtime_1.jsxs)(NextThemeContext.Provider, { value: contextValue, children: [!disablePreHydrationScript && ((0, jsx_runtime_1.jsx)(ThemePreHydrationScript, { storageKey: storageKey, colorStorageKey: colorStorageKey, defaultTheme: defaultTheme, defaultColorTheme: defaultColorTheme, attribute: attribute, globalClassName: globalClassName, colorThemePrefix: colorThemePrefix, nonce: nonce })), children] }));
208
+ };
209
+ exports.NextThemeProvider = NextThemeProvider;
210
+ /**
211
+ * Hook to consume the Next Theme context
212
+ *
213
+ * @returns Theme context value with all theme state and methods
214
+ * @throws Error if used outside of NextThemeProvider
215
+ */
216
+ const useNextTheme = () => {
217
+ const context = (0, react_1.useContext)(NextThemeContext);
218
+ if (context === undefined) {
219
+ throw new Error('useNextTheme must be used within a NextThemeProvider');
220
+ }
221
+ return context;
222
+ };
223
+ exports.useNextTheme = useNextTheme;
@@ -0,0 +1,68 @@
1
+ import React, { ReactNode } from 'react';
2
+ import { Theme, ColorTheme, ThemeAnimationType } from '../types';
3
+ /**
4
+ * Context type for the TanStack Start Theme Provider
5
+ */
6
+ interface TanStackThemeContextType {
7
+ ref: React.RefObject<HTMLButtonElement | null>;
8
+ theme: Theme;
9
+ colorTheme: ColorTheme;
10
+ resolvedTheme: 'light' | 'dark';
11
+ systemTheme: 'light' | 'dark';
12
+ isHydrated: boolean;
13
+ setTheme: (theme: Theme) => void;
14
+ setColorTheme: (colorTheme: ColorTheme) => void;
15
+ switchTheme: (theme: Theme, animationOff?: boolean) => Promise<void>;
16
+ switchColorTheme: (colorTheme: string) => void;
17
+ toggleTheme: (animationOff?: boolean) => Promise<void>;
18
+ toggleLightTheme: (animationOff?: boolean) => Promise<void>;
19
+ toggleDarkTheme: (animationOff?: boolean) => Promise<void>;
20
+ toggleColorTheme: () => void;
21
+ createColorThemeToggle: (targetColorTheme: string) => () => void;
22
+ isColorThemeActive: (targetColorTheme: string) => boolean;
23
+ switchThemeFromElement: (theme: Theme, element: HTMLButtonElement) => Promise<void>;
24
+ }
25
+ /**
26
+ * Props for the TanStack Start Theme Provider
27
+ */
28
+ interface TanStackThemeProviderProps {
29
+ children: ReactNode;
30
+ themes?: Theme[];
31
+ colorThemes?: ColorTheme[];
32
+ defaultTheme?: Theme;
33
+ defaultColorTheme?: ColorTheme;
34
+ animationType?: ThemeAnimationType;
35
+ duration?: number;
36
+ /** Storage key for theme preference */
37
+ storageKey?: string;
38
+ /** Storage key for color theme preference */
39
+ colorStorageKey?: string;
40
+ /** Class name for dark theme */
41
+ globalClassName?: string;
42
+ /** Prefix for color theme classes */
43
+ colorThemePrefix?: string;
44
+ }
45
+ /**
46
+ * TanStack Start Theme Provider - Optimized for TanStack Start Framework
47
+ *
48
+ * @param children - React children to wrap with theme context
49
+ * @param themes - Available theme options
50
+ * @param colorThemes - Available color theme options
51
+ * @param defaultTheme - Default theme to use
52
+ * @param defaultColorTheme - Default color theme to use
53
+ * @param animationType - Animation type for theme transitions
54
+ * @param duration - Animation duration in milliseconds
55
+ * @param storageKey - LocalStorage key for theme preference
56
+ * @param colorStorageKey - LocalStorage key for color theme preference
57
+ * @param globalClassName - Class name for dark theme
58
+ * @param colorThemePrefix - Prefix for color theme classes
59
+ */
60
+ export declare const TanStackThemeProvider: React.FC<TanStackThemeProviderProps>;
61
+ /**
62
+ * Hook to consume the TanStack Theme context
63
+ *
64
+ * @returns Theme context value with all theme state and methods
65
+ * @throws Error if used outside of TanStackThemeProvider
66
+ */
67
+ export declare const useTanStackTheme: () => TanStackThemeContextType;
68
+ export {};
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useTanStackTheme = exports.TanStackThemeProvider = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const use_theme_animation_1 = require("../hooks/use-theme-animation");
7
+ const types_1 = require("../types");
8
+ const TanStackThemeContext = (0, react_1.createContext)(undefined);
9
+ /**
10
+ * Custom hook to detect hydration state
11
+ * This is a simplified version compatible with TanStack Start's isomorphic model
12
+ *
13
+ * In TanStack Start, you can also use the built-in `useHydrated()` hook
14
+ * from `@tanstack/start` if available
15
+ */
16
+ const useHydrated = () => {
17
+ return (0, react_1.useSyncExternalStore)(
18
+ // Subscribe function (no-op for hydration state)
19
+ () => () => { },
20
+ // Client snapshot: always true after hydration
21
+ () => true,
22
+ // Server snapshot: always false during SSR
23
+ () => false);
24
+ };
25
+ /**
26
+ * TanStack Start Theme Provider - Optimized for TanStack Start Framework
27
+ *
28
+ * @param children - React children to wrap with theme context
29
+ * @param themes - Available theme options
30
+ * @param colorThemes - Available color theme options
31
+ * @param defaultTheme - Default theme to use
32
+ * @param defaultColorTheme - Default color theme to use
33
+ * @param animationType - Animation type for theme transitions
34
+ * @param duration - Animation duration in milliseconds
35
+ * @param storageKey - LocalStorage key for theme preference
36
+ * @param colorStorageKey - LocalStorage key for color theme preference
37
+ * @param globalClassName - Class name for dark theme
38
+ * @param colorThemePrefix - Prefix for color theme classes
39
+ */
40
+ const TanStackThemeProvider = ({ children, themes = ['light', 'dark', 'system'], colorThemes = ['default'], defaultTheme = 'system', defaultColorTheme = 'default', animationType = types_1.ThemeAnimationType.CIRCLE, duration = 750, storageKey = 'theme', colorStorageKey = 'color-theme', globalClassName = 'dark', colorThemePrefix = 'theme-', }) => {
41
+ const isHydrated = useHydrated();
42
+ const themeState = (0, use_theme_animation_1.useThemeAnimation)({
43
+ themes,
44
+ colorThemes,
45
+ defaultTheme,
46
+ defaultColorTheme,
47
+ animationType,
48
+ duration,
49
+ storageKey,
50
+ colorStorageKey,
51
+ globalClassName,
52
+ colorThemePrefix,
53
+ });
54
+ // Hydration-aware theme switch: instant during hydration, animated after
55
+ const switchThemeWithHydrationAwareness = (0, react_1.useCallback)(async (theme, animationOff = false) => {
56
+ if (!isHydrated) {
57
+ // During hydration: instant switch, no animation
58
+ themeState.setTheme(theme);
59
+ }
60
+ else {
61
+ // After hydration: animated switch
62
+ await themeState.switchTheme(theme, animationOff);
63
+ }
64
+ }, [isHydrated, themeState]);
65
+ const toggleThemeWithHydrationAwareness = (0, react_1.useCallback)(async (animationOff = false) => {
66
+ if (!isHydrated) {
67
+ const nextTheme = themeState.resolvedTheme === 'dark' ? 'light' : 'dark';
68
+ themeState.setTheme(nextTheme);
69
+ }
70
+ else {
71
+ await themeState.toggleTheme(animationOff);
72
+ }
73
+ }, [isHydrated, themeState]);
74
+ const toggleLightThemeWithHydrationAwareness = (0, react_1.useCallback)(async (animationOff = false) => {
75
+ if (!isHydrated) {
76
+ themeState.setTheme('light');
77
+ }
78
+ else {
79
+ await themeState.toggleLightTheme(animationOff);
80
+ }
81
+ }, [isHydrated, themeState]);
82
+ const toggleDarkThemeWithHydrationAwareness = (0, react_1.useCallback)(async (animationOff = false) => {
83
+ if (!isHydrated) {
84
+ themeState.setTheme('dark');
85
+ }
86
+ else {
87
+ await themeState.toggleDarkTheme(animationOff);
88
+ }
89
+ }, [isHydrated, themeState]);
90
+ const switchThemeFromElement = (0, react_1.useCallback)(async (theme, element) => {
91
+ if (!isHydrated) {
92
+ themeState.setTheme(theme);
93
+ return;
94
+ }
95
+ if (themeState.ref.current) {
96
+ const originalRef = themeState.ref.current;
97
+ Object.defineProperty(themeState.ref, 'current', {
98
+ value: element,
99
+ writable: true,
100
+ configurable: true,
101
+ });
102
+ await themeState.switchTheme(theme);
103
+ Object.defineProperty(themeState.ref, 'current', {
104
+ value: originalRef,
105
+ writable: true,
106
+ configurable: true,
107
+ });
108
+ }
109
+ else {
110
+ Object.defineProperty(themeState.ref, 'current', {
111
+ value: element,
112
+ writable: true,
113
+ configurable: true,
114
+ });
115
+ await themeState.switchTheme(theme);
116
+ }
117
+ }, [isHydrated, themeState]);
118
+ const systemTheme = typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches
119
+ ? 'dark'
120
+ : 'light';
121
+ // During SSR/hydration: provide safe context without animations
122
+ if (!isHydrated) {
123
+ const loadingContextValue = {
124
+ ref: { current: null },
125
+ theme: defaultTheme,
126
+ colorTheme: defaultColorTheme,
127
+ resolvedTheme: defaultTheme === 'dark' ? 'dark' : 'light',
128
+ systemTheme: 'light',
129
+ isHydrated: false,
130
+ setTheme: themeState.setTheme,
131
+ setColorTheme: themeState.setColorTheme,
132
+ switchTheme: async (theme, animationOff = false) => themeState.setTheme(theme),
133
+ switchThemeFromElement: async (theme) => themeState.setTheme(theme),
134
+ switchColorTheme: themeState.switchColorTheme,
135
+ toggleTheme: async (animationOff = false) => {
136
+ const nextTheme = themeState.resolvedTheme === 'dark' ? 'light' : 'dark';
137
+ themeState.setTheme(nextTheme);
138
+ },
139
+ toggleLightTheme: async (animationOff = false) => themeState.setTheme('light'),
140
+ toggleDarkTheme: async (animationOff = false) => themeState.setTheme('dark'),
141
+ toggleColorTheme: themeState.toggleColorTheme,
142
+ createColorThemeToggle: themeState.createColorThemeToggle,
143
+ isColorThemeActive: themeState.isColorThemeActive,
144
+ };
145
+ return ((0, jsx_runtime_1.jsx)(TanStackThemeContext.Provider, { value: loadingContextValue, children: children }));
146
+ }
147
+ // After hydration: full context with animations enabled
148
+ const contextValue = {
149
+ ref: themeState.ref,
150
+ theme: themeState.theme,
151
+ colorTheme: themeState.colorTheme,
152
+ resolvedTheme: themeState.resolvedTheme,
153
+ systemTheme,
154
+ isHydrated: true,
155
+ setTheme: themeState.setTheme,
156
+ setColorTheme: themeState.setColorTheme,
157
+ switchTheme: switchThemeWithHydrationAwareness,
158
+ switchThemeFromElement,
159
+ switchColorTheme: themeState.switchColorTheme,
160
+ toggleTheme: toggleThemeWithHydrationAwareness,
161
+ toggleLightTheme: toggleLightThemeWithHydrationAwareness,
162
+ toggleDarkTheme: toggleDarkThemeWithHydrationAwareness,
163
+ toggleColorTheme: themeState.toggleColorTheme,
164
+ createColorThemeToggle: themeState.createColorThemeToggle,
165
+ isColorThemeActive: themeState.isColorThemeActive,
166
+ };
167
+ return ((0, jsx_runtime_1.jsx)(TanStackThemeContext.Provider, { value: contextValue, children: children }));
168
+ };
169
+ exports.TanStackThemeProvider = TanStackThemeProvider;
170
+ /**
171
+ * Hook to consume the TanStack Theme context
172
+ *
173
+ * @returns Theme context value with all theme state and methods
174
+ * @throws Error if used outside of TanStackThemeProvider
175
+ */
176
+ const useTanStackTheme = () => {
177
+ const context = (0, react_1.useContext)(TanStackThemeContext);
178
+ if (context === undefined) {
179
+ throw new Error('useTanStackTheme must be used within a TanStackThemeProvider');
180
+ }
181
+ return context;
182
+ };
183
+ exports.useTanStackTheme = useTanStackTheme;
@@ -8,11 +8,11 @@ interface ViteThemeContextType {
8
8
  systemTheme: 'light' | 'dark';
9
9
  setTheme: (theme: Theme) => void;
10
10
  setColorTheme: (colorTheme: ColorTheme) => void;
11
- switchTheme: (theme: Theme) => Promise<void>;
11
+ switchTheme: (theme: Theme, animationOff?: boolean) => Promise<void>;
12
12
  switchColorTheme: (colorTheme: string) => void;
13
- toggleTheme: () => Promise<void>;
14
- toggleLightTheme: () => Promise<void>;
15
- toggleDarkTheme: () => Promise<void>;
13
+ toggleTheme: (animationOff?: boolean) => Promise<void>;
14
+ toggleLightTheme: (animationOff?: boolean) => Promise<void>;
15
+ toggleDarkTheme: (animationOff?: boolean) => Promise<void>;
16
16
  toggleColorTheme: () => void;
17
17
  createColorThemeToggle: (targetColorTheme: string) => () => void;
18
18
  isColorThemeActive: (targetColorTheme: string) => boolean;
@@ -33,6 +33,29 @@ interface ViteThemeProviderProps {
33
33
  globalClassName?: string;
34
34
  colorThemePrefix?: string;
35
35
  }
36
+ /**
37
+ * Vite Theme Provider - Optimized for Vite SPA applications
38
+ *
39
+ * This provider is designed for Vite-based single-page applications with optional
40
+ * transition disabling to prevent flash during theme switches.
41
+ *
42
+ * Note: For flash prevention on initial load, add a script to your index.html
43
+ * or inject it via Vite plugin to apply theme before React loads.
44
+ *
45
+ * @param children - React children to wrap with theme context
46
+ * @param themes - Available theme options
47
+ * @param colorThemes - Available color theme options
48
+ * @param defaultTheme - Default theme to use
49
+ * @param defaultColorTheme - Default color theme to use
50
+ * @param animationType - Animation type for theme transitions
51
+ * @param duration - Animation duration in milliseconds
52
+ * @param attribute - HTML attribute to use for theme ('class' or 'data-theme')
53
+ * @param disableTransitionOnChange - Temporarily disable CSS transitions during theme switch
54
+ * @param storageKey - LocalStorage key for theme preference
55
+ * @param colorStorageKey - LocalStorage key for color theme preference
56
+ * @param globalClassName - Class name for dark theme (when using 'class' attribute)
57
+ * @param colorThemePrefix - Prefix for color theme classes
58
+ */
36
59
  export declare const ViteThemeProvider: React.FC<ViteThemeProviderProps>;
37
60
  export declare const useViteTheme: () => ViteThemeContextType;
38
61
  export {};
@@ -6,6 +6,29 @@ const react_1 = require("react");
6
6
  const use_theme_animation_1 = require("../hooks/use-theme-animation");
7
7
  const types_1 = require("../types");
8
8
  const ViteThemeContext = (0, react_1.createContext)(undefined);
9
+ /**
10
+ * Vite Theme Provider - Optimized for Vite SPA applications
11
+ *
12
+ * This provider is designed for Vite-based single-page applications with optional
13
+ * transition disabling to prevent flash during theme switches.
14
+ *
15
+ * Note: For flash prevention on initial load, add a script to your index.html
16
+ * or inject it via Vite plugin to apply theme before React loads.
17
+ *
18
+ * @param children - React children to wrap with theme context
19
+ * @param themes - Available theme options
20
+ * @param colorThemes - Available color theme options
21
+ * @param defaultTheme - Default theme to use
22
+ * @param defaultColorTheme - Default color theme to use
23
+ * @param animationType - Animation type for theme transitions
24
+ * @param duration - Animation duration in milliseconds
25
+ * @param attribute - HTML attribute to use for theme ('class' or 'data-theme')
26
+ * @param disableTransitionOnChange - Temporarily disable CSS transitions during theme switch
27
+ * @param storageKey - LocalStorage key for theme preference
28
+ * @param colorStorageKey - LocalStorage key for color theme preference
29
+ * @param globalClassName - Class name for dark theme (when using 'class' attribute)
30
+ * @param colorThemePrefix - Prefix for color theme classes
31
+ */
9
32
  const ViteThemeProvider = ({ children, themes = ['light', 'dark', 'system'], colorThemes = ['default'], defaultTheme = 'system', defaultColorTheme = 'default', animationType = types_1.ThemeAnimationType.CIRCLE, duration = 750, attribute = 'class', disableTransitionOnChange = false, storageKey = 'vite-theme', colorStorageKey = 'vite-color-theme', globalClassName = 'dark', colorThemePrefix = 'theme-', }) => {
10
33
  const themeState = (0, use_theme_animation_1.useThemeAnimation)({
11
34
  themes,
@@ -37,24 +60,24 @@ const ViteThemeProvider = ({ children, themes = ['light', 'dark', 'system'], col
37
60
  themeState.setTheme(theme);
38
61
  cleanup();
39
62
  }, [themeState.setTheme, applyTransitionDisable]);
40
- const wrappedSwitchTheme = (0, react_1.useCallback)(async (theme) => {
63
+ const wrappedSwitchTheme = (0, react_1.useCallback)(async (theme, animationOff = false) => {
41
64
  const cleanup = applyTransitionDisable();
42
- await themeState.switchTheme(theme);
65
+ await themeState.switchTheme(theme, animationOff);
43
66
  cleanup();
44
67
  }, [themeState.switchTheme, applyTransitionDisable]);
45
- const wrappedToggleTheme = (0, react_1.useCallback)(async () => {
68
+ const wrappedToggleTheme = (0, react_1.useCallback)(async (animationOff = false) => {
46
69
  const cleanup = applyTransitionDisable();
47
- await themeState.toggleTheme();
70
+ await themeState.toggleTheme(animationOff);
48
71
  cleanup();
49
72
  }, [themeState.toggleTheme, applyTransitionDisable]);
50
- const wrappedToggleLightTheme = (0, react_1.useCallback)(async () => {
73
+ const wrappedToggleLightTheme = (0, react_1.useCallback)(async (animationOff = false) => {
51
74
  const cleanup = applyTransitionDisable();
52
- await themeState.toggleLightTheme();
75
+ await themeState.toggleLightTheme(animationOff);
53
76
  cleanup();
54
77
  }, [themeState.toggleLightTheme, applyTransitionDisable]);
55
- const wrappedToggleDarkTheme = (0, react_1.useCallback)(async () => {
78
+ const wrappedToggleDarkTheme = (0, react_1.useCallback)(async (animationOff = false) => {
56
79
  const cleanup = applyTransitionDisable();
57
- await themeState.toggleDarkTheme();
80
+ await themeState.toggleDarkTheme(animationOff);
58
81
  cleanup();
59
82
  }, [themeState.toggleDarkTheme, applyTransitionDisable]);
60
83
  const switchThemeFromElement = async (theme, element) => {