Jump to…
snowfeat: given a new redesign and emulated terminal homepagerxkspqmsoknz1mo
Matt W1// Theme toggle. Enhancement only — the site is fully usable in its default
Matt W2// dark theme with this script blocked or absent; it just cannot be switched.
Matt W3//
Matt W4// The control reads out the theme it is currently *in* ("DARK"), not the one
Matt W5// it would switch to. That is the design's choice, and it is the right one for
Matt W6// a control that is also the only place the current theme is stated.
Matt W7(function () {
Matt W8 var KEY = "dogfood-theme";
Matt W9
Matt W10 function current() {
Matt W11 return document.documentElement.getAttribute("data-theme") === "light"
Matt W12 ? "light"
Matt W13 : "dark";
Matt W14 }
Matt W15
Matt W16 function apply(theme) {
Matt W17 if (theme === "light") {
Matt W18 document.documentElement.setAttribute("data-theme", "light");
Matt W19 } else {
Matt W20 document.documentElement.removeAttribute("data-theme");
Matt W21 }
Matt W22 document.querySelectorAll("[data-theme-toggle]").forEach(function (btn) {
Matt W23 btn.setAttribute("aria-pressed", String(theme === "light"));
Matt W24 var label = btn.querySelector("[data-theme-label]");
Matt W25 if (label) label.textContent = theme;
Matt W26 });
Matt W27 }
Matt W28
Matt W29 // Exposed so the palette's "Toggle theme" command drives this same code
Matt W30 // path rather than reimplementing persistence next to it.
Matt W31 window.dogfoodToggleTheme = function () {
Matt W32 var next = current() === "light" ? "dark" : "light";
Matt W33 apply(next);
Matt W34 try {
Matt W35 localStorage.setItem(KEY, next);
Matt W36 } catch (e) {
Matt W37 // Storage blocked (private mode, quota). The toggle still works for
Matt W38 // this page load; it just will not persist across visits.
Matt W39 }
Matt W40 };
Matt W41
Matt W42 document.querySelectorAll("[data-theme-toggle]").forEach(function (btn) {
Matt W43 btn.hidden = false;
Matt W44 btn.addEventListener("click", window.dogfoodToggleTheme);
Matt W45 });
Matt W46
Matt W47 apply(current());
Matt W48})();

48 lines · JavaScript