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);
});
})();
Binary file not shown.
+52 -2
View File
@@ -12,6 +12,56 @@ script setBasePath(prefix string) {
window.__BASE_PATH__ = prefix;
}
// themeScript applies the saved color theme (auto, light or dark) inline, so
// the root is themed before any external script or stylesheet is fetched.
templ themeScript() {
<script>
(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);
root.style.colorScheme = dark ? 'dark' : 'light';
}
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);
});
})();
</script>
}
templ Layout(view ViewContext, content templ.Component) {
{{
username := view.Username
@@ -55,7 +105,7 @@ templ Layout(view ViewContext, content templ.Component) {
<meta name="csrf-token" content={ csrfToken }>
<link rel="icon" href={ string(view.P("/static/favicon.ico")) } type="image/x-icon">
<script src={ string(view.P("/static/js/theme.js")) }></script>
@themeScript()
<!-- Bootstrap CSS -->
<link href={ string(view.P("/static/css/bootstrap.min.css")) } rel="stylesheet">
<!-- Bootstrap Icons CSS -->
@@ -404,7 +454,7 @@ templ LoginForm(title string, errorMessage string, csrfToken string) {
<title>{title} - Login</title>
<link rel="icon" href={ prefix + "/static/favicon.ico" } type="image/x-icon">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src={ prefix + "/static/js/theme.js" }></script>
@themeScript()
<link href={ prefix + "/static/css/bootstrap.min.css" } rel="stylesheet">
<link href={ prefix + "/static/css/bootstrap-icons.min.css" } rel="stylesheet">
<link href={ prefix + "/static/css/admin.css" } rel="stylesheet">
File diff suppressed because it is too large. Load diff