-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme-toggle.js
109 lines (91 loc) · 2.96 KB
/
theme-toggle.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class ThemeToggle extends HTMLElement {
static tagName = "theme-toggle";
static register(
tagName = this.tagName,
registry = globalThis.customElements
) {
registry.define(tagName, this);
}
static attr = {
storage: "storage",
};
connectedCallback() {
if (this.shadowRoot) return;
this.attachShadow({ mode: "open" }).appendChild(
document.createElement("slot")
);
this.button = this.querySelector("button");
this.metaColorScheme = document.querySelector('meta[name="color-scheme"]');
this.prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)");
this.mode = this.getAttribute("mode") || "toggle";
this.currentTheme = this.storage.getItem("theme") || "auto";
this.setTheme();
this.addEventListeners();
}
get storage() {
const storageValue = this.getAttribute(ThemeToggle.attr.storage);
return storageValue === "local" ? localStorage : sessionStorage;
}
getPreferredTheme() {
return this.prefersDarkScheme.matches ? "dark" : "light";
}
setTheme() {
if (this.currentTheme === "auto") {
this.metaColorScheme.setAttribute("content", "light dark");
document.documentElement.style.colorScheme = "";
} else {
this.metaColorScheme.setAttribute("content", this.currentTheme);
document.documentElement.style.colorScheme = this.currentTheme;
}
this.updateIcons();
}
updateIcons() {
const lightIcon = this.querySelector(".light");
const darkIcon = this.querySelector(".dark");
const autoIcon = this.querySelector(".auto");
if (this.currentTheme === "light") {
lightIcon.style.display = "inline";
darkIcon.style.display = "none";
autoIcon.style.display = "none";
} else if (this.currentTheme === "dark") {
lightIcon.style.display = "none";
darkIcon.style.display = "inline";
autoIcon.style.display = "none";
} else {
lightIcon.style.display = "none";
darkIcon.style.display = "none";
autoIcon.style.display = "inline";
}
}
toggleCycle() {
if (this.currentTheme === "auto") {
this.currentTheme = "light";
} else if (this.currentTheme === "light") {
this.currentTheme = "dark";
} else {
this.currentTheme = "auto";
}
if (this.currentTheme !== "auto") {
this.storage.setItem("theme", this.currentTheme);
} else {
this.storage.removeItem("theme");
}
this.setTheme();
}
onPreferredColorSchemeChange(event) {
if (this.currentTheme === "auto") {
const preferredTheme = event.matches ? "dark" : "light";
document.documentElement.style.colorScheme = preferredTheme;
this.metaColorScheme.setAttribute("content", "light dark");
}
}
addEventListeners() {
if (this.button) {
this.button.addEventListener("click", () => this.toggleCycle());
}
this.prefersDarkScheme.addEventListener("change", (event) =>
this.onPreferredColorSchemeChange(event)
);
}
}
ThemeToggle.register();