Combining Exit and Layout Animations
You'll learn how to orchestrate exit animations and layout shifts together for seamless, professional transitions when components unmount and siblings reflow.
The Challenge of Coordinated Motion
Exit animations (via AnimatePresence) and layout animations (via the layout prop) are powerful individually, but they solve different problems. Exit animations handle disappearing elements gracefully. Layout animations handle remaining elements smoothly repositioning. When you combine them, you can create transitions where removed items fade out while siblings elegantly shift into their new positions—think deleting a list item and watching others slide up simultaneously.
The Coordination Pattern
The key is enabling both systems on the same component tree:
<AnimatePresence mode="popLayout">
{items.map(item => (
<motion.div
key={item.id}
layout
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.8 }}
>
{item.name}
</motion.div>
))}
</AnimatePresence>
Notice the layout prop on the motion.div inside AnimatePresence. When an item exits, it fades/scales away (exit prop) while its siblings detect the layout change and smoothly reposition (layout prop). The mode="popLayout" tells AnimatePresence to immediately trigger layout recalculations, preventing stutters.
Why This Matters
Without combining them, you'd see janky behavior: items might snap to new positions while others are still fading, or exit animations might not align with the reflow timing. Together, they create what designers call "choreographed motion"—every element knows its role in the transition.
Key Takeaway: Combine AnimatePresence with layout-enabled children and use mode="popLayout" to create sophisticated transitions where exiting elements and repositioning siblings move in harmony, eliminating jarring layout shifts.