~ruther/blog

ref: 1e4dd2e3a3a3029087f7d501e0715d4756ff8a78 blog/themes/poison/assets/js/light_dark.js -rw-r--r-- 1.2 KiB
1e4dd2e3 — Rutherther chore: add rsync 3 months ago
                                                                                
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
const btn = document.querySelector(".btn-light-dark");
const moon = document.querySelector(".moon");
const sun = document.querySelector(".sun");

const themeFromLS = localStorage.getItem("theme")
const themeFromHugo = document.body.classList.contains("dark-theme") ? "dark" : null
const currentTheme = themeFromLS ? themeFromLS : themeFromHugo;

if (currentTheme == "dark") {
    document.body.classList.add("dark-theme");
    moon.style.display = 'none';
    sun.style.display = 'block';
} else {
    document.body.classList.remove("dark-theme");
    moon.style.display = 'block';
    sun.style.display = 'none';
}

btn.addEventListener("click", function () {
    document.body.classList.toggle("dark-theme");
    let hasComments = document.getElementById("remark42");
    let theme = "light";

    if (document.body.classList.contains("dark-theme")) {
        theme = "dark";
        moon.style.display = 'none';
        sun.style.display = 'block';
        if (hasComments) {
            window.REMARK42.changeTheme("dark");
        }
    } else {
        moon.style.display = 'block';
        sun.style.display = 'none';
        if (hasComments) {
            window.REMARK42.changeTheme("light");
        }
    }
    localStorage.setItem("theme", theme);
});
Do not follow this link