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; const computed = getComputedStyle(htmlElement); let toggleEl; let togglePressed = "false"; // Configuration embedded within the script let colorModeDuration = attr(0.5, "0.5"); // Default duration let colorModeEase = attr("power1.out", "power1.out"); // Default ease function // ✅ Updated list (from your screenshot) const cssVariables = "bg-primary,bg-dark,bg-subtle,bg-accent," + "text-primary,text-muted,text-secondary,text-inverse,text-disabled,text-link,text-link-hover," + "border-default,border-subtle,border-strong," + "divider-default,divider-subtle,divider-strong," + "action-primary,action-primary-hover,action-primary-active," + "action-secondary,action-secondary-hover,action-secondary-active," + "action-tertiary,action-tertiary-hover,icon-primary,icon-alternate"; let lightColors = {}; let darkColors = {}; cssVariables.split(",").forEach(function (item) { let lightValue = computed.getPropertyValue(`--color--${item}`); let darkValue = computed.getPropertyValue(`--dark--${item}`); if (lightValue.length) { if (!darkValue.length) darkValue = lightValue; lightColors[`--color--${item}`] = lightValue; darkColors[`--color--${item}`] = darkValue; } }); if (!Object.keys(lightColors).length) { console.warn("No variables found matching tr-color-vars attribute value"); return; } function setColors(colorObject, animate) { if (typeof gsap !== "undefined" && animate) { gsap.to(htmlElement, { ...colorObject, duration: colorModeDuration, ease: colorModeEase }); } else { Object.keys(colorObject).forEach(function (key) { htmlElement.style.setProperty(key, colorObject[key]); }); } } function goDark(dark, animate) { if (dark) { localStorage.setItem("dark-mode", "true"); htmlElement.classList.add("dark-mode"); setColors(darkColors, animate); togglePressed = "true"; } else { localStorage.setItem("dark-mode", "false"); htmlElement.classList.remove("dark-mode"); setColors(lightColors, 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 osPreference = colorPreference.matches; let storagePreference = localStorage.getItem("dark-mode"); if (storagePreference !== null) { osPreference = null; storagePreference === "true" ? goDark(true, false) : goDark(false, false); } if (osPreference !== null) { goDark(osPreference, false); } window.addEventListener("DOMContentLoaded", (event) => { toggleEl = document.querySelectorAll("[tr-color-toggle]"); toggleEl.forEach(function (element) { element.setAttribute("aria-label", "View Dark Mode"); element.setAttribute("role", "button"); element.setAttribute("aria-pressed", togglePressed); }); toggleEl.forEach(function (element) { element.addEventListener("click", function () { let darkClass = htmlElement.classList.contains("dark-mode"); darkClass ? goDark(false, true) : goDark(true, true); }); }); }); } colorModeToggle();