Skip to content

Commit

Permalink
Merge branch 'release/2.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
D3strukt0r committed May 3, 2024
2 parents 51d3971 + 2aa8944 commit 6e168a6
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions pwa/src/hooks/useDarkMode.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import { useEffect, useState } from 'react';

const useLocalStorage = (key: string, initialValue?: any) => {
const [storedValue, setStorageValue] = useState(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.log(error);
return initialValue;
}
});

const setValue = (value: any) => {
try {
const valueToStore = value instanceof Function ? value(storedValue) : value;

setStorageValue(valueToStore);

window.localStorage.setItem(key, JSON.stringify(valueToStore));
} catch (error) {
console.log(error);
}
};

return [storedValue, setValue];
const [storedValue, setStorageValue] = useState(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.log(error);
return initialValue;
}
});

const setValue = (value: any) => {
try {
const valueToStore = value instanceof Function ? value(storedValue) : value;

setStorageValue(valueToStore);

window.localStorage.setItem(key, JSON.stringify(valueToStore));
} catch (error) {
console.log(error);
}
};

return [storedValue, setValue];
};

const useDarkMode = () => {
const [enabled, setEnabled] = useLocalStorage('dark-theme');
// const isEnabled = typeof enabledState === 'undefined' && enabled;
const isEnabled = enabled;
const [enabled, setEnabled] = useLocalStorage('dark-theme');
// const isEnabled = typeof enabledState === 'undefined' && enabled;
const isEnabled = enabled;

useEffect(() => {
const className = 'dark';
const bodyClass = window.document.body.classList;
useEffect(() => {
const className = 'dark';
const bodyClass = window.document.body.classList;

isEnabled ? bodyClass.add(className) : bodyClass.remove(className);
}, [enabled, isEnabled]);
isEnabled ? bodyClass.add(className) : bodyClass.remove(className);
}, [enabled, isEnabled]);

return [enabled, setEnabled];
return [enabled, setEnabled];
};

export default useDarkMode;

0 comments on commit 6e168a6

Please sign in to comment.