Communitygithub.com

alfathafp/portofolio-web-design

使用 React 的 View Transition API(`<ViewTransition>` 组件、`addTransitionType` 和 CSS 视图过渡伪元素)实现流畅、原生般动画的指南。当用户想要添加页面过渡、动画化路由变化、创建共享元素动画、动画化组件的进入/退出、列表重排动画、实现定向(前进/后退)导航动画,或在 Next.js 中集成视图过渡时使用此技能。

portofolio-web-design 是什么?

该技能提供了一套完整的指南,用于在 React(包括 Next.js)应用中利用浏览器原生 `document.startViewTransition` 实现流畅的视图过渡动画。核心是使用 `<ViewTransition>` 组件声明动画作用域,通过 `startTransition`、`useDeferredValue` 或 `Suspense` 触发动画,并使用 CSS 类精确控制动画效果。指南详细介绍了五种动画模式(共享元素、Suspense 揭示、列表标识、状态变化、路由变化)及其使用场景,并提供了按优先级实现的明确顺序。它支持 Next.js App Router(开箱即用)和标准 React(需安装 canary 版本),在不支持的浏览器上可优雅降级。此外,还涵盖了通过 `addTransitionType` 实现基于上下文的定向导航动画(如前进/后退滑动),以及如何组合多个 `<ViewTransition>` 来处理复杂场景(如列表重排与共享元素共存),并强调了无障碍性(必须添加减少动画 CSS)。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/alfathafp/portofolio-web-design/tree/HEAD/.agents/skills/vercel-react-view-transitions

在你喜欢的 AI 中提问

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

预览

来自技能 README

portofolio-web-design preview
portofolio-web-design preview

文档

React 视图过渡(View Transitions)

使用浏览器原生的 document.startViewTransition 在 UI 状态之间进行动画。使用 <ViewTransition> 声明要动画的内容,通过 startTransition / useDeferredValue / Suspense 触发时机,使用 CSS 类控制方式。不支持此功能的浏览器会优雅地跳过动画。

何时使用动画

每个 <ViewTransition> 都应传达一种空间关系或连续性。如果你无法说明它传达了什么,就不要添加它。

按此顺序实现列表中所有适用的模式:

优先级模式传达的含义
1共享元素 (name)"同一事物——更深入一层"
2Suspense 揭示"数据已加载"
3列表标识 (每个元素 key)"相同项目,新排列"
4状态变化 (enter/exit)"某物出现/消失"
5路由变化 (页面级别)"前往新位置"

这是实现顺序,不是“挑选一个”的列表。实现所有适合该应用的模式。仅当应用没有该模式的用例时才跳过它。

选择动画风格

上下文动画原因
层级导航 (列表 → 详情)类型键控的 nav-forward / nav-back传达空间深度
横向导航 (标签页到标签页)裸 <ViewTransition> (淡入淡出) 或 default="none"无需传达深度
Suspense 揭示enter/exit 字符串属性内容到达
重新验证 / 后台刷新default="none"静默——不需要动画

将定向滑动保留给层级导航 (列表 → 详情) 和有序序列 (上一张/下一张照片、轮播图、分页结果)。对于有序序列,方向传达位置:“下一张”从右侧滑入,“上一张”从左侧滑入。横向/无序导航(标签页到标签页)不应使用定向滑动——它会错误地暗示空间深度。


可用性

  • Next.js: 不要安装 react@canary——App Router 已经在内部捆绑了 React canary 版本。ViewTransition 开箱即用。npm ls react 可能显示一个看起来像稳定版的版本;这是预期的。
  • 没有 Next.js: 安装 react@canary react-dom@canary (ViewTransition 不在稳定版 React 中)。
  • 浏览器支持:Chromium 125+, Firefox 144+, Safari 18.2+。在不支持的浏览器上优雅降级。

实现工作流程

当向现有应用添加视图过渡时,请逐步遵循 references/implementation.md。从审计开始——不要跳过。使用 references/css-recipes.md 获取适用的 CSS 并使其适配应用。


核心概念

<ViewTransition> 组件

import { ViewTransition } from 'react';

<ViewTransition>
  <Component />
</ViewTransition>

React 自动分配一个唯一的 view-transition-name 并在后台调用 document.startViewTransition。永远不要自己调用 startViewTransition。

动画触发器

触发器触发时机
enter在过渡期间 <ViewTransition> 首次插入时
exit在过渡期间 <ViewTransition> 首次移除时
update<ViewTransition> 内的 DOM 突变,或者由于直接兄弟元素导致边界本身的大小/位置发生变化。对于嵌套的 VT,突变应用于最内层的一个
share命名 VT 卸载,并且另一个具有相同 name 的 VT 在同一个过渡中挂载时

