Blob: theme.js

Blob id: 055f44c5aa63db92915fe9f6f0979a704e746a8a

Size: 913 B

 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
document.addEventListener('DOMContentLoaded', function() {
	const themeButton = document.getElementById("theme-button");
	loadTheme(themeButton);
	themeButton.addEventListener('click', () => {
		if (theme === "dark") {
			setTheme("light", themeButton)
			theme = "light"
		} else {
			setTheme("dark", themeButton)
			theme = "dark"
		}
	});
})
const html = document.documentElement;

let theme
const setTheme = (theme, btn) => {
   document.documentElement.removeAttribute('data-theme');

   if (theme === 'dark') {
	   html.setAttribute('data-theme', 'dark');
	   btn.innerHTML = "<img src='/static/aurinko.svg' class='light' />"
	} else {
		btn.innerHTML = "<img src='/static/kuu.svg' class='dark' />"
	}
	
	localStorage.setItem('theme', theme);
};

const loadTheme = (btn) => {
  const savedTheme = localStorage.getItem('theme');
  theme = savedTheme
  if (savedTheme) {
	  setTheme(savedTheme, btn);
	}
};