Skip to main content
Theme UI provides built-in support for multiple color modes, including dark mode, with automatic persistence to localStorage and media query detection.

Basic Color Modes Setup

Define color modes in your theme:

The useColorMode Hook

Use the useColorMode hook to read and update the current color mode:
Example:

Color Mode Provider

Wrap your app with ColorModeProvider:

Configuration Options

initialColorModeName

Set the default color mode:
Do not use a key from theme.colors.modes as the initialColorModeName. It should be a unique name for your base color mode.

useColorSchemeMediaQuery

Detect the user’s system preference:
Options:
  • false (default): Don’t detect system preference
  • true or 'initial': Detect on initial load only
  • 'system': Always follow system preference with live updates
Example from Theme UI source:

useLocalStorage

Persist color mode to localStorage:
Set to false to disable persistence:
From the source code:

printColorModeName

Set a specific color mode for printing:

useCustomProperties

Enable or disable CSS custom properties (CSS variables):
Set to false for legacy browser support (IE11):

Multiple Color Modes

Define multiple color modes beyond just light and dark:
Toggle between modes:

Media Query Detection

Theme UI automatically detects the prefers-color-scheme media query:

Example: Complete Color Mode Setup

Here’s a complete example from the Theme UI source:

Accessing Color Values

When using CSS custom properties (default), colors are converted to CSS variables:
When useCustomProperties is enabled, use theme.rawColors to access the original color values, as theme.colors will contain CSS custom property references.

Preventing Flash on Load

Theme UI includes a script to prevent color mode flash during SSR:
Add this to your document head in SSR frameworks:

TypeScript Support

Type-safe color mode with TypeScript: