UI & Layout
usePreferredColorScheme
About
React hook to detect and track the user's preferred color scheme (dark mode or light mode). Automatically updates when the user changes their system preference.
Installation
Usage
Return Value
Returns an object with the following properties:
| Property | Type | Description |
|---|---|---|
| colorScheme | "light" | "dark" | "no-preference" | null | The preferred color scheme |
| isDark | boolean | Whether the preferred scheme is dark |
| isLight | boolean | Whether the preferred scheme is light |
Examples
Theme Toggler with System Preference
Automatic Theme Application
Theme-Aware Image Loading
CSS Variables with Color Scheme
Features
- Automatic detection - Reads system color scheme preference
- Real-time updates - Updates when user changes system settings
- Helper booleans - Convenient
isDarkandisLightflags - SSR compatible - Returns
nullon server-side rendering - TypeScript support - Full type definitions included
Browser Support
The prefers-color-scheme media query is supported in:
- Chrome 76+
- Firefox 67+
- Safari 12.1+
- Edge 79+
- Opera 62+
Notes
- The hook automatically subscribes to system theme changes
- Returns
nullduring server-side rendering colorSchemecan be"light","dark", or"no-preference"- Updates happen in real-time when user changes system settings
- Use this to provide automatic dark mode support
- Consider allowing users to override system preference