summaryrefslogtreecommitdiff
path: root/js/theme.ts
blob: 45101daca1457397a0b1bf1252c574b08a46050a (plain) (blame)
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
namespace Ach {
	export function toggleTheme() {
		let body = Ach.getFirstElement(document, "body");

		let theme = "light";

		if (body.classList.contains("light")) {
			theme = "dark";
		}

		console.log("setting theme to `" + theme + "`");

		body.classList.toggle("light");
		sessionStorage.setItem("theme", theme);
	}

	export function loadTheme() {
		let theme = sessionStorage.getItem("theme");

		if (!theme) {
			console.log("theme not set, using default");
			theme = defaultTheme();
		}

		switch (theme) {
		case "dark":
			// We assume this theme in our stylesheets.
			break;

		case "light":
			let body = Ach.getFirstElement(document, "body");
			body.classList.add("light");

			break;

		default:
			console.log(`invalid theme \"${theme}\", using default`);
			//theme = DEFAULT_THEME; // Redundant now.

			break;
		}

		console.log(`note: theme is now \`${theme}\``);
	}

	function defaultTheme(): string {
		let hour = new Date().getHours();

		if (hour >= 0x6 && hour <= 0x12) {
			return "light";
		} else {
			return "dark";
		}
	}
}