Skip to content
UI
On this page

Theme Switcher

Switches between light and dark themes or follows the system setting.

Overview

Copy the source into your project. Import the parts that you need. Styles use local StyleX declarations and shared theme tokens. You can change the source without a wrapper or replacement package.

Preview

Light, dark, and system theme controls

Installation

Run the init command from your project root first. The CLI configures StyleX and installs shared files and dependencies.

Run from your project root. The CLI installs required components, shared files, and packages. Previews may need additional components. The CLI keeps existing files.

Usage

Set up ThemeProvider and ThemeScript as shown in the theming guide. Render either control inside ThemeProvider.

Use ThemeToggle for an icon button that sets the opposite of the current light or dark theme. This sets a fixed preference, even if the previous preference was System.

Use ThemeSwitcher to select Light, Dark, or System. System follows the device setting. The provider saves the selected preference when browser storage is available.

Both controls stay disabled before hydration. Set disabled to prevent changes. Use className or xstyle to style the control.

Use the arrow keys to move between the theme options. Press Space or Enter to select the focused option.

Import from the local file installed by the CLI.

API reference

The generator reads the component TypeScript source. Only component and Base UI props appear below. The Required column shows whether you must supply a property. It does not mean you must use the component.

View API reference