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

github.com/yursan9/manis-hugo-theme.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'static/js/dark-mode.js')
-rw-r--r--static/js/dark-mode.js35
1 files changed, 35 insertions, 0 deletions
diff --git a/static/js/dark-mode.js b/static/js/dark-mode.js
new file mode 100644
index 0000000..5a67798
--- /dev/null
+++ b/static/js/dark-mode.js
@@ -0,0 +1,35 @@
+var toggle = document.getElementById("dark-mode-toggle");
+var darkTheme = document.getElementById("dark-mode-theme");
+
+// probe system default dark mode setting
+var systemDefault = null
+if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
+ systemDefault = "dark";
+} else {
+ systemDefault = "light";
+}
+
+// use user preference if possible
+var savedTheme = localStorage.getItem("dark-mode-storage") || systemDefault;
+setTheme(savedTheme);
+
+toggle.addEventListener("click", () => {
+ if (toggle.src.endsWith("/img/moon.svg") ) {
+ setTheme("dark");
+ } else if (toggle.src.endsWith("/img/sun.svg") ) {
+ setTheme("light");
+ }
+
+});
+
+function setTheme(mode) {
+ localStorage.setItem("dark-mode-storage", mode);
+
+ if (mode === "dark") {
+ darkTheme.disabled = false;
+ toggle.src = "/img/sun.svg";
+ } else if (mode === "light") {
+ darkTheme.disabled = true;
+ toggle.src = "/img/moon.svg";
+ }
+} \ No newline at end of file