UI Patterns
Native-like

Motion

Interruptible animations and springs — why native motion feels physical, and how to get the same on the web with CSS transitions, linear() and a small spring.

Native motion has two properties most web animations lack. You can grab anything that's moving and send it somewhere else, and the motion continues from where it is, at the speed it had. Apple calls this out in Designing Fluid Interfaces (WWDC 2018): "we want to allow for constant redirection and interruption. This one's big." The second property follows from the first: if motion can start at any speed, a fixed easing curve can't describe it, so native toolkits use springs.

Interruption

Open the panel and change your mind half-way:

Keyframesrestarts from the far end
CSS transitionreverses from where it is
Springkeeps its speed, then turns
  • Keyframe animations (CSS @keyframes, or a naive element.animate()) describe a whole journey from A to B. Starting a new one jumps to its first frame.
  • CSS transitions start from the element's current value, so they reverse without a jump. The spec even shortens a reversed transition to the distance already travelled (reversing shortening factor). But the speed flips instantly: full speed one way, full speed the other.
  • A spring keeps its velocity when the target changes: it slows, turns and accelerates back, like a physical object.

So the rule of thumb: anything the user can trigger again before it finishes — a toggle, a drawer, a tab indicator, a press state — should be a CSS transition, not a keyframe animation. Reach for a spring when the reversal itself should feel physical, and always when a gesture hands over a speed (next section).

View transitions don't fit either case. They animate snapshots between two states; skipTransition() "skips the animation part" and lands on the new state, and there's no built-in way to send one back from where it is. Keep them for screen changes that won't be interrupted, and keep them short.

Handing over velocity

When a finger lets go of a sheet, it's already moving. An animation with a fixed curve starts at whatever speed its curve dictates and ignores that. Flick the sheet in both modes:

Drag the sheet up or down, or flick it.

Order #1042 · 3 items
  • Oat milk ×2
  • Sourdough loaf
  • Coffee beans

Release speed: —

With ease-out, the speed right after release comes from the curve and the remaining distance, not from the finger: a fast flick suddenly slows down, a slow drag released near the end suddenly speeds up. The spring starts at exactly the release speed, so there's no seam. Apple's WWDC 2023 talk Animate with springs puts it as: "springs are the only type of animation that maintains continuity both for static cases and cases with an initial velocity."

The demo also decides where to settle from where the sheet would end up, not where it is: position + velocity × 0.2 s. A quick flick from the bottom opens the sheet even though the finger barely moved.

Duration and bounce

Physics parameters (stiffness, damping, mass) are hard to design with. Apple's springs take two numbers instead — "It uses just 2 parameters: duration and bounce":

  • Duration is perceptual: roughly when the motion looks finished. The spring keeps settling a little longer than that.
  • Bounce 0 is a smooth stop with no overshoot. Positive values overshoot, up to 1 (no friction, never stops). Negative values flatten the end, approaching more slowly.

Apple's advice: "When you're not sure, use a spring with bounce 0", and be careful above about 0.4. Small bounce suits motion driven by a gesture with momentum; UI that just appears shouldn't wobble.

The conversion to physics, with mass 1:

lib/spring.ts
function dampingRatio(bounce: number) {
  return bounce >= 0 ? 1 - bounce : 1 / (1 + bounce);
}

export function springPhysics({ duration, bounce }: Spring) {
  const omega = (2 * Math.PI) / duration;
  return {
    mass: 1,
    stiffness: omega * omega,
    damping: 2 * dampingRatio(bounce) * omega,
  };
}

Stiffness is (2π / duration)², as in the talk. Bounce maps to the damping ratio: 0 is critically damped (fastest stop without overshoot), 1 has no damping at all, and negative values are overdamped — the "bouncy, smooth, and flattened" springs of the talk.

Springs in plain CSS

linear() easing (Baseline since December 2023) takes a list of points and draws straight lines between them; with enough points it traces any curve, overshoot included. So a spring that starts from rest can be precomputed into a CSS transition, no JavaScript at runtime:

settles in 558 ms · stiffness 247 · damping 26.7
CSS
transition: transform 558ms linear(0, 0.029, 0.099, 0.193, 0.297, 0.401, 0.501, 0.592, 0.672, 0.742, 0.8, 0.849, 0.888, 0.92, 0.944, 0.963, 0.977, 0.988, 0.995, 1, 1.003, 1.005, 1.006, 1.006, 1.006, 1.006, 1.005, 1.005, 1.004, 1.003, 1.003, 1.002, 1.002, 1.001, 1);

What CSS can't do is start from a velocity: the curve is fixed when the transition starts. Interrupting a linear() spring reverses like any other transition. For press states, toggles and things that appear, that's enough. For gestures, run the spring in JavaScript.

A spring in JavaScript

Without a library, a spring is a loop that integrates acceleration each frame. Because the state is just position and velocity, changing the target mid-flight keeps the velocity automatically:

lib/spring.ts
export function createSpringValue(
  initial: number,
  onUpdate: (value: number) => void,
  {
    restDelta = 0.5,
    restSpeed = 5,
    onRest,
  }: {
    restDelta?: number;
    restSpeed?: number;
    onRest?: (value: number) => void;
  } = {},
) {
  let value = initial;
  let velocity = 0;
  let target = initial;
  let spring: Spring = { duration: 0.4, bounce: 0 };
  let frame = 0;
  let last = 0;

  const tick = (now: number) => {
    const elapsed = Math.min((now - last) / 1000, 0.064);
    last = now;
    const { stiffness, damping } = springPhysics(spring);
    const substeps = Math.max(1, Math.ceil(elapsed * 480));
    const h = elapsed / substeps;
    for (let i = 0; i < substeps; i++) {
      const acceleration = -stiffness * (value - target) - damping * velocity;
      velocity += acceleration * h;
      value += velocity * h;
    }
    if (
      Math.abs(value - target) < restDelta &&
      Math.abs(velocity) < restSpeed
    ) {
      value = target;
      velocity = 0;
      frame = 0;
      onUpdate(value);
      onRest?.(value);
      return;
    }
    onUpdate(value);
    frame = requestAnimationFrame(tick);
  };

  return {
    to(next: number, options: Partial<Spring> & { velocity?: number } = {}) {
      target = next;
      spring = { ...spring, ...options };
      if (options.velocity !== undefined) velocity = options.velocity;
      if (!frame) {
        last = performance.now();
        frame = requestAnimationFrame(tick);
      }
    },
    // value, velocity, jump() and stop() omitted
  };
}

Small substeps keep stiff springs stable on slow frames. Write the value to transform (or opacity), never to layout properties, and set will-change: transform on the element while it moves.

For anything bigger — several values, gestures, layout animations — use a library. Motion takes the same model: { type: "spring", visualDuration: 0.4, bounce: 0.2 }, where visualDuration is "a time, set in seconds, that the animation will take to visually appear to reach its target", and it carries over the velocity of whatever it interrupts.

Reduced motion

With prefers-reduced-motion: reduce, the demos above jump straight to the end. Interruptibility doesn't need motion to be useful — the state still changes the moment the user asks — so the fallback is simply no animation, or a short opacity fade for things that appear. Don't keep bounce for these users: overshoot is exactly the kind of motion they asked to remove.

Sources

On this page