-
-
Notifications
You must be signed in to change notification settings - Fork 16
Add Dark Mode Toggle Feature (#85-10) #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
4a692a6
213dc01
e758bd8
bb05600
811e7d1
c62c857
52a8baf
c73304f
0dcb22c
a07db53
8a2d7d5
8b81cd7
7387b13
29b1eb0
7d71e0b
88bfb0f
ad63804
9d9aa48
f7e562f
73f88bb
53a6d30
ab42079
8eb146b
86c9333
3ef2d81
e7ff00b
113bc4b
c4ec293
d22457d
a9576ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,12 @@ | |
|
|
||
| let allUsers = []; | ||
| let filteredUsers = []; | ||
| let darkModeToggleElement = null; | ||
|
|
||
| // Dark mode constants | ||
| const DARK_MODE_KEY = 'darkMode'; | ||
| const THEME_ENABLED = 'enabled'; | ||
| const THEME_DISABLED = 'disabled'; | ||
|
|
||
| // ============================================================================ | ||
| // INITIALIZATION | ||
|
|
@@ -41,6 +47,7 @@ document.addEventListener('DOMContentLoaded', initializeApp); | |
| * Initialize the application on page load | ||
| */ | ||
| function initializeApp() { | ||
| initializeDarkMode(); | ||
| const cards = document.querySelectorAll('.card'); | ||
| showLoadingState(); | ||
| document.getElementById('totalCount').textContent = cards.length.toLocaleString(); | ||
|
|
@@ -508,3 +515,48 @@ function hideLoadingState() { | |
| if (loadingState) loadingState.style.display = 'none'; | ||
|
jbampton marked this conversation as resolved.
Outdated
|
||
| if (loadingStateDesktop) loadingStateDesktop.style.display = 'none'; | ||
| } | ||
| // ============================================================================ | ||
| // DARK MODE TOGGLE | ||
| // ============================================================================ | ||
| /** | ||
| * Initialize dark mode functionality | ||
| * - Load saved preference from localStorage | ||
|
jbampton marked this conversation as resolved.
Outdated
|
||
| * - Set up toggle button listener | ||
| * - Apply dark mode class if preference is set | ||
| * - Cache DOM element for performance | ||
| */ | ||
| function initializeDarkMode() { | ||
| darkModeToggleElement = document.getElementById('darkModeToggle'); | ||
| const isDarkMode = localStorage.getItem(DARK_MODE_KEY) === THEME_ENABLED; | ||
|
|
||
| if (isDarkMode) { | ||
| document.body.classList.add('dark-mode'); | ||
| updateDarkModeIcon(true); | ||
| } | ||
|
|
||
| if (darkModeToggleElement) { | ||
| darkModeToggleElement.addEventListener('click', toggleDarkMode); | ||
| } | ||
| } | ||
|
jbampton marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Toggle dark mode on/off | ||
| * - Saves preference to localStorage | ||
| * - Applies/removes dark-mode class from body | ||
| */ | ||
| function toggleDarkMode() { | ||
| const isDarkMode = document.body.classList.toggle('dark-mode'); | ||
| localStorage.setItem(DARK_MODE_KEY, isDarkMode ? THEME_ENABLED : THEME_DISABLED); | ||
| updateDarkModeIcon(isDarkMode); | ||
| } | ||
|
Comment on lines
+1136
to
+1140
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing function toggleDarkMode() {
const isDarkMode = document.documentElement.classList.toggle('dark-mode');
try {
localStorage.setItem(DARK_MODE_KEY, isDarkMode ? THEME_ENABLED : THEME_DISABLED);
} catch (error) {
console.error('Failed to save dark mode preference:', error);
}
updateDarkModeIcon(isDarkMode);
} |
||
|
|
||
| /** | ||
| * Update dark mode toggle icon | ||
| * @param {boolean} isDarkMode - Whether dark mode is currently enabled | ||
| */ | ||
| function updateDarkModeIcon(isDarkMode) { | ||
| if (darkModeToggleElement) { | ||
| darkModeToggleElement.textContent = isDarkMode ? 'βοΈ' : 'π'; | ||
| darkModeToggleElement.title = isDarkMode ? 'Switch to Light Mode' : 'Switch to Dark Mode'; | ||
| } | ||
| } | ||
|
jbampton marked this conversation as resolved.
Outdated
|
||
Uh oh!
There was an error while loading. Please reload this page.