Skip to content

Commit dbf3c8f

Browse files
kartbendleach02
authored andcommitted
doc: fix dark mode "flashing" on page load
This hooks up the dark-mode-toggle-stylesheets-loader so that the dark mode styles are loaded immediately, rather than waiting for the page to load and then switching to dark mode. This prevents the "flashing" of light mode styles on page load. Fixes #79791 Signed-off-by: Benjamin Cabé <[email protected]>
1 parent 7aa6222 commit dbf3c8f

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

doc/_templates/layout.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@
2828
{% endblock %}
2929
{% block extrahead %}
3030
<meta name="color-scheme" content="dark light">
31-
{# Light/Dark stylesheets added here due to https://github.com/readthedocs/sphinx_rtd_theme/issues/1100 #}
32-
<link rel="stylesheet" href="{{ pathto('_static/css/light.css', 1) }}" type="text/css" media="(prefers-color-scheme: light)"/>
33-
<link rel="stylesheet" href="{{ pathto('_static/css/dark.css', 1) }}" type="text/css" media="(prefers-color-scheme: dark)"/>
31+
{# Use dark mode loader script to prevent "flashing" of the page on load.
32+
As we need a <noscript> tag and very specific orderding of the tags, this can't be done via
33+
the usual add_js_file()/add_css_file() Sphinx API.
34+
See https://github.com/GoogleChromeLabs/dark-mode-toggle/issues/77 #}
35+
<noscript id="dark-mode-toggle-stylesheets">
36+
<link rel="stylesheet" href="{{ pathto('_static/css/light.css', 1) }}" type="text/css" media="(prefers-color-scheme: light)"/>
37+
<link rel="stylesheet" href="{{ pathto('_static/css/dark.css', 1) }}" type="text/css" media="(prefers-color-scheme: dark)"/>
38+
</noscript>
39+
<script src="{{ pathto('_static/js/dark-mode-toggle-stylesheets-loader.min.js', 1) }}"></script>
40+
<script type="module" src="{{ pathto('_static/js/dark-mode-toggle.min.mjs', 1) }}"></script>
3441
{% endblock %}

doc/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,3 @@ def setup(app):
354354
# theme customizations
355355
app.add_css_file("css/custom.css")
356356
app.add_js_file("js/custom.js")
357-
app.add_js_file("js/dark-mode-toggle.min.mjs", type="module")

0 commit comments

Comments
 (0)