Skip to content

Commit

Permalink
Update darkmode.js
Browse files Browse the repository at this point in the history
Here’s an improved version of your dark mode toggle script, focusing on readability, organization, and additional features:
  • Loading branch information
ManmathX authored Oct 13, 2024
1 parent f1fa8af commit c43d90a
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions darkmode.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
let darkmode= localStorage.getItem('darkmode')
const themeSwitch = document.getElementById('theme-switch')
const enableDarkmode = () => {
document.body.classList.add('darkmode')
localStorage.setItem('darkmode','active')

let darkmode = localStorage.getItem('darkmode');
const themeSwitch = document.getElementById('theme-switch');


const enableDarkMode = () => {
document.body.classList.add('darkmode');
localStorage.setItem('darkmode', 'active');
updateThemeSwitchText();
};


const disableDarkMode = () => {
document.body.classList.remove('darkmode');
localStorage.setItem('darkmode', 'inactive');
updateThemeSwitchText();
};


const updateThemeSwitchText = () => {
themeSwitch.textContent = darkmode === "active" ? "Switch to Light Mode ☀️" : "Switch to Dark Mode 🌙";
};


if (darkmode === "active") {
enableDarkMode();
} else {
disableDarkMode();
}
const disableDarkmode = () => {
document.body.classList.remove('darkmode')
localStorage.setItem('darkmode',null)
}
if(darkmode==="active") enableDarkmode()
themeSwitch.addEventListener("click", () =>{
darkmode=localStorage.getItem('darkmode')
darkmode!=="active" ? enableDarkmode() : disableDarkmode()
})


themeSwitch.addEventListener("click", () => {
darkmode = localStorage.getItem('darkmode');
darkmode === "active" ? disableDarkMode() : enableDarkMode();
});

// Update button text on initial load
updateThemeSwitchText();

0 comments on commit c43d90a

Please sign in to comment.