Skip to content

Framer Motion

Declarative animation for React — layout transitions, gestures and exit animations.

UI & GraphicsAnimationJavaScript

What it is

Framer Motion is a production-ready motion library for React that allows developers to create animations and interactive UI transitions easily, using a declarative API.

Framer Motion uses components like `<motion.div>` and hooks such as `useAnimation` to animate React elements. It supports animations for position, scale, rotation, opacity, and more, along with gestures, layout transitions, keyframes, and variants for complex animation sequences.

Licence
MIT
Watch for
Respect `prefers-reduced-motion` — the library supports it

When to use it

The question documentation cannot answer for you — because it cannot recommend something else.

Reach for it when

  • React interfaces where animation should be expressed as component state, not imperative code
  • You need layout animations or animated route transitions

Look elsewhere when

  • A CSS transition would do — it costs nothing and ships no JavaScript
  • Complex timeline sequencing outside React, where GSAP is stronger

Installation

npm install framer-motion

Getting started

The smallest useful thing you can do with it, and what each part means.

Animate a div on mount
import { motion } from 'framer-motion';

function App() {
  return (
    <motion.div
      initial={{ opacity: 0 }}
      animate={{ opacity: 1 }}
      transition={{ duration: 1 }}
    >
      Hello, Framer Motion!
    </motion.div>
  );
}
Animates a div from opacity 0 to 1 over 1 second when the component mounts.
Hover animation
<motion.button whileHover={{ scale: 1.2 }}>
  Hover me!
</motion.button>
Scales the button up when hovered.

Advanced usage

Where the library earns its place over a simpler alternative.

Using variants for complex animations
const boxVariants = {
  hidden: { opacity: 0, x: -100 },
  visible: { opacity: 1, x: 0 }
};

<motion.div initial='hidden' animate='visible' variants={boxVariants} transition={{ duration: 1 }} />
Defines reusable animation states (variants) for a component and transitions between them.
Gestures with drag
<motion.div drag dragConstraints={{ left: 0, right: 100, top: 0, bottom: 100 }}>
  Drag me!
</motion.div>
Allows the user to drag the element within specified constraints.
Animating layout changes
<motion.div layout>
  {items.map(item => (
    <motion.div key={item.id} layout>{item.name}</motion.div>
  ))}
</motion.div>
Animates the position of items automatically when the layout changes.
Keyframes animation
<motion.div animate={{ x: [0, 100, 50, 100, 0] }} transition={{ duration: 2 }} />
Animates the element along multiple positions sequentially using keyframes.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

Animations not triggering
Ensure that you are using `<motion.div>` (or other motion elements) and that `initial` and `animate` props are correctly set.
Unexpected jump on layout animation
Use the `layout` prop consistently and avoid conflicting CSS transforms.
Performance issues with many elements
Consider using `AnimatePresence` for conditional components and limit simultaneous animations.

Best practices

  • Use variants to manage multiple animation states efficiently.
  • Prefer `layout` prop for automatic layout animations on reordering components.
  • Use `transition` properties to control timing and easing for smoother animations.
  • Leverage gesture props like `whileHover`, `whileTap`, and `drag` for interactive UIs.
  • Keep performance in mind: avoid animating large numbers of elements unnecessarily.

Alternatives

Comparable options, and the reason you would pick one over the other.

Background

Why it exists, and what it was reacting to.

Framer Motion was created by the team behind Framer to provide a simple, yet powerful, way to animate React components. It leverages the power of the Framer library while being optimized for performance and accessibility, making complex animations achievable with minimal code.