Skip to main content

Overview

The useColorMode hook provides access to the current color mode state and a function to update it. It must be used within a ColorModeProvider component.

Import

Signature

Return Value

Returns a tuple with two elements:
string
required
The current color mode name (e.g., 'light', 'dark', or a custom mode name)
Dispatch<SetStateAction<string>>
required
A function to update the color mode. Accepts either a color mode name string or a function that receives the current mode and returns the new mode.

Usage

Basic Example

With Type Safety

You can specify a narrower type for your color mode names:

With Functional Updates

Configuration

The behavior of useColorMode is controlled by settings in your theme configuration:

Configuration Options

string
The default color mode to use. Should be a unique name and not reference a key in theme.colors.modes.
boolean
default:true
Whether to persist the color mode selection in localStorage.
boolean | 'system'
default:true
Controls system color scheme integration:
  • false: Disabled
  • true: Use system preference on initial load only
  • 'system': Always follow system preference (overrides user selection)

Error Handling

useColorMode throws an error if called outside of a ColorModeProvider. Make sure your component tree is wrapped with ColorModeProvider or use the main ThemeProvider from theme-ui which includes color mode support.

Notes

  • Color mode changes are automatically persisted to localStorage (unless disabled)
  • The selected color mode is stored with the key theme-ui-color-mode
  • If localStorage is disabled in the browser, a warning will be logged to the console
  • When useColorSchemeMediaQuery is set to 'system', the hook will automatically update when the system color scheme changes