# Theme toggle

> The sun sets into a moon, and the new theme spreads from the button in a circle.

Most theme toggles swap a sun icon for a moon icon and flash the page. Here the sun and moon are one drawing: its rays fold in and turn away while a shadow slides across the disc and carves the crescent, and the disc swells into the moon with a little give. The page follows in the same breath: the new theme spreads out from the button in a circle, using View Transitions where the browser has them, and switches without a flicker of half-animated colours where it doesn’t. It comes as a round button, a pill whose label morphs from "Light" to "Dark", or a Light, System, Dark switch with a thumb thrown to the choice. On its own it remembers the choice and follows the system; or drive it from next-themes.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/theme-toggle.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `text-morph`
- Docs and live preview: https://hairlineui.com/ui/theme-toggle
- Registry item (source): https://hairlineui.com/r/theme-toggle.json

## Usage

```tsx
import { ThemeToggle } from "@/components/theme-toggle";

// Manages itself: remembers the choice, follows the system, sets the dark class on <html>.
<ThemeToggle />
<ThemeToggle variant="segmented" />
```

## With next-themes

Already using next-themes? Hand it the theme and the setter; the toggle keeps the motion and the reveal.

```tsx
"use client";
import { useTheme } from "next-themes";
import { ThemeToggle, type Theme } from "@/components/theme-toggle";

export function NavThemeToggle() {
  const { theme, setTheme } = useTheme();
  return <ThemeToggle value={(theme as Theme) ?? "system"} onValueChange={setTheme} />;
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"icon" \| "pill" \| "segmented"` | `"icon"` | A round button, a button with a label, or a three-way switch with System. |
| `value / defaultValue / onValueChange` | `"light" \| "dark" \| "system"` | `— / "system"` | Control it (e.g. from next-themes), or let it keep its own choice. |
| `storageKey` | `string` | `"theme"` | Where an uncontrolled toggle remembers the choice in localStorage. |
| `attribute` | `"class" \| "data-theme"` | `"class"` | How an uncontrolled toggle marks dark mode on <html>; it also sets color-scheme. |
| `reveal` | `boolean` | `true` | Spread the new theme from the button in a circle where View Transitions are available. |

## States

- **light → dark**: Rays turn −90° and fold to 40% as they fade (520ms); the shadow slides onto the disc (560ms) and the disc grows from 55% to full size with a slight overshoot.
- **reveal**: A circle grows from the button’s centre to the far corner of the screen over 620ms, showing the new theme inside it. Skipped when the colours don’t actually change (System on a light system to Light).
- **pill**: The same icon, and "Light" morphs into "Dark", keeping no letters but easing the pill’s width.
- **segmented**: Light, System and Dark; the thumb is thrown to the choice (460ms, slight overshoot) while its width eases. Arrow keys, Home and End move between them.

## What it handles

Most theme toggles swap a sun icon for a moon and flash the page. This turns one drawing into the other, and the page follows in the same breath.

- **The sun sets into a moon**: The rays fold in, a shadow slides across the disc and carves the crescent. It is one drawing, not two icons.
- **The page follows in a circle**: The new theme spreads out from the button with View Transitions, and switches without a flicker where they are not supported.
- **Three looks**: A round icon button, a pill whose label morphs from “Light” to “Dark”, or a Light, System, Dark switch.
- **It remembers, or hands over**: On its own it remembers the choice and follows the system. Or drive it from next-themes.
- **First paint stays still**: A remembered or system theme is placed, not played, so nothing animates on load.
- **Says what it will do**: The button is named “Switch to dark theme” and the switch is a radio group with roving focus.

## Replaces

a theme switch, sun and moon icons, theme storage code, system theme detection, a view transition reveal

## Where it fits

- **Site headers**: A light and dark switch in the navigation.
- **App settings**: A Light, System and Dark choice in preferences.
- **Documentation sites**: A toggle that follows the reader’s system by default.
- **Landing pages with dark mode**: A small button with a moment people notice.

## Notes

- The button says what it will do ("Switch to dark theme") and is aria-pressed while dark; the switch is a radio group with roving focus.
- The first paint never animates: a remembered or system theme is placed, not played.
- Without View Transitions, colour transitions across the page are held for one frame during the switch, so nothing flickers.
- Reduced motion: the icon changes in place and there is no reveal.
- To avoid a flash of the wrong theme on load, set the class on <html> before your app renders (next-themes does this for you).

License: MIT (https://hairlineui.com/license#components)
