Animated Icon

Animated

Two icons in one place that swap with a transition.

Source
$npx @dinachi/cli@latest add animated-icon
active
mode
className

Usage

import { useState } from "react"
import { Pause, Play } from "lucide-react"
import { AnimatedIcon } from "@/components/ui/animated-icon"

export function PlayButton() {
  const [playing, setPlaying] = useState(false)
  return (
    <button
      type="button"
      aria-label={playing ? "Pause" : "Play"}
      onClick={() => setPlaying((p) => !p)}
    >
      <AnimatedIcon active={playing} from={<Play />} to={<Pause />} />
    </button>
  )
}

Examples

The shell holds any two icons you give it.
import { AnimatedIcon } from '@/components/ui/animated-icon';
import { Play, Pause, Volume2, VolumeX, Copy, Check } from 'lucide-react';

export function Example() {
  const [playing, setPlaying] = useState(false);

  // The icon is decoration; the button is what gets named.
  return (
    <button
      type="button"
      aria-label={playing ? 'Pause' : 'Play'}
      onClick={() => setPlaying((v) => !v)}
    >
      <AnimatedIcon active={playing} from={<Play />} to={<Pause />} />
    </button>
  );
}

// Nothing about the shell is play-specific.
<AnimatedIcon active={muted} from={<Volume2 />} to={<VolumeX />} />
<AnimatedIcon active={copied} from={<Copy />} to={<Check />} />

Notes

Motion tier

Needs motion. add --all skips it; install with add --motion or by name.

Bring your own icons

Ships no icons. Pass any two.

<AnimatedIcon active={muted} from={<Volume2 />} to={<VolumeX />} />
<AnimatedIcon active={copied} from={<Copy />} to={<Check />} />
<AnimatedIcon active={open} from={<Menu />} to={<X />} mode="rotate" />

It does not morph paths; it swaps with a transform.

Modes

  • scale (default): the outgoing icon shrinks to 0.7 and fades; the incoming one grows from 0.7.
  • rotate: adds a quarter turn each way.
  • flip: rotates about the horizontal axis, with perspective.
  • fade: opacity only.

Timing

150ms, no bounce, no delay. Nothing animates on mount.

Sizing

Size the shell with className; both icons fill it. Do not size the icons individually, or they jump as they swap.

<AnimatedIcon active={dark} from={<Sun />} to={<Moon />} className="size-8" />

When to use it

  • A two-state control: play, mute, a disclosure.
  • A one-off confirmation, such as copy becoming a check.
  • Skip it for icons that never change or are decorative.

API

activeboolean
Which icon shows. false shows from, true shows to.
fromReactNode
The resting icon. Any element: a lucide icon, inline svg or emoji.
toReactNode
The icon for the active state.
mode"scale" | "rotate" | "flip" | "fade" = "scale"
How the two trade places.
classNamestring = "size-5"
Sizes and colours both icons.

Accessibility

  • The shell is aria-hidden. Name the control around it.
  • If the button's label does not change with state, use aria-pressed or aria-expanded.
  • Reduced motion, from the OS or an app-level MotionConfig, cuts straight to the new icon.