fix(theme-toggle): quick flash (light/dark) (#768)

* fix(theme-toggle): quick flash (light/dark)

* chore: simplify

* chore: simplify

* chore: simplify
This commit is contained in:
Ludovic Fernandez
2025-08-21 01:33:24 +02:00
committed by GitHub
parent 18a9335d4b
commit 201ce3f763
2 changed files with 24 additions and 33 deletions

View File

@@ -59,6 +59,24 @@
{{ partial "google-analytics.html" . -}}
{{- end }}
<script>
// The section must not be in the theme.js file because it can create a quick flash (switch between light and dark).
function setTheme(theme) {
document.documentElement.classList.remove("light", "dark");
if (theme !== "light" && theme !== "dark") {
theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}
document.documentElement.classList.add(theme);
document.documentElement.style.colorScheme = theme;
}
setTheme("color-theme" in localStorage ? localStorage.getItem("color-theme") : '{{ site.Params.theme.default | default `system`}}')
</script>
<!-- Math engine -->
{{ $noop := .WordCount -}}
{{- $engine := site.Params.math.engine | default "katex" -}}