summaryrefslogtreecommitdiff
path: root/src/js/theme.js
blob: a49fc8cfdbd11333d1557f15f06ac610e3b16b16 (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
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
123
124
125
126
window.addEventListener("DOMContentLoaded", () => {
  const storageTheme = window.localStorage?.getItem("theme");
  const preferesDark = window.matchMedia(
    "(prefers-color-scheme: dark)",
  ).matches;

  let isDark = preferesDark;

  const hid = document.querySelector("#hid");
  const html = document.querySelector("html");
  const aside = document.querySelector("#side-bar");
  const sidenotes = document.querySelector(".sidenotes");
  const footnotes = document.querySelector(".footnotes");
  const harr = document.querySelector("#harr");
  const links = document.querySelectorAll("a");
  const likes = document.querySelectorAll(".likes");
  const tables = document.querySelectorAll("table");
  const codes = document.querySelectorAll("p code");

  if (storageTheme === "dark" || storageTheme === "light") {
    const theme = storageTheme;
    isDark = storageTheme === "dark";
    hid.classList.add(theme);
    html.classList.add(theme);
    aside.classList.add(theme);
    sidenotes.classList.add(theme);
    harr.classList.add(theme);

    if (footnotes) {
      footnotes.classList.add(theme);
    }

    for (let link of links) {
      link.classList.add(theme);
    }
    for (let likeBox of likes) {
      likeBox.classList.add(theme);
    }
    for (let table of tables) {
      table.classList.add(theme);
    }
    for (let code of codes) {
      code.classList.add(theme);
    }

    for (const article of document.querySelectorAll("article")) {
      article.classList.add(theme);
    }

    for (const article of document.querySelectorAll("article")) {
      article.classList.add(theme);
    }
  }

  function toogleTheme() {
    const arg = isDark ? ["dark", "light"] : ["light", "dark"];
    const theme = arg[1];

    if (!hid.classList.replace(...arg)) {
      hid.classList.add(theme);
    }

    if (!html.classList.replace(...arg)) {
      html.classList.add(theme);
    }

    if (!aside.classList.replace(...arg)) {
      aside.classList.add(theme);
    }

    if (!sidenotes.classList.replace(...arg)) {
      sidenotes.classList.add(theme);
    }
    if (footnotes && !footnotes.classList.replace(...arg)) {
      footnotes.classList.add(theme);
    }

    if (!harr.classList.replace(...arg)) {
      harr.classList.add(theme);
    }

    for (let link of links) {
      if (!link.classList.replace(...arg)) {
        link.classList.add(theme);
      }
    }
    for (let table of tables) {
      if (!table.classList.replace(...arg)) {
        table.classList.add(theme);
      }
    }
    for (let code of codes) {
      if (!code.classList.replace(...arg)) {
        code.classList.add(theme);
      }
    }
    for (let like of likes) {
      if (!like.classList.replace(...arg)) {
        like.classList.add(theme);
      }
    }

    for (const article of document.querySelectorAll("article")) {
      if (!article.classList.replace(...arg)) {
        article.classList.add(theme);
      }
    }

    isDark = !isDark;
    if (window.localStorage) {
      window.localStorage.setItem("theme", isDark ? "dark" : "light");
    }
  }

  const sun = document.querySelector(".sun");
  const moon = document.querySelector(".moon");

  sun.addEventListener("click", toogleTheme);
  moon.addEventListener("click", toogleTheme);

  if (isDark) {
    hid.classList.add("dark");
  } else {
    hid.classList.add("light");
  }
});