diff --git a/assets/js/mode.js b/assets/js/mode.js new file mode 100644 index 00000000..5e93f98f --- /dev/null +++ b/assets/js/mode.js @@ -0,0 +1,19 @@ +const toggleSwitch = document.querySelector("#dark-mode-toggle"); +// const currentTheme = localStorage.getItem("theme"); + +if (currentTheme) { + document.documentElement.setAttribute("data-theme", currentTheme); +} + +function switchTheme() { + const currentTheme = document.documentElement.getAttribute("data-theme"); + if (currentTheme === "dark") { + document.documentElement.setAttribute("data-theme", "light"); + localStorage.setItem("theme", "light"); + } else { + document.documentElement.setAttribute("data-theme", "dark"); + localStorage.setItem("theme", "dark"); + } +} + +toggleSwitch.addEventListener("click", switchTheme, false);