-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdark.js
41 lines (35 loc) · 1.15 KB
/
dark.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
36
37
38
39
40
41
const toggeleSwitch = document.querySelector('input[type="checkbox"]');
const body = document.querySelector('.body');
const toggleIcon = document.querySelector('.toggle-icon');
const iconSvg = document.querySelector('.icon');
const svgMode = color => {
iconSvg.src = `images/icon-${color}.svg`;
};
const toggleLightandDarkMode = isDark => {
body.style.background = isDark ? 'light' : 'dark';
isDark ? svgMode('dark') : svgMode('light');
};
const switchTheme = e => {
e.preventDefault();
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light');
toggleLightandDarkMode(true);
} else {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
toggleLightandDarkMode(false);
}
};
toggeleSwitch.addEventListener('change', switchTheme);
const current = localStorage.getItem('theme');
const currentTheme = () => {
if (current) {
document.documentElement.setAttribute('data-theme', current);
if (current === 'light') {
toggeleSwitch.checked = true;
toggleLightandDarkMode(true);
}
}
};
currentTheme();