Shared Layout Animations
What you'll learn: How to create smooth morphing animations between different components by sharing layout state with layoutId.
Imagine you have a grid of product thumbnails, and when clicked, one expands into a full-screen detail view. Without shared layout animations, you'd see a jarring cut or separate fade-in/fade-out. With layoutId, Framer Motion treats the thumbnail and detail card as the same element for animation purposes, creating a seamless morph between them.
The Core Concept
When two motion components in different parts of your React tree share the same layoutId string, Framer Motion recognizes them as connected. When one unmounts and the other mounts, it automatically animates the layout properties (position, size, border-radius, etc.) from the old component to the new one.
This works even across completely different component hierarchies—the components don't need to be siblings or share a parent.
// Thumbnail in grid
<motion.div layoutId="product-card" />
// Detail view in modal
<motion.div layoutId="product-card" />
Wrap both in AnimatePresence, and when you conditionally render one or the other, Framer Motion creates a magical morphing transition.
How It Works
The layoutId acts as a unique identifier. When React unmounts the first component and mounts the second, Framer Motion:
- Captures the layout of the outgoing element
- Measures the incoming element's target layout
- Animates between them smoothly
This is perfect for list-to-detail transitions, tab switches with moving indicators, or any "shared element" pattern you've seen in native apps.
Key Takeaway: Use layoutId to create seamless morphing animations between different components across your component tree—Framer Motion handles the complex layout interpolation automatically.