danieltslz commited on
Commit
df8f829
·
verified ·
1 Parent(s): 5d6671b

Add theme preference controller

Browse files
Files changed (1) hide show
  1. theme.js +67 -0
theme.js ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (() => {
2
+ "use strict";
3
+
4
+ const storageKey = "gene-embeddings-theme";
5
+ const root = document.documentElement;
6
+ const button = document.getElementById("theme-toggle");
7
+
8
+ if (!button) {
9
+ return;
10
+ }
11
+
12
+ function readSavedTheme() {
13
+ try {
14
+ return window.localStorage.getItem(storageKey);
15
+ } catch {
16
+ return null;
17
+ }
18
+ }
19
+
20
+ function saveTheme(theme) {
21
+ try {
22
+ window.localStorage.setItem(storageKey, theme);
23
+ } catch {
24
+ // The toggle still works when browser storage is unavailable.
25
+ }
26
+ }
27
+
28
+ function applyTheme(theme) {
29
+ const night = theme === "night";
30
+
31
+ root.dataset.theme = night ? "night" : "day";
32
+
33
+ button.textContent = night
34
+ ? "☀️ Day"
35
+ : "🌙 Night";
36
+
37
+ button.setAttribute(
38
+ "aria-pressed",
39
+ String(night),
40
+ );
41
+
42
+ button.setAttribute(
43
+ "aria-label",
44
+ night
45
+ ? "Switch to day mode"
46
+ : "Switch to night mode",
47
+ );
48
+ }
49
+
50
+ const savedTheme = readSavedTheme();
51
+
52
+ applyTheme(
53
+ savedTheme === "night"
54
+ ? "night"
55
+ : "day",
56
+ );
57
+
58
+ button.addEventListener("click", () => {
59
+ const nextTheme =
60
+ root.dataset.theme === "night"
61
+ ? "day"
62
+ : "night";
63
+
64
+ applyTheme(nextTheme);
65
+ saveTheme(nextTheme);
66
+ });
67
+ })();