Skip to content

Commit 7c92b9d

Browse files
authored
Merge pull request #297 from teamEPYC/Murtaza-TC1-patch-5
Refactor dark-light mode handling
2 parents 6435cd1 + b03e07b commit 7c92b9d

File tree

1 file changed

+34
-60
lines changed

1 file changed

+34
-60
lines changed

dark-light-mode.js

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,52 @@ function colorModeToggle() {
88
if (!isNaN(attrVal) && defaultValType === "number") return +attrVal;
99
return defaultVal;
1010
}
11+
1112
const htmlElement = document.documentElement;
1213
const computed = getComputedStyle(htmlElement);
13-
let toggleEl;
14-
let togglePressed = "false";
14+
1515
const scriptTag = document.querySelector("[tr-color-vars]");
1616
if (!scriptTag) {
1717
console.warn("Script tag with tr-color-vars attribute not found");
1818
return;
1919
}
20+
2021
let colorModeDuration = attr(0.5, scriptTag.getAttribute("duration"));
2122
let colorModeEase = attr("power1.out", scriptTag.getAttribute("ease"));
23+
2224
const cssVariables = scriptTag.getAttribute("tr-color-vars");
2325
if (!cssVariables.length) {
2426
console.warn("Value of tr-color-vars attribute not found");
2527
return;
2628
}
27-
let lightColors = {};
29+
2830
let darkColors = {};
31+
2932
cssVariables.split(",").forEach(function (item) {
30-
let lightValue = computed.getPropertyValue(`--color--${item}`);
31-
let darkValue = computed.getPropertyValue(`--dark--${item}`);
33+
// Clean up the variable name - remove "var(" and ")" and trim whitespace
34+
let cleanedItem = item.trim().replace(/^var\(/, "").replace(/\)$/, "");
35+
36+
// Extract the variable name (e.g., "--polygon-v3---color--primary")
37+
let lightVarName = cleanedItem;
38+
39+
// Create dark variable name by replacing "--color--" with "--dark--"
40+
// Adjust this based on your actual CSS variable naming convention
41+
let darkVarName = cleanedItem.replace("---color--", "---dark--");
42+
43+
let lightValue = computed.getPropertyValue(lightVarName);
44+
let darkValue = computed.getPropertyValue(darkVarName);
45+
3246
if (lightValue.length) {
3347
if (!darkValue.length) darkValue = lightValue;
34-
lightColors[`--color--${item}`] = lightValue;
35-
darkColors[`--color--${item}`] = darkValue;
48+
darkColors[lightVarName] = darkValue;
3649
}
3750
});
38-
if (!Object.keys(lightColors).length) {
51+
52+
if (!Object.keys(darkColors).length) {
3953
console.warn("No variables found matching tr-color-vars attribute value");
4054
return;
4155
}
56+
4257
function setColors(colorObject, animate) {
4358
if (typeof gsap !== "undefined" && animate) {
4459
gsap.to(htmlElement, {
@@ -52,60 +67,19 @@ function colorModeToggle() {
5267
});
5368
}
5469
}
55-
function goDark(dark, animate) {
56-
if (dark) {
57-
localStorage.setItem("dark-mode", "true");
58-
htmlElement.classList.add("dark-mode");
59-
setColors(darkColors, animate);
60-
togglePressed = "true";
61-
} else {
62-
localStorage.setItem("dark-mode", "false");
63-
htmlElement.classList.remove("dark-mode");
64-
setColors(lightColors, animate);
65-
togglePressed = "false";
66-
}
67-
if (typeof toggleEl !== "undefined") {
68-
toggleEl.forEach(function (element) {
69-
element.setAttribute("aria-pressed", togglePressed);
70-
});
71-
}
72-
}
73-
function checkPreference(e) {
74-
goDark(e.matches, false);
75-
}
76-
const colorPreference = window.matchMedia("(prefers-color-scheme: dark)");
77-
colorPreference.addEventListener("change", (e) => {
78-
checkPreference(e);
79-
});
80-
let storagePreference = localStorage.getItem("dark-mode");
81-
if (storagePreference !== null) {
82-
storagePreference === "true" ? goDark(true, false) : goDark(false, false);
83-
} else {
84-
checkPreference(colorPreference);
70+
71+
function forceDarkMode() {
72+
localStorage.setItem("dark-mode", "true");
73+
htmlElement.classList.add("dark-mode");
74+
setColors(darkColors, false);
8575
}
76+
77+
// Force dark mode immediately
78+
forceDarkMode();
79+
8680
window.addEventListener("DOMContentLoaded", (event) => {
87-
toggleEl = document.querySelectorAll("[tr-color-toggle]");
88-
toggleEl.forEach(function (element) {
89-
element.setAttribute("aria-label", "View Dark Mode");
90-
element.setAttribute("role", "button");
91-
element.setAttribute("aria-pressed", togglePressed);
92-
});
93-
document.addEventListener("click", function (e) {
94-
const targetElement = e.target.closest("[tr-color-toggle]");
95-
if (targetElement) {
96-
let darkClass = htmlElement.classList.contains("dark-mode");
97-
darkClass ? goDark(false, true) : goDark(true, true);
98-
}
99-
});
100-
document.addEventListener("click", function (e) {
101-
const darkTrigger = e.target.closest("[tr-color-mode='dark']");
102-
const lightTrigger = e.target.closest("[tr-color-mode='light']");
103-
if (darkTrigger) {
104-
goDark(true, true);
105-
} else if (lightTrigger) {
106-
goDark(false, true);
107-
}
108-
});
81+
forceDarkMode();
10982
});
11083
}
84+
11185
colorModeToggle();

0 commit comments

Comments
 (0)