Skip to content

Commit

Permalink
Create darkModeToggle.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 4, 2024
1 parent bf0b22f commit 224e79f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/ui/darkModeToggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// darkModeToggle.js

class DarkModeToggle {
constructor() {
this.toggleButton = document.getElementById('dark-mode-toggle');
this.init();
}

// Initialize the dark mode toggle
init() {
this.loadTheme();
this.toggleButton.addEventListener('click', () => this.toggleTheme());
}

// Load the user's theme preference from local storage
loadTheme() {
const currentTheme = localStorage.getItem('theme');
if (currentTheme) {
document.body.classList.toggle('dark-mode', currentTheme === 'dark');
} else {
// Default to light mode
document.body.classList.remove('dark-mode');
}
}

// Toggle between light and dark themes
toggleTheme() {
const isDarkMode = document.body.classList.toggle('dark-mode');
localStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
}
}

// Example usage
document.addEventListener('DOMContentLoaded', () => {
new DarkModeToggle();
});

0 comments on commit 224e79f

Please sign in to comment.