forked from Code-Social/official-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
darkmode.js
38 lines (26 loc) · 910 Bytes
/
darkmode.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
let darkmode = localStorage.getItem('darkmode');
const themeSwitch = document.getElementById('theme-switch');
const enableDarkMode = () => {
document.body.classList.add('darkmode');
localStorage.setItem('darkmode', 'active');
updateThemeSwitchText();
};
const disableDarkMode = () => {
document.body.classList.remove('darkmode');
localStorage.setItem('darkmode', 'inactive');
updateThemeSwitchText();
};
const updateThemeSwitchText = () => {
themeSwitch.textContent = darkmode === "active" ? "Switch to Light Mode ☀️" : "Switch to Dark Mode 🌙";
};
if (darkmode === "active") {
enableDarkMode();
} else {
disableDarkMode();
}
themeSwitch.addEventListener("click", () => {
darkmode = localStorage.getItem('darkmode');
darkmode === "active" ? disableDarkMode() : enableDarkMode();
});
// Update button text on initial load
updateThemeSwitchText();