blob: d9c3d066b2110a88916348ba22b35b6cfb58a489 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import { create } from 'zustand'
export type Theme = 'dark' | 'light'
interface ThemeState {
theme: Theme
setTheme: (theme: Theme) => void
toggleTheme: () => void
}
function getInitialTheme(): Theme {
const stored = localStorage.getItem('yace_theme')
if (stored === 'light' || stored === 'dark') return stored
return 'dark'
}
function applyTheme(theme: Theme) {
document.documentElement.setAttribute('data-theme', theme)
}
const initial = getInitialTheme()
applyTheme(initial)
export const useThemeStore = create<ThemeState>((set) => ({
theme: initial,
setTheme: (theme: Theme) => {
applyTheme(theme)
localStorage.setItem('yace_theme', theme)
set({ theme })
},
toggleTheme: () => {
set((state) => {
const next = state.theme === 'dark' ? 'light' : 'dark'
applyTheme(next)
localStorage.setItem('yace_theme', next)
return { theme: next }
})
},
}))
|