只有 startTransition、useDeferredValue 或 Suspense 会激活 VT。常规的 setState 不会触发动画。

关键放置规则

<ViewTransition> 只有出现在任何 DOM 节点之前时,才会激活 enter/exit:

// 有效
<ViewTransition enter="auto" exit="auto">
  <div>内容</div>
</ViewTransition>

// 无效 —— div 包裹了 VT,抑制了 enter/exit
<div>
  <ViewTransition enter="auto" exit="auto">
    <div>内容</div>
  </ViewTransition>
</div>

使用视图过渡类进行样式设置

属性

值:"auto" (浏览器交叉淡入淡出)、"none" (禁用)、"class-name" (自定义 CSS),或者 { [type]: value } 用于特定类型的动画。

<ViewTransition default="none" enter="slide-in" exit="slide-out" share="morph" />

如果 default 是 "none",除非明确列出,否则所有触发器都关闭。

CSS 伪元素

  • ::view-transition-old(.class) — 旧快照(出发)
  • ::view-transition-new(.class) — 新快照(到达)
  • ::view-transition-group(.class) — 容器
  • ::view-transition-image-pair(.class) — 旧 + 新快照对

请参阅 references/css-recipes.md 获取即用型动画配方。


过渡类型 (Transition Types)

使用 addTransitionType 标记过渡,以便 VT 可以根据上下文选择不同的动画。多次调用以堆叠类型——树中的不同 VT 对不同的类型做出反应:

startTransition(() => {
  addTransitionType('nav-forward');
  addTransitionType('select-item');
  router.push('/detail/1');
});

传递对象以将类型映射到 CSS 类。适用于 enter、exit 和 share:

<ViewTransition
  enter={{ 'nav-forward': 'slide-from-right', 'nav-back': 'slide-from-left', default: 'none' }}
  exit={{ 'nav-forward': 'slide-to-left', 'nav-back': 'slide-to-right', default: 'none' }}
  share={{ 'nav-forward': 'morph-forward', 'nav-back': 'morph-back', default: 'morph' }}
  default="none"
>
  <Page />
</ViewTransition>

enter 和 exit 不必对称。例如,淡入但定向滑出:

<ViewTransition
  enter={{ 'nav-forward': 'fade-in', 'nav-back': 'fade-in', default: 'none' }}
  exit={{ 'nav-forward': 'nav-forward', 'nav-back': 'nav-back', default: 'none' }}
  default="none"
>

TypeScript: ViewTransitionClassPerType 要求在对象中包含一个 default 键。

对于具有多个页面的应用,将类型键控的 VT 提取为可重用的包装器:

export function DirectionalTransition({ children }: { children: React.ReactNode }) {
  return (
    <ViewTransition
      enter={{ 'nav-forward': 'nav-forward', 'nav-back': 'nav-back', default: 'none' }}
      exit={{ 'nav-forward': 'nav-forward', 'nav-back': 'nav-back', default: 'none' }}
      default="none"
    >
      {children}
    </ViewTransition>
  );
}

router.back() 和浏览器后退按钮

router.back() 以及浏览器的前进/后退按钮不携带过渡类型,因此类型键控的动画(定向滑动)会解析为它们的 default 并且不会播放——无类型的共享元素变形仍然适用。对于类型化动画,请使用带有显式 URL 的 router.push()。

类型与 Suspense

类型在导航期间可用,但在随后的 Suspense 揭示期间(单独的过渡,没有类型)不可用。对页面级别的 enter/exit 使用类型映射;对 Suspense 揭示使用简单的字符串属性。

共享元素准备就绪

只有当旧视图和新视图在同一个过渡中渲染时,共享元素过渡才能配对元素。如果传入内容挂起,则该更新仅存在其 fallback;解析后的内容会出现在后续的 Suspense 过渡中,并且可以单独动画化。


共享元素过渡

在两个 VT 上使用相同的 name——一个正在卸载,一个正在挂载——会创建一个共享元素变形:

<ViewTransition name="hero-image">
  <img src="/thumb.jpg" onClick={() => startTransition(() => onSelect())} />
</ViewTransition>

// 在另一个视图上——相同的 name
<ViewTransition name="hero-image">
  <img src="/full.jpg" />
