-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathdark.js
42 lines (34 loc) · 1.08 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
42
//
// The Dark Mode System
//
/* Body and Toggle */
var body = document.querySelector("body");
const darkModeToggle = document.querySelector("#dark-mode-toggle");
// Get Status of Dark Mode
let darkMode = localStorage.getItem("darkMode");
// Enable Dark Mode
const enableDarkMode = () => {
body.classList.add("dark-mode");
localStorage.setItem("darkMode", "enabled")
}
// Disable Dark mode
const disableDarkMode = () => {
body.classList.remove("dark-mode");
localStorage.setItem("darkMode", null)
}
// Check the state of dark mode in local storage
if (darkMode == "enabled") {
enableDarkMode();
}
// add event listener to the dark mode button toggle
darkModeToggle.addEventListener('click', () => {
// on click, check localstorage for the dark mode value
darkMode = localStorage.getItem("darkMode");
if (darkMode !== "enabled") {
// if dark mode is not enabled, run this function to set it to enabled
enableDarkMode();
} else {
// if dark mode is enabled, run this function to set it to disabled
disableDarkMode();
}
})