-
Notifications
You must be signed in to change notification settings - Fork 2
/
dark-mode-switch.js
117 lines (100 loc) · 3.2 KB
/
dark-mode-switch.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Light Switch @version v0.1.4
*/
(function () {
let inputSwicher = document.getElementById('inputSwicher');
if (!inputSwicher) {
return;
}
/**
* @function darkmode
* @summary: changes the theme to 'dark mode' and save settings to local stroage.
* Basically, replaces/toggles every CSS class that has '-light' class with '-dark'
*/
function darkMode() {
document.querySelector('#formSwitch .light-mode').classList.add('d-none');
document.querySelector('#formSwitch .dark-mode').classList.remove('d-none');
document.querySelectorAll('.bg-light').forEach((element) => {
element.className = element.className.replace(/-light/g, '-dark');
});
document.body.classList.add('bg-dark');
if (document.body.classList.contains('text-dark')) {
document.body.classList.replace('text-dark', 'text-light');
} else {
document.body.classList.add('text-light');
}
// Tables
var tables = document.querySelectorAll('table');
for (var i = 0; i < tables.length; i++) {
// add table-dark class to each table
tables[i].classList.add('table-dark');
}
// set light switch input to true
if (!inputSwicher.checked) {
inputSwicher.checked = true;
}
localStorage.setItem('inputSwicher', 'dark');
}
/**
* @function lightmode
* @summary: changes the theme to 'light mode' and save settings to local stroage.
*/
function lightMode() {
document.querySelector('#formSwitch .light-mode').classList.remove('d-none');
document.querySelector('#formSwitch .dark-mode').classList.add('d-none');
document.querySelectorAll('.bg-dark').forEach((element) => {
element.className = element.className.replace(/-dark/g, '-light');
});
document.body.classList.add('bg-light');
if (document.body.classList.contains('text-light')) {
document.body.classList.replace('text-light', 'text-dark');
} else {
document.body.classList.add('text-dark');
}
// Tables
var tables = document.querySelectorAll('table');
for (var i = 0; i < tables.length; i++) {
if (tables[i].classList.contains('table-dark')) {
tables[i].classList.remove('table-dark');
}
}
if (inputSwicher.checked) {
inputSwicher.checked = false;
}
localStorage.setItem('inputSwicher', 'light');
}
/**
* @function onToggleMode
* @summary: the event handler attached to the switch. calling @darkMode or @lightMode depending on the checked state.
*/
function onToggleMode() {
if (inputSwicher.checked) {
darkMode();
} else {
lightMode();
}
}
/**
* @function getSystemDefaultTheme
* @summary: get system default theme by media query
*/
function getSystemDefaultTheme() {
const darkThemeMq = window.matchMedia('(prefers-color-scheme: dark)');
if (darkThemeMq.matches) {
return 'dark';
}
return 'light';
}
function setup() {
var settings = localStorage.getItem('inputSwicher');
if (settings == null) {
settings = getSystemDefaultTheme();
}
if (settings == 'dark') {
inputSwicher.checked = true;
}
inputSwicher.addEventListener('change', onToggleMode);
onToggleMode();
}
setup();
})();