Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/s4n7h0/hugo-theme-timeline.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'assets/js/theme.js')
-rw-r--r--assets/js/theme.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/assets/js/theme.js b/assets/js/theme.js
new file mode 100644
index 0000000..930f638
--- /dev/null
+++ b/assets/js/theme.js
@@ -0,0 +1,25 @@
+// Toggle theme
+
+const theme = window.localStorage && window.localStorage.getItem("theme");
+const themeToggle = document.querySelector(".theme-toggle");
+const isDark = theme === "dark";
+var metaThemeColor = document.querySelector("meta[name=theme-color]");
+
+if (theme !== null) {
+ document.body.classList.toggle("dark-theme", isDark);
+ isDark
+ ? metaThemeColor.setAttribute("content", "#252627")
+ : metaThemeColor.setAttribute("content", "#fafafa");
+}
+
+themeToggle.addEventListener("click", () => {
+ document.body.classList.toggle("dark-theme");
+ window.localStorage &&
+ window.localStorage.setItem(
+ "theme",
+ document.body.classList.contains("dark-theme") ? "dark" : "light"
+ );
+ document.body.classList.contains("dark-theme")
+ ? metaThemeColor.setAttribute("content", "#252627")
+ : metaThemeColor.setAttribute("content", "#fafafa");
+});