add a template for project and try to fix darkmode

This commit is contained in:
Rainyy21
2026-05-28 02:46:42 -04:00
parent 4e40999a0c
commit e76a02b17b
5 changed files with 242 additions and 22 deletions
+24 -18
View File
@@ -1,21 +1,27 @@
let darkmode = localStorage.getItem('dark');
const switch_button = document.getElementById("theme_switch");
const enableDarkmode = () => {
document.body.classList.add('dark');
localStorage.setItem('dark' , 'active');
}
const disableDarkmode = () => {
document.body.classList.remove('dark');
localStorage.removeItem('dark');
// Function to apply the theme based on localStorage
const applyTheme = () => {
const darkmode = localStorage.getItem('dark');
if (darkmode === 'active') {
document.body.classList.add('dark');
} else {
document.body.classList.remove('dark');
}
};
if (darkmode === 'active') enableDarkmode();
switch_button.addEventListener("click", () => {
darkmode = localStorage.getItem('dark');
darkmode !== "active" ? enableDarkmode() : disableDarkmode();
});
// Run on initial load
// Since the script is 'defer', document.body will be available
applyTheme();
// Set up the toggle button listener
const switch_button = document.getElementById("theme_switch");
if (switch_button) {
switch_button.addEventListener("click", () => {
const darkmode = localStorage.getItem('dark');
if (darkmode !== "active") {
localStorage.setItem('dark', 'active');
} else {
localStorage.setItem('dark', 'inactive');
}
applyTheme();
});
}