aboutsummaryrefslogtreecommitdiff
path: root/dst/scripts/darktheme.js
blob: b44db9ab8fd13abaa6672fe7916bb8757ef28c29 (plain)
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
const themeButton = document.getElementById('darkmodetoggle');
const theme = localStorage.getItem("theme");
var isDark = false;

if (theme) {
    setTheme(theme === "dark");
} else if (window.matchMedia
    && window.matchMedia("(prefers-color-scheme: dark)").matches) {
    setTheme(true);
}

function setTheme(dark) {
    if (dark) {
        document.body.classList.add("dark-mode");
        themeButton.className = "fa fa-sun-o";
        localStorage.setItem("theme", "dark");
    } else {
        document.body.classList.remove("dark-mode");
        themeButton.className = "fa fa-moon-o";
        localStorage.setItem("theme", "light");
    }
    isDark = dark;
}

function switchTheme() {
    setTheme(!isDark);
}