Light and dark: a switch in the top bar
Three choices: as the device is (the default), light, or dark, kept in a "theme" cookie per browser. The dark colours move to dark.css, linked with a media attribute from the cookie, so the server renders the page in the chosen colours with no flash (the CSP allows no inline script). app.js sets the cookie and switches the media at once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -29,7 +29,28 @@
|
||||
}
|
||||
});
|
||||
|
||||
// Light, dark, or as the device is (layout.html's theme switch). The
|
||||
// choice is a cookie so the server renders the page in it, with no flash;
|
||||
// here it takes effect at once by switching dark.css's media.
|
||||
var darkMedia = { system: "(prefers-color-scheme: dark)", light: "not all", dark: "all" };
|
||||
function setTheme(name) {
|
||||
if (!(name in darkMedia)) return;
|
||||
document.cookie = name === "system"
|
||||
? "theme=; path=/; max-age=0; SameSite=Lax"
|
||||
: "theme=" + name + "; path=/; max-age=31536000; SameSite=Lax";
|
||||
var css = document.getElementById("dark-css");
|
||||
if (css) css.media = darkMedia[name];
|
||||
document.querySelectorAll("[data-theme-set]").forEach(function (b) {
|
||||
b.setAttribute("aria-pressed", String(b.getAttribute("data-theme-set") === name));
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener("click", function (e) {
|
||||
var theme = e.target.closest("[data-theme-set]");
|
||||
if (theme) {
|
||||
setTheme(theme.getAttribute("data-theme-set"));
|
||||
return;
|
||||
}
|
||||
// The menu on narrow screens.
|
||||
if (e.target.closest("[data-nav-toggle]")) {
|
||||
document.body.classList.toggle("nav-open");
|
||||
|
||||
Reference in New Issue
Block a user