summaryrefslogtreecommitdiff
path: root/src/js/theme.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/theme.js')
-rw-r--r--src/js/theme.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/js/theme.js b/src/js/theme.js
new file mode 100644
index 0000000..687574e
--- /dev/null
+++ b/src/js/theme.js
@@ -0,0 +1,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');
+ }
+});