// Detect user's OS/browser color preference (dark/light mode)
const { colorScheme, toggleColorScheme } = usePreferredColorScheme({ autoApply: true });
Essential for theme switching, dark/light mode detection, and automatic UI theming based on user preferences.
Browser does not support color scheme detection
Try this:
Change your OS theme settings and see the automatic detection in action!
import { usePreferredColorScheme } from '@usegrand/react-hokss'; function ThemeProvider({ children }) { const { colorScheme, toggleColorScheme, isSupported } = usePreferredColorScheme({ autoApply: true, classNames: { light: 'light-theme', dark: 'dark-theme' } }); return ( <div className={colorScheme === 'dark' ? 'dark' : 'light'}> <button onClick={toggleColorScheme}> Switch to {colorScheme === 'dark' ? 'Light' : 'Dark'} Mode </button> {children} </div> ); }
Configuration options for color scheme detection
Default color scheme if no preference detected (default: 'light')
Whether to automatically apply classes to document (default: false)
CSS class names to apply for each scheme
Object containing color scheme state and controls
Current color scheme ('light' or 'dark')
Whether current scheme is from user preference
Whether media query is supported by browser
Function to manually set color scheme
Function to toggle between light and dark themes