git repos / sago_web_git

static/theme.js

browsing at commit = 77397b212d0d732ef2490d1693232ba2f72a756c

raw · blame · history

// Applies the saved (or system-preferred) theme before first paint, so it is
// in place before the stylesheet renders and there is no flash of the wrong
// theme. Kept in a static file rather than inline so the strict page CSP
// (script-src 'self') allows it. app.js handles switching and persistence.
(function () {
  try {
    var t = localStorage.getItem("theme");
    if (t !== "light" && t !== "dark") {
      t = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
    }
    document.documentElement.setAttribute("data-theme", t);
  } catch (e) { /* private mode / storage disabled: fall back to CSS default */ }
})();