mirror of
https://github.com/adityatelange/hugo-PaperMod.git
synced 2025-10-27 14:42:30 +01:00
Refactor theme decision logic into head.html for faster script execution.
This commit is contained in:
@@ -149,6 +149,51 @@
|
|||||||
{{- end }}
|
{{- end }}
|
||||||
</noscript>
|
</noscript>
|
||||||
|
|
||||||
|
{{- /* theme-toggle is enabled */}}
|
||||||
|
{{- if (not site.Params.disableThemeToggle) }}
|
||||||
|
{{- /* theme is light */}}
|
||||||
|
{{- if (eq site.Params.defaultTheme "light") }}
|
||||||
|
<script>
|
||||||
|
if (localStorage.getItem("pref-theme") === "dark") {
|
||||||
|
document.querySelector("html").dataset.theme = 'dark';
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
{{- /* theme is dark */}}
|
||||||
|
{{- else if (eq site.Params.defaultTheme "dark") }}
|
||||||
|
<script>
|
||||||
|
if (localStorage.getItem("pref-theme") === "light") {
|
||||||
|
document.querySelector("html").dataset.theme = 'light';
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
{{- else }}
|
||||||
|
{{- /* theme is auto */}}
|
||||||
|
<script>
|
||||||
|
if (localStorage.getItem("pref-theme") === "dark") {
|
||||||
|
document.querySelector("html").dataset.theme = 'dark';
|
||||||
|
} else if (localStorage.getItem("pref-theme") === "light") {
|
||||||
|
document.querySelector("html").dataset.theme = 'light';
|
||||||
|
} else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||||
|
document.querySelector("html").dataset.theme = 'dark';
|
||||||
|
} else {
|
||||||
|
document.querySelector("html").dataset.theme = 'light';
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
{{- end }}
|
||||||
|
{{- /* theme-toggle is disabled and theme is auto */}}
|
||||||
|
{{- else if (and (ne site.Params.defaultTheme "light") (ne site.Params.defaultTheme "dark"))}}
|
||||||
|
<script>
|
||||||
|
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||||
|
document.querySelector("html").dataset.theme = 'dark';
|
||||||
|
} else {
|
||||||
|
document.querySelector("html").dataset.theme = 'light';
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
{{- partial "extend_head.html" . -}}
|
{{- partial "extend_head.html" . -}}
|
||||||
|
|
||||||
{{- /* Misc */}}
|
{{- /* Misc */}}
|
||||||
|
|||||||
@@ -1,48 +1,3 @@
|
|||||||
{{- /* theme-toggle is enabled */}}
|
|
||||||
{{- if (not site.Params.disableThemeToggle) }}
|
|
||||||
{{- /* theme is light */}}
|
|
||||||
{{- if (eq site.Params.defaultTheme "light") }}
|
|
||||||
<script>
|
|
||||||
if (localStorage.getItem("pref-theme") === "dark") {
|
|
||||||
document.querySelector("html").dataset.theme = 'dark';
|
|
||||||
}
|
|
||||||
|
|
||||||
</script>
|
|
||||||
{{- /* theme is dark */}}
|
|
||||||
{{- else if (eq site.Params.defaultTheme "dark") }}
|
|
||||||
<script>
|
|
||||||
if (localStorage.getItem("pref-theme") === "light") {
|
|
||||||
document.querySelector("html").dataset.theme = 'light';
|
|
||||||
}
|
|
||||||
|
|
||||||
</script>
|
|
||||||
{{- else }}
|
|
||||||
{{- /* theme is auto */}}
|
|
||||||
<script>
|
|
||||||
if (localStorage.getItem("pref-theme") === "dark") {
|
|
||||||
document.querySelector("html").dataset.theme = 'dark';
|
|
||||||
} else if (localStorage.getItem("pref-theme") === "light") {
|
|
||||||
document.querySelector("html").dataset.theme = 'light';
|
|
||||||
} else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
||||||
document.querySelector("html").dataset.theme = 'dark';
|
|
||||||
} else {
|
|
||||||
document.querySelector("html").dataset.theme = 'light';
|
|
||||||
}
|
|
||||||
|
|
||||||
</script>
|
|
||||||
{{- end }}
|
|
||||||
{{- /* theme-toggle is disabled and theme is auto */}}
|
|
||||||
{{- else if (and (ne site.Params.defaultTheme "light") (ne site.Params.defaultTheme "dark"))}}
|
|
||||||
<script>
|
|
||||||
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
||||||
document.querySelector("html").dataset.theme = 'dark';
|
|
||||||
} else {
|
|
||||||
document.querySelector("html").dataset.theme = 'light';
|
|
||||||
}
|
|
||||||
|
|
||||||
</script>
|
|
||||||
{{- end }}
|
|
||||||
|
|
||||||
<header class="header">
|
<header class="header">
|
||||||
<nav class="nav">
|
<nav class="nav">
|
||||||
<div class="logo">
|
<div class="logo">
|
||||||
|
|||||||
Reference in New Issue
Block a user