</ViewTransition>
  • 同一时间只能挂载一个具有给定 name 的 VT —— 使用唯一的名称 (photo-${id})。注意可重用组件:如果具有命名 VT 的组件同时在模态框/弹出框和页面中渲染,那么两者会同时挂载并破坏变形。要么使名称有条件(通过 prop),要么将命名 VT 从共享组件中移到特定的消费者中。
  • share 优先于 enter/exit。仔细思考每个导航路径:当没有形成匹配的对时(例如,目标页面没有相同的 name),enter/exit 会触发。考虑该元素是否需要为这些路径提供一个后备动画。
  • 有两种方式会导致已接线的变形静默地从不触发:(1) default="none" 且没有显式的 share prop —— share 解析为 none;(2) 类型键控的 share,但导航从未添加该类型 —— 一个普通的链接点击会解析映射的 default。每个应该变形的链接都必须添加该类型(在 next/link 上使用 transitionTypes,或使用 addTransitionType)。
  • 永远不要在具有共享变形的页面上使用淡出退出动画 —— 应使用定向滑动代替。

常见模式

进入/退出

{show && (
  <ViewTransition enter="fade-in" exit="fade-out"><Panel /></ViewTransition>
)}

列表重排

{items.map(item => (
  <ViewTransition key={item.id}><ItemCard item={item} /></ViewTransition>
))}

在 startTransition 内部触发。避免在列表和 VT 之间使用包装器 <div>。

布局位移变形

只有已激活边界内的内容会动画化位置——其他所有内容会跳到其新的布局位置。将增长/缩小列表下方的兄弟内容包装在一个裸 <ViewTransition> 中,使其平滑移动而不是跳跃。请参阅 布局位移变形。

组合共享元素与列表标识

共享元素和列表标识是独立的概念——不要混淆它们。当列表项包含共享元素时(例如,一个图像变形为详图视图),请使用两个嵌套的 <ViewTransition> 边界:

{items.map(item => (
  <ViewTransition key={item.id}>                                      {/* 列表标识 */}
    <Link href={`/items/${item.id}`}>
      <ViewTransition name={`item-image-${item.id}`} share="morph">   {/* 共享元素 */}
        <Image src={item.image} />
      </ViewTransition>
      <p>{item.name}</p>
    </Link>
  </ViewTransition>
))}

外层的 VT 处理列表重排/进入动画。内层的 VT 处理跨路由的共享元素变形。缺少任何一层都意味着该动画静默地不会发生。

使用 key 强制重新进入

<ViewTransition key={searchParams.toString()} enter="slide-up" default="none">
  <ResultsGrid />
</ViewTransition>

注意: 如果包裹 <Suspense>,更改 key 会重新挂载边界并重新获取数据。

从 Suspense Fallback 到内容

简单的交叉淡入淡出:

<ViewTransition>
  <Suspense fallback={<Skeleton />}><Content /></Suspense>
</ViewTransition>

定向揭示:

<Suspense fallback={<ViewTransition exit="slide-down"><Skeleton /></ViewTransition>}>
  <ViewTransition enter="slide-up" default="none"><Content /></ViewTransition>
</Suspense>

更多模式,请参阅 references/patterns.md。


多个 VT 如何交互

每个匹配触发器的 VT 会在单个 document.startViewTransition 中同时触发。位于不同过渡中(导航 vs 后续 Suspense 解析)的 VT 不会相互竞争。

有意使用 default="none"

没有它,每个 VT 会在每次过渡上都触发浏览器交叉淡入淡出——Suspense 解析、useDeferredValue 更新、后台重新验证。在命名/共享元素和类型键控的页面 VT 上使用 default="none"。

但它也会关闭 update(布局/重排变形)和 share(命名对没有显式 share prop 则永远不会变形)。键控列表项和位移的兄弟元素需要 update——让它们保持裸状态或设置 update="auto"。

两种模式共存

模式 A — 定向滑动: 每个页面上的类型键控 VT,在导航期间触发。 模式 B — Suspense 揭示: 简单的字符串属性,在数据加载时触发(无类型)。

它们可以共存是因为它们在不同时刻触发。两者都使用 default="none" 可防止交叉干扰。始终将 enter 与 exit 配对。将定向 VT 放在页面组件中,而不是布局中。

嵌套 VT 限制

当父 VT 作为一个整体挂载/卸载,并且内部包含嵌套的 VT 时,嵌套的 VT 不会触发它们自己的 enter/exit——只有最外层的 VT 会动画化。(挂载在持久父 VT 内部的子 VT 会正常触发 enter/exit)。页面导航期间每个项目的交错动画目前在 Next.js 中不可用;请参阅 故障排除 了解上游实验性状态。


Next.js 集成

关于 Next.js 集成(next/link 和 useRouter 上的 transitionTypes、App Router 模式、服务器组件),请参阅 references/nextjs.md。


无障碍性

始终将从 references/css-recipes.md 中获取的减少动画 CSS 添加到你的全局样式表中。


参考文件

