Files
DefectingCat.github.io/lib/hooks/useDarkMode.ts
DefectingCat 7c21f3c607 Add dark mode state to global state.
* update mobile menu icon dark color
* update post page dark mode
2022-01-19 17:38:02 +08:00

28 lines
671 B
TypeScript

import { ActionKind, useRUAContext } from '../store';
const useDarkMode = () => {
const { state, dispatch } = useRUAContext();
const { isDark } = state;
const toggleDark = () => {
if (isDark) {
document.documentElement.classList.remove('dark');
localStorage.setItem('rua-theme', 'light');
dispatch({
type: ActionKind.SETTHEME,
payload: '',
});
} else {
document.documentElement.classList.add('dark');
localStorage.setItem('rua-theme', 'dark');
dispatch({
type: ActionKind.SETTHEME,
payload: '',
});
}
};
return { isDark, toggleDark };
};
export default useDarkMode;