Skip to content

Commit

Permalink
refactor(hooks): resolve pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wingkwong committed Oct 3, 2024
1 parent fdc1f92 commit a87ad5f
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions packages/hooks/use-theme/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,20 @@ export function useTheme(defaultTheme: Theme = ThemeProps.SYSTEM) {

const setTheme = useCallback(
(newTheme: Theme) => {
try {
const targetTheme =
newTheme === ThemeProps.SYSTEM
? window.matchMedia?.(MEDIA).matches
? ThemeProps.DARK
: ThemeProps.LIGHT
: newTheme;

localStorage.setItem(ThemeProps.KEY, targetTheme);

document.documentElement.classList.remove(theme);
document.documentElement.classList.add(targetTheme);
setThemeState(newTheme);
} catch {}
const targetTheme =
newTheme === ThemeProps.SYSTEM
? window.matchMedia?.(MEDIA).matches
? ThemeProps.DARK
: ThemeProps.LIGHT
: newTheme;

localStorage.setItem(ThemeProps.KEY, targetTheme);

document.documentElement.classList.remove(theme);
document.documentElement.classList.add(targetTheme);
setThemeState(newTheme);
},
[theme, MEDIA],
[theme],
);

const handleMediaQuery = useCallback(
Expand Down

0 comments on commit a87ad5f

Please sign in to comment.