Motion (Framer Motion) — Modern Animation Library for React & JS
Motion (formerly Framer Motion) is a modern animation library for React and JavaScript with a simple declarative API. Hardware-accelerated transforms, gestures, layout animations, and SVG morphing — all in a tiny package.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install 89ca3afc-356f-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
What it is
Motion (formerly Framer Motion) is a modern animation library for React and vanilla JavaScript. It provides a simple declarative API for hardware-accelerated transforms, gesture recognition, layout animations, scroll-triggered animations, and SVG path morphing. The library handles the complexity of performant animations while keeping the developer API minimal.
This tool is for frontend developers who need smooth, production-quality animations without writing raw CSS keyframes or JavaScript requestAnimationFrame loops.
How it saves time or tokens
Motion abstracts animation complexity into a few props. What would require dozens of lines of CSS and JavaScript becomes a single <motion.div> component with animate, whileHover, and layout props. Gesture detection, spring physics, and layout change animations work out of the box. For AI-assisted frontend development, generating Motion animations requires minimal code.
How to use
- Install Motion via npm.
- Import the
motioncomponent. - Add animation props to your JSX elements.
- Motion handles the rest: physics, performance, and cleanup.
# Install Motion
npm install motion
# For React
npm install framer-motion
Example
import { motion } from 'framer-motion'
function AnimatedCard() {
return (
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -20 }}
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
transition={{ type: 'spring', stiffness: 300 }}
className='card'
>
<h2>Animated Card</h2>
<p>This card animates in, responds to hover and tap.</p>
</motion.div>
)
}
// Layout animation: smooth transitions when list items reorder
function AnimatedList({ items }) {
return (
<ul>
{items.map(item => (
<motion.li
key={item.id}
layout
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
{item.name}
</motion.li>
))}
</ul>
)
}
Related on TokRepo
- AI coding tools — Frontend development tools
- Design tools — UI and animation design
Common pitfalls
- Animating layout properties (width, height) can cause layout thrashing. Use
transformproperties (x, y, scale, rotate) whenever possible for GPU-accelerated performance. - The
layoutprop triggers re-renders. In lists with many items, this can cause performance issues. UselayoutIdselectively. - Motion adds bundle size. If you only need simple CSS transitions, consider native CSS instead of importing the full library.
- Exit animations require
AnimatePresencewrapper. Forgetting it means components disappear without animation. - Spring animations look natural but can overshoot. Tune
stiffnessanddampingvalues for your use case. - Review the official documentation before deploying to production to ensure compatibility with your specific environment and requirements.
常见问题
Yes. The library was renamed from Framer Motion to Motion. The React package is still framer-motion on npm. The standalone JavaScript version is available as the motion package.
Yes. Motion works with Next.js including App Router and Server Components. The motion components are client components, so use the 'use client' directive in files that use animations.
Motion uses hardware-accelerated CSS transforms by default, runs animations on the compositor thread when possible, and batches DOM reads and writes. It avoids layout thrashing by preferring transform properties.
Yes. Use AnimatePresence with your router to animate page transitions. Motion supports exit animations that play before the component unmounts, enabling smooth route transitions.
Yes. Motion provides useScroll, useMotionValueEvent, and whileInView for scroll-driven animations. Elements can animate as they enter or exit the viewport.
引用来源 (3)
- Motion GitHub— Motion is a modern animation library for React
- Motion Documentation— Framer Motion API reference and examples
- MDN Web Docs - CSS Animations— Hardware-accelerated CSS animations
讨论
相关资产
Mo.js — Motion Graphics Library for the Web
A JavaScript motion graphics toolbelt that provides declarative shape, burst, and stagger animations with a retina-ready rendering engine for expressive UI effects.
react-spring — Physics-Based Animation Library for React
A spring-physics animation library for React that produces natural, fluid motion for UI transitions, gestures, and complex choreography.
date-fns — Modern Modular JavaScript Date Utility Library
date-fns is a modern JavaScript date utility library providing 200+ functions for parsing, formatting, arithmetic, and comparison. Tree-shakeable, immutable, TypeScript-first, and the modern alternative to Moment.js.
Preact — Fast 3kB React Alternative with Same Modern API
Preact is a fast 3kB alternative to React with the same modern API. Virtual DOM, Components, Hooks — all in a tiny package. Drop-in compatible via preact/compat. Perfect for performance-critical apps and islands architecture.