-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththeme-toggle.js
More file actions
96 lines (88 loc) · 3.12 KB
/
theme-toggle.js
File metadata and controls
96 lines (88 loc) · 3.12 KB
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
/**
* Theme Toggle 1.1.1
* Released under the MIT License
* Released on: February 3, 2025
*/
function colorModeToggle() {
function attr(defaultVal, attrVal) {
const defaultValType = typeof defaultVal;
if (typeof attrVal !== "string" || attrVal.trim() === "") return defaultVal;
if (attrVal === "true" && defaultValType === "boolean") return true;
if (attrVal === "false" && defaultValType === "boolean") return false;
if (isNaN(attrVal) && defaultValType === "string") return attrVal;
if (!isNaN(attrVal) && defaultValType === "number") return +attrVal;
return defaultVal;
}
const htmlElement = document.documentElement;
let toggleEl;
let togglePressed = "false";
const scriptTag = document.querySelector("[data-theme-toggle-script]");
if (!scriptTag) {
console.warn(
"Script tag with data-theme-toggle-script attribute not found"
);
return;
}
let colorModeDuration = attr(0.5, scriptTag.getAttribute("duration"));
let colorModeEase = attr("power1.out", scriptTag.getAttribute("ease"));
function setColors(themeString, animate) {
if (typeof gsap !== "undefined" && animate) {
gsap.to(htmlElement, {
...colorThemes.getTheme(themeString),
duration: colorModeDuration,
ease: colorModeEase,
});
} else {
htmlElement.classList.remove("u-theme-dark");
htmlElement.classList.remove("u-theme-light");
htmlElement.classList.add("u-theme-" + themeString);
}
}
function goDark(dark, animate) {
if (dark) {
localStorage.setItem("dark-mode", "true");
htmlElement.classList.add("dark-mode");
setColors("dark", animate);
togglePressed = "true";
} else {
localStorage.setItem("dark-mode", "false");
htmlElement.classList.remove("dark-mode");
setColors("light", animate);
togglePressed = "false";
}
if (typeof toggleEl !== "undefined") {
toggleEl.forEach(function (element) {
element.setAttribute("aria-pressed", togglePressed);
});
}
}
function checkPreference(e) {
goDark(e.matches, false);
}
const colorPreference = window.matchMedia("(prefers-color-scheme: dark)");
colorPreference.addEventListener("change", (e) => {
checkPreference(e);
});
let storagePreference = localStorage.getItem("dark-mode");
if (storagePreference !== null) {
storagePreference === "true" ? goDark(true, false) : goDark(false, false);
} else {
checkPreference(colorPreference);
}
window.addEventListener("DOMContentLoaded", (event) => {
toggleEl = document.querySelectorAll("[data-theme-toggle-button]");
toggleEl.forEach(function (element) {
element.setAttribute("aria-label", "View Dark Mode");
element.setAttribute("role", "button");
element.setAttribute("aria-pressed", togglePressed);
});
document.addEventListener("click", function (e) {
const targetElement = e.target.closest("[data-theme-toggle-button]");
if (targetElement) {
let darkClass = htmlElement.classList.contains("dark-mode");
darkClass ? goDark(false, true) : goDark(true, true);
}
});
});
}
colorModeToggle();