-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.js
122 lines (102 loc) · 3.41 KB
/
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
118
119
120
121
122
/**
* Light Switch @version v0.1.4
*/
(function () {
let lightSwitch = document.getElementById('lightSwitch');
if (!lightSwitch) {
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.querySelectorAll('.bg-light').forEach((element) => {
element.className = element.className.replace(/-light/g, '-dark');
});
document.querySelectorAll('.link-dark').forEach((element) => {
element.className = element.className.replace(/link-dark/, 'text-white');
});
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 (!lightSwitch.checked) {
lightSwitch.checked = true;
}
localStorage.setItem('lightSwitch', 'dark');
}
/**
* @function lightmode
* @summary: changes the theme to 'light mode' and save settings to local stroage.
*/
function lightMode() {
document.querySelectorAll('.bg-dark').forEach((element) => {
element.className = element.className.replace(/-dark/g, '-light');
});
document.querySelectorAll('.text-white').forEach((element) => {
element.className = element.className.replace(/text-white/, 'link-dark');
});
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 (lightSwitch.checked) {
lightSwitch.checked = false;
}
localStorage.setItem('lightSwitch', 'light');
}
/**
* @function onToggleMode
* @summary: the event handler attached to the switch. calling @darkMode or @lightMode depending on the checked state.
*/
function onToggleMode() {
if (lightSwitch.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('lightSwitch');
if (settings == null) {
settings = getSystemDefaultTheme();
}
if (settings == 'dark') {
lightSwitch.checked = true;
}
lightSwitch.addEventListener('change', onToggleMode);
onToggleMode();
}
setup();
})();