toggleTheme utility is a simple JavaScript function that toggles dark mode by adding or removing the dark class from the document root.
Overview
This utility provides a minimalist approach to theme switching, directly manipulating the DOM to toggle between light and dark modes. It’s used by the ButtonToggle component to enable dark mode functionality.Source File
src/components/toggleTheme.jsx
Function Signature
Source Code
Usage
In React Components
Import and call the function on button click:In Astro Components
Use with a client directive for interactivity:How It Works
The function uses the DOMclassList.toggle() method to add or remove the dark class from document.documentElement (the <html> element).
Initial state
When the page loads, the
<html> element has no dark class, so light mode styles apply.First toggle
Calling
toggleTheme() adds the dark class to <html>, activating dark mode styles defined with the .dark selector.CSS Integration
The function works in conjunction with CSS that uses the.dark selector. For example, from src/styles/global.css:
Client Directive
The"use client" directive at the top of the file indicates this code should only run in the browser (client-side), not during server-side rendering.
documentis only available in the browser- Server-side rendering would fail if trying to access
document.documentElement
Limitations
No Persistence
The current implementation doesn’t save the user’s theme choice. Each page load starts in light mode regardless of the user’s previous selection.No System Preference Detection
The utility doesn’t check the user’s system-level dark mode preference (prefers-color-scheme).
Enhancement Ideas
Add localStorage persistence
Add localStorage persistence
Save the theme preference so it persists across page loads:
Respect system preference
Respect system preference
Initialize theme based on user’s system preference:
Return current theme
Return current theme
Make the function return the new theme state:This allows components to update their UI based on the current theme.
Add smooth transition
Add smooth transition
Prevent jarring color changes with CSS transitions:
Browser Compatibility
The function uses standard DOM APIs that are supported in all modern browsers:document.documentElement- IE9+classList.toggle()- IE10+ (with polyfill for IE9)
For IE9 support, you may need a
classList polyfill. Modern browsers (Chrome, Firefox, Safari, Edge) support this natively.Usage in Chitagá Tech
In the Chitagá Tech project, this utility is imported by the ButtonToggle component:Testing
To test the utility:- Open your browser’s developer console
- Run:
document.documentElement.classList.contains('dark') - Should return
falseinitially - Click the theme toggle button
- Run the same command again - should return
true - Reload the page - returns to
false(no persistence)
Related
ButtonToggle Component
React component that uses this utility
Styling Guide
Learn about the dark mode CSS implementation
Global Styles
See how dark mode styles are defined