|
| 1 | +'use client' |
| 2 | + |
| 3 | +import React, { createContext, useContext, useEffect, useState, useCallback } from 'react' |
| 4 | + |
| 5 | +type Theme = 'light' | 'dark' |
| 6 | + |
| 7 | +interface ThemeContextType { |
| 8 | + theme: Theme |
| 9 | + toggleTheme: () => void |
| 10 | +} |
| 11 | + |
| 12 | +const ThemeContext = createContext<ThemeContextType | undefined>(undefined) |
| 13 | + |
| 14 | +const THEME_STORAGE_KEY = 'ceo-theme' |
| 15 | + |
| 16 | +export function ThemeProvider({ children }: { children: React.ReactNode }) { |
| 17 | + const [theme, setTheme] = useState<Theme>('dark') |
| 18 | + const [mounted, setMounted] = useState(false) |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + setMounted(true) |
| 22 | + |
| 23 | + // Check localStorage first, then system preference |
| 24 | + const savedTheme = localStorage.getItem(THEME_STORAGE_KEY) as Theme | null |
| 25 | + |
| 26 | + if (savedTheme === 'light' || savedTheme === 'dark') { |
| 27 | + setTheme(savedTheme) |
| 28 | + } else { |
| 29 | + // Check system preference |
| 30 | + const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches |
| 31 | + setTheme(prefersDark ? 'dark' : 'light') |
| 32 | + } |
| 33 | + }, []) |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + if (!mounted) return |
| 37 | + |
| 38 | + const root = document.documentElement |
| 39 | + |
| 40 | + if (theme === 'dark') { |
| 41 | + root.classList.add('dark') |
| 42 | + root.classList.remove('light') |
| 43 | + } else { |
| 44 | + root.classList.remove('dark') |
| 45 | + root.classList.add('light') |
| 46 | + } |
| 47 | + |
| 48 | + localStorage.setItem(THEME_STORAGE_KEY, theme) |
| 49 | + }, [theme, mounted]) |
| 50 | + |
| 51 | + // Listen for system preference changes |
| 52 | + useEffect(() => { |
| 53 | + if (!mounted) return |
| 54 | + |
| 55 | + const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)') |
| 56 | + |
| 57 | + const handleChange = (e: MediaQueryListEvent) => { |
| 58 | + // Only update if user hasn't explicitly set a preference |
| 59 | + const savedTheme = localStorage.getItem(THEME_STORAGE_KEY) |
| 60 | + if (!savedTheme) { |
| 61 | + setTheme(e.matches ? 'dark' : 'light') |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + mediaQuery.addEventListener('change', handleChange) |
| 66 | + return () => mediaQuery.removeEventListener('change', handleChange) |
| 67 | + }, [mounted]) |
| 68 | + |
| 69 | + const toggleTheme = useCallback(() => { |
| 70 | + setTheme((prev) => (prev === 'dark' ? 'light' : 'dark')) |
| 71 | + }, []) |
| 72 | + |
| 73 | + // Prevent hydration mismatch by not rendering until mounted |
| 74 | + if (!mounted) { |
| 75 | + return <>{children}</> |
| 76 | + } |
| 77 | + |
| 78 | + return ( |
| 79 | + <ThemeContext.Provider value={{ theme, toggleTheme }}> |
| 80 | + {children} |
| 81 | + </ThemeContext.Provider> |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +export function useTheme() { |
| 86 | + const context = useContext(ThemeContext) |
| 87 | + if (context === undefined) { |
| 88 | + throw new Error('useTheme must be used within a ThemeProvider') |
| 89 | + } |
| 90 | + return context |
| 91 | +} |
0 commit comments