Add dark mode
This commit is contained in:
parent
bcd45d9006
commit
2588ef9729
5 changed files with 72 additions and 36 deletions
33
static/build/darkmode.js
Normal file
33
static/build/darkmode.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Check if user preference for dark mode is stored in a cookie
|
||||
let darkModeEnabled = (document.cookie.indexOf('darkModeEnabled=true') !== -1);
|
||||
|
||||
// Apply dark mode based on the user's preference
|
||||
function applyDarkMode() {
|
||||
const theme = darkModeEnabled ? "dark" : "light";
|
||||
document.documentElement.setAttribute("data-bs-theme", theme);
|
||||
|
||||
// Change the icon based on the theme
|
||||
const icon = darkModeEnabled ? "fa-sun" : "fa-moon";
|
||||
const iconElement = document.getElementById("darkModeIcon");
|
||||
iconElement.classList.remove(darkModeEnabled ? "fa-moon" : "fa-sun");
|
||||
iconElement.classList.add(icon);
|
||||
}
|
||||
|
||||
// Toggle dark mode and store the preference in a cookie
|
||||
function toggleDarkMode() {
|
||||
darkModeEnabled = !darkModeEnabled;
|
||||
const expirationDate = new Date();
|
||||
expirationDate.setFullYear(expirationDate.getFullYear() + 1);
|
||||
const cookieValue = `darkModeEnabled=${darkModeEnabled}; expires=${expirationDate.toUTCString()}`;
|
||||
document.cookie = cookieValue;
|
||||
applyDarkMode();
|
||||
}
|
||||
|
||||
// Add event listener to the toggle button if it exists
|
||||
const darkModeButton = document.getElementById("darkModeButton");
|
||||
if (darkModeButton) {
|
||||
darkModeButton.addEventListener("click", toggleDarkMode);
|
||||
}
|
||||
|
||||
// Apply dark mode on page load
|
||||
applyDarkMode();
|
Loading…
Add table
Add a link
Reference in a new issue