Fixed darkmode not keeping the user preference
This commit is contained in:
parent
ee9a9a67fc
commit
2dacf190d2
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue