Back to ButtonsYou are on Theme Toggler Button.

Theme Toggler Button

A theme button whose icon spins, shrinks and blurs away.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks
PrequelUI

Libraries

PrequelUI

Motion

Spring + short tween

Reduced motion

Fade in place
theme-toggler-button.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/theme-toggler-button.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/theme-toggler-button.json
npmthe default on every machine

First component? Set up your project first

Frameworks

The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.

5 runtimes
theme-toggler-button.tsx
"use client";

import * as React from "react";
import { AnimatePresence, motion, type Transition } from "motion/react";
import { cn } from "@/lib/utils";
import { Button, type ButtonProps } from "./button";
import {
  blurIcon,
  swapTween,
  useReduceMotion,
  useTempoTransition,
} from "./motion";

export type ThemeSelection = "light" | "dark" | "system";
export type ResolvedTheme = "light" | "dark";

type ThemeContextValue = {
  theme: ThemeSelection;
  resolvedTheme: ResolvedTheme;
  next: ThemeSelection;
};

const ThemeContext = React.createContext<ThemeContextValue | null>(null);

const DEFAULT_MODES: ThemeSelection[] = ["light", "dark", "system"];

function nextTheme(
  theme: ThemeSelection,
  resolved: ResolvedTheme,
  modes: ThemeSelection[],
): ThemeSelection {
  let index = modes.indexOf(theme);
  if (index === -1) index = modes.indexOf(resolved);
  if (index === -1) return modes[0] ?? resolved;
  return modes[(index + 1) % modes.length]!;
}

export type ThemeTogglerButtonProps = ButtonProps & {
  theme: ThemeSelection;
  resolvedTheme: ResolvedTheme;
  setTheme: (theme: ThemeSelection) => void;
  modes?: ThemeSelection[];
};

export function ThemeTogglerButton({
  theme,
  resolvedTheme,
  setTheme,
  modes = DEFAULT_MODES,
  onClick,
  className,
  ...props
}: ThemeTogglerButtonProps) {
  const next = nextTheme(theme, resolvedTheme, modes);
  const value = React.useMemo(
    () => ({ theme, resolvedTheme, next }),
    [next, resolvedTheme, theme],
  );

  return (
    <ThemeContext.Provider value={value}>
      <Button
        data-slot="theme-toggler-button"
        data-theme={theme}
        aria-label={`Switch to ${next} theme`}
        onClick={(event) => {
          onClick?.(event);
          if (!event.defaultPrevented) setTheme(next);
        }}
        className={cn("border bg-background text-foreground hover:bg-accent", className)}
        {...props}
      />
    </ThemeContext.Provider>
  );
}

export type ThemeTogglerButtonIconProps = Omit<React.ComponentProps<"span">, "children"> & {
  light: React.ReactNode;
  dark: React.ReactNode;
  system?: React.ReactNode;
  rotate?: number;
  transition?: Transition;
};

export function ThemeTogglerButtonIcon({
  light,
  dark,
  system,
  rotate = 90,
  transition,
  className,
  ...props
}: ThemeTogglerButtonIconProps) {
  const context = React.useContext(ThemeContext);
  const reduce = useReduceMotion();
  const tween = useTempoTransition(transition ?? swapTween);
  const key = context?.theme ?? "light";
  const content = key === "system" ? system ?? light : key === "dark" ? dark : light;
  const hidden = reduce
    ? { opacity: 0, filter: blurIcon }
    : { opacity: 0, scale: 0.5, rotate: -rotate, filter: blurIcon };
  const shown = reduce
    ? { opacity: 1, filter: "blur(0px)" }
    : { opacity: 1, scale: 1, rotate: 0, filter: "blur(0px)" };

  return (
    <span
      data-slot="theme-toggler-button-icon"
      className={cn("relative inline-flex size-4 items-center justify-center", className)}
      {...props}
    >
      <AnimatePresence mode="popLayout" initial={false}>
        <motion.span
          key={key}
          aria-hidden="true"
          initial={hidden}
          animate={shown}
          exit={hidden}
          transition={reduce ? { duration: 0.15 } : tween}
          className="absolute inset-0 inline-flex items-center justify-center"
        >
          {content}
        </motion.span>
      </AnimatePresence>
    </span>
  );
}

export function useThemeTogglerButton() {
  const value = React.useContext(ThemeContext);
  if (!value) throw new Error("useThemeTogglerButton must be used inside ThemeTogglerButton");
  return value;
}

Install

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/theme-toggler-button.json
npmthe default on every machine

Dependencies

motion lucide-react

The registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.

02

Usage

Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.

theme-toggler-button.tsx
import { Moon, Sun } from "lucide-react";
import { useTheme } from "@/components/site/theme-provider";
import {
  ThemeTogglerButton,
  ThemeTogglerButtonIcon,
} from "@/components/prequelui/theme-toggler-button";

export default function Demo() {
  const { theme, resolvedTheme, setTheme } = useTheme();
  return (
    <ThemeTogglerButton
      theme={theme}
      resolvedTheme={resolvedTheme}
      setTheme={setTheme}
    >
      <ThemeTogglerButtonIcon light={<Sun />} dark={<Moon />} />
    </ThemeTogglerButton>
  );
}
import { ThemeTogglerButton } from "@/components/prequelui/theme-toggler-button";

export function Variant() {
  return (
    <ThemeTogglerButton tempo={1.2}>
      …
    </ThemeTogglerButton>
  );
}
03

Animation

The icon does not disappear: it turns a quarter turn, shrinks to half and blurs while the next icon arrives from the opposite rotation. The swap runs in 300 ms and the button keeps the shared press spring.

  • Icon swap: 300 ms ease-out with rotate 90° and scale 0.5.
  • The outgoing icon rotates one way, the incoming icon the other.
  • The first paint is instant, so hydration can never animate the initial icon.
  • Reduced motion swaps with a plain fade.
swap 300 msrotate 90°scale 0.5instant first paint
04

API Reference

Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.

ThemeTogglerButton

PropTypeDefault
theme"light" | "dark" | "system"—
resolvedTheme"light" | "dark"—
setTheme(theme: Theme) => void—
modesTheme[]["light","dark","system"]

ThemeTogglerButtonIcon

PropTypeDefault
lightReactNode—
darkReactNode—
systemReactNode—
rotatenumber90

Next component

Gradient Text

Text filled with a gradient that sweeps slowly across it.