# Theme Toggle Button

A persistent theme control with optional View Transition shapes.

A persistent theme control with optional View Transition shapes.

```tsx
import { MoonIcon, SunIcon } from "@phosphor-icons/react";
import { ThemeToggleButton } from "@zentara-co/zenos-ui";

function ThemeToggleButtonExample() {
  return (
    <ThemeToggleButton
      storageKey="starlight-theme"
      icon={({ theme: current }) => (current === "dark" ? <MoonIcon /> : <SunIcon />)}
    />
  );
}
```

## Import

```tsx
import { ThemeToggleButton } from "@zentara-co/zenos-ui";
```

## Text and icons

The button accepts text, an icon, or both. Each can be a render function receiving the current and next theme. Icon-only controls receive a default accessible name based on the next action.

```tsx
import { MoonIcon, SunIcon } from "@phosphor-icons/react";
import { Stack, ThemeToggleButton } from "@zentara-co/zenos-ui";

function ThemeToggleButtonContentExample() {
  return (
    <Stack direction="row" gap="3" align="center" wrap="wrap">
      <ThemeToggleButton storageKey="starlight-theme">
        {({ nextTheme }) => `Use ${nextTheme} theme`}
      </ThemeToggleButton>
      <ThemeToggleButton
        storageKey="starlight-theme"
        icon={({ theme: current }) => (current === "dark" ? <MoonIcon /> : <SunIcon />)}
      />
      <ThemeToggleButton
        storageKey="starlight-theme"
        icon={({ theme: current }) => (current === "dark" ? <MoonIcon /> : <SunIcon />)}
      >
        {({ nextTheme }) => `Switch to ${nextTheme}`}
      </ThemeToggleButton>
    </Stack>
  );
}
```

## Transition shapes

When the browser supports the View Transition API, the next theme is revealed using the selected clip shape. Unsupported browsers and users requesting reduced motion receive an immediate theme change.

```tsx
import { MoonIcon, SunIcon } from "@phosphor-icons/react";
import { Stack, ThemeToggleButton, type ThemeTransitionShape } from "@zentara-co/zenos-ui";

function ThemeToggleButtonShapesExample() {
  const shapes: ThemeTransitionShape[] = [
    "circle",
    "square",
    "diamond",
    "rectangle",
    "hexagon",
    "triangle",
  ];

  return (
    <Stack direction="row" gap="3" wrap="wrap" justify="center">
      {shapes.map((shape) => (
        <ThemeToggleButton
          key={shape}
          storageKey="starlight-theme"
          shape={shape}
          icon={({ theme: current }) => (current === "dark" ? <MoonIcon /> : <SunIcon />)}
        >
          {shape}
        </ThemeToggleButton>
      ))}
    </Stack>
  );
}
```

## Origin and duration

The reveal starts at the button by default. Set `fromCenter` for a viewport-centered transition and tune `duration` in milliseconds.

```tsx
import { Stack, ThemeToggleButton } from "@zentara-co/zenos-ui";

function ThemeToggleButtonOriginExample() {
  return (
    <Stack direction="row" gap="3" wrap="wrap">
      <ThemeToggleButton storageKey="starlight-theme" shape="circle" duration={650}>
        Reveal from button
      </ThemeToggleButton>
      <ThemeToggleButton storageKey="starlight-theme" shape="circle" duration={650} fromCenter>
        Reveal from center
      </ThemeToggleButton>
    </Stack>
  );
}
```

## Controlled and persistent themes

Pass `theme` and `onThemeChange` when an application theme provider owns state. Without `theme`, the button updates the document root and persists through `useTheme`; `storageKey={null}` disables persistence.

The live examples on this page use `storageKey="starlight-theme"`, matching the documentation shell so every toggle reads and writes the same preference. In your application, use the same key as your theme initialization script or provider.

## Variants

circle, square, diamond, rectangle, hexagon, triangle

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| theme | light \| dark | — | Controlled theme. |
| defaultTheme | light \| dark | light | Initial uncontrolled theme. |
| onThemeChange | (theme: Theme) => void | — | Runs after theme changes. |
| shape | ThemeTransitionShape | circle | View-transition reveal shape. |
| duration | number | 400 | Transition duration in milliseconds. |
| storageKey | string \| null | theme | Persistence key; null disables persistence. |
| icon / children | ReactNode \| (renderProps) => ReactNode | — | Theme-aware icon and visible content. |
| fromCenter | boolean | false | Starts the reveal at the viewport center instead of the button. |
| native props | React.ButtonHTMLAttributes<HTMLButtonElement> | — | Forwards native attributes, events, aria-*, data-*, className, style, and ref. |

Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props, including className, style, aria-*, and data-*.

## Accessibility

ThemeToggleButton is a native pressed button. `aria-pressed` communicates whether dark mode is active, while its accessible name describes the next action. Keep icon-only controls labeled, and do not disable the reduced-motion fallback. Consumers remain responsible for semantic token contrast in both themes.

Source: https://zenos-ui.pages.dev/components/theme-toggle-button/