From 0722f0bca05c2d9b6bfc69d92abce093ed5d3211 Mon Sep 17 00:00:00 2001 From: Flancian <0@flancia.org> Date: Sun, 15 Nov 2020 02:24:44 +0100 Subject: [PATCH] Dark mode is now sticky. --- app/static/js/main.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/static/js/main.js b/app/static/js/main.js index 76fdcf1..308ae6a 100644 --- a/app/static/js/main.js +++ b/app/static/js/main.js @@ -18,6 +18,14 @@ document.addEventListener("DOMContentLoaded", function() { // Select button const btn = document.querySelector(".theme-toggle"); + var theme = document.querySelector("#theme-link"); + const currentTheme = localStorage.getItem("theme"); + // If the user's preference in localStorage is dark... + if (currentTheme == "dark") { + theme.href = "/static/css/screen-dark.css"; + } else if (currentTheme == "light") { + theme.href = "/static/css/screen-light.css"; + } // Listen for a click on the button btn.addEventListener("click", function() { @@ -25,8 +33,10 @@ document.addEventListener("DOMContentLoaded", function() { var theme = document.querySelector("#theme-link"); if (theme.getAttribute("href") == "/static/css/screen-light.css") { theme.href = "/static/css/screen-dark.css"; + localStorage.setItem("theme", "dark"); } else { theme.href = "/static/css/screen-light.css"; + localStorage.setItem("theme", "light"); } }); });