
Implementing a Dark Mode Toggle on a Website
In this article, I will discuss how I implemented a dark mode toggle feature on a website. This feature enhances user experience by allowing them to switch between light and dark modes, which can be particularly beneficial for different lighting conditions or personal preferences.
Overview
The implementation involves JavaScript to toggle between dark and light modes and to remember the user’s preference across sessions. This is achieved using localStorage and event listeners. We have three main objectives when coding this functionality:
1. Respecting System OS Theme Preferences
The implementation automatically detects and applies the user’s preferred color scheme based on their operating system settings. This ensures the website aligns with the user’s global preferences from their first visit.
- Implementation Details: Upon loading the website, the JavaScript code checks the user’s system preference. If the user prefers a dark theme (
prefers-color-scheme: dark), the website automatically loads with dark mode enabled. If the user prefers a light theme, the default light mode is applied.
2. Dark Mode Toggleable on the Website
For user convenience, a toggle is provided on the website, allowing users to manually switch between dark and light modes. This feature gives users the flexibility to override the system default setting if they desire.
- How it’s Achieved: Implementing a toggle button on the website that triggers a function to switch between dark and light themes.
- Implementation Details: The toggle button is linked to a JavaScript function that changes the website’s theme. This function adjusts the site’s styling to reflect the chosen theme.
3. Remembering User’s Choice Across Sessions
The website remembers the user’s theme preference across different sessions and pages, enhancing the user experience by providing consistency.
- How it’s Achieved: Utilizing the browser’s local storage to store the user’s theme preference.
- Implementation Details: When the user selects a theme, this choice is saved in local storage. The website checks this stored preference on each visit or page load and applies the user’s preferred theme accordingly.
Code Explanation
The implementation consists of two main functions and some event listeners:
Function: toggleColorMode
const toggleColorMode = () => {
const isDarkMode = localStorage.getItem('color-mode') === 'dark';
if (isDarkMode) {
document.documentElement.setAttribute('color-mode', 'light');
localStorage.setItem('color-mode', 'light');
} else {
document.documentElement.setAttribute('color-mode', 'dark');
localStorage.setItem('color-mode', 'dark');
}
updateToggleState();
}; This function checks the current color mode from localStorage and toggles it. It then updates the color mode by setting an attribute on the document’s root element and updates localStorage to remember the user’s choice. Finally, it calls updateToggleState to synchronize the UI state.
Function: updateToggleState
const updateToggleState = () => {
const isDarkMode = localStorage.getItem('color-mode') === 'dark';
const toggles = document.querySelectorAll('.dark-mode-toggle');
toggles.forEach(toggle => {
if (toggle.type === 'checkbox') {
toggle.checked = isDarkMode;
} else {
// Other UI updates for non-checkbox elements
}
});
}; This function updates the state of all toggle elements based on the current color mode. It’s called after changing the color mode to ensure the UI reflects the current state.
Event Listeners
Event listeners are added to all elements with the .dark-mode-toggle class. These listeners call toggleColorMode when the toggle state changes. Additionally, the code ensures the correct state of toggles on page load and listens for a custom event colorModeChanged for any external triggers.
Conclusion
This approach to implementing a dark mode toggle is effective and user-friendly. It allows for a seamless experience, remembers user preferences, and is flexible enough to be adapted to various UI elements.

