-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.js
45 lines (37 loc) · 1.05 KB
/
theme.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
const storageTheme = "mb_theme";
const themeButton = document.getElementById("theme");
function setDarkTheme() {
const head = document.head;
const link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = 'dark.css';
head.appendChild(link);
themeButton.innerHTML = "Light";
window.localStorage.setItem(storageTheme, "dark");
}
function setLightTheme() {
const head = document.head;
for (el of head.getElementsByTagName("link")) {
if (el.href.indexOf("dark.css") > -1) {
el.remove();
}
}
themeButton.innerHTML = "Dark";
window.localStorage.setItem(storageTheme, "light");
}
function getCurrentTheme() {
return window.localStorage.getItem(storageTheme) || 'light';
}
function toggleTheme() {
getCurrentTheme() === 'light' ? setDarkTheme() : setLightTheme();
}
if (window.localStorage.getItem(storageTheme) === 'dark') {
setDarkTheme();
} else {
setLightTheme();
}
themeButton.onclick = function() {
toggleTheme();
hideMenu();
}