Add light/dark theme switch to all pages
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2026-10-21 01:54:30 +00:00
parent 6ee95c955c
commit 1af842f71a
5 changed files with 73 additions and 1 deletions
+35
View File
@@ -0,0 +1,35 @@
// Light/dark switch. The page follows the OS theme until the button is used;
// the choice is then remembered in localStorage. Loaded in <head> so a saved
// theme is applied before the page paints.
(function () {
var root = document.documentElement;
var media = window.matchMedia('(prefers-color-scheme: dark)');
try {
var saved = localStorage.getItem('theme');
if (saved === 'light' || saved === 'dark') root.dataset.theme = saved;
} catch (e) {}
function current() {
return root.dataset.theme || (media.matches ? 'dark' : 'light');
}
function label(btn) {
var next = current() === 'dark' ? 'light' : 'dark';
btn.textContent = next === 'dark' ? 'Dark mode' : 'Light mode';
btn.setAttribute('aria-label', 'Switch to ' + next + ' mode');
}
document.addEventListener('DOMContentLoaded', function () {
var btn = document.getElementById('theme-toggle');
if (!btn) return;
label(btn);
btn.hidden = false;
btn.addEventListener('click', function () {
root.dataset.theme = current() === 'dark' ? 'light' : 'dark';
try { localStorage.setItem('theme', root.dataset.theme); } catch (e) {}
label(btn);
});
media.addEventListener('change', function () { label(btn); });
});
})();