generated from suren-atoyan/react-pwa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
30 lines (22 loc) · 849 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { atom, useRecoilState } from 'recoil';
import { Themes } from '@/theme/types';
import type { AtomEffectParams } from '../types';
import type { Actions } from './types';
const themeModeState = atom({
key: 'theme-mode-state',
default: 'dark' as Themes,
effects: [synchronizeWithLocalStorage],
});
function synchronizeWithLocalStorage({ setSelf, onSet }: AtomEffectParams) {
const storedTheme = localStorage.getItem('theme-mode');
storedTheme && setSelf(storedTheme);
onSet((value: Themes) => localStorage.setItem('theme-mode', value));
}
function useTheme(): [Themes, Actions] {
const [themeMode, setThemeMode] = useRecoilState(themeModeState);
function toggle() {
setThemeMode((mode: Themes) => (mode === Themes.DARK ? Themes.LIGHT : Themes.DARK));
}
return [themeMode, { toggle }];
}
export default useTheme;