-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
50 lines (44 loc) · 1.48 KB
/
main.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
43
44
45
46
47
48
49
50
/* Body */
const body = document.querySelector('body');
// Main
const main = document.querySelector('main');
// Dark Mode Action
// checks if dark mode has previously been activated
let darkMode = localStorage.getItem('darkMode');
const darkModeToggle = document.querySelector('.dark-mode-button');
// Enable Dark Mode by adding styling classes to the appropriate DOM elements
const enableDarkMode = () => {
body.classList.add('dark-mode');
if (localStorage.darkMode != 'enabled') {
main.classList.add('dark-main-transition');
body.classList.add('dark-body-transition');
} else {
main.classList.remove('dark-main-transition');
body.classList.remove('dark-body-transition');
main.classList.add('dark-main-no-transition');
}
localStorage.setItem('darkMode', 'enabled');
};
// Disable Dark Mode
const disableDarkMode = () => {
body.classList.remove('dark-mode');
body.classList.remove('dark-body-transition');
main.classList.remove('dark-main-transition');
main.classList.remove('dark-main-no-transition');
body.classList.add('normal-body-transition');
main.classList.add('normal-main-transition');
localStorage.setItem('darkMode', null);
};
// if dark mode is activated in storage, run enableDarkMode function
if (darkMode == 'enabled') {
enableDarkMode();
}
// Desktop Button
darkModeToggle.addEventListener('click', () => {
darkMode = localStorage.getItem('darkMode');
if (darkMode !== 'enabled') {
enableDarkMode();
} else {
disableDarkMode();
}
});