diff options
| author | Aditya Telange <21258296+adityatelange@users.noreply.github.com> | 2020-11-26 09:10:24 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-26 09:10:24 +0530 |
| commit | b301d60f032879af25dc9c5747c868db9bf7cc6c (patch) | |
| tree | 2ef191791a732cd5c1bdb2faf70a88e497b3676a | |
| parent | 678b5d82fc5ddccfdcc7433328a380473c6807dd (diff) | |
theme-switch: Improve theme switch logic (#86)
- removes unnecessary local-storage item removal
- less inline script on build
- assumes `defaultTheme as auto when value is not `dark` or `light`
- should work according to table at docs
| -rw-r--r-- | layouts/partials/footer.html | 6 | ||||
| -rw-r--r-- | layouts/partials/header.html | 26 |
2 files changed, 16 insertions, 16 deletions
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html index 779cd69..90cbeec 100644 --- a/layouts/partials/footer.html +++ b/layouts/partials/footer.html @@ -65,10 +65,4 @@ }) </script> -{{- else -}} -{{/* case where owner disables theme button after deployment, this resets the stored theme */}} -<script> - localStorage.removeItem("pref-theme"); - -</script> {{- end }} diff --git a/layouts/partials/header.html b/layouts/partials/header.html index c68db5c..0066a79 100644 --- a/layouts/partials/header.html +++ b/layouts/partials/header.html @@ -1,26 +1,23 @@ +{{- /* 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.body.classList.add('dark'); - } else if (localStorage.getItem("pref-theme") === "light") { - document.body.classList.remove('dark') - } else { - document.body.classList.remove('dark') } </script> +{{- /* theme is dark */}} {{- else if (eq .Site.Params.defaultTheme "dark") }} <script> - if (localStorage.getItem("pref-theme") === "dark") { - document.body.classList.add('dark'); - } else if (localStorage.getItem("pref-theme") === "light") { + if (localStorage.getItem("pref-theme") === "light") { document.body.classList.remove('dark') - } else { - document.body.classList.add('dark'); } </script> -{{- else if (or (eq .Site.Params.defaultTheme "auto") (not .Site.Params.disableThemeToggle) ) }} +{{- else }} +{{- /* theme is auto */}} <script> if (localStorage.getItem("pref-theme") === "dark") { document.body.classList.add('dark'); @@ -32,6 +29,15 @@ </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.body.classList.add('dark'); + } + +</script> +{{- end }} <noscript> <style type="text/css"> .theme-toggle, |
