1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-12-03 03:48:55 -05:00

move dark mode script inline to avoid blinding flash of white background

This commit is contained in:
2021-05-28 11:11:09 -04:00
parent 4a59d3dd51
commit 468cc14d4b
5 changed files with 8 additions and 5 deletions

View File

@@ -1,14 +1,13 @@
{{/* only parse and append analytics script on production site */}}
{{- $includeProdScripts := eq hugo.Environment "production" -}}
{{- $darkmode := resources.Get "js/dark-mode.js" | resources.ExecuteAsTemplate "js/dark-mode.js" . -}}
{{- $twemoji := resources.Get "js/vendor/twemoji.js" | resources.ExecuteAsTemplate "js/vendor/twemoji.js" . -}}
{{- $bundle := slice $darkmode $twemoji -}}
{{- $bundle := slice $twemoji -}}
{{- if $includeProdScripts }}
{{- $fathom := resources.Get "js/fathom.js" | resources.ExecuteAsTemplate "js/fathom.js" . }}
{{- $bundle = slice $darkmode $twemoji $fathom }}
{{- $bundle = $bundle | append $fathom }}
{{- end }}
{{- $js := $bundle | resources.Concat "/js/app.js" -}}

View File

@@ -0,0 +1 @@
<script>(function(d){var u=d.document,f=u.body.classList,e=localStorage,c="dark_mode_pref",t=e.getItem(c),a="dark",n="light",r="{{ .Site.Params.Theme.defaultTheme | safeJS }}",o=u.querySelector(".dark-mode-toggle"),i=r===a,b=function(d){f.remove(a,n);f.add(d);i=d===a};t===a&&b(a);t===n&&b(n);if(!t){var s=function(d){return"(prefers-color-scheme: "+d+")"};d.matchMedia(s(a)).matches?b(a):d.matchMedia(s(n)).matches?b(n):b(r);d.matchMedia(s(a)).addListener((function(d){d.matches&&b(a)}));d.matchMedia(s(n)).addListener((function(d){d.matches&&b(n)}))}if(o){o.style.display="block";o.addEventListener("click",(function(){if(i){b(n);e.setItem(c,n)}else{b(a);e.setItem(c,a)}}),!0)}})(window)</script>