forked from Code-Social/official-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Code-Social#211 from ManmathX/patch-1
Update darkmode.js
- Loading branch information
Showing
1 changed file
with
37 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |