theme-switch: Enable theme toggle unless specifically disabled (#69)

Co-authored-by: Aditya Telange <21258296+adityatelange@users.noreply.github.com>
This commit is contained in:
Itay Cohen 2020-11-04 10:34:45 +02:00 committed by GitHub
parent 12b6b132e4
commit 3064e8cfc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View File

@ -50,7 +50,7 @@
localStorage.setItem("menu-scroll-position", document.getElementById('menu').scrollLeft); localStorage.setItem("menu-scroll-position", document.getElementById('menu').scrollLeft);
} }
</script> </script>
{{- if (and (not .Site.Params.disableThemeToggle) (not (or (eq .Site.Params.defaultTheme "light") (eq .Site.Params.defaultTheme "dark")))) }} {{- if (not .Site.Params.disableThemeToggle) }}
<script> <script>
document.getElementById("theme-toggle").addEventListener("click", () => { document.getElementById("theme-toggle").addEventListener("click", () => {
if (document.body.className.includes("dark")) { if (document.body.className.includes("dark")) {
@ -63,7 +63,7 @@
}) })
</script> </script>
{{- else -}} {{- else -}}
<!-- case where owner disables theme button after deployment, this resets the stored theme --> {{/* case where owner disables theme button after deployment, this resets the stored theme */}}
<script> <script>
localStorage.removeItem("pref-theme"); localStorage.removeItem("pref-theme");
</script> </script>

View File

@ -1,21 +1,32 @@
{{- if (and (not .Site.Params.disableThemeToggle) (not (or (eq .Site.Params.defaultTheme "light") (eq .Site.Params.defaultTheme "dark")))) }} {{- if (eq .Site.Params.defaultTheme "light") }}
<script> <script>
// load memory
if (localStorage.getItem("pref-theme") === "dark") { if (localStorage.getItem("pref-theme") === "dark") {
document.body.classList.add('dark'); document.body.classList.add('dark');
} else if (localStorage.getItem("pref-theme") === "light") { } else if (localStorage.getItem("pref-theme") === "light") {
document.body.classList.remove('dark') document.body.classList.remove('dark')
} else { } else {
if (window.matchMedia('(prefers-color-scheme: dark)').matches) { document.body.classList.remove('dark')
document.body.classList.add('dark');
}
} }
</script> </script>
{{- end }} {{- else if (eq .Site.Params.defaultTheme "dark") }}
{{- if (and (.Site.Params.disableThemeToggle) (eq .Site.Params.defaultTheme "auto")) }}
<script> <script>
if (window.matchMedia('(prefers-color-scheme: dark)').matches) { 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.add('dark');
}
</script>
{{- else if (or (eq .Site.Params.defaultTheme "auto") (not .Site.Params.disableThemeToggle) ) }}
<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 if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.classList.add('dark'); document.body.classList.add('dark');
} }
@ -41,7 +52,7 @@
{{- .Site.Params.label.text | default .Site.Title -}} {{- .Site.Params.label.text | default .Site.Title -}}
</a> </a>
<span class="logo-switches"> <span class="logo-switches">
{{- if (and (not .Site.Params.disableThemeToggle) (not (or (eq .Site.Params.defaultTheme "light") (eq .Site.Params.defaultTheme "dark")))) }} {{- if (not .Site.Params.disableThemeToggle) }}
<span class="theme-toggle"> <span class="theme-toggle">
<a id="theme-toggle" accesskey="t"> <a id="theme-toggle" accesskey="t">
<svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" <svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"