完整文档

关于包含所有展开的参考文件的完整指南:AGENTS.md

Individual skills in this repo

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

alfathafp/portofolio-web-design

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).

alfathafp/portofolio-web-design

Tests in real browsers via Chrome DevTools MCP. Use when building or debugging anything that runs in a browser. Use when you need to inspect the DOM, capture console errors, analyze network requests, profile performance, or verify visual output with real runtime data. Requires the chrome-devtools MCP server to be configured.

alfathafp/portofolio-web-design

Automates CI/CD pipeline setup. Use when setting up or modifying build and deployment pipelines. Use when you need to automate quality gates, configure test runners in CI, or establish deployment strategies.

alfathafp/portofolio-web-design

Conducts multi-axis code review. Use before merging any change. Use when reviewing code written by yourself, another agent, or a human. Use when you need to assess code quality across multiple dimensions before it enters the main branch. Use when asked to review a diff or a pull request, even when the diff is pasted inline.

alfathafp/portofolio-web-design

Simplifies code for clarity. Use when refactoring code for clarity without changing behavior. Use when code works but is harder to read, maintain, or extend than it should be. Use when reviewing code that has accumulated unnecessary complexity.

alfathafp/portofolio-web-design

Establishes a project's quality bar as a written contract and stops agents quietly lowering it. Interviews the user on which dimensions matter, supplies sane default thresholds when they have no number in mind, records everything in CONSTRAINTS.md, and watches the diff for a weakened bar — new @ts-ignore or eslint-disable suppressions, skipped or deleted tests, assertions stripped out, unimplemented stubs, thresholds edited down. Use when no quality bar is written down, when the user says "set up constraints" or "define our standards", when the user wants dimensions they care about — accessibility, web performance, coverage — set up as enforced constraints, when an agent keeps silencing checks or skipping tests to get to green, when you need a coverage or performance threshold and don't know what number to pick, or when an agent writes more code than anyone will read.

alfathafp/portofolio-web-design

Optimizes agent context setup. Use when starting a new session, when agent output quality degrades, when switching between tasks, or when you need to configure rules files and context for a project.

alfathafp/portofolio-web-design

Guides systematic root-cause debugging. Use when tests fail, builds break, something that worked yesterday broke, behavior doesn't match expectations, or you encounter any unexpected error. Use when you need to figure out what broke and why — a systematic approach to finding and fixing the root cause rather than guessing.

alfathafp/portofolio-web-design

Deploy applications and websites to Vercel. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment".

alfathafp/portofolio-web-design

Manages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.

alfathafp/portofolio-web-design

Use when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.

alfathafp/portofolio-web-design

Use when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies

alfathafp/portofolio-web-design

Records decisions and documentation. Use when you need to document an architecture decision (ADR) or the reasoning behind a design choice, when changing public APIs, shipping features, or when you need to record context that future engineers and agents will need to understand the codebase.

alfathafp/portofolio-web-design

Subjects every non-trivial decision to a fresh-context adversarial review before it stands. Use when you want every assumption cross-examined before proceeding, when stress-testing a plan for hidden failure modes, when correctness matters more than speed, when working in unfamiliar code, when stakes are high (production auth, security-sensitive logic, a high-stakes migration, irreversible operations), or any time a confident output would be cheaper to verify now than to debug later.

alfathafp/portofolio-web-design

Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available

alfathafp/portofolio-web-design

Use when implementation is complete, all tests pass, and you need to decide how to integrate the work

alfathafp/portofolio-web-design

Builds production-quality, accessible, responsive user-facing UIs. Use when building or modifying interfaces and pages, creating components, implementing layouts, meeting WCAG accessibility requirements, managing state, or when the output needs to look and feel production-quality rather than AI-generated.

alfathafp/portofolio-web-design

Structures git workflow practices. Use when making any code change. Use when committing, branching, resolving conflicts, splitting uncommitted work in a messy working tree into clean atomic commits, opening or reviewing a pull request (PR), pushing to a remote, or when you need to organize work across multiple parallel streams. Use when cutting a release, choosing a semantic version bump, tagging, or writing a changelog.

alfathafp/portofolio-web-design

Refines raw ideas into sharp, actionable concepts through structured divergent and convergent thinking. Use when an idea is still vague, when you need to stress-test assumptions before committing to a plan, or when you want to expand options before converging on one. Triggers on "ideate", "refine this idea", or "stress-test my plan".

alfathafp/portofolio-web-design

Guides stable API and interface design. Use when designing APIs, module boundaries, or any public interface. Use when creating REST or GraphQL endpoints, defining type contracts between modules, or establishing boundaries between frontend and backend.

相关技能