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 != Theme.Dark && storedTheme != Theme.Light)
) {
localStorage.setItem(themeKey, Theme.Dark)
setTheme(Theme.Dark)
return Theme.Dark
}
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
*/
@ -43,10 +50,15 @@ function applyTheme() {
export const useTheme = create<ThemeState>()((set) => {
applyTheme()
addEventListener("storage", () => {
setTheme(getStoredThemeSetting())
applyTheme()
})
return {
theme: getStoredThemeSetting(),
setTheme: (themeSetting: Theme) => {
localStorage.setItem(themeKey, themeSetting)
setTheme(themeSetting)
applyTheme()
set((state) => ({
...state,