/** * Theme switcher for onepager sites. * * Sets data-theme="light"|"dark" on . Persists to localStorage * ("onepager-theme"). Initial-default falls back to prefers-color-scheme. * * Anti-FOUC companion: this file expects an inline IIFE in to set * data-theme BEFORE CSS loads, so the first paint is correct. This file * only handles runtime toggling and exposes window.onepagerTheme. * * Per-site opt-out: forces dark and ignores * localStorage. toggles.js consumes data-theme-lock to hide the theme button. */ (function () { var KEY = 'onepager-theme'; var SUPPORTED = ['light', 'dark']; function lock() { var v = document.documentElement.getAttribute('data-theme-lock'); return v && SUPPORTED.indexOf(v) !== -1 ? v : null; } function detect() { var locked = lock(); if (locked) return locked; var stored = null; try { stored = localStorage.getItem(KEY); } catch (e) { /* private browsing */ } if (stored && SUPPORTED.indexOf(stored) !== -1) return stored; if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) { return 'light'; } return 'dark'; } function get() { return document.documentElement.getAttribute('data-theme') || 'dark'; } function set(theme) { if (lock()) return; if (SUPPORTED.indexOf(theme) === -1) return; document.documentElement.setAttribute('data-theme', theme); try { localStorage.setItem(KEY, theme); } catch (e) { /* private browsing */ } } function toggle() { set(get() === 'light' ? 'dark' : 'light'); } // Reconcile with anti-FOUC inline script. If already has data-theme // set (by anti-FOUC IIFE), keep it. Otherwise apply detect(). if (!document.documentElement.getAttribute('data-theme')) { document.documentElement.setAttribute('data-theme', detect()); } window.onepagerTheme = { get: get, set: set, toggle: toggle, detect: detect, isLocked: function () { return lock() !== null; } }; })();