feat: close #22
This commit is contained in:
parent
ea01dc435e
commit
cd0103a255
1 changed files with 14 additions and 2 deletions
|
@ -22,13 +22,20 @@ function getStoredThemeSetting(): Theme {
|
||||||
!storedTheme ||
|
!storedTheme ||
|
||||||
(storedTheme != Theme.Dark && storedTheme != Theme.Light)
|
(storedTheme != Theme.Dark && storedTheme != Theme.Light)
|
||||||
) {
|
) {
|
||||||
localStorage.setItem(themeKey, Theme.Dark)
|
setTheme(Theme.Dark)
|
||||||
return Theme.Dark
|
return Theme.Dark
|
||||||
}
|
}
|
||||||
|
|
||||||
return storedTheme
|
return storedTheme
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets theme setting without applying them
|
||||||
|
*/
|
||||||
|
function setTheme(targetTheme: Theme) {
|
||||||
|
localStorage.setItem(themeKey, targetTheme)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies tailwind theme using classes based on current theme setting
|
* Applies tailwind theme using classes based on current theme setting
|
||||||
*/
|
*/
|
||||||
|
@ -43,10 +50,15 @@ function applyTheme() {
|
||||||
export const useTheme = create<ThemeState>()((set) => {
|
export const useTheme = create<ThemeState>()((set) => {
|
||||||
applyTheme()
|
applyTheme()
|
||||||
|
|
||||||
|
addEventListener("storage", () => {
|
||||||
|
setTheme(getStoredThemeSetting())
|
||||||
|
applyTheme()
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
theme: getStoredThemeSetting(),
|
theme: getStoredThemeSetting(),
|
||||||
setTheme: (themeSetting: Theme) => {
|
setTheme: (themeSetting: Theme) => {
|
||||||
localStorage.setItem(themeKey, themeSetting)
|
setTheme(themeSetting)
|
||||||
applyTheme()
|
applyTheme()
|
||||||
set((state) => ({
|
set((state) => ({
|
||||||
...state,
|
...state,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue