Communitygithub.com

Motion (Framer Motion) — 技能

在 React 中使用 motion/react (Framer Motion) 实现流畅、可访问的动画。适用于添加动画、手势或布局过渡的场景。需要 'use client' 边界;不适用于纯服务端渲染组件或无需动画的场景。

Motion (Framer Motion) — 技能 是什么?

Motion (Framer Motion) 技能是针对 React 开发者的专业动画实现指南,基于 `motion/react` 库提供声明式的动画、手势和布局过渡能力。该技能强调客户端边界(`'use client'`)的正确使用,推荐从小、简单的动画(如不透明度和位移)开始,逐步引入弹性物理效果。它支持 `AnimatePresence` 管理元素的进入/退出动画,`layout` 实现自动布局过渡,以及 `whileHover`/`whileTap` 处理手势交互。同时,通过 `useReducedMotion()` 尊重用户减少动效的偏好,并使用 `LazyMotion` 优化包体积。该技能适用于所有 React 项目(包括 Next.js App Router),并提供实现和审查清单,帮助开发者避免常见错误(如离开动画未执行、客户端树膨胀)。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Asymmetric-al/core/tree/HEAD/.agents/skills/motion

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Motion (Framer Motion) — 技能 是做什么的?

名称: motion 目的: 使用 motion/react 在 React 中实现流畅、可访问的动画。 每当需要添加动画、手势或布局过渡时,请使用此技能。

适用场景: 使用 motion/react(motion、AnimatePresence、useScroll 等)时。 不使用场景: 无需动画或组件为纯服务端渲染时。

规则

  • 仅限客户端边界: Motion 需要 'use client';尽量保持客户端隔离区小。
  • 从简单开始: 在添加弹性物理效果之前,先从不透明度和位移开始。
  • 使用正确的工具: 使用 AnimatePresence 处理进入/退出,layout 用于布局,whileHover/whileTap 用于手势。
  • 尊重减少动效的偏好: 使用 useReducedMotion()。
  • 包体积纪律: 对于较大的动画场景,使用 LazyMotion。

工作流程

  1. 明确 UX 目的(反馈、过渡、焦点)。
  2. 添加最小的 'use client' 边界。
  3. 正确实现进入/退出或布局过渡。
  4. 尊重减少动效的偏好。
  5. 检查性能和包体积。

检查清单

实现检查清单

  • 'use client' 仅在需要的地方使用
  • 定义了减少动效的行为
  • AnimatePresence 包裹条件渲染的 UI
  • 布局过渡使用 layout

审查检查清单

  • 动画不会在每次渲染时重新执行
  • Motion 代码不会不必要地扩展客户端树

最小示例

进入/退出

"use client";

import { AnimatePresence, motion } from "motion/react";

export function Toast({
  open,
  children,
}: {
  open: boolean;
  children: React.ReactNode;
}) {
  return (
    <AnimatePresence>
      {open ? (
        <motion.div
          initial={{ opacity: 0, y: 8 }}
          animate={{ opacity: 1, y: 0 }}
          exit={{ opacity: 0, y: 8 }}
          transition={{ duration: 0.18 }}
        >
          {children}
        </motion.div>
      ) : null}
    </AnimatePresence>
  );
}

布局动画

"use client";

import { motion } from "motion/react";

export function Expander({ open }: { open: boolean }) {
  return (
    <motion.div layout className="overflow-hidden rounded-md border p-3">
      <div className="font-medium">标题</div>
      {open ? <div className="mt-2 text-sm">更多内容...</div> : null}
    </motion.div>
  );
}

减少动效

"use client";

import { motion, useReducedMotion } from "motion/react";

export function FadeIn({ children }: { children: React.ReactNode }) {
  const reduce = useReducedMotion();
  return (
    <motion.div
      initial={reduce ? false : { opacity: 0, y: 6 }}
      animate={reduce ? { opacity: 1 } : { opacity: 1, y: 0 }}
      transition={{ duration: 0.2 }}
    >
      {children}
    </motion.div>
  );
}

常见错误/陷阱

  • 卸载时未使用 AnimatePresence(退出动画永远不会执行)
  • 动画布局时未使用 layout
  • 将 Motion 放置在巨大的客户端树中
  • 动画大型列表时未进行虚拟化

Individual skills in this repo

This repo contains 7 individual skills — each has its own dedicated page.

Asymmetric-al/core

Build animations for Expo and React Native only, using Reanimated, Gesture Handler, Expo Router, and expo-haptics. Use when the target runtime is an Expo or React Native app. Do not use it for Core Next.js web motion; use `animate` for web. Only runs when explicitly invoked; it does not trigger on its own.

Asymmetric-al/core

Build an animation from scratch, making the decisions in the order that determines whether it feels right — should it animate at all, what purpose, which tool, which properties, which curve and duration, how it interrupts, how it exits. Writes the implementation. Use when asked to animate something, add motion, make a component feel alive, or build a transition. For critiquing existing motion use review-animations; for auditing a whole codebase use improve-animations. Only runs when explicitly invoked; it does not trigger on its own.

Asymmetric-al/core

Reverse-lookup glossary that turns a vague description of a web animation or motion effect into its exact term ("the bouncy thing when a popover opens" → Pop in; "the iOS rubber-band scroll" → Rubber-banding). Use when the user asks "what's it called when…", or describes a motion effect without knowing its name and wants the right word to prompt an AI or designer with. For naming an effect, not designing or building one.

Asymmetric-al/core

Inspect a Core UI or code surface for a small number of justified motion opportunities and explicitly identify what should remain static. Use when asked what should animate, where motion would improve feedback or comprehension, how to make an interface feel more alive without over-animating it, or whether a proposed animation is worth adding. Read-only; do not use to review or fix existing motion code or to implement animations.

Asymmetric-al/core

Survey a codebase's animation and motion code as a senior motion advisor, then produce a prioritized audit and self-contained implementation plans for other agents (or cheaper models) to execute. Read-only on source code — it plans improvements, it does not apply them. Use when the user asks to "improve the animations", "audit the motion", "make this app feel better", or wants a roadmap of animation fixes rather than a review of a single diff.

Asymmetric-al/core

Best practices for Remotion - Video creation in React

Asymmetric-al/core

Reviews animation and motion code against a high craft bar derived from Emil Kowalski's design engineering philosophy. Default to flagging; approval is earned.

相关技能