Dark mode
This commit is contained in:
+48
-2
@@ -1,4 +1,50 @@
|
||||
// Please see documentation at https://learn.microsoft.com/aspnet/core/client-side/bundling-and-minification
|
||||
// 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.
|
||||
|
||||
// Write your JavaScript code.
|
||||
(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");
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user