-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathuseThemeSwitcher.tsx
More file actions
36 lines (32 loc) · 1013 Bytes
/
Copy pathuseThemeSwitcher.tsx
File metadata and controls
36 lines (32 loc) · 1013 Bytes
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
31
32
33
34
35
36
import { useCallback } from 'react';
import { useStore } from './useStore';
const useThemeSwitcher = () => {
const { ui } = useStore() ?? {
ui: {
setDarkMode: () => {},
is_dark_mode_on: false,
},
};
const { setDarkMode, is_dark_mode_on } = ui;
const toggleTheme = useCallback(() => {
const body = document.querySelector('body');
if (!body) return;
if (body.classList.contains('theme--dark')) {
localStorage.setItem('theme', 'light');
body.classList.remove('theme--dark');
body.classList.add('theme--light');
setDarkMode(false);
} else {
localStorage.setItem('theme', 'dark');
body.classList.remove('theme--light');
body.classList.add('theme--dark');
setDarkMode(true);
}
}, [setDarkMode]);
return {
toggleTheme,
is_dark_mode_on,
setDarkMode,
};
};
export default useThemeSwitcher;