Files
maintenancepages/wwwroot/js/site.js
T
2026-04-13 14:31:07 -04:00

51 lines
1.7 KiB
JavaScript

// Please see documentation at https://learn.microsoft.com/aspnet/core/client-side/bundling-and-minification
// for details on configuring this project to bundle and minify static web assets.
(function () {
const storageKey = "maintenance-theme";
const root = document.documentElement;
const toggle = document.querySelector("[data-theme-toggle]");
const mediaQuery = window.matchMedia ? window.matchMedia("(prefers-color-scheme: dark)") : null;
function getPreferredTheme() {
const storedTheme = localStorage.getItem(storageKey);
if (storedTheme === "light" || storedTheme === "dark") {
return storedTheme;
}
return mediaQuery && mediaQuery.matches ? "dark" : "light";
}
function applyTheme(theme) {
root.setAttribute("data-theme", theme);
root.style.colorScheme = theme;
if (toggle) {
const nextTheme = theme === "dark" ? "light" : "dark";
toggle.textContent = theme === "dark" ? "Light mode" : "Dark mode";
toggle.setAttribute("aria-pressed", theme === "dark" ? "true" : "false");
toggle.setAttribute("title", "Switch to " + nextTheme + " mode");
}
}
applyTheme(getPreferredTheme());
if (toggle) {
toggle.addEventListener("click", function () {
const nextTheme = root.getAttribute("data-theme") === "dark" ? "light" : "dark";
localStorage.setItem(storageKey, nextTheme);
applyTheme(nextTheme);
});
}
if (mediaQuery) {
mediaQuery.addEventListener("change", function (event) {
if (localStorage.getItem(storageKey)) {
return;
}
applyTheme(event.matches ? "dark" : "light");
});
}
})();