Components

Theme Toggle

An animated dark mode toggle for React and Tailwind CSS: the icon morphs from a sun into a moon and the new theme is revealed with a circle that grows out of the button, using the View Transitions API. It works with any theme setup (next-themes, a class on <html>, or a single panel) and falls back to an instant switch.

  • View Transitions API
  • Sun ↔ moon morph
  • Works with next-themes
  • Scoped to any element
  • prefers-reduced-motion
Acme Analytics

Last 7 days

Visitors

24.8k

+12%

Signups

1,204

+8%

Installation

$ npx shadcn@latest add https://duskui.com/r/theme-toggle.json

On its own it toggles a dark class and color-scheme on <html>. Using next-themes? Pass its state in and set applyClass={false} so next-themes stays in charge of the class.

Usage

site-header.tsx
"use client"

import { useTheme } from "next-themes"
import { ThemeToggle } from "@/components/dusk/theme-toggle/theme-toggle"

export function SiteHeaderToggle() {
  const { resolvedTheme, setTheme } = useTheme()

  return (
    <ThemeToggle
      theme={resolvedTheme === "light" ? "light" : "dark"}
      onThemeChange={setTheme}
      applyClass={false}
    />
  )
}

Examples

Sizes, variants & speed

Three sizes, an outline or ghost look and a duration for the reveal. Each tile is its own target, so only that tile switches.

sm · ghost
default
lg · 1s reveal

Controlled

Drive it with theme and onThemeChange. Clicks on the toggle get the circular reveal; changes made elsewhere apply instantly.

Reading mode

The toggle reveals the new theme from the button. Changing the theme from outside (the buttons below) updates the same state and switches instantly.

theme: dark

API Reference

Also accepts any <button> attribute except onClick.

PropTypeDefaultDescription
theme"light" | "dark"—Current theme (controlled).
defaultTheme"light" | "dark""dark"Initial theme when uncontrolled.
onThemeChange(theme) => void—Called with the next theme, inside the view transition so the new snapshot includes it.
targetRefObject<HTMLElement> | HTMLElement | () => HTMLElementdocument.documentElementElement that receives the dark class and is revealed. Pass a ref to scope the switch to one panel.
applyClassbooleantrueToggle darkClass and color-scheme on the target. Turn off when a theme library manages the class.
darkClassstring"dark"Class added to the target in dark mode.
transition"circle" | "none""circle"Circular reveal from the button, or an instant switch.
durationnumber600Reveal duration in milliseconds.
easingstring"cubic-bezier(0.65, 0, 0.35, 1)"CSS easing for the reveal.
size"sm" | "default" | "lg""default"Button size.
variant"outline" | "ghost""outline"Button style. Colors follow currentColor, so they adapt to either theme.
labelstring"Dark mode"Accessible name of the button.

Accessibility

  • It is a real <button> with aria-pressed, so screen readers announce “Dark mode, toggle button, pressed” when dark mode is on.
  • Fully keyboard operable with Enter and Space, with a visible focus ring.
  • With prefers-reduced-motion the theme switches instantly, with no circular reveal.
  • Browsers without the View Transitions API get the same instant switch; nothing breaks.
  • Setting color-scheme on the target keeps native controls and scrollbars in the matching theme.