What You'll Learn
- 1Match an animation library to the problem: reactive layout, choreography, or imperative timelines
- 2Structure enter, exit and staggered animations with variants instead of scattered state
- 3Keep animation work on the compositor by animating transform and opacity only
- 4Honor reduced-motion preferences without removing every transition
Animation decisions go wrong when they are made by feel rather than by task. A library is chosen because a demo looks impressive, and six months later the codebase contains three approaches: inline style changes driven by state, a library used for one modal, and a CSS transition fighting a re-render for control of the same property. The useful question is not which library is best. It is what the animation has to coordinate â React's lifecycle, layout that other content depends on, a scroll position, a sequence of forty timed steps â because each of those problems has a different natural tool.
What each tool is actually built for
Framer Motion is reactive. Animations are derived from props and state, so when a component renders with a new value the animation follows, and when it unmounts the exit animation runs because the library is inside React's commit cycle. That is what makes layout animation and shared-element transitions possible: the library measures before and after and interpolates between the two, which no stylesheet can do because the stylesheet never sees the old position.
GSAP is imperative and timeline-based. You build a timeline, add tweens with positions, and control playback â pause, reverse, seek, repeat â which is exactly what a product tour, a logo sting or a scroll-driven sequence needs. It does not know about React lifecycles, so you own creation and teardown, but in exchange it will animate anything: SVG attributes, canvas, text splitting, custom properties and properties that have no CSS equivalent.
| Task | Natural fit | Why |
|---|---|---|
| Enter and exit of conditional UI | Framer Motion or CSS | Exit needs a component that outlives the state flip |
| Layout shifts that push siblings | Framer Motion | Measures old and new boxes and tweens between them |
| Scroll-linked storytelling | GSAP with ScrollTrigger | Sequences tied to scroll offsets with scrubbing |
| Long choreographed sequences | GSAP timeline | Positions, labels and seeking in one place |
| Hover, focus and simple states | Plain CSS | Zero JavaScript, no re-render, browser-optimised |
| Physics-based drag release | Framer Motion | Springs and gestures are declarative props |
Variants, staggering and the exit problem
The mistake that makes animation code unreadable is putting animation state in component state: an isEntering flag, a setTimeout to clear it, and a second flag for leaving. Variants invert that. A component declares named visual states, and the parent propagates one of them to every child, so a list does not need to know whether it is animating in or out â it just responds to the state the parent set. Exit animations work because AnimatePresence keeps a component mounted until its exit variant completes, which is the piece that state flags can never reproduce correctly.
import { motion, AnimatePresence } from 'framer-motion';
const listVariants = {
hidden: {},
visible: { transition: { staggerChildren: 0.06, delayChildren: 0.1 } },
};
const itemVariants = {
hidden: { opacity: 0, y: 12, scale: 0.98 },
visible: { opacity: 1, y: 0, scale: 1, transition: { type: 'spring', stiffness: 320, damping: 28 } },
exit: { opacity: 0, x: -16, transition: { duration: 0.18 } },
};
export function ResultsList({ items, query }) {
return (
<AnimatePresence mode="popLayout">
<motion.ul key={query} variants={listVariants} initial="hidden" animate="visible" exit="hidden">
{items.map((item) => (
<motion.li key={item.id} variants={itemVariants} layout>
{item.title}
</motion.li>
))}
</motion.ul>
</AnimatePresence>
);
}
The mode prop on AnimatePresence decides what happens when one list replaces another: wait for the old one to finish, or let them overlap while the new one enters. Pop layout is the one to reach for when items are being removed from the middle of a list, because it takes the leaving item out of flow immediately so the remaining rows can slide up instead of waiting.
Springs deserve a moment. A spring with a stiffness and a damping has a physical feel that a duration and an easing curve only approximate, and it reacts correctly when interrupted: a user who clicks again mid-animation gets a smooth reversal instead of a jump to a start value. Durations are still right for opacity fades, where a physical metaphor makes no sense.
Layout animation is measurement, not magic
When a row changes height â an accordion opens, a badge appears â everything below it moves, and the honest thing to animate is that movement. The library does it by capturing the element's box before the change, letting React commit the new box, then transforming from the old geometry to the new one so no intermediate layout is forced. Two constraints follow. Animate transform and opacity only, because those are handled by the compositor while width, top and margin trigger layout on the main thread for every frame. And avoid animating a container whose children have their own layout animations, since the measurements then depend on each other and the result jitters.
Shared-element transitions use the same mechanism across two components: both declare a shared id, and when the id moves from one tree position to another the library animates the element from the old box to the new. The usual failure is that the destination renders before the source has been measured, which produces a slide from the wrong place â a key and a mount ordering issue rather than an animation one.
Timelines when choreography matters
Some animation is not a reaction to state at all. An onboarding tour highlights six elements in order, pauses for reading, and can be skipped backwards. Expressing that as six pieces of React state produces a pyramid of timeouts. A timeline expresses it as one object with positions, and seeking becomes trivial.
import { useEffect, useRef } from 'react';
import gsap from 'gsap';
export function OnboardingTour({ steps, onDone }) {
const scopeRef = useRef(null);
useEffect(() => {
const ctx = gsap.context(() => {
const timeline = gsap.timeline({ paused: true, onComplete: onDone });
steps.forEach((step, index) => {
const target = `[data-tour="${step.id}"]`;
timeline
.to('.tour-highlight', {
duration: 0.35,
autoAlpha: 1,
onStart: () => {
const el = document.querySelector(target);
if (!el) return;
const rect = el.getBoundingClientRect();
gsap.set('.tour-highlight', {
top: rect.top,
left: rect.left,
width: rect.width,
height: rect.height,
});
},
}, index * 1.2)
.fromTo(
'.tour-card',
{ autoAlpha: 0, y: 12 },
{ autoAlpha: 1, y: 0, duration: 0.3 },
index * 1.2
)
.to('.tour-card', { autoAlpha: 0, duration: 0.2 }, index * 1.2 + 0.9);
});
timeline.play();
}, scopeRef);
return () => ctx.revert();
}, [steps, onDone]);
return (
<div ref={scopeRef} className="tour">
<div className="tour-highlight" />
<div className="tour-card" />
</div>
);
}
The revert call in cleanup is the entire React integration story for an imperative library: every element, style and inline property the library touched is restored when the component unmounts. Without it, a route change leaves inline transforms on nodes that React no longer manages, and the next visit renders with positions from the previous screen. The same discipline applies to event listeners the library adds and to ScrollTrigger instances that must be killed with the scope.
Performance: keep work off the main thread
Smoothness is not about frames per second in a profiler; it is about not blocking the thread that handles input. Animating transform and opacity lets the compositor run the animation independently of JavaScript, which is why a card can slide while a list re-renders without stuttering. Animating height, top, margin or box-shadow forces layout and paint every frame, and on a mid-range phone that is the difference between sixty frames and twenty.
Mount cost matters too. Animating the entrance of two hundred rows means two hundred simultaneous animations, which is why staggering with a cap and virtualising long lists both help. Measuring on every scroll frame is the other trap: prefer transforms driven by a single requestAnimationFrame loop or a library's scroll integration over reading getBoundingClientRect inside a scroll handler that also writes styles.
A practical audit list: check how many animations run at once, check which properties they touch, and check whether any of them is triggered by a re-render that could have been avoided with a memo or a ref.
Motion that respects the user
Some users get nauseated by movement, and their preference is exposed by the operating system as a reduced-motion signal. The rule is not to delete all transitions â removing every fade can break spatial understanding just as badly as a sweeping animation â but to shorten them and remove travel. A slide of two hundred pixels becomes a fade of eighty milliseconds; a parallax scroll sequence becomes a static state change.
Implement it once. Read the media query at the top level, put the result in context, and have animations branch on it, or in CSS wrap the ambitious rules in a prefers-reduced-motion block. Testing means toggling the setting in the operating system and walking the flow again, because the animation that violates the rule is usually the one added last by someone who never saw the setting.
Common Mistakes
Driving entrance animation from component state and a timeout
Fix: Declare variants and let AnimatePresence own the mounted window during exit
Animating width, height or top so every frame triggers layout
Fix: Animate transform and opacity, or use layout animation to let the library measure instead
Creating a timeline in an effect without tearing it down
Fix: Revert the whole context in cleanup so styles and listeners are restored
Ignoring the reduced-motion preference because the animation looks fine
Fix: Branch on the media query and swap travel for short fades
Best Practices
- Pick one library per project and use CSS for everything it does not need
- Prefer springs for interactive movement and durations for fades
- Cap staggered animations and virtualise anything with hundreds of entering nodes
- Clean up imperative animations with a scoped revert on unmount
- Profile on a mid-range device, since that is where layout animation shows its cost
Key Takeaways
- Framer Motion coordinates with React lifecycle and layout; GSAP coordinates sequences and time
- Variants move animation out of component state and make exit animations possible
- Compositor-friendly properties are what separate a smooth animation from a janky one
- Imperative libraries need explicit teardown or they leave inline styles behind on unmount
- Reduced motion means shorter and flatter, not absent