Framer Motion: Creating Dynamic User Interfaces with Motion-First Design in React

Animations have become more than just visual enhancements—they’re essential to improving UX, guiding user flow, and bringing interfaces to life. For React developers looking to add sophisticated motion to their applications, Framer Motion offers an expressive and robust solution that doesn’t compromise performance or developer experience.
This post takes a deeper look at what makes Framer Motion one of the most powerful animation libraries in the React ecosystem and explores how to get the most out of its features—from basic transitions to complex layout animations and shared element transitions.
What is Framer Motion?
Framer Motion is an open-source animation library developed by the creators of Framer. It leverages the popmotion
animation engine under the hood and provides a simple yet powerful declarative API built specifically for React.
At its core, Framer Motion extends HTML and SVG elements with a special motion.
prefix, turning them into animatable components. It lets you define entry/exit animations, transitions, gestures (drag, tap, hover), scroll events, and shared layout animations—all in a React-friendly way.
Key Features of Framer Motion
- Declarative syntax: Define what you want to happen, and let the library handle the transitions.
- Variants: Create animation “states” for easier management of complex interactions.
- Layout animations: Animate between layout changes smoothly, even with dynamic content.
- Gestures and physics-based motion: Add real-world physics like spring-based motion, inertia, and draggable interactions.
- Exit animations: Animate elements out of the DOM using
AnimatePresence
. - SVG path animations: Animate strokes, fills, paths, and other SVG properties natively.
Installation and Setup
Install via npm or yarn:
bashCopyEditnpm install framer-motion
# or
yarn add framer-motion
Once installed, import the motion
component:
tsxCopyEditimport { motion } from "framer-motion";
Now you’re ready to start animating.
Basic Animation Example
Here’s how to animate a simple box that fades and slides in on render:
tsxCopyEdit<motion.div
initial={{ opacity: 0, y: -50 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5, ease: "easeOut" }}
className="w-32 h-32 bg-blue-500"
/>
The initial
prop defines the starting state, animate
defines the end state, and transition
controls how the animation progresses.
Variants: Managing Multiple Animation States
Variants let you abstract and organize animations into reusable “states” for your components.
tsxCopyEditconst boxVariants = {
hidden: { opacity: 0, scale: 0.8 },
visible: { opacity: 1, scale: 1 },
};
<motion.div
variants={boxVariants}
initial="hidden"
animate="visible"
transition={{ type: "spring", stiffness: 300 }}
/>
This pattern becomes especially useful when you’re animating lists, menus, or toggling between UI states.
AnimatePresence: Animating Elements On Unmount
By default, React unmounts components without giving you a chance to animate them out. Framer Motion solves this using the AnimatePresence
component.
tsxCopyEditimport { AnimatePresence, motion } from "framer-motion";
{showModal && (
<AnimatePresence>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
Modal Content
</motion.div>
</AnimatePresence>
)}
This enables beautiful fade-in and fade-out effects for modals, dropdowns, and dynamic content.
Layout Animations
Framer Motion can automatically animate layout changes when elements move or resize, making transitions feel seamless.
tsxCopyEdit<motion.div layout className="grid gap-4">
{items.map(item => (
<motion.div layout key={item.id}>
{item.content}
</motion.div>
))}
</motion.div>
When the items
array updates (e.g., via sorting or filtering), Framer Motion animates the position changes between states.
Shared Layout Transitions
Want to animate an element smoothly from one part of the UI to another (like opening an image in fullscreen)? Shared layout transitions use the same layoutId
across components to achieve this.
tsxCopyEdit// In the list view
<motion.div layoutId="preview-image" />
// In the detail view
<motion.div layoutId="preview-image" />
When switching between these views, Framer Motion interpolates between the two states, creating a fluid morphing transition.
Drag, Gestures, and Physics
Framer Motion has built-in support for interactive gestures like drag, tap, hover, and pan.
tsxCopyEdit<motion.div
drag
dragConstraints={{ left: 0, right: 300 }}
dragElastic={0.2}
whileTap={{ scale: 0.95 }}
/>
You can also apply physics like inertia, spring, bounce, and damping to create realistic, tactile experiences.
Scroll-Based Animation
Using the useScroll
and useTransform
hooks, Framer Motion can animate elements based on scroll position.
tsxCopyEditconst { scrollYProgress } = useScroll();
const scale = useTransform(scrollYProgress, [0, 1], [1, 2]);
<motion.div style={{ scale }} />
This allows for parallax effects, progress indicators, and scroll-triggered transitions with minimal setup.
Performance and Production Considerations
Framer Motion is highly performant and leverages requestAnimationFrame
and hardware acceleration wherever possible. However, to keep animations smooth:
- Avoid animating
width
andheight
directly; preferscale
. - Debounce or throttle layout changes when working with dynamic content.
- Use
AnimatePresence
selectively on only the components that need it.
Use Cases
Framer Motion is suitable for a wide range of use cases:
- Landing page animations
- Onboarding sequences
- Scroll-based storytelling
- Interactive UI feedback
- Modals, accordions, and carousels
- Real-time layout transitions in dashboards
Conclusion
Framer Motion offers a powerful, intuitive way to implement complex animations and interactions in React. From small micro-interactions to full-scale shared element transitions, it provides the tools needed to craft high-quality user experiences without resorting to CSS hacks or third-party plugins.
If you’re building React interfaces with an emphasis on polish and interactivity, Framer Motion deserves a place in your toolkit.
For more examples, docs, and advanced guides, visit the official site: https://www.framer.com/motion
Hi, my name is Toni Naumoski, and I’m a Senior Frontend Developer with a passion for blending code and design. With years of experience as a Frontend Developer, Web Designer, and Creative Technologist, I specialize in crafting unique, responsive, and detail-oriented websites and web applications that stand out. I bring deep expertise in HTML, CSS, and JavaScript—working fluently with modern frameworks like React, Angular, and Vue, as well as animation libraries like GSAP. My creative side thrives in Photoshop and Figma, and I enjoy extending functionality using tools like Express.js and ChatGPT. My work is guided by high integrity, strong communication, a positive attitude, and a commitment to being a reliable collaborator. I take pride in delivering high-quality digital experiences that are both technically solid and visually compelling.
Post Comment