@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.
@@ -86,8 +86,8 @@ const useThemeAnimation = (props = {}) => {
86
86
  onColorThemeChange(newColorTheme);
87
87
  }
88
88
  }, [onColorThemeChange, externalColorTheme, colorStorageKey]);
89
- const switchTheme = (0, react_1.useCallback)(async (newTheme) => {
90
- if (!ref.current || !(0, animations_1.supportsViewTransitions)() || (0, animations_1.prefersReducedMotion)()) {
89
+ const switchTheme = (0, react_1.useCallback)(async (newTheme, animationOff = false) => {
90
+ if (!ref.current || !(0, animations_1.supportsViewTransitions)() || (0, animations_1.prefersReducedMotion)() || animationOff) {
91
91
  setTheme(newTheme);
92
92
  return;
93
93
  }
@@ -182,19 +182,19 @@ const useThemeAnimation = (props = {}) => {
182
182
  }
183
183
  setColorTheme(newColorTheme);
184
184
  }, [colorThemes, setColorTheme]);
185
- const toggleTheme = (0, react_1.useCallback)(async () => {
185
+ const toggleTheme = (0, react_1.useCallback)(async (animationOff = false) => {
186
186
  const newTheme = resolvedTheme === 'dark' ? 'light' : 'dark';
187
- await switchTheme(newTheme);
187
+ await switchTheme(newTheme, animationOff);
188
188
  }, [resolvedTheme, switchTheme]);
189
- const toggleLightTheme = (0, react_1.useCallback)(async () => {
189
+ const toggleLightTheme = (0, react_1.useCallback)(async (animationOff = false) => {
190
190
  if (resolvedTheme === 'light')
191
191
  return;
192
- await switchTheme('light');
192
+ await switchTheme('light', animationOff);
193
193
  }, [resolvedTheme, switchTheme]);
194
- const toggleDarkTheme = (0, react_1.useCallback)(async () => {
194
+ const toggleDarkTheme = (0, react_1.useCallback)(async (animationOff = false) => {
195
195
  if (resolvedTheme === 'dark')
196
196
  return;
197
- await switchTheme('dark');
197
+ await switchTheme('dark', animationOff);
198
198
  }, [resolvedTheme, switchTheme]);
199
199
  const toggleColorTheme = (0, react_1.useCallback)(() => {
200
200
  const currentIndex = colorThemes.indexOf(currentColorTheme);
@@ -2,6 +2,8 @@ export { useThemeAnimation } from './hooks/use-theme-animation';
2
2
  export { ThemeSwitcher } from './components/ThemeSwitcher';
3
3
  export { ThemeSelector } from './components/ThemeSelector';
4
4
  export { SpacemanThemeProvider, useSpacemanTheme } from './components/SpacemanThemeProvider';
5
+ export { NextThemeProvider, useNextTheme } from './components/NextThemeProvider';
6
+ export { TanStackThemeProvider, useTanStackTheme } from './components/TanStackThemeProvider';
5
7
  export { ViteThemeProvider, useViteTheme } from './components/ViteThemeProvider';
6
8
  export type { UseThemeAnimationProps, UseThemeAnimationReturn, ThemeSwitcherProps, ThemeSelectorProps, Theme, ColorTheme, SlideDirection, } from './types';
7
9
  export { ThemeAnimationType } from './types';
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prefersReducedMotion = exports.supportsViewTransitions = exports.resolveTheme = exports.getSystemTheme = exports.injectBaseStyles = exports.ThemeAnimationType = exports.useViteTheme = exports.ViteThemeProvider = exports.useSpacemanTheme = exports.SpacemanThemeProvider = exports.ThemeSelector = exports.ThemeSwitcher = exports.useThemeAnimation = void 0;
3
+ exports.prefersReducedMotion = exports.supportsViewTransitions = exports.resolveTheme = exports.getSystemTheme = exports.injectBaseStyles = exports.ThemeAnimationType = exports.useViteTheme = exports.ViteThemeProvider = exports.useTanStackTheme = exports.TanStackThemeProvider = exports.useNextTheme = exports.NextThemeProvider = exports.useSpacemanTheme = exports.SpacemanThemeProvider = exports.ThemeSelector = exports.ThemeSwitcher = exports.useThemeAnimation = void 0;
4
4
  var use_theme_animation_1 = require("./hooks/use-theme-animation");
5
5
  Object.defineProperty(exports, "useThemeAnimation", { enumerable: true, get: function () { return use_theme_animation_1.useThemeAnimation; } });
6
6
  var ThemeSwitcher_1 = require("./components/ThemeSwitcher");
@@ -10,6 +10,13 @@ Object.defineProperty(exports, "ThemeSelector", { enumerable: true, get: functio
10
10
  var SpacemanThemeProvider_1 = require("./components/SpacemanThemeProvider");
11
11
  Object.defineProperty(exports, "SpacemanThemeProvider", { enumerable: true, get: function () { return SpacemanThemeProvider_1.SpacemanThemeProvider; } });
12
12
  Object.defineProperty(exports, "useSpacemanTheme", { enumerable: true, get: function () { return SpacemanThemeProvider_1.useSpacemanTheme; } });
13
+ // Framework-specific optimized providers
14
+ var NextThemeProvider_1 = require("./components/NextThemeProvider");
15
+ Object.defineProperty(exports, "NextThemeProvider", { enumerable: true, get: function () { return NextThemeProvider_1.NextThemeProvider; } });
16
+ Object.defineProperty(exports, "useNextTheme", { enumerable: true, get: function () { return NextThemeProvider_1.useNextTheme; } });
17
+ var TanStackThemeProvider_1 = require("./components/TanStackThemeProvider");
18
+ Object.defineProperty(exports, "TanStackThemeProvider", { enumerable: true, get: function () { return TanStackThemeProvider_1.TanStackThemeProvider; } });
19
+ Object.defineProperty(exports, "useTanStackTheme", { enumerable: true, get: function () { return TanStackThemeProvider_1.useTanStackTheme; } });
13
20
  var ViteThemeProvider_1 = require("./components/ViteThemeProvider");
14
21
  Object.defineProperty(exports, "ViteThemeProvider", { enumerable: true, get: function () { return ViteThemeProvider_1.ViteThemeProvider; } });
15
22
  Object.defineProperty(exports, "useViteTheme", { enumerable: true, get: function () { return ViteThemeProvider_1.useViteTheme; } });
@@ -38,11 +38,11 @@ export interface UseThemeAnimationReturn {
38
38
  resolvedTheme: 'light' | 'dark';
39
39
  setTheme: (theme: Theme) => void;
40
40
  setColorTheme: (colorTheme: ColorTheme) => void;
41
- switchTheme: (theme: Theme) => Promise<void>;
41
+ switchTheme: (theme: Theme, animationOff?: boolean) => Promise<void>;
42
42
  switchColorTheme: (colorTheme: string) => void;
43
- toggleTheme: () => Promise<void>;
44
- toggleLightTheme: () => Promise<void>;
45
- toggleDarkTheme: () => Promise<void>;
43
+ toggleTheme: (animationOff?: boolean) => Promise<void>;
44
+ toggleLightTheme: (animationOff?: boolean) => Promise<void>;
45
+ toggleDarkTheme: (animationOff?: boolean) => Promise<void>;
46
46
  toggleColorTheme: () => void;
47
47
  createColorThemeToggle: (targetColorTheme: string) => () => void;
48
48
  isColorThemeActive: (targetColorTheme: string) => boolean;
@@ -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,185 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import React, { createContext, useContext, useState, useEffect, useCallback, } from 'react';
3
+ import { useThemeAnimation } from '../hooks/use-theme-animation';
4
+ import { ThemeAnimationType } from '../types';
5
+ const NextThemeContext = createContext(undefined);
6
+ /**
7
+ * Pre-hydration script content generator
8
+ * This script runs BEFORE React hydrates to prevent flash of wrong theme
9
+ */
10
+ const generatePreHydrationScript = (storageKey, colorStorageKey, defaultTheme, defaultColorTheme, attribute, globalClassName, colorThemePrefix) => {
11
+ return `
12
+ (function() {
13
+ try {
14
+ var theme = localStorage.getItem('${storageKey}') || '${defaultTheme}';
15
+ var colorTheme = localStorage.getItem('${colorStorageKey}') || '${defaultColorTheme}';
16
+ var resolved = theme === 'system'
17
+ ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
18
+ : theme;
19
+
20
+ if ('${attribute}' === 'class') {
21
+ if (resolved === 'dark') {
22
+ document.documentElement.classList.add('${globalClassName}');
23
+ } else {
24
+ document.documentElement.classList.remove('${globalClassName}');
25
+ }
26
+ } else {
27
+ document.documentElement.setAttribute('data-theme', resolved);
28
+ }
29
+
30
+ document.documentElement.style.colorScheme = resolved;
31
+ document.documentElement.classList.add('${colorThemePrefix}' + colorTheme);
32
+ } catch (e) {
33
+ console.warn('Theme pre-hydration script failed:', e);
34
+ }
35
+ })();
36
+ `;
37
+ };
38
+ /**
39
+ * Pre-hydration script component
40
+ * Injects inline script before React hydration to prevent theme flash
41
+ */
42
+ const ThemePreHydrationScript = React.memo(({ storageKey, colorStorageKey, defaultTheme, defaultColorTheme, attribute, globalClassName, colorThemePrefix, nonce, }) => {
43
+ const scriptContent = generatePreHydrationScript(storageKey, colorStorageKey, defaultTheme, defaultColorTheme, attribute, globalClassName, colorThemePrefix);
44
+ return (_jsx("script", { nonce: nonce, suppressHydrationWarning: true, dangerouslySetInnerHTML: { __html: scriptContent } }));
45
+ });
46
+ ThemePreHydrationScript.displayName = 'ThemePreHydrationScript';
47
+ /**
48
+ * Next Theme Provider - Next.js optimized theme management with animations
49
+ *
50
+ * This provider is specifically optimized for Next.js and SSR frameworks with
51
+ * pre-hydration script injection to prevent flash of wrong theme.
52
+ *
53
+ * Features:
54
+ * - Pre-hydration script for flash prevention (SSR apps)
55
+ * - Animated theme transitions via View Transitions API
56
+ * - Multi-dimensional theming (theme + colorTheme)
57
+ * - System theme detection
58
+ * - CSP support via nonce prop
59
+ *
60
+ * @param children - React children to wrap with theme context
61
+ * @param themes - Available theme options
62
+ * @param colorThemes - Available color theme options
63
+ * @param defaultTheme - Default theme to use
64
+ * @param defaultColorTheme - Default color theme to use
65
+ * @param animationType - Animation type for theme transitions
66
+ * @param duration - Animation duration in milliseconds
67
+ * @param storageKey - LocalStorage key for theme preference
68
+ * @param colorStorageKey - LocalStorage key for color theme preference
69
+ * @param attribute - HTML attribute to use for theme ('class' or 'data-theme')
70
+ * @param globalClassName - Class name for dark theme (when using 'class' attribute)
71
+ * @param colorThemePrefix - Prefix for color theme classes
72
+ * @param nonce - CSP nonce for inline script (SSR apps)
73
+ * @param disableAnimationOnInit - Skip animation during initial hydration
74
+ * @param disablePreHydrationScript - Disable flash prevention script (for SPAs without SSR)
75
+ */
76
+ export const NextThemeProvider = ({ children, themes = ['light', 'dark', 'system'], colorThemes = ['default'], defaultTheme = 'system', defaultColorTheme = 'default', animationType = ThemeAnimationType.CIRCLE, duration = 750, storageKey = 'theme', colorStorageKey = 'color-theme', attribute = 'class', globalClassName = 'dark', colorThemePrefix = 'theme-', nonce, disableAnimationOnInit = true, disablePreHydrationScript = false, }) => {
77
+ const [mounted, setMounted] = useState(false);
78
+ const themeState = useThemeAnimation({
79
+ themes,
80
+ colorThemes,
81
+ defaultTheme,
82
+ defaultColorTheme,
83
+ animationType,
84
+ duration,
85
+ storageKey,
86
+ colorStorageKey,
87
+ globalClassName: attribute === 'class' ? globalClassName : undefined,
88
+ colorThemePrefix,
89
+ });
90
+ useEffect(() => {
91
+ setMounted(true);
92
+ }, []);
93
+ const switchThemeWithHydrationAwareness = useCallback(async (theme, animationOff = false) => {
94
+ if (!mounted && disableAnimationOnInit) {
95
+ themeState.setTheme(theme);
96
+ }
97
+ else {
98
+ await themeState.switchTheme(theme, animationOff);
99
+ }
100
+ }, [mounted, disableAnimationOnInit, themeState]);
101
+ const switchThemeFromElement = useCallback(async (theme, element) => {
102
+ if (!mounted && disableAnimationOnInit) {
103
+ themeState.setTheme(theme);
104
+ return;
105
+ }
106
+ if (themeState.ref.current) {
107
+ const originalRef = themeState.ref.current;
108
+ Object.defineProperty(themeState.ref, 'current', {
109
+ value: element,
110
+ writable: true,
111
+ configurable: true,
112
+ });
113
+ await themeState.switchTheme(theme);
114
+ Object.defineProperty(themeState.ref, 'current', {
115
+ value: originalRef,
116
+ writable: true,
117
+ configurable: true,
118
+ });
119
+ }
120
+ else {
121
+ Object.defineProperty(themeState.ref, 'current', {
122
+ value: element,
123
+ writable: true,
124
+ configurable: true,
125
+ });
126
+ await themeState.switchTheme(theme);
127
+ }
128
+ }, [mounted, disableAnimationOnInit, themeState]);
129
+ const systemTheme = typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches
130
+ ? 'dark'
131
+ : 'light';
132
+ if (!mounted) {
133
+ const loadingContextValue = {
134
+ ref: { current: null },
135
+ theme: defaultTheme,
136
+ colorTheme: defaultColorTheme,
137
+ resolvedTheme: defaultTheme === 'dark' ? 'dark' : 'light',
138
+ systemTheme: 'light',
139
+ setTheme: () => { },
140
+ setColorTheme: () => { },
141
+ switchTheme: async () => { },
142
+ switchThemeFromElement: async () => { },
143
+ switchColorTheme: () => { },
144
+ toggleTheme: async () => { },
145
+ toggleLightTheme: async () => { },
146
+ toggleDarkTheme: async () => { },
147
+ toggleColorTheme: () => { },
148
+ createColorThemeToggle: () => () => { },
149
+ isColorThemeActive: () => false,
150
+ };
151
+ return (_jsxs(NextThemeContext.Provider, { value: loadingContextValue, children: [!disablePreHydrationScript && (_jsx(ThemePreHydrationScript, { storageKey: storageKey, colorStorageKey: colorStorageKey, defaultTheme: defaultTheme, defaultColorTheme: defaultColorTheme, attribute: attribute, globalClassName: globalClassName, colorThemePrefix: colorThemePrefix, nonce: nonce })), children] }));
152
+ }
153
+ const contextValue = {
154
+ ref: themeState.ref,
155
+ theme: themeState.theme,
156
+ colorTheme: themeState.colorTheme,
157
+ resolvedTheme: themeState.resolvedTheme,
158
+ systemTheme,
159
+ setTheme: themeState.setTheme,
160
+ setColorTheme: themeState.setColorTheme,
161
+ switchTheme: switchThemeWithHydrationAwareness,
162
+ switchThemeFromElement,
163
+ switchColorTheme: themeState.switchColorTheme,
164
+ toggleTheme: themeState.toggleTheme,
165
+ toggleLightTheme: themeState.toggleLightTheme,
166
+ toggleDarkTheme: themeState.toggleDarkTheme,
167
+ toggleColorTheme: themeState.toggleColorTheme,
168
+ createColorThemeToggle: themeState.createColorThemeToggle,
169
+ isColorThemeActive: themeState.isColorThemeActive,
170
+ };
171
+ return (_jsxs(NextThemeContext.Provider, { value: contextValue, children: [!disablePreHydrationScript && (_jsx(ThemePreHydrationScript, { storageKey: storageKey, colorStorageKey: colorStorageKey, defaultTheme: defaultTheme, defaultColorTheme: defaultColorTheme, attribute: attribute, globalClassName: globalClassName, colorThemePrefix: colorThemePrefix, nonce: nonce })), children] }));
172
+ };
173
+ /**
174
+ * Hook to consume the Next Theme context
175
+ *
176
+ * @returns Theme context value with all theme state and methods
177
+ * @throws Error if used outside of NextThemeProvider
178
+ */
179
+ export const useNextTheme = () => {
180
+ const context = useContext(NextThemeContext);
181
+ if (context === undefined) {
182
+ throw new Error('useNextTheme must be used within a NextThemeProvider');
183
+ }
184
+ return context;
185
+ };
@@ -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,178 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext, useCallback, useSyncExternalStore, } from 'react';
3
+ import { useThemeAnimation } from '../hooks/use-theme-animation';
4
+ import { ThemeAnimationType } from '../types';
5
+ const TanStackThemeContext = createContext(undefined);
6
+ /**
7
+ * Custom hook to detect hydration state
8
+ * This is a simplified version compatible with TanStack Start's isomorphic model
9
+ *
10
+ * In TanStack Start, you can also use the built-in `useHydrated()` hook
11
+ * from `@tanstack/start` if available
12
+ */
13
+ const useHydrated = () => {
14
+ return useSyncExternalStore(
15
+ // Subscribe function (no-op for hydration state)
16
+ () => () => { },
17
+ // Client snapshot: always true after hydration
18
+ () => true,
19
+ // Server snapshot: always false during SSR
20
+ () => false);
21
+ };
22
+ /**
23
+ * TanStack Start Theme Provider - Optimized for TanStack Start Framework
24
+ *
25
+ * @param children - React children to wrap with theme context
26
+ * @param themes - Available theme options
27
+ * @param colorThemes - Available color theme options
28
+ * @param defaultTheme - Default theme to use
29
+ * @param defaultColorTheme - Default color theme to use
30
+ * @param animationType - Animation type for theme transitions
31
+ * @param duration - Animation duration in milliseconds
32
+ * @param storageKey - LocalStorage key for theme preference
33
+ * @param colorStorageKey - LocalStorage key for color theme preference
34
+ * @param globalClassName - Class name for dark theme
35
+ * @param colorThemePrefix - Prefix for color theme classes
36
+ */
37
+ export const TanStackThemeProvider = ({ children, themes = ['light', 'dark', 'system'], colorThemes = ['default'], defaultTheme = 'system', defaultColorTheme = 'default', animationType = ThemeAnimationType.CIRCLE, duration = 750, storageKey = 'theme', colorStorageKey = 'color-theme', globalClassName = 'dark', colorThemePrefix = 'theme-', }) => {
38
+ const isHydrated = useHydrated();
39
+ const themeState = useThemeAnimation({
40
+ themes,
41
+ colorThemes,
42
+ defaultTheme,
43
+ defaultColorTheme,
44
+ animationType,
45
+ duration,
46
+ storageKey,
47
+ colorStorageKey,
48
+ globalClassName,
49
+ colorThemePrefix,
50
+ });
51
+ // Hydration-aware theme switch: instant during hydration, animated after
52
+ const switchThemeWithHydrationAwareness = useCallback(async (theme, animationOff = false) => {
53
+ if (!isHydrated) {
54
+ // During hydration: instant switch, no animation
55
+ themeState.setTheme(theme);
56
+ }
57
+ else {
58
+ // After hydration: animated switch
59
+ await themeState.switchTheme(theme, animationOff);
60
+ }
61
+ }, [isHydrated, themeState]);
62
+ const toggleThemeWithHydrationAwareness = useCallback(async (animationOff = false) => {
63
+ if (!isHydrated) {
64
+ const nextTheme = themeState.resolvedTheme === 'dark' ? 'light' : 'dark';
65
+ themeState.setTheme(nextTheme);
66
+ }
67
+ else {
68
+ await themeState.toggleTheme(animationOff);
69
+ }
70
+ }, [isHydrated, themeState]);
71
+ const toggleLightThemeWithHydrationAwareness = useCallback(async (animationOff = false) => {
72
+ if (!isHydrated) {
73
+ themeState.setTheme('light');
74
+ }
75
+ else {
76
+ await themeState.toggleLightTheme(animationOff);
77
+ }
78
+ }, [isHydrated, themeState]);
79
+ const toggleDarkThemeWithHydrationAwareness = useCallback(async (animationOff = false) => {
80
+ if (!isHydrated) {
81
+ themeState.setTheme('dark');
82
+ }
83
+ else {
84
+ await themeState.toggleDarkTheme(animationOff);
85
+ }
86
+ }, [isHydrated, themeState]);
87
+ const switchThemeFromElement = useCallback(async (theme, element) => {
88
+ if (!isHydrated) {
89
+ themeState.setTheme(theme);
90
+ return;
91
+ }
92
+ if (themeState.ref.current) {
93
+ const originalRef = themeState.ref.current;
94
+ Object.defineProperty(themeState.ref, 'current', {
95
+ value: element,
96
+ writable: true,
97
+ configurable: true,
98
+ });
99
+ await themeState.switchTheme(theme);
100
+ Object.defineProperty(themeState.ref, 'current', {
101
+ value: originalRef,
102
+ writable: true,
103
+ configurable: true,
104
+ });
105
+ }
106
+ else {
107
+ Object.defineProperty(themeState.ref, 'current', {
108
+ value: element,
109
+ writable: true,
110
+ configurable: true,
111
+ });
112
+ await themeState.switchTheme(theme);
113
+ }
114
+ }, [isHydrated, themeState]);
115
+ const systemTheme = typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches
116
+ ? 'dark'
117
+ : 'light';
118
+ // During SSR/hydration: provide safe context without animations
119
+ if (!isHydrated) {
120
+ const loadingContextValue = {
121
+ ref: { current: null },
122
+ theme: defaultTheme,
123
+ colorTheme: defaultColorTheme,
124
+ resolvedTheme: defaultTheme === 'dark' ? 'dark' : 'light',
125
+ systemTheme: 'light',
126
+ isHydrated: false,
127
+ setTheme: themeState.setTheme,
128
+ setColorTheme: themeState.setColorTheme,
129
+ switchTheme: async (theme, animationOff = false) => themeState.setTheme(theme),
130
+ switchThemeFromElement: async (theme) => themeState.setTheme(theme),
131
+ switchColorTheme: themeState.switchColorTheme,
132
+ toggleTheme: async (animationOff = false) => {
133
+ const nextTheme = themeState.resolvedTheme === 'dark' ? 'light' : 'dark';
134
+ themeState.setTheme(nextTheme);
135
+ },
136
+ toggleLightTheme: async (animationOff = false) => themeState.setTheme('light'),
137
+ toggleDarkTheme: async (animationOff = false) => themeState.setTheme('dark'),
138
+ toggleColorTheme: themeState.toggleColorTheme,
139
+ createColorThemeToggle: themeState.createColorThemeToggle,
140
+ isColorThemeActive: themeState.isColorThemeActive,
141
+ };
142
+ return (_jsx(TanStackThemeContext.Provider, { value: loadingContextValue, children: children }));
143
+ }
144
+ // After hydration: full context with animations enabled
145
+ const contextValue = {
146
+ ref: themeState.ref,
147
+ theme: themeState.theme,
148
+ colorTheme: themeState.colorTheme,
149
+ resolvedTheme: themeState.resolvedTheme,
150
+ systemTheme,
151
+ isHydrated: true,
152
+ setTheme: themeState.setTheme,
153
+ setColorTheme: themeState.setColorTheme,
154
+ switchTheme: switchThemeWithHydrationAwareness,
155
+ switchThemeFromElement,
156
+ switchColorTheme: themeState.switchColorTheme,
157
+ toggleTheme: toggleThemeWithHydrationAwareness,
158
+ toggleLightTheme: toggleLightThemeWithHydrationAwareness,
159
+ toggleDarkTheme: toggleDarkThemeWithHydrationAwareness,
160
+ toggleColorTheme: themeState.toggleColorTheme,
161
+ createColorThemeToggle: themeState.createColorThemeToggle,
162
+ isColorThemeActive: themeState.isColorThemeActive,
163
+ };
164
+ return (_jsx(TanStackThemeContext.Provider, { value: contextValue, children: children }));
165
+ };
166
+ /**
167
+ * Hook to consume the TanStack Theme context
168
+ *
169
+ * @returns Theme context value with all theme state and methods
170
+ * @throws Error if used outside of TanStackThemeProvider
171
+ */
172
+ export const useTanStackTheme = () => {
173
+ const context = useContext(TanStackThemeContext);
174
+ if (context === undefined) {
175
+ throw new Error('useTanStackTheme must be used within a TanStackThemeProvider');
176
+ }
177
+ return context;
178
+ };
@@ -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 {};