-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
35 lines (32 loc) · 1.03 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
// define variables to hold the classname for the 2 themes
// define variable to hold reference to the switch button
let theme_dark = 'theme-dark';
let theme_light = 'theme-light';
let theme_switch = document.getElementById("theme-switch");
// function to set to a given theme/color-scheme
function setTheme(themeName){
console.log("setting theme to ", themeName);
localStorage.setItem('theme', themeName);
document.documentElement.className = themeName;
if (themeName === theme_dark){
theme_switch.innerHTML = "Switch ☀"
} else {
theme_switch.innerHTML = "Switch 🌙"
}
}
// function to toggle between light and dark theme
function toggleTheme(){
if (localStorage.getItem('theme') === theme_dark){
setTheme(theme_light);
} else {
setTheme(theme_dark);
}
}
// Immediately invoke the function to set the theme on initial load
(function (){
if (localStorage.getItem('theme') === theme_dark){
setTheme(theme_dark);
} else {
setTheme(theme_light);
}
})();