From 224e79f6998ef2c5706baaf13e62fd2e2d88b490 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Wed, 4 Dec 2024 18:45:46 +0700 Subject: [PATCH] Create darkModeToggle.js --- src/ui/darkModeToggle.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/ui/darkModeToggle.js diff --git a/src/ui/darkModeToggle.js b/src/ui/darkModeToggle.js new file mode 100644 index 000000000..cf5fc48ae --- /dev/null +++ b/src/ui/darkModeToggle.js @@ -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(); +});