feat: close #22

This commit is contained in:
Kim, Jimin 2023-07-10 20:49:25 +09:00
parent ea01dc435e
commit cd0103a255

View file

@ -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,