Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Set the light/dark theme before the first paint on the main and Inquire pages: the theme was applied by Vue after mounting, so dark mode showed a white page that faded to dark

Murtaza Nasirf00f7842b42e
diff --git a/templates/includes/theme_bootstrap.html b/templates/includes/theme_bootstrap.html
new file mode 100644
index 00000000..404e85a5
--- /dev/null
+++ b/templates/includes/theme_bootstrap.html
@@ -0,0 +1,15 @@
+{#- Applies the saved light/dark mode and color scheme before the first paint.
+ Without it, a page whose theme is applied by Vue after mounting is painted
+ in the light theme first and fades to dark (a white flash on load and
+ between pages). Same rules as initializeDarkMode / applyColorScheme in
+ static/js/modules/composables/ui.js. Include as early in <head> as possible. -#}
+<script>
+ (function () {
+ var root = document.documentElement, mode = null, scheme = null;
+ try { mode = localStorage.getItem('darkMode'); scheme = localStorage.getItem('colorScheme'); } catch (e) {}
+ var dark = mode !== null ? mode === 'true'
+ : !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
+ root.classList.toggle('dark', dark);
+ if (scheme && scheme !== 'blue') root.classList.add((dark ? 'theme-dark-' : 'theme-light-') + scheme);
+ })();
+</script>
diff --git a/templates/index.html b/templates/index.html
index 4991b10d..43c88463 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -5,6 +5,7 @@
<!-- interactive-widget=resizes-content: on Android Chrome the on-screen
keyboard resizes the layout so the UI fits the space above it. -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, interactive-widget=resizes-content">
+ {% include 'includes/theme_bootstrap.html' %}
<script>
// Keyboard-safe viewport. The on-screen keyboard shrinks the VISUAL
// viewport but not the LAYOUT viewport, so a 100vh/100% shell overflows
diff --git a/templates/inquire.html b/templates/inquire.html
index aa571910..ff47c2af 100644
--- a/templates/inquire.html
+++ b/templates/inquire.html
@@ -4,6 +4,7 @@
<meta charset="UTF-8">
<!-- interactive-widget=resizes-content: keyboard resizes layout (Android). -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, interactive-widget=resizes-content">
+ {% include 'includes/theme_bootstrap.html' %}
<script>
// Keyboard-safe viewport (see index.html for rationale): mirror the
// visible viewport height into --app-height so the keyboard never pushes
diff --git a/tests/test_page_assets.py b/tests/test_page_assets.py
index bb6c9d4e..28d4bfa4 100644
--- a/tests/test_page_assets.py
+++ b/tests/test_page_assets.py
@@ -163,3 +163,22 @@ def test_rendered_pages_embed_the_users_language_before_i18n(german_client, path
assert data["locale"] == "de" and set(data["translations"]) == {"de", "en"}, path
# |tojson escapes markup characters, so no translation can end the script.
assert "</" not in m.group(1), path
+
+
+THEME_INCLUDE = "{% include 'includes/theme_bootstrap.html' %}"
+
+
[email protected]("page", PAGES)
+def test_theme_is_applied_before_first_paint(page):
+ """Each page sets the dark/theme classes in <head>, before any stylesheet.
+
+ On a page whose theme was set only by Vue after mounting, the first paint
+ was light, and the body's color transition then faded it to dark.
+ """
+ html = _read(page)
+ head = html.split("</head>")[0]
+ applies = THEME_INCLUDE in head or re.search(r"classList\.(add|toggle)\('dark'", head)
+ assert applies, f"{page}: no theme script in <head>"
+ if THEME_INCLUDE in head:
+ first_css = head.find('rel="stylesheet"')
+ assert first_css == -1 or head.find(THEME_INCLUDE) < first_css, f"{page}: theme include after a stylesheet"