fix: save the dark mode in local storage

This commit is contained in:
qinluhe 2024-07-03 14:56:43 +08:00
parent b2d6876347
commit 1b69f7841c

View File

@ -9,7 +9,11 @@ export const ThemeModeContext = createContext<
>(undefined);
export function useAppThemeMode() {
const [isDark, setIsDark] = useState<boolean>(false);
const [isDark, setIsDark] = useState<boolean>(() => {
const darkMode = localStorage.getItem('dark-mode');
return darkMode === 'true';
});
useEffect(() => {
function detectColorScheme() {
@ -28,6 +32,7 @@ export function useAppThemeMode() {
useEffect(() => {
document.documentElement.setAttribute('data-dark-mode', isDark ? 'true' : 'false');
localStorage.setItem('dark-mode', isDark ? 'true' : 'false');
}, [isDark]);
return {