Skip to main content

Motion Implementation Patterns

This guide documents production animation patterns using Motion (Framer Motion) in a Next.js 16 + React 19 environment with React Compiler optimization.

Installation & Setup

Package Configuration

Use the motion/react import path, not framer-motion. Motion is the new package name optimized for React 19.

Core Patterns

Basic Motion Component

Motion with Base UI Components

Integrate Motion with headless component libraries:
components/button/index.tsx
Key Technique:
  • motion.create() wraps third-party components
  • Preserves all original component functionality
  • Adds Motion animation props

AnimatePresence

Basic Exit Animations

components/card.tsx
Always provide a unique key prop to elements inside AnimatePresence. The key determines when an element is considered removed.

useIsPresent Hook

Track whether a component is currently mounted:
demos/presence-state.tsx
Use Case: Update content during exit animation based on presence state.

AnimatePresence Modes

Behavior: Exit and enter animations run simultaneously (default).Use Case: Crossfades, overlapping transitions.

Modes Comparison Demo

demos/modes-demo.tsx

Spring Configurations

Spring vs Easing Comparison

Characteristics:
  • Fixed duration
  • Predictable timing
  • Better for choreographed sequences

Production Spring Values

demos/ease-vs-spring.tsx

Spring Parameter Reference

Common Presets:

Advanced Patterns

Page Transitions

components/page-transition/index.tsx
Technique:
  • Key on pathname triggers animation on route change
  • Blur effect adds depth to transition
  • Delay prevents flashing on fast navigation

MotionValues and Velocity

demos/spring-with-velocity.tsx
Advanced Technique:
  • useMotionValue creates imperative animation state
  • useVelocity tracks rate of change
  • Pass velocity to spring for momentum preservation

Transform Performance

Animating width, height, top, left triggers layout recalculation. Use transform properties when possible.

Layout Animations

When you must animate layout properties:
Use Cases:
  • Reordering lists
  • Expanding/collapsing containers
  • Grid rearrangement

Timing Standards

Duration Guidelines

Never exceed 300ms for user-initiated actions. Longer animations feel sluggish.

Custom Easing Functions

Reduced Motion

Respecting User Preferences

CSS Alternative

styles.module.css

React Compiler Optimization

Memoization Patterns

React Compiler automatically optimizes these patterns:
Do not manually use useMemo or useCallback with React Compiler enabled. The compiler handles memoization automatically.

Server Component Boundaries

Rule: Only add "use client" to components that use Motion or other client-side features.

Performance Best Practices

Bundle Size Optimization

Dynamic Imports for Heavy Animations

Animate Presence with Lists

Next Steps

Web Audio API

Implement procedural sound generation for UI feedback

Performance Optimization

Learn bundle size and runtime optimization techniques