Persisting Theme Preference

typescript
1import { useTheme } from '@flint-ui/core'
2import { lightTheme, darkTheme } from '@flint-ui/themes'
3
4function usePersistedTheme() {
5 const { setTheme } = useTheme()
6
7 useEffect(() => {
8 const saved = localStorage.getItem('flint-theme')
9 if (saved === 'dark') setTheme(darkTheme)
10 if (saved === 'light') setTheme(lightTheme)
11 }, [])
12
13 const persistTheme = (theme: 'light' | 'dark') => {
14 localStorage.setItem('flint-theme', theme)
15 setTheme(theme === 'dark' ? darkTheme : lightTheme)
16 }
17
18 return { persistTheme }
19}

localStorage wrapper for persisting theme choice