framer-motion
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity Framer Motion Best Practices
社区版Framer Motion最佳实践
Comprehensive performance optimization guide for Framer Motion animations in React applications. Contains 42 rules across 9 categories, prioritized by impact to guide automated refactoring and code generation.
针对React应用中Framer Motion动画的全面性能优化指南。包含9个类别下的42条规则,按影响优先级排序,可指导自动化重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Adding animations to React components with Framer Motion
- Optimizing bundle size for animation-heavy applications
- Preventing unnecessary re-renders during animations
- Implementing layout transitions or shared element animations
- Building scroll-linked or gesture-based interactions
在以下场景中参考本指南:
- 为使用Framer Motion的React组件添加动画
- 优化动画密集型应用的包体积
- 防止动画期间不必要的重渲染
- 实现布局过渡或共享元素动画
- 构建滚动联动或基于手势的交互
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Bundle Optimization | CRITICAL | |
| 2 | Re-render Prevention | CRITICAL | |
| 3 | Animation Properties | HIGH | |
| 4 | Layout Animations | HIGH | |
| 5 | Scroll Animations | MEDIUM-HIGH | |
| 6 | Gesture Optimization | MEDIUM | |
| 7 | Spring & Physics | MEDIUM | |
| 8 | SVG & Path Animations | LOW-MEDIUM | |
| 9 | Exit Animations | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 包体积优化 | 关键 | |
| 2 | 重渲染预防 | 关键 | |
| 3 | 动画属性 | 高 | |
| 4 | 布局动画 | 高 | |
| 5 | 滚动动画 | 中高 | |
| 6 | 手势优化 | 中 | |
| 7 | 弹簧与物理效果 | 中 | |
| 8 | SVG与路径动画 | 中低 | |
| 9 | 退出动画 | 低 | |
Quick Reference
快速参考
1. Bundle Optimization (CRITICAL)
1. 包体积优化(关键)
- - Use LazyMotion and m component instead of motion
bundle-lazy-motion - - Dynamically import motion features
bundle-dynamic-features - - Use domAnimation for basic animations
bundle-dom-animation - - Use mini useAnimate for simple cases
bundle-use-animate-mini - - Enable strict mode to catch accidental imports
bundle-strict-mode
- - 使用LazyMotion和m组件替代motion组件
bundle-lazy-motion - - 动态导入motion功能
bundle-dynamic-features - - 对基础动画使用domAnimation
bundle-dom-animation - - 对简单场景使用迷你版useAnimate
bundle-use-animate-mini - - 启用严格模式以捕获意外导入
bundle-strict-mode
2. Re-render Prevention (CRITICAL)
2. 重渲染预防(关键)
- - Use useMotionValue instead of useState
rerender-motion-value - - Derive values with useTransform
rerender-use-transform - - Keep animation callbacks stable
rerender-stable-callbacks - - Define variants outside component
rerender-variants-object - - Use stable animate values
rerender-animate-prop - - Use motion value events
rerender-motion-value-event
- - 使用useMotionValue替代useState
rerender-motion-value - - 通过useTransform派生值
rerender-use-transform - - 保持动画回调稳定
rerender-stable-callbacks - - 在组件外部定义variants
rerender-variants-object - - 使用稳定的animate值
rerender-animate-prop - - 使用motion值事件
rerender-motion-value-event
3. Animation Properties (HIGH)
3. 动画属性(高)
- - Animate transform properties
anim-transform-properties - - Prefer opacity and filter for visual effects
anim-opacity-filter - - Leverage hardware acceleration
anim-hardware-acceleration - - Use willChange prop judiciously
anim-will-change - - Animate transforms independently
anim-independent-transforms - - Use keyframe arrays for sequences
anim-keyframes-array
- - 为transform属性添加动画
anim-transform-properties - - 优先使用opacity和filter实现视觉效果
anim-opacity-filter - - 利用硬件加速
anim-hardware-acceleration - - 谨慎使用willChange属性
anim-will-change - - 独立为transform属性添加动画
anim-independent-transforms - - 使用关键帧数组实现序列动画
anim-keyframes-array
4. Layout Animations (HIGH)
4. 布局动画(高)
- - Use layoutDependency to limit measurements
layout-dependency - - Use layout="position" or "size" appropriately
layout-position-size - - Group related layout animations
layout-group - - Use layoutId for shared element transitions
layout-id-shared - - Add layoutScroll to scrollable ancestors
layout-scroll
- - 使用layoutDependency限制测量范围
layout-dependency - - 合理使用layout="position"或"size"
layout-position-size - - 对相关布局动画进行分组
layout-group - - 使用layoutId实现共享元素过渡
layout-id-shared - - 为可滚动父元素添加layoutScroll
layout-scroll
5. Scroll Animations (MEDIUM-HIGH)
5. 滚动动画(中高)
- - Use useScroll hook for scroll-linked animations
scroll-use-scroll - - Smooth scroll animations with useSpring
scroll-use-spring-smooth - - Track specific elements entering viewport
scroll-element-tracking - - Configure scroll offsets
scroll-offset-configuration - - Track scroll within specific containers
scroll-container-ref
- - 使用useScroll钩子实现滚动联动动画
scroll-use-scroll - - 结合useSpring实现平滑滚动动画
scroll-use-spring-smooth - - 追踪特定元素进入视口的状态
scroll-element-tracking - - 配置滚动偏移量
scroll-offset-configuration - - 追踪特定容器内的滚动
scroll-container-ref
6. Gesture Optimization (MEDIUM)
6. 手势优化(中)
- - Use whileHover/whileTap instead of handlers
gesture-while-props - - Let gesture variants flow to children
gesture-variants-flow - - Use dragConstraints ref for boundaries
gesture-drag-constraints - - Configure dragElastic for natural feel
gesture-drag-elastic - - Use onTapCancel for interrupted gestures
gesture-tap-cancel
- - 使用whileHover/whileTap替代事件处理器
gesture-while-props - - 让手势variants传递给子组件
gesture-variants-flow - - 使用dragConstraints ref设置边界
gesture-drag-constraints - - 配置dragElastic实现自然拖拽效果
gesture-drag-elastic - - 使用onTapCancel处理中断的手势
gesture-tap-cancel
7. Spring & Physics (MEDIUM)
7. 弹簧与物理效果(中)
- - Use physics-based springs for interruptibility
spring-physics-based - - Configure damping to control oscillation
spring-damping-ratio - - Adjust mass for heavier/lighter feel
spring-mass-inertia - - Use useSpring for reactive values
spring-use-spring-hook
- - 使用基于物理的弹簧效果以支持中断
spring-physics-based - - 配置阻尼比控制振荡
spring-damping-ratio - - 调整质量参数实现轻重不同的动画手感
spring-mass-inertia - - 使用useSpring钩子处理响应式值
spring-use-spring-hook
8. SVG & Path Animations (LOW-MEDIUM)
8. SVG与路径动画(中低)
- - Use pathLength for line drawing animations
svg-path-length - - Use motion.path and motion.circle
svg-motion-components - - Animate viewBox for zoom effects
svg-viewbox-animation - - Match point counts for morphing
svg-morph-matching-points
- - 使用pathLength实现路径绘制动画
svg-path-length - - 使用motion.path和motion.circle组件
svg-motion-components - - 为viewBox添加动画实现缩放效果
svg-viewbox-animation - - 变形动画需匹配路径点数量
svg-morph-matching-points
9. Exit Animations (LOW)
9. 退出动画(低)
- - Wrap conditional renders with AnimatePresence
exit-animate-presence - - Provide unique keys for AnimatePresence children
exit-unique-keys - - Use mode="wait" for sequential transitions
exit-mode-wait
- - 用AnimatePresence包裹条件渲染的组件
exit-animate-presence - - 为AnimatePresence的子元素提供唯一key
exit-unique-keys - - 使用mode="wait"实现序列过渡
exit-mode-wait
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构和影响等级说明
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序规则 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |