admin: keep the dark theme from flashing light on reload (#12689)

* admin: apply the saved theme inline at the top of the page head

The theme script was an external file, so a reload had to fetch or
revalidate it before the root got its theme, and the page could paint
with the light default in the meantime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* admin: set color-scheme on the root together with the theme

Until the stylesheets load, the browser paints the page canvas from the
root's color-scheme, which only the stylesheets set, so a dark theme
showed a white canvas for the duration of every reload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-11-16 20:31:00 -08:00
1 parent eed00c3737
commit 40182a68bc
4 files changed
+317 -293

No files matched your search

-43
View File
@@ -1,43 +0,0 @@
// Applies the saved color theme (auto, light or dark) before the page paints.
(function () {
var storageKey = 'admin-theme';
var choices = ['auto', 'light', 'dark'];
var media = window.matchMedia ? window.matchMedia('(prefers-color-scheme: dark)') : null;
var root = document.documentElement;
function savedChoice() {
try {
var value = localStorage.getItem(storageKey);
return choices.indexOf(value) >= 0 ? value : 'auto';
} catch (e) {
return 'auto';
}
}
function apply(choice) {
var dark = choice === 'dark' || (choice === 'auto' && media && media.matches);
root.setAttribute('data-bs-theme', dark ? 'dark' : 'light');
root.setAttribute('data-theme-choice', choice);
}
var current = savedChoice();
apply(current);
if (media && media.addEventListener) {
media.addEventListener('change', function () {
apply(current);
});
}
document.addEventListener('click', function (event) {
var toggle = event.target.closest && event.target.closest('[data-theme-toggle]');
if (!toggle) {
return;
}
current = choices[(choices.indexOf(current) + 1) % choices.length];
try {
localStorage.setItem(storageKey, current);
} catch (e) {}
apply(current);
});
})();