Framer Motion Basics — Web Animation

Motion (formerly Framer Motion) is an animation library for React. It has been independent since 12 November 2024: install it with npm install motion, import…

Booking a ride-hailing motorbike is easy: you only fill in the pick-up point and the destination. The driver takes care of the route, the speed and when to turn. Motion for React (formerly Framer Motion) works the same way. Motion is an animation library for React (a library for building pages from components). A component is a piece of a page written as a function, such as one room card, and it is written in JSX (code that looks like HTML inside JavaScript). In JSX, you only write the state you want, and Motion takes care of the journey. The driver is Motion, the pick-up point is initial (the starting state), the destination is animate (the target state), and the driving style is transition (how it moves between the two).

Name, package and version

Framer Motion was made at the company Framer. On 12 November 2024 it became an independent project called Motion. The package is motion, installed with npm (the JavaScript package installer). Its React version is imported from motion/react:

npm install motion
import { motion, AnimatePresence, stagger, useReducedMotion } from 'motion/react';

The old framer-motion package is still published with the same version number, because the motion package uses it inside. But new code should use motion/react. As of 11 October 2026 the latest version is 14.1.0, while the BelajarWeb.dev app uses 12.38. From 12 to 14, almost nothing changed for ordinary code. Version 13 only removed the optional dependency (an extra package that is not required) @emotion/is-prop-valid, which matters if you use styled-components. Version 14 has no breaking changes (changes that make old code stop working). What changed is inside: since 14.2.0, x, y and scale written separately also go through WAAPI (see the WAAPI section below).

motion.div: initial and animate

motion.div is a Motion component: an ordinary <div> that can animate. There is one for every HTML tag, such as motion.li, motion.button, motion.section. This code makes one room card appear as an <li>:

<motion.li
  initial={{ opacity: 0, y: 16 }}        // the state when first mounted
  animate={{ opacity: 1, y: 0 }}         // the target state
  transition={{ duration: 0.3, ease: 'easeOut' }}
>
  Terrace Room · $35 / night
</motion.li>

initial, animate and transition are called props: settings written on a component's tag, like attributes on an HTML tag. The outer curly brackets mean "this is JavaScript", and the inner ones are the object.

The "Book the room" button that shrinks when pressed needs a single prop: whileTap={{ scale: 0.96 }} (the state while it is pressed), with transition={{ duration: 0.1, ease: 'easeOut' }}. There are also whileHover, whileFocus and whileInView, for when the element is hovered, gets focus, and is visible on the screen.

transition: a duration or a spring

If you don't write a transition, Motion picks one. We read the Motion 12.38 source code. x, y and rotate use a spring (stiffness 500, damping 25), scale a stiffer spring (stiffness 550, damping 30), while opacity and colours use an ordinary 0.3 second curve.

There are two kinds of transition:

A spring is set with three numbers: stiffness (the bigger, the faster it pulls), damping (the bigger, the less it bounces) and mass (the bigger, the heavier and slower). The duration emerges from those three numbers by itself. We ran Motion's own spring function for a card rising from y: 16 to 0. We stopped counting when the spring was considered still, with less than 0.5 px left to go:

transition Still after Overshoots the target by
default for y: stiffness 500, damping 25 287 ms 1.9 px
just type: 'spring' (100, 10, mass 1) 687 ms 2.6 px
stiffness 300, damping 30 315 ms 0.03 px
stiffness 300, damping 10 741 ms 6.2 px
stiffness 300, damping 30, mass 2 580 ms 1.4 px

If you lower damping from 30 to 10, the card bounces 6.2 px and only rests after 741 ms. If you double the mass, 315 ms becomes 580 ms. Springs suit movement that can be interrupted (dragged, released, or given a new target halfway) because the new movement keeps the speed of the one already running. For a fade, a tween is more predictable.

variants and the room cards' stagger

Three cards (the Terrace, Attic and Garden Rooms) enter in turn (a stagger), 60 ms apart. Instead of writing a delay on each card, give the states names:

