Skip to content
Browse docs and components

Theme Toggle

theme-toggle

A sun that sets into a crescent moon, and the new theme revealed from the button with a View Transition: a circle, a soft-edged spill or a shutter, measured to the farthest corner. No theme library, no flash, and a setTheme you can call from anywhere.

Responsive

Install

$npxshadcnadd@forme/theme-toggle

Adds the component, its registry dependencies, and the token stylesheet you import once.

Usage

import { ThemeToggle, themeScript } from "@/components/forme/theme-toggle/theme-toggle";

// In <head>, so the stored theme applies before the first paint:
<script dangerouslySetInnerHTML={{ __html: themeScript }} />

<ThemeToggle reveal="circle" />

When to use

Reach for it

  • Switching between the light and dark theme from a header or a settings page.

Use something else

  • Offering a “system” option as well — that needs a three-way control, such as a SegmentedControl.

API reference

Read from the source, so it is the API of the file you install. A prop marked * is required.

ThemeToggle

Switches between the light and dark theme: a sun that sets into a crescent moon, and a reveal of the new theme that grows out of the button.

PropTypeDefaultDescription
reveal'circle' | 'none' | 'soft' | 'shutter'"circle"
onChange(theme: Theme) => void—Called with the new theme, e.g. to save it to an account.

Also takes every prop of <button> attributes.

Accessibility

  • It is a toggle button named “Dark mode” and pressed while dark is on.
  • The icon is drawn from the .dark class, so the server and client render the same and there is no flash of the wrong icon.

Motion

  • The new theme is revealed from the button with a View Transition: a circle, a soft circle, a shutter, or none.
  • Where View Transitions are missing, or with reduced motion, the theme simply changes.