Layout Animations with layout Prop
What you'll learn: How to automatically animate position and size changes in React components using Framer Motion's layout prop.
The Problem with Manual Layout Animations
When a component's position or dimensions change—say, a card moves from one column to another, or a list item expands—the browser instantly "jumps" to the new layout. Manually animating these changes would require tracking before/after positions and writing complex animation code.
The Magic of the layout Prop
Framer Motion's layout prop solves this elegantly. Simply add layout to any motion component, and it will automatically animate whenever its position or size changes due to layout shifts.
<motion.div layout>
{/* Content */}
</motion.div>
Think of it like marking a component with "remember where I was, and smoothly move me when things change." Framer Motion handles all the calculations behind the scenes.
How It Works
When layout changes occur (parent flex direction switches, items reorder, siblings appear/disappear), Framer Motion:
- Captures the component's current position/size
- Lets the layout change happen
- Calculates the difference
- Smoothly animates from old to new state
Common Use Cases
Reordering lists:
<motion.div layout key={item.id}>
{item.name}
</motion.div>
Expanding/collapsing content:
<motion.div layout>
<h3>Title</h3>
{isExpanded && <p>Details...</p>}
</motion.div>
Grid to list view toggles, responsive layouts, drag-and-drop interfaces—anywhere layout shifts happen, layout provides smooth transitions without manual animation code.
You can customize the animation with transition:
<motion.div
layout
transition={{ duration: 0.3, ease: "easeOut" }}
>
Key Takeaway: The layout prop enables effortless, automatic animations for position and size changes, turning jarring layout shifts into smooth, polished transitions with a single attribute.