Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

4.5 KB
Raw Blame
---
title: Theme Toggler
date: 2025-08-17
description: Animated theme toggle using the View Transitions API with configurable clip-path shapes and origin.
author: Nazam Kalsi
published: true
---

<ComponentPreview name="animated-theme-toggler-demo" />

## Installation

<Tabs defaultValue="cli">

<TabsList>
  <TabsTrigger value="cli">CLI</TabsTrigger>
  <TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>
<TabsContent value="cli">

```bash
npx shadcn@latest add @magicui/animated-theme-toggler
```

</TabsContent>

<TabsContent value="manual">

<Steps>

<Step>Copy and paste the following code into your project.</Step>

<ComponentSource name="animated-theme-toggler" />

<Step>Update the import paths to match your project setup.</Step>

<Step>Add the required CSS into your global CSS file</Step>

The component sets `--magicui-theme-toggle-vt-duration` and a `data-magicui-theme-vt="active"` flag on `<html>` only while a toggle is in flight, so the clip-path animation stays in sync with the view-transition group **without affecting any other view transitions in your app** (e.g. Next.js navigation transitions).

```css title="app/globals.css" showLineNumbers
::view-transition-old(root),
::view-transition-new(root) {
  animation: none;
  mix-blend-mode: normal;
}

/* Scoped to AnimatedThemeToggler — the toggler sets the data-attribute and the variable only during a theme toggle. */
html[data-magicui-theme-vt="active"]::view-transition-group(root) {
  animation-duration: var(--magicui-theme-toggle-vt-duration);
}
```

</Steps>

</TabsContent>

</Tabs>

## Usage

```tsx showLineNumbers
import { AnimatedThemeToggler } from "@/components/ui/animated-theme-toggler"
```

```tsx showLineNumbers
<AnimatedThemeToggler />
```

Use `variant` to change the clip-path shape of the view transition (`circle`, `square`, `triangle`, `diamond`, `rectangle`, `hexagon`, or `star`):

```tsx showLineNumbers
<AnimatedThemeToggler variant="star" />
```

Expand from the viewport center instead of the button with `fromCenter`:

```tsx showLineNumbers
<AnimatedThemeToggler variant="hexagon" duration={600} fromCenter />
```

## Examples

The preview at the top of this page uses the default **circle** reveal. Below, each variant has its own isolated preview so you can test one shape at a time.

### Square

<ComponentPreview name="animated-theme-toggler-square-demo" />

### Diamond

<ComponentPreview name="animated-theme-toggler-diamond-demo" />

### Rectangle

<ComponentPreview name="animated-theme-toggler-rectangle-demo" />

### Hexagon

<ComponentPreview name="animated-theme-toggler-hexagon-demo" />

### Triangle

<ComponentPreview name="animated-theme-toggler-triangle-demo" />

### Star

<ComponentPreview name="animated-theme-toggler-star-demo" />

### Next.js (next-themes)

Pass `theme` and `onThemeChange` to keep `useTheme()` subscribers in sync on toggle. When controlled, the component skips `localStorage` and lets the parent own persistence.

<ComponentPreview name="animated-theme-toggler-next-themes-demo" />

## Props

| Prop            | Type                                 | Default    | Description                                                                         |
| --------------- | ------------------------------------ | ---------- | ----------------------------------------------------------------------------------- |
| `className`     | `string`                             | —          | Additional classes for the button                                                   |
| `duration`      | `number`                             | `400`      | Duration of the theme transition animation in milliseconds                          |
| `variant`       | `TransitionVariant`                  | `"circle"` | Shape used for the view-transition clip-path reveal                                 |
| `fromCenter`    | `boolean`                            | `false`    | If true, the clip expands from the viewport center instead of the button            |
| `theme`         | `"light" \| "dark"`                  | —          | Controlled theme value. When set, the parent owns persistence (e.g. `next-themes`). |
| `onThemeChange` | `(theme: "light" \| "dark") => void` | —          | Called on toggle. Pair with `theme` for controlled usage.                           |

Export `TransitionVariant` from the same module when you need typed props or menus.

## Credits

- Credit to [Nazam Kalsi](https://nazam-kalsi-portfolio.vercel.app), [chishiyac](https://github.com/chishiyac), [dikshantgulekar20-oss](https://github.com/dikshantgulekar20-oss)