From c672ce902639c47b3a1902035e9eb88cb7b959aa Mon Sep 17 00:00:00 2001 From: Wonderfall Date: Thu, 21 Jul 2022 20:24:29 +0200 Subject: [PATCH] handle theme switching without inline js --- assets/js/theme.js | 9 +++ layouts/partials/header.html | 118 +++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 assets/js/theme.js create mode 100644 layouts/partials/header.html diff --git a/assets/js/theme.js b/assets/js/theme.js new file mode 100644 index 0000000..be17dc9 --- /dev/null +++ b/assets/js/theme.js @@ -0,0 +1,9 @@ +// for now this is assuming default theme is set to dark +// will probably refactor in the future for much better handling +function loadPreferredTheme() { + if (localStorage.getItem("pref-theme") === "light") { + document.body.classList.remove('dark') + } +} + +loadPreferredTheme(); \ No newline at end of file diff --git a/layouts/partials/header.html b/layouts/partials/header.html new file mode 100644 index 0000000..b25f852 --- /dev/null +++ b/layouts/partials/header.html @@ -0,0 +1,118 @@ +{{- /* theme.js */}} +{{- if not site.Params.disableThemeToggle }} +{{- $theme := resources.Get "js/theme.js" | resources.Minify }} +{{- if not site.Params.assets.disableFingerprinting }} +{{- $theme_js := (slice $theme) | resources.Concat "assets/js/theme.js" | fingerprint }} + +{{- else }} +{{- $theme_js := (slice $theme) | resources.Concat "assets/js/theme.js" }} + +{{- end }} +{{- end }} + +
+ +