diff --git a/packages/use-theme/src/use-theme.ts b/packages/use-theme/src/use-theme.ts index c6ce2c7c..5d3ae2a0 100644 --- a/packages/use-theme/src/use-theme.ts +++ b/packages/use-theme/src/use-theme.ts @@ -49,6 +49,8 @@ export function useTheme(options?: UseThemeOptions): UseThemeReturn { // Sync across tabs via storage event useEffect(() => { + if (typeof window === 'undefined') return; + function handleStorage(event: StorageEvent) { if (event.key !== storageKey) { return; @@ -65,7 +67,9 @@ export function useTheme(options?: UseThemeOptions): UseThemeReturn { const setTheme = useCallback( (t: Theme) => { setThemeState(t); - window.localStorage.setItem(storageKey, t); + if (typeof window !== 'undefined') { + window.localStorage.setItem(storageKey, t); + } }, [storageKey] ); @@ -73,7 +77,9 @@ export function useTheme(options?: UseThemeOptions): UseThemeReturn { const toggleTheme = useCallback(() => { setThemeState(prev => { const next: Theme = prev === 'dark' ? 'light' : 'dark'; - window.localStorage.setItem(storageKey, next); + if (typeof window !== 'undefined') { + window.localStorage.setItem(storageKey, next); + } applyTheme(next, attribute); return next; });