-
Notifications
You must be signed in to change notification settings - Fork 158
/
darkmode-toggle.client.ts
35 lines (33 loc) · 1.09 KB
/
darkmode-toggle.client.ts
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
const colorThemes = document.querySelectorAll("[data-color-mode]");
const toggleDarkMode = () => {
const getCurrentTheme = () => {
const userPrefersDark = globalThis.matchMedia(
"(prefers-color-scheme: dark)",
).matches;
if (localStorage.denoDocsTheme) return localStorage.denoDocsTheme;
return userPrefersDark ? "dark" : "light";
};
if (getCurrentTheme() === "light") {
localStorage.denoDocsTheme = "dark";
document.documentElement.classList.add("dark");
document.documentElement.classList.remove("light");
colorThemes.forEach((el) => {
el.setAttribute("data-color-mode", "dark");
});
} else {
localStorage.denoDocsTheme = "light";
document.documentElement.classList.add("light");
document.documentElement.classList.remove("dark");
colorThemes.forEach((el) => {
el.setAttribute("data-color-mode", "light");
});
}
};
const darkModeToggleButtons = document.querySelectorAll(
".dark-mode-toggle.button",
);
darkModeToggleButtons.forEach((button) => {
button.addEventListener("click", () => {
toggleDarkMode();
});
});