-
Notifications
You must be signed in to change notification settings - Fork 107
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 #418 from REHAN-18/new
change the theme toogle button with modern style button
- Loading branch information
Showing
3 changed files
with
99 additions
and
52 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,53 +1,54 @@ | ||
// Check for saved dark mode preference in localStorage | ||
let darkmode = localStorage.getItem("darkmode"); | ||
const themeToggleBtn = document.getElementById("theme-toggle"); | ||
const themeIcon = document.getElementById("theme-toggle-icon"); | ||
const body = document.body; | ||
const footer = document.querySelector("footer"); // Select the footer element | ||
const footerSpan = footer.querySelector(".website-name"); // Select the span inside the footer | ||
const footer = document.querySelector("footer"); | ||
const footerSpan = footer.querySelector(".website-name"); | ||
const footerParagraph = footer.querySelector(".footer-about-text"); | ||
|
||
// Enable Dark Mode | ||
const enableDarkMode = () => { | ||
body.classList.add("dark-mode"); | ||
body.classList.remove("light-mode"); // Ensure light mode class is removed | ||
footer.classList.add("dark-mode-footer"); // Add dark mode class to footer | ||
footer.classList.remove("light-mode-footer"); | ||
footerSpan.classList.add("dark-text"); | ||
footerSpan.classList.remove("light-text"); | ||
footerParagraph.classList.add("dark-text"); | ||
footerParagraph.classList.remove("light-text"); | ||
localStorage.setItem("darkmode", "active"); | ||
themeIcon.src = "images/light-mode.png"; // Light mode icon in dark mode | ||
body.classList.add("dark-mode"); | ||
body.classList.remove("light-mode"); | ||
footer.classList.add("dark-mode-footer"); | ||
footer.classList.remove("light-mode-footer"); | ||
footerSpan.classList.add("dark-text"); | ||
footerSpan.classList.remove("light-text"); | ||
footerParagraph.classList.add("dark-text"); | ||
footerParagraph.classList.remove("light-text"); | ||
localStorage.setItem("darkmode", "active"); | ||
themeToggleBtn.querySelector(".theme-icon-light").style.opacity = '0'; | ||
themeToggleBtn.querySelector(".theme-icon-dark").style.opacity = '1'; | ||
}; | ||
|
||
// Disable Dark Mode (Switch to Light Mode) | ||
const disableDarkMode = () => { | ||
body.classList.remove("dark-mode"); | ||
body.classList.add("light-mode"); // Ensure light mode class is added | ||
footer.classList.remove("dark-mode-footer"); // Remove dark mode class from footer | ||
footer.classList.add("light-mode-footer"); | ||
footerSpan.classList.add("light-text"); | ||
footerSpan.classList.remove("dark-text"); | ||
footerParagraph.classList.add("light-text"); | ||
footerParagraph.classList.remove("dark-text"); | ||
localStorage.setItem("darkmode", "inactive"); | ||
themeIcon.src = "images/dark-mode.png"; // Dark mode icon in light mode | ||
body.classList.remove("dark-mode"); | ||
body.classList.add("light-mode"); | ||
footer.classList.remove("dark-mode-footer"); | ||
footer.classList.add("light-mode-footer"); | ||
footerSpan.classList.add("light-text"); | ||
footerSpan.classList.remove("dark-text"); | ||
footerParagraph.classList.add("light-text"); | ||
footerParagraph.classList.remove("dark-text"); | ||
localStorage.setItem("darkmode", "inactive"); | ||
themeToggleBtn.querySelector(".theme-icon-light").style.opacity = '1'; | ||
themeToggleBtn.querySelector(".theme-icon-dark").style.opacity = '0'; | ||
}; | ||
|
||
// Apply the saved theme on page load | ||
if (darkmode === "active") { | ||
enableDarkMode(); // Start in dark mode if the user prefers it | ||
enableDarkMode(); // Start in dark mode if the user prefers it | ||
} else { | ||
disableDarkMode(); // Otherwise, start in light mode | ||
disableDarkMode(); // Otherwise, start in light mode | ||
} | ||
|
||
// Toggle dark mode on button click | ||
themeToggleBtn.addEventListener("click", () => { | ||
darkmode = localStorage.getItem("darkmode"); // Get the current state | ||
if (darkmode === "active") { | ||
disableDarkMode(); // Switch to light mode | ||
} else { | ||
enableDarkMode(); // Switch to dark mode | ||
} | ||
darkmode = localStorage.getItem("darkmode"); // Get the current state | ||
if (darkmode === "active") { | ||
disableDarkMode(); // Switch to light mode | ||
} else { | ||
enableDarkMode(); // Switch to dark mode | ||
} | ||
}); |
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
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