diff --git a/static/js/theme.js b/static/js/theme.js index 04b2ac3..532d9cd 100644 --- a/static/js/theme.js +++ b/static/js/theme.js @@ -33,13 +33,21 @@ apply(prefersLight ? 'light' : 'dark'); } - document.addEventListener('DOMContentLoaded', function () { + document.addEventListener('click', function (e) { + var btn = e.target.closest('[data-theme-pick]'); + if (!btn) return; + e.preventDefault(); + apply(btn.getAttribute('data-theme-pick')); + }); + + function syncButtons() { var cur = document.documentElement.getAttribute('data-theme') || 'dark'; updateButtons(cur); - document.querySelectorAll('[data-theme-pick]').forEach(function (btn) { - btn.addEventListener('click', function () { - apply(btn.getAttribute('data-theme-pick')); - }); - }); - }); + } + + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', syncButtons); + } else { + syncButtons(); + } })(); diff --git a/templates/base.html b/templates/base.html index 9ea9173..5e53117 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,12 +1,18 @@ - +