ThemeProvider
TheThemeProvider component manages dark mode by setting the dark class on the document root. It prioritizes the widget theme from OpenAI Apps SDK, falling back to system preference if the widget API is not available.
Import
Props
| Prop | Type | Default | Description |
|---|---|---|---|
children | React.ReactNode | required | The widget content to wrap |
Theme Priority
The provider uses the following priority:- Widget theme (from
window.openai.theme) - When Apps SDK is available - System preference (
prefers-color-scheme: dark) - Fallback when widget API is not available
Basic Usage
Automatic Inclusion
ThemeProvider is automatically included when using McpUseProvider:
Theme Synchronization
The provider:- Syncs with ChatGPT: Automatically matches ChatGPT’s theme when running in Apps SDK
- Listens to system changes: Updates when user changes system theme preference
- Prevents flash: Uses
useLayoutEffectto apply theme synchronously before browser paint
Dark Class Management
The provider adds/removes thedark class on document.documentElement:
dark class for dark mode.
Usage with Tailwind CSS
When using Tailwind CSS, the dark class enables dark mode variants:System Preference Detection
When the widget API is not available, the provider:- Detects initial system preference on mount
- Listens to
prefers-color-schememedia query changes - Updates theme when user changes system preference
Related Components
McpUseProvider- Includes ThemeProvider automaticallyuseWidget- Hook for accessing theme and other widget data