havre: light or dark, before the page draws, and the switch in the nav (shared with /open as havre.theme)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BQqGFv54JJTw7p3rgGjP7C
This commit is contained in:
teahubxyzandClaude Opus 5.5 committed 2026-10-19 19:09:30 +00:00
1 parent 65175dbefa
commit 901c902d76
1 file changed
+19
+19
View File
@@ -0,0 +1,19 @@
// theme.js: light or dark, set before the page draws (so it doesn't flash), and the switch in the nav.
// "havre.theme" is "light", "dark", or absent (follow the system). /open reads the same key.
(function () {
var root = document.documentElement, K = "havre.theme";
function get() { try { return localStorage.getItem(K) || ""; } catch (e) { return ""; } }
function dark() { var t = get(); return t ? t === "dark" : !!(window.matchMedia && matchMedia("(prefers-color-scheme: dark)").matches); }
function apply() {
var t = get();
if (t) root.setAttribute("data-theme", t); else root.removeAttribute("data-theme");
var b = document.getElementById("theme");
if (b) { b.textContent = dark() ? "light mode" : "dark mode"; b.setAttribute("aria-pressed", dark() ? "true" : "false"); }
}
apply();
document.addEventListener("DOMContentLoaded", function () {
apply();
var b = document.getElementById("theme");
if (b) b.addEventListener("click", function () { try { localStorage.setItem(K, dark() ? "light" : "dark"); } catch (e) { /* this time only */ } apply(); });
});
})();