summaryrefslogtreecommitdiff
path: root/src/js/theme.js
blob: 687574ea12cf69dc25cf6ade0f0257eee97476c5 (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
window.addEventListener('DOMContentLoaded', () => {
  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 harr = document.querySelector('#harr');

  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 (!harr.classList.replace(...arg)) {
      harr.classList.add(theme);
    }

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

    isDark = !isDark;
  }

  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');
  }
});