Animated icon swap
When a copy button turns into a checkmark, a short crossfade tells you something happened. A hard cut reads as a glitch, or isn't noticed at all.
12345{ "rules": { "systomic/animated-icon-swap": "warn" }}See the difference
pnpm add -D systomic{copied ? <CheckIcon /> : <CopyIcon />}pnpm add -D systomic<AnimatePresence mode="popLayout">…</AnimatePresence>What doesn't work
2 findingsinvalid.tsx
123456789101112131415161718import { Check, Copy } from "lucide-react";import { AnimatePresence } from "motion/react";
export function CopyButton({ copied }: { copied: boolean }) { return ( <button> {copied ? <Check /> : <Copy />} </button> );}
export function UnkeyedSwap({ copied }: { copied: boolean }) { return ( <AnimatePresence mode="popLayout" initial={false}> {copied ? <Check /> : <Copy />} </AnimatePresence> );}systomic/animated-icon-swap This swaps Check for Copy without animation. Wrap it in <AnimatePresence> or <IconSwap>, or animate both icons with motion.
systomic/animated-icon-swap Give each icon a unique key so AnimatePresence can animate the swap.
What works
No findingsvalid.tsx
1234567891011121314151617181920import { Check, Copy } from "lucide-react";import { AnimatePresence, motion } from "motion/react";
export function CopyButton({ copied }: { copied: boolean }) { return ( <button> <AnimatePresence mode="popLayout" initial={false}> {copied ? ( <motion.span key="check" initial={{ scale: 0.5, opacity: 0 }} animate={{ scale: 1, opacity: 1 }}> <Check /> </motion.span> ) : ( <motion.span key="copy" initial={{ scale: 0.5, opacity: 0 }} animate={{ scale: 1, opacity: 1 }}> <Copy /> </motion.span> )} </AnimatePresence> </button> );}Details
Reports a ternary that renders two icons as JSX children unless it is inside AnimatePresence, an animated wrapper or a motion.* element. Inside AnimatePresence, both icons need a key.
| Option | Default |
|---|---|
iconSources |
lucide-react, @heroicons/react/*, @phosphor-icons/react, @radix-ui/react-icons, @tabler/icons-react, react-icons/*, @/icons |
iconNamePattern |
^[A-Z]…Icon$ or ^Icon[A-Z]…$ |
animatedWrappers |
["AnimatePresence", "IconSwap"] |
animatedElementPattern |
^(motion|m)\. |
checkAttributes |
false (also check icon={a ? <A /> : <B />}) |