const list = {
  hidden: {},
  visible: { transition: { delayChildren: stagger(0.06) } },  // child n starts after n × 60 ms
};
const card = {
  hidden: { opacity: 0, y: 16 },
  visible: { opacity: 1, y: 0, transition: { duration: 0.3, ease: 'easeOut' } },
};

<motion.ul variants={list} initial="hidden" animate="visible">
  {rooms.map((r) => (
    <motion.li key={r.id} variants={card}>{r.name}</motion.li>
  ))}
</motion.ul>

AnimatePresence: exit animations

The exit prop holds the state for when an element leaves. Take a guess first: the "Booking sent" toast is written {shown && <motion.div exit={{ opacity: 0 }} />}, with no wrapper at all. The toast only shows while shown is true. When shown turns false, does the toast fade out first?

No. React removes an element from the DOM as soon as its condition is false, so there is no time left to play any animation. Without a wrapper, the toast disappears at once, and the exit prop never runs.

AnimatePresence is a wrapper component from Motion. It keeps an element that is being removed until its exit animation finishes, and only then removes it:

<AnimatePresence>
  {shown && (
    <motion.div
      key="toast"
      role="status"
      className="toast"
      initial={{ opacity: 0, y: 16 }}
      animate={{ opacity: 1, y: 0, transition: { duration: 0.2, ease: 'easeOut' } }}
      exit={{ opacity: 0, y: 16, transition: { duration: 0.15, ease: 'easeIn' } }}
    >
      Booking sent. Mrs. Sri will confirm on WhatsApp.
    </motion.div>
  )}
</AnimatePresence>

Inside Motion: WAAPI or JavaScript

Motion uses the Web Animations API (WAAPI) when it can. In Motion 12.38 the properties that can use it are opacity, transform, filter and clipPath, as long as you don't attach onUpdate (a callback called on every frame). Those animations can run on the compositor thread, like CSS (lesson 5, Transform & Composite). Springs are sent to WAAPI too. Motion calculates the curve, then writes it as a fixed duration plus a linear(...) easing (Chrome 113, Firefox 112, Safari 17.2). That curve is computed for a distance of 0 to 100, and the duration is rounded to a multiple of 50 ms. That's why the same 300/30 spring runs 500 ms on this path, not 315 ms as in the table.

But up to Motion 14.1, x, y, scale and rotate written separately (not as transform) do not go through WAAPI. Motion computes their values itself on every frame with requestAnimationFrame, on the main thread, then writes them into style.transform. We measured it in Chromium 147 with Motion 12.38. A motion.div moved down 300 px over 1 second, then we blocked the main thread for 500 ms:

Written as WAAPI animations on the element Frames drawn while blocked
animate={{ y: 300 }} 0 0 to 2
animate={{ transform: 'translateY(300px)' }} 1 60

The y version stands still and then jumps, exactly like the rAF result in lesson 6 (requestAnimationFrame). In the Lab, a card entering with y has one WAAPI animation, for opacity only. A card entering with transform has two.

Since Motion 14.2.0 (13 October 2026), separate x, y, scale and rotate go through WAAPI too. In Chromium, animate={{ y: 300 }} creates one WAAPI transform animation. So on Motion 14.1 and older, including 12.38 in this app, write transform and opacity directly for animations that must stay smooth while the page is busy.

useReducedMotion

