-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.js
35 lines (32 loc) · 1.05 KB
/
theme.js
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
const changeTheme = () => {
const theme = localStorage.getItem('theme');
if (theme && theme.toLowerCase() === 'dark') {
// dark theme was set
localStorage.setItem('theme', 'light');
} else {
// light, no or invalid theme was set
localStorage.setItem('theme', 'dark');
}
updateTheme();
}
const updateTheme = () => {
const theme = localStorage.getItem('theme');
if (theme && theme.toLowerCase() === 'dark') {
// dark theme is set
document.body.classList.remove('light');
document.body.classList.add('dark');
} else {
// light, no or invalid theme is set
document.body.classList.remove('dark');
document.body.classList.add('light');
}
}
const saveUserTheme = () => {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
localStorage.setItem('theme', 'dark');
} else {
localStorage.setItem('theme', 'light');
}
}
if (!localStorage.getItem('theme'))
saveUserTheme();