diff options
author | Athul Cyriac Ajay <athul8720@gmail.com> | 2021-09-25 23:20:43 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-25 23:20:43 +0300 |
commit | ddcd4cdcfc0b150fafd6dac1af1ec3d46e39753b (patch) | |
tree | 2a55f4b2ae261df2685edeff72f4093922ee38e4 | |
parent | 5321e4817fbed7cf57de9171f994f8720cbcf411 (diff) | |
parent | 1d2ab6bf7ce3712c23c8e1b5d80ba5bb1ff24dcb (diff) |
Merge pull request #36 from hsand/master
Add dark mode toggle
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | layouts/partials/head.html | 4 | ||||
-rw-r--r-- | layouts/partials/header.html | 4 | ||||
-rw-r--r-- | static/js/themetoggle.js | 23 |
4 files changed, 31 insertions, 3 deletions
@@ -14,6 +14,7 @@ Forked from [Ezhil Theme](https://github.com/vividvilla/ezhil) - Callouts - Tags - Auto Dark Mode(based on system theme) +- Dark/Light Mode toggle - tl:dr; frontamatter - Cache busting for CSS files @@ -75,7 +76,7 @@ pygmentscodefencesguesssyntax = true paginate=3 # articles per page [params] - mode="auto" # color-mode → light,dark or auto + mode="auto" # color-mode → light,dark,toggle or auto useCDN=false # don't use CDNs for fonts and icons, instead serve them locally. subtitle = "Minimal and Clean [blog theme for Hugo](https://github.com/athul/archie)" diff --git a/layouts/partials/head.html b/layouts/partials/head.html index ab3f39d..e8a637a 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -6,5 +6,9 @@ {{ range .Site.Menus.main }} <a href="{{ .URL }}">{{ .Name }}</a> {{ end }} + {{ if eq .Site.Params.mode "toggle" -}} + | <a id="dark-mode-toggle" onclick="toggleTheme()" href=""></a> + <script src="{{ .Site.BaseURL }}js/themetoggle.js"></script> + {{ end }} </nav> </header> diff --git a/layouts/partials/header.html b/layouts/partials/header.html index c5ae589..98ab662 100644 --- a/layouts/partials/header.html +++ b/layouts/partials/header.html @@ -39,9 +39,9 @@ {{ $style := resources.Get "css/main.css" | fingerprint }} <link rel="stylesheet" type="text/css" media="screen" href="{{ $style.Permalink }}" /> - {{- if or (eq .Site.Params.mode "auto") (eq .Site.Params.mode "dark") -}} + {{- if or (eq .Site.Params.mode "auto") (eq .Site.Params.mode "dark") (eq .Site.Params.mode "toggle") -}} {{ $darkstyle := resources.Get "css/dark.css" | fingerprint }} - <link rel="stylesheet" type="text/css" href="{{ $darkstyle.Permalink }}" {{ if eq .Site.Params.mode "auto" }}media="(prefers-color-scheme: dark)"{{ end }} /> + <link id="darkModeStyle" rel="stylesheet" type="text/css" href="{{ $darkstyle.Permalink }}" {{ if eq .Site.Params.mode "auto" }}media="(prefers-color-scheme: dark)"{{ end }} {{ if eq .Site.Params.mode "toggle" }}disabled{{ end }} /> {{ end }} <!-- Custom CSS style get applied last --> diff --git a/static/js/themetoggle.js b/static/js/themetoggle.js new file mode 100644 index 0000000..f8c6dcd --- /dev/null +++ b/static/js/themetoggle.js @@ -0,0 +1,23 @@ +function setTheme(mode) { + localStorage.setItem("theme-storage", mode); + if (mode === "dark") { + document.getElementById("darkModeStyle").disabled=false; + document.getElementById("dark-mode-toggle").innerHTML = "<i data-feather=\"sun\"></i>"; + feather.replace() + } else if (mode === "light") { + document.getElementById("darkModeStyle").disabled=true; + document.getElementById("dark-mode-toggle").innerHTML = "<i data-feather=\"moon\"></i>"; + feather.replace() + } +} + +function toggleTheme() { + if (localStorage.getItem("theme-storage") === "light") { + setTheme("dark"); + } else if (localStorage.getItem("theme-storage") === "dark") { + setTheme("light"); + } +} + +var savedTheme = localStorage.getItem("theme-storage") || "light"; +setTheme(savedTheme); |