Motion does not follow the "Reduce motion" setting by itself. The default of MotionConfig (the component that sets Motion's options for everything inside it) is reducedMotion: "never". There are two ways to change that. The first uses the hook useReducedMotion() (a React function that reads the setting):

const reduce = useReducedMotion();   // true when the system asks for less motion
const card = {
  hidden: { opacity: 0, y: reduce ? 0 : 16 },
  visible: { opacity: 1, y: 0, transition: { duration: reduce ? 0.15 : 0.3 } },
};

The second: wrap the app in <MotionConfig reducedMotion="user">, so Motion follows the user's setting. When "Reduce motion" is on, animations of x, y, scale, rotate, size and position jump straight to the target, while opacity still fades. That matches Mist Homestay's rule: no movement, just a fade (150 ms in the useReducedMotion code above).

⚠️ An exit that never runs, a five-minute duration, and more

TL;DR: Motion (formerly Framer Motion) is an animation library for React. It has been independent since 12 November 2024: install it with npm install motion, import from motion/react, latest 14.14.0 as of 15 November 2026. You write states as props (settings on a React component's tag): initial is the state at mount (when the component is first put on the page), animate the target, transition how it moves, with durations in seconds. Tween = duration + ease. Spring = stiffness, damping, mass, and the duration emerges from them (300/30/1 rests at 315 ms, damping 10 bounces 6.2 px and rests at 741 ms). variants give the states names. delayChildren: stagger(0.06) on the parent makes the cards enter in turn at 0, 60, 120 ms (staggerChildren is deprecated, outdated). exit (the leaving animation) only runs inside <AnimatePresence>, with a child that has a key (an identifier). Motion uses WAAPI for opacity, transform, filter, clipPath (a spring becomes a linear() easing). But up to 14.1, separate x, y, scale are computed on the main thread (since 14.2.0 they go through WAAPI too): with the main thread blocked for 500 ms, y drew 0 to 2 frames and transform 60. Motion ignores "Reduce motion" unless you use useReducedMotion() or MotionConfig reducedMotion="user".

⚡ Pick-up point and destination

Write:

<motion.li
  initial={{ opacity: 0, y: 16 }}        // the state when first mounted
  animate={{ opacity: 1, y: 0 }}         // the target state
  transition={{ duration: 0.3, ease: 'easeOut' }}
>
  Terrace Room · $35 / night
</motion.li>
Guess first: in this code, duration: 0.3 means 300 ms. So how long is duration: 300?

300 seconds, five minutes. Motion's durations are written in seconds, so 300 ms is written 0.3.

Meaning: Motion is an animation library for React, and motion.li is an <li> that can animate. Like booking a motorbike ride, you only write the pick-up point and the destination. You write them as props (settings on the tag): initial is the state when the component is first put on the page, animate the target state, and transition how it moves. Motion takes care of the journey. y: 16 is the same as translateY(16px).

⚡ A spring: the duration emerges

Meaning: Besides a tween (a duration and an easing, like CSS), Motion has a spring transition, such as { type: 'spring', stiffness: 300, damping: 30, mass: 1 }. A spring has no duration for you to write. The bigger stiffness, the faster it pulls. The bigger damping, the less it bounces. The bigger mass, the heavier it moves. If you lower damping from 30 to 10, the card bounces 6.2 px and only rests at 741 ms.

⚡ variants and stagger

Write:

const list = {
  hidden: {},
  visible: { transition: { delayChildren: stagger(0.06) } },  // child n starts after n × 60 ms
};
const card = {
  hidden: { opacity: 0, y: 16 },
  visible: { opacity: 1, y: 0, transition: { duration: 0.3, ease: 'easeOut' } },
};

<motion.ul variants={list} initial="hidden" animate="visible">
  {rooms.map((r) => (
    <motion.li key={r.id} variants={card}>{r.name}</motion.li>
  ))}
</motion.ul>

Meaning: variants give names to states: hidden and visible. initial and animate only need to be written on the parent (motion.ul). Children (motion.li) that have variants follow the same names. delayChildren: stagger(0.06) makes the cards enter in turn at 0, 60 and 120 ms.

⚡ exit needs AnimatePresence

Guess first: the exit prop holds the state for when an element leaves. A toast written {shown && <motion.div exit={{ opacity: 0 }} />} has no wrapper, and shows while shown is true. When shown turns false, does it fade out first?

No, it disappears right away. React removes the element as soon as its condition is false, so the exit prop never runs.

Meaning: exit only runs when the component is removed from inside <AnimatePresence>. AnimatePresence holds the element until its exit animation finishes. Its direct child needs a unique key (an identifier).

What you will learn