forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-provider.tsx
More file actions
147 lines (120 loc) · 3.91 KB
/
Copy paththeme-provider.tsx
File metadata and controls
147 lines (120 loc) · 3.91 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from 'react'
import { getCookie, setCookie, removeCookie } from '@/lib/cookies'
type Theme = 'dark' | 'light' | 'system'
type ResolvedTheme = Exclude<Theme, 'system'>
const DEFAULT_THEME = 'system'
const THEME_COOKIE_NAME = 'vite-ui-theme'
const THEME_COOKIE_MAX_AGE = 60 * 60 * 24 * 365 // 1 year
const THEMES = new Set<Theme>(['dark', 'light', 'system'])
type ThemeProviderProps = {
children: React.ReactNode
defaultTheme?: Theme
storageKey?: string
}
type ThemeProviderState = {
defaultTheme: Theme
resolvedTheme: ResolvedTheme
theme: Theme
setTheme: (theme: Theme) => void
resetTheme: () => void
}
const initialState: ThemeProviderState = {
defaultTheme: DEFAULT_THEME,
resolvedTheme: 'light',
theme: DEFAULT_THEME,
setTheme: () => null,
resetTheme: () => null,
}
const ThemeContext = createContext<ThemeProviderState>(initialState)
function getSystemTheme(): ResolvedTheme {
if (typeof window === 'undefined') return 'light'
return window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light'
}
function resolveTheme(theme: Theme): ResolvedTheme {
return theme === 'system' ? getSystemTheme() : theme
}
function getStoredTheme(storageKey: string, fallback: Theme): Theme {
const storedTheme = getCookie(storageKey) as Theme | undefined
return storedTheme && THEMES.has(storedTheme) ? storedTheme : fallback
}
export function ThemeProvider({
children,
defaultTheme = DEFAULT_THEME,
storageKey = THEME_COOKIE_NAME,
...props
}: ThemeProviderProps) {
const [theme, _setTheme] = useState<Theme>(() =>
getStoredTheme(storageKey, defaultTheme)
)
const [resolvedTheme, setResolvedTheme] = useState<ResolvedTheme>(() =>
resolveTheme(getStoredTheme(storageKey, defaultTheme))
)
useEffect(() => {
const root = window.document.documentElement
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
const applyTheme = () => {
const nextResolvedTheme = theme === 'system' ? getSystemTheme() : theme
root.classList.remove('light', 'dark')
root.classList.add(nextResolvedTheme)
setResolvedTheme(nextResolvedTheme)
}
applyTheme()
mediaQuery.addEventListener('change', applyTheme)
return () => mediaQuery.removeEventListener('change', applyTheme)
}, [theme])
const setTheme = useCallback(
(theme: Theme) => {
setCookie(storageKey, theme, THEME_COOKIE_MAX_AGE)
_setTheme(theme)
},
[storageKey]
)
const resetTheme = useCallback(() => {
removeCookie(storageKey)
_setTheme(defaultTheme)
}, [defaultTheme, storageKey])
const contextValue = useMemo(
() => ({
defaultTheme,
resolvedTheme,
resetTheme,
theme,
setTheme,
}),
[defaultTheme, resolvedTheme, resetTheme, theme, setTheme]
)
return (
<ThemeContext value={contextValue} {...props}>
{children}
</ThemeContext>
)
}
// eslint-disable-next-line react-refresh/only-export-components
export const useTheme = () => {
const context = useContext(ThemeContext)
if (!context) throw new Error('useTheme must be used within a ThemeProvider')
return context
}