$npx @dinachi/cli@latest add animated-icon
activemodescale
rotate
flip
fade
classNamemedia-controls.tsx
import { useState } from "react"
import { Check, Copy, Heart, Pause, Play, Volume2, VolumeX } from "lucide-react"
import { AnimatedIcon } from "@/components/ui/animated-icon"
export function MediaControls() {
const [playing, setPlaying] = useState(false)
const [muted, setMuted] = useState(false)
const [copied, setCopied] = useState(false)
const [liked, setLiked] = useState(false)
return (
<div className="flex gap-3">
<button type="button" aria-label="Play" aria-pressed={playing} onClick={() => setPlaying((v) => !v)}>
<AnimatedIcon active={playing} from={<Play />} to={<Pause />} />
</button>
<button type="button" aria-label="Mute" aria-pressed={muted} onClick={() => setMuted((v) => !v)}>
<AnimatedIcon active={muted} from={<Volume2 />} to={<VolumeX />} />
</button>
<button type="button" aria-label="Copy" aria-pressed={copied} onClick={() => setCopied((v) => !v)}>
<AnimatedIcon active={copied} from={<Copy />} to={<Check />} />
</button>
<button type="button" aria-label="Like" aria-pressed={liked} onClick={() => setLiked((v) => !v)}>
<AnimatedIcon active={liked} from={<Heart />} to={<Heart className="fill-current text-rose-500" />} />
</button>
</div>
)
}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 />} />scaleDefault
rotateTurns over
flipAbout the horizontal
fadeOpacity only
Four ways for the two icons to trade places.
// scale is the default: the outgoing icon drops to 0.7 and the incoming one rises.
<AnimatedIcon active={open} from={<Menu />} to={<X />} />
<AnimatedIcon active={open} from={<Menu />} to={<X />} mode="rotate" />
<AnimatedIcon active={open} from={<Menu />} to={<X />} mode="flip" />
<AnimatedIcon active={open} from={<Menu />} to={<X />} mode="fade" />48px
One class on the shell sizes both icons.
// The shell owns the size and both icons fill it. Default is size-5.
<AnimatedIcon active={dark} mode="rotate" from={<Sun />} to={<Moon />} className="size-4" />
<AnimatedIcon active={dark} mode="rotate" from={<Sun />} to={<Moon />} className="size-8" />
// Do not size the icons individually: an icon library ships an intrinsic 24x24,
// which a smaller box squashes on one axis, and two icons sized apart jump.
<AnimatedIcon active={dark} from={<Bell />} to={<BellOff />} className="size-12" />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.