Fixed darkmode not keeping the user preference

This commit is contained in:
DrMint 2022-09-21 18:12:47 +02:00
parent ee9a9a67fc
commit 2dacf190d2
1 changed files with 3 additions and 6 deletions

View File

@ -1,6 +1,6 @@
import { useEffect } from "react"; import { useEffect } from "react";
import { useLocalStorage } from "usehooks-ts";
import { usePrefersDarkMode } from "./useMediaQuery"; import { usePrefersDarkMode } from "./useMediaQuery";
import { useStateWithLocalStorage } from "./useStateWithLocalStorage";
export const useDarkMode = ( export const useDarkMode = (
key: string, key: string,
@ -11,12 +11,9 @@ export const useDarkMode = (
React.Dispatch<React.SetStateAction<boolean>>, React.Dispatch<React.SetStateAction<boolean>>,
React.Dispatch<React.SetStateAction<boolean>> React.Dispatch<React.SetStateAction<boolean>>
] => { ] => {
const [darkMode, setDarkMode] = useStateWithLocalStorage(key, initialValue); const [darkMode, setDarkMode] = useLocalStorage(key, initialValue);
const prefersDarkMode = usePrefersDarkMode(); const prefersDarkMode = usePrefersDarkMode();
const [selectedThemeMode, setSelectedThemeMode] = useStateWithLocalStorage( const [selectedThemeMode, setSelectedThemeMode] = useLocalStorage("selectedThemeMode", false);
"selectedThemeMode",
false
);
useEffect(() => { useEffect(() => {
if (!selectedThemeMode) setDarkMode(prefersDarkMode); if (!selectedThemeMode) setDarkMode(prefersDarkMode);