// #127: dark mode toggle: flips to the other variant of the active pair. // #127: dark mode toggle - flips to the other variant of the active pair. (function () { var PAIRS = { 'midnight': { light: 'midnight-light', dark: 'midnight' }, 'smooth': { light: 'smooth', dark: 'smooth-dark' }, 'pastel-lavender': { light: 'pastel-lavender', dark: 'pastel-lavender-dark' }, 'pastel-peach': { light: 'pastel-peach', dark: 'pastel-peach-dark' }, 'pastel-cloud': { light: 'pastel-cloud', dark: 'pastel-cloud-dark' } }; var root = document.documentElement; function state() { var p = root.dataset.preset || 'midnight'; for (var base in PAIRS) { if (p === PAIRS[base].dark) return { base: base, dark: true }; if (p === PAIRS[base].light) return { base: base, dark: false }; } return { base: 'midnight', dark: true }; } function apply() { var s = state(); document.body.classList.toggle('dark', s.dark); root.classList.toggle('dark', s.dark); } function sync(btns) { btns.forEach(function (b) { b.setAttribute('aria-pressed', state().dark ? 'true' : 'false'); }); } var btns = document.querySelectorAll('.dark-toggle'); apply(); sync(Array.prototype.slice.call(btns)); btns.forEach(function (b) { // mark as wired so settings.js does not add a second handler // (#197: double-binding made the settings toggle flip twice = no-op) b.dataset.darkWired = '1'; b.addEventListener('click', function () { var s = state(); var dark = !s.dark; root.dataset.preset = dark ? PAIRS[s.base].dark : PAIRS[s.base].light; try { localStorage.setItem('palette-dark', dark ? 'true' : 'false'); } catch (e) {} apply(); sync(Array.prototype.slice.call(btns)); document.dispatchEvent(new CustomEvent('palette-darkchange')); }); }); })();