From c7187ebcc4b145b5b2a8c720ddc8a22e712fc955 Mon Sep 17 00:00:00 2001 From: Damillora Date: Wed, 14 Apr 2021 22:19:52 +0700 Subject: [PATCH] feat(theme): persist dark mode --- assets/js/dark-mode.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/assets/js/dark-mode.js b/assets/js/dark-mode.js index 41e1899..66eeb85 100644 --- a/assets/js/dark-mode.js +++ b/assets/js/dark-mode.js @@ -1,14 +1,27 @@ +function setDarkMode() { + if (!window.localStorage.getItem('theme') && window.document.body.getAttribute("data-theme")){ + window.localStorage.setItem('theme', window.document.body.getAttribute("data-theme")); + } + if (window.document.body.getAttribute("data-theme") == "dark") { + $("#darkMode i").text("brightness_high"); + $("#darkMode p").text("Light Mode"); + } else { + $("#darkMode i").text("brightness_low"); + $("#darkMode p").text("Dark Mode"); + } +} +if (window.localStorage.getItem('theme')) { + window.document.body.setAttribute("data-theme", window.localStorage.getItem("theme")); +} $(document).ready(function () { + setDarkMode(); $("#darkMode").click(function () { if (window.document.body.getAttribute("data-theme") != "dark") { window.document.body.setAttribute("data-theme", "dark"); - $("#darkMode i").text("brightness_high"); - $("#darkMode p").text("Light Mode"); } else { window.document.body.setAttribute("data-theme", "light"); - $("#darkMode i").text("brightness_low"); - $("#darkMode p").text("Dark Mode"); } + setDarkMode(); }) });