Skip to content

Commit

Permalink
theme: set proper conditions
Browse files Browse the repository at this point in the history
for having default-theme with theme-toggle

* can set/unset default theme
* for default-theme: dark/light : theme-toggle will not be visible
* theme-toggle with defaultheme: auto is functional

fixes #25 for #15
  • Loading branch information
adityatelange committed Oct 5, 2020
1 parent 994c0b5 commit a9e00a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions layouts/partials/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
localStorage.setItem("menu-scroll-position", document.getElementById('menu').scrollLeft);
}
</script>
{{- if not .Site.Params.disableThemeToggle }}
{{- if (and (not .Site.Params.disableThemeToggle) (not (or (eq .Site.Params.defaultTheme "light") (eq .Site.Params.defaultTheme "dark")))) }}
<script>
document.getElementById("theme-toggle").addEventListener("click", () => {
if (document.body.className.includes("dark")) {
Expand All @@ -66,4 +66,4 @@
<script>
localStorage.removeItem("pref-theme");
</script>
{{- end }}
{{- end }}
11 changes: 9 additions & 2 deletions layouts/partials/header.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if eq .Site.Params.defaultTheme "auto" }}
{{- if (and (not .Site.Params.disableThemeToggle) (not (or (eq .Site.Params.defaultTheme "light") (eq .Site.Params.defaultTheme "dark")))) }}
<script>
// load memory
if (localStorage.getItem("pref-theme") === "dark") {
Expand All @@ -12,6 +12,13 @@
}
</script>
{{- end }}
{{- if (and (.Site.Params.disableThemeToggle) (eq .Site.Params.defaultTheme "auto")) }}
<script>
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.classList.add('dark');
}
</script>
{{- end }}
<noscript>
<style type="text/css">
.theme-toggle,
Expand All @@ -24,7 +31,7 @@
<nav class="nav">
<p class="logo">
<a href="{{ "" | absLangURL }}">{{ .Site.Title }}</a>
{{- if not .Site.Params.disableThemeToggle }}
{{- if (and (not .Site.Params.disableThemeToggle) (not (or (eq .Site.Params.defaultTheme "light") (eq .Site.Params.defaultTheme "dark")))) }}
<span class="theme-toggle">
<a id="theme-toggle">
<svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
Expand Down

0 comments on commit a9e00a3

Please sign in to comment.