Files
calcserubi/assets/js/theme.js
T
2026-10-20 13:16:38 +09:00

18 lines
751 B
JavaScript

/* Light/dark toggle; remembers the choice per browser. */
(function () {
'use strict';
var root = document.documentElement;
function current() {
if (root.dataset.theme) return root.dataset.theme;
return window.matchMedia && matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
document.querySelectorAll('[data-theme-toggle]').forEach(function (btn) {
btn.addEventListener('click', function () {
var next = current() === 'dark' ? 'light' : 'dark';
root.dataset.theme = next;
try { localStorage.setItem('serubi-theme', next); } catch (e) { /* storage unavailable */ }
btn.setAttribute('aria-label', next === 'dark' ? 'Ganti ke tema terang' : 'Ganti ke tema gelap');
});
});
})();