Jotley / website · jotley.ai / src / contexts / ThemeContext.tsx
| 1 | 'use client' |
| 2 | |
| 3 | import { createContext, useContext, useEffect, useState, ReactNode } from 'react' |
| 4 | |
| 5 | type Theme = 'light' | 'dark' | 'system' |
| 6 | |
| 7 | interface ThemeContextType { |
| 8 | theme: Theme |
| 9 | setTheme: (theme: Theme) => void |
| 10 | resolvedTheme: 'light' | 'dark' |
| 11 | } |
| 12 | |
| 13 | const ThemeContext = createContext<ThemeContextType | undefined>(undefined) |
| 14 | const STORAGE_KEY = 'theme' |
| 15 | |
| 16 | export function ThemeProvider({ children }: { children: ReactNode }) { |
| 17 | const [mounted, setMounted] = useState(false) |
| 18 | const [theme, setTheme] = useState<Theme>('light') |
| 19 | const [resolvedTheme, setResolvedTheme] = useState<'light' | 'dark'>('light') |
| 20 | |
| 21 | useEffect(() => { |
| 22 | setMounted(true) |
| 23 | // Get saved theme from localStorage |
| 24 | try { |
| 25 | const savedTheme = localStorage.getItem(STORAGE_KEY) as Theme | null |
| 26 | if (savedTheme) { |
| 27 | setTheme(savedTheme) |
| 28 | return |
| 29 | } |
| 30 | } catch { |
| 31 | // Ignore storage access errors; the bright theme is the default |
| 32 | } |
| 33 | }, []) |
| 34 | |
| 35 | useEffect(() => { |
| 36 | if (!mounted) return |
| 37 | |
| 38 | const root = window.document.documentElement |
| 39 | |
| 40 | const applyTheme = (theme: Theme) => { |
| 41 | let effectiveTheme: 'light' | 'dark' |
| 42 | |
| 43 | if (theme === 'system') { |
| 44 | effectiveTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' |
| 45 | } else { |
| 46 | effectiveTheme = theme |
| 47 | } |
| 48 | |
| 49 | root.classList.remove('light', 'dark') |
| 50 | root.classList.add(effectiveTheme) |
| 51 | root.dataset.theme = effectiveTheme |
| 52 | root.style.colorScheme = effectiveTheme |
| 53 | setResolvedTheme(effectiveTheme) |
| 54 | } |
| 55 | |
| 56 | applyTheme(theme) |
| 57 | |
| 58 | // Save theme to localStorage |
| 59 | try { |
| 60 | localStorage.setItem(STORAGE_KEY, theme) |
| 61 | } catch { |
| 62 | // Ignore storage access errors |
| 63 | } |
| 64 | |
| 65 | // Listen for system theme changes |
| 66 | if (theme === 'system') { |
| 67 | const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)') |
| 68 | const handleChange = () => applyTheme('system') |
| 69 | mediaQuery.addEventListener('change', handleChange) |
| 70 | return () => mediaQuery.removeEventListener('change', handleChange) |
| 71 | } |
| 72 | }, [theme, mounted]) |
| 73 | |
| 74 | // Prevent flash during SSR |
| 75 | if (!mounted) { |
| 76 | return ( |
| 77 | <ThemeContext.Provider value={{ theme: 'light', setTheme: () => {}, resolvedTheme: 'light' }}> |
| 78 | {children} |
| 79 | </ThemeContext.Provider> |
| 80 | ) |
| 81 | } |
| 82 | |
| 83 | return ( |
| 84 | <ThemeContext.Provider value={{ theme, setTheme, resolvedTheme }}> |
| 85 | {children} |
| 86 | </ThemeContext.Provider> |
| 87 | ) |
| 88 | } |
| 89 | |
| 90 | export function useTheme() { |
| 91 | const context = useContext(ThemeContext) |
| 92 | if (context === undefined) { |
| 93 | throw new Error('useTheme must be used within a ThemeProvider') |
| 94 | } |
| 95 | return context |
| 96 | } |