Skip to content

Commit

Permalink
Add dark mode toggle functionality to mode.js
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadcodes committed Nov 14, 2023
1 parent 2f1e79c commit a815979
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions assets/js/mode.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit a815979

Please sign in to comment.