/* weirsoe-theme.js — persisted light/dark toggle (matches asger.weirsøe.dk). * * 1) No-flash: put this MINIMAL snippet inline in , BEFORE any stylesheet * (runs pre-paint; respects the OS preference on first visit): * * * * 2) Load this file and drop the toggle button anywhere (exact markup from * asger.weirsøe.dk): * * * * Cycles light ⇄ dark and persists to localStorage['theme']. */ (function () { var root = document.documentElement; function preferred() { return window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'; } function apply() { var t; try { t = localStorage.getItem('theme'); } catch (e) { /* ignore */ } root.dataset.theme = t || preferred(); } apply(); // Re-apply after client-side navigations (Astro etc.) — harmless if unused. document.addEventListener('astro:after-swap', apply); document.addEventListener('click', function (e) { var btn = e.target.closest && e.target.closest('[data-theme-toggle]'); if (!btn) return; var next = root.dataset.theme === 'light' ? 'dark' : 'light'; root.dataset.theme = next; try { localStorage.setItem('theme', next); } catch (err) { /* ignore */ } }); // Tiny API for custom controls (e.g. a 3-way picker incl. 'terminal'). window.weirsoeTheme = { set: function (t) { root.dataset.theme = t; try { localStorage.setItem('theme', t); } catch (e) {} }, get: function () { return root.dataset.theme; }, }; })();