-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathlight-dark-mode.js
More file actions
72 lines (70 loc) · 2.21 KB
/
light-dark-mode.js
File metadata and controls
72 lines (70 loc) · 2.21 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(function(){
body = document.querySelector('body')
let p = document.querySelectorAll('p')
let input = document.querySelectorAll('pre')
let btn = document.querySelector('#modes')
let h2 = document.querySelectorAll('h2');
let card = document.querySelector('.card')
let modeStauts = document.querySelector('#off-on');
const darkmode = () =>{
switch (modeStauts.innerText) {
case 'OFF':
modeStauts.innerText = 'ON'
break;
case 'ON':
modeStauts.innerText = 'OFF'
break;
default:
break;
}
// common of all pages
body.classList.toggle('dark');
p.forEach(element => {
element.classList.toggle('dark-content')
});
input.forEach(element => {
element.classList.toggle('dark-content')
})
h2.forEach(element =>{
element.classList.toggle('h-dark')
})
// For index page
if (window.location.href.match(/index$/)){
card.classList.toggle('card-dark');
}
// For Documentation Page and Changelog Page
if(window.location.href.match(/documentation$/) || window.location.href.match(/changelog$/)){
let li = document.querySelectorAll('.doc-ul > li')
// The .doc-ul class is also added to the 'ul's of changelog page
li.forEach(element => {
element.classList.toggle('dark-content')
});
}
// For stats page
if(window.location.href.match(/stats$/)){
let span = document.querySelectorAll('span');
span.forEach(element => {
element.classList.toggle('dark')
})
}
}
btn.addEventListener('click', ()=>{
// get value of 'dark' item form localstorage on every click
setDarkMode = localStorage.getItem('dark');
if(setDarkMode !== 'ON'){
darkmode();
// set vlaue to 'ON' of dark node is on
setDarkMode = localStorage.setItem('dark', 'ON')
}else{
darkmode();
// set vlaues to 'null' of dark mode is off
setDarkMode = localStorage.setItem('dark', null)
}
})
// get vlaue of 'dark' item form local storage;
let setDarkMode = localStorage.getItem('dark')
// cheak mode 'on-off' on page reload;
if(setDarkMode === 'ON'){
darkmode()
}
})();