Communitygithub.com

alfathafp/portofolio-web-design

使用 React 的 View Transition API(`<ViewTransition>` 元件、`addTransitionType` 及 CSS 檢視轉換偽元素)實作流暢、原生動畫效果的指南。

portofolio-web-design 是什麼?

此技能提供一份完整的指南,說明如何利用 React 的 View Transition API 建立流暢、原生風格的動畫。它涵蓋所有核心概念,例如使用 `<ViewTransition>` 元件宣告動畫內容、透過 `startTransition`、`useDeferredValue` 或 `Suspense` 觸發動畫,並使用 CSS 類別控制動畫方式。指南詳細說明各種動畫模式,包括共享元素、列表重新排序、進出場過渡與路線變更,並提供基於上下文(例如層級導航 vs 橫向導航)的動畫風格選擇建議。此技能也包含與 Next.js 的整合說明,強調在 App Router 中直接支援。透過此指南,開發者可以了解如何實作不需要第三方動畫函式庫的頁面轉場、路線動畫及元件動畫。

相容平台~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 檢視轉換

使用瀏覽器原生的 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+ (React 需要 startViewTransition 的 v2 物件形式)、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> 在 Transition 中首次插入時
exit<ViewTransition> 在 Transition 中首次移除時
update<ViewTransition> 內部的 DOM 發生突變,或邊界本身因直接兄弟元素而改變大小/位置時。巢狀 VT 的情況下,突變應用於最內層的 VT
share命名 VT 卸載,且另一個具有相同 name 的 VT 在同一個 Transition 中掛載時

只有 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。


轉換類型

使用 addTransitionType 標記 Transition,以便 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 顯示 (這是不同的 Transition,沒有類型)。對頁面層級的 enter/exit 使用類型映射;對 Suspense 顯示使用簡單的字串屬性。

共享元素就緒狀態

共享元素轉換僅在舊視圖和新視圖都在同一個 Transition 中渲染時,才能配對元素。如果傳入內容暫停,則該次更新僅會存在其 fallback;解析後的內容會在後續的 Suspense Transition 中出現,並且可以單獨製作動畫。


共享元素轉換

兩個 VT 使用相同的 name — 一個卸載,一個掛載 — 會建立共享元素變形:

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

// 在另一個視圖上 — 相同名稱
<ViewTransition name="hero-image">
  <img src="/full.jpg" />
</ViewTransition>
  • 一次只能掛載一個具有特定 name 的 VT — 請使用唯一名稱 (photo-${id})。注意可重複使用的元件:如果一個具有命名 VT 的元件同時在 modal/popover 和頁面中渲染,兩者會同時掛載並破壞變形。可以讓名稱變成條件式 (透過 prop),或將命名 VT 移出共享元件,放入特定的消費端。
  • share 優先於 enter/exit。仔細思考每個導航路徑:當沒有形成匹配的配對時 (例如,目標頁面沒有相同名稱),enter/exit 會觸發。考慮該元素在這些路徑下是否需要一個 fallback 動畫。
  • 接線變形靜默永不觸發的兩種情況:(1) default="none" 且沒有明確的 share prop — share 解析為 none;(2) 類型鍵值的 share,但導航從未添加該類型 — 一個普通連結點擊會解析映射的 default。每個應該變形的連結都必須添加類型 (next/link 上的 transitionTypes,或 addTransitionType)。
  • 切勿在包含共享變形的頁面上使用淡出 exit — 應使用方向性滑動。

常見模式

進/出場

{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 中同時觸發。處於不同 Transition (導航 vs 後續 Suspense 解析) 中的 VT 不會互相競爭。

審慎使用 default="none"

如果沒有它,每個 VT 會在每一次 Transition 上觸發瀏覽器的交叉淡入淡出 — 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.

相關技能