Communitygithub.com

nehakanjamala/personal_website

當使用者要求「為頁面或路由切換加入動畫」、「在檢視之間加入頁面轉場或交叉淡出」、「在 Next.js App Router 中為路由變化加入動畫」、「使用 View Transitions API」、「AnimatePresence 的 exit 未在導航時觸發」或「Framer Motion 退出動畫無法運作 / 在 Next.js 中卸載前退出動畫未執行」時,應使用此技能。涵蓋使用 View Transitions API 與 Framer Motion 實現的進入/退出頁面轉場動畫,適用於 Next.js App Router 及一般 SPA 路由。

personal_website 是什麼?

此技能專門處理 Next.js App Router 中的頁面進入與退出轉場動畫,並解決一個最常見的故障:Framer Motion 的 `AnimatePresence` 退出動畫在導航時從不觸發。其根本原因是結構性的——在 App Router 中,導航時 Next.js 會立即卸載舊路由的內容並掛載新路由的內容,導致 `AnimatePresence` 無法在足夠的時間內持有退出中的元素來執行動畫。技能提供兩種解決方案:一是透過 `template.tsx`、以路徑名為鍵的 `AnimatePresence` 搭配 `FrozenRouter` 模式;二是使用原生的 View Transitions API 或 `next-view-transitions` 函式庫。此外,內容包含詳細的除錯清單、快速參考表、注意事項,以及一個用於驗證動畫形狀的獨立 HTML 測試工具(可透過 `?view=` 或 `?phase=` 參數凍結轉場階段),並整合了 Playwright 腳本進行截圖比對。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/nehakanjamala/personal_website/tree/HEAD/.agents/skills/page-transition-animation

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

頁面轉場動畫 (Next.js App Router)

在 Next.js App Router 中實作頁面進入/退出轉場,並修復最常見的故障:Framer Motion AnimatePresence 退出動畫在導航時從不觸發。原因是結構性的——在 App Router 中,導航時 Next.js 會卸載舊路由的內容並幾乎立即掛載新的內容。AnimatePresence 只有在退出中的元素在一個持續存在的 AnimatePresence 包裝器下保持掛載足夠長的時間以執行動畫時,才能為退出過程製作動畫。由於路由器從其底下交換了子元素,因此退出過程被跳過。有兩種解決方法:使用 pathname 作為鍵的 AnimatePresence 搭配 FrozenRouter 模式,或是使用 View Transitions API(原生 / next-view-transitions)。

何時使用

在 Next.js App Router 應用程式中加入動畫頁面轉場時、當 Framer Motion 的 exit prop 在路由變更時無作用時、當舊頁面立即消失而非動畫退出時、當在 Framer Motion 和 View Transitions API 之間為 Next.js 做選擇時,或是在除錯為什麼 AnimatePresence 無法在路由之間製作動畫時使用。

為何 exit 不觸發(核心問題)

AnimatePresence 偵測直接作為鍵值的子元素的移除。在 App Router 導航時:

  1. 包裝器必須在導航期間持續存在。如果 AnimatePresence 位於一個自身也會卸載的元件中,就沒有東西可以執行動畫。
  2. 子元素的 key 必須根據路由變更,以便 AnimatePresence 看到「舊的已移除,新的已加入」。
  3. 在短暫的重疊期間,正在退出的子樹必須仍然渲染其舊內容——但 App Router 已經交換了路由上下文,因此退出中的樹會渲染新頁面的資料。這就是 FrozenRouter 要修復的問題。

解決方案 A: template.tsx + 以 key 驅動的 AnimatePresence + FrozenRouter

template.tsx 是 App Router 為此目的設計的鉤子:與 layout.tsx(它會持續存在)不同,template 在每次導航時都會重新掛載,為每個路由提供一個全新的實例——非常適合每個路由的進入動畫。將其與以 pathname 為鍵的 AnimatePresence 和 FrozenRouter 結合使用,以實現乾淨的退出 + 進入。

// app/template.tsx
'use client';
import { AnimatePresence } from 'framer-motion';
import { usePathname } from 'next/navigation';
import { FrozenRouter } from './frozen-router';
import { PageTransition } from './page-transition';

export default function Template({ children }: { children: React.ReactNode }) {
  const pathname = usePathname();
  return (
    <AnimatePresence mode="wait" initial={false}>
      {/* 將 key 設為 pathname,以便 AnimatePresence 看到舊的已移除 / 新的已加入 */}
      <PageTransition key={pathname}>
        {/* FrozenRouter 讓退出中的樹在動畫期間渲染其舊內容 */}
        <FrozenRouter>{children}</FrozenRouter>
      </PageTransition>
    </AnimatePresence>
  );
}
// app/page-transition.tsx
'use client';
import { motion } from 'framer-motion';

export function PageTransition({ children }: { children: React.ReactNode }) {
  return (
    <motion.div
      initial={{ opacity: 0, y: 12 }}
      animate={{ opacity: 1, y: 0 }}
      exit={{ opacity: 0, y: -12 }}
      transition={{ duration: 0.3, ease: [0.22, 1, 0.36, 1] }}
    >
      {children}
    </motion.div>
  );
}
// app/frozen-router.tsx
'use client';
import { useContext, useRef } from 'react';
import {
  LayoutRouterContext,
} from 'next/dist/shared/lib/app-router-context.shared-runtime';

// 凍結路由器上下文,以便退出中的頁面在退出動畫期間
// 渲染其*自身*的內容,而不是下一個路由的內容。
export function FrozenRouter({ children }: { children: React.ReactNode }) {
  const context = useContext(LayoutRouterContext ?? {});
  const frozen = useRef(context).current;

  return (
    <LayoutRouterContext.Provider value={frozen}>
      {children}
    </LayoutRouterContext.Provider>
  );
}

它們如何協同工作:

  • mode="wait" 讓 AnimatePresence 在掛載新頁面前完全完成退出動畫。(如果想要進入/退出重疊/交叉淡出,請使用預設模式。)
  • initial={false} 可跳過首次載入時的進入動畫(可選)。
  • key={pathname} 是讓 AnimatePresence 將每個路由視為不同存在實體的關鍵。
  • FrozenRouter 快照 LayoutRouterContext,以便退出中的子樹在整個退出過程中渲染前一個路由的內容,而不是閃現新路由的內容。沒有它,退出過程要麼被跳過,要麼顯示錯誤的內容。

注意:LayoutRouterContext 是 Next.js 的內部實作;其匯入路徑可能隨 Next 版本而變。如果匯入在升級後失效,請更新該路徑。

解決方案 B: 原生 View Transitions API

View Transitions API 可在兩個 DOM 狀態之間製作動畫,瀏覽器會捕捉之前/之後的快照——無需為每個元素退出元件。

/* globals.css */
@view-transition { navigation: auto; } /* 在支援的地方啟用 MPA 風格(可選) */

::view-transition-old(root) { animation: fade 0.25s both reverse; }
::view-transition-new(root) { animation: fade 0.25s both; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

對於 SPA 風格的 App Router 導航,請包覆路由器更新:

if (document.startViewTransition) {
  document.startViewTransition(() => router.push(href));
} else {
  router.push(href);
}

next-view-transitions 函式庫

next-view-transitions 將 View Transitions API 與 App Router 的客戶端導航整合,使其可以直接使用:

// app/layout.tsx
import { ViewTransitions } from 'next-view-transitions';
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <ViewTransitions>
      <html lang="en"><body>{children}</body></html>
    </ViewTransitions>
  );
}
// 使用此函式庫的 Link 代替 next/link
import { Link } from 'next-view-transitions';
<Link href="/about">About</Link>;

對於簡單的跨頁面淡入/淡出/變形以及共享元素轉場,且希望使用最少的 JS 時,選擇 View Transitions;當需要精細、協調、可中斷或交錯的退出動畫時,選擇 Framer Motion。View Transitions API 的瀏覽器支援仍然不均,請提供優雅降級方案(if (document.startViewTransition) 保護)。

AnimatePresence 除錯清單

當 exit 仍然無法觸發時,請按順序檢查:

  1. 包裝器保持掛載狀態。 AnimatePresence 必須存在於某個持續存在的東西中——理想情況下是 template.tsx,或是佈局層級的客戶端元件——而不是在正在卸載的頁面內部。
  2. 穩定且會變化的鍵。 動畫子元素需要 key={pathname}(或類似方式),以便偵測到移除。沒有 key,或 key 沒有變化 → 不會有退出動畫。
  3. 直接的 motion 後代。 帶有 exit prop 的元素必須是 motion.* 元件,並且是 AnimatePresence 的子元素(中間的非 motion 包裝器可能會阻斷偵測)。
  4. mode="wait" 如果新舊頁面不應重疊;沒有它的話,兩者會同時渲染,可能導致跳動。
  5. 存在 FrozenRouter 如果退出中的頁面在退出動畫期間閃現新路由的內容。
  6. 每個使用 AnimatePresence/usePathname/motion 的檔案都需要 'use client'。
  7. 在 mode="wait" 下一次只有一個子元素——AnimatePresence 期望一次只交換一個帶有鍵的存在實體。

交付與驗證(獨立 HTML)

打包好的輔助工具 (scripts/):scripts/seek-shot.sh anim.html 0 1.5 3 會凍結 ?t=N 測試工具並對每個時刻截圖;scripts/contact-sheet.sh sheet.png frame-*.png 將它們拼貼成一張概覽圖。請參閱 scripts/README.md。

真正的目標是 Next.js 應用程式,但為了證明轉場的形狀(進入/退出曲線、方向、交叉淡出),你可以交付一個可以直接在瀏覽器中開啟的 HTML 檔案——兩個可以切換的「頁面」存根,使用來自 CDN 的 motion 或原生的 View Transitions API 製作動畫。在連接路由器之前,一個檔案是驗證動畫的正確層級;不需要為了肉眼觀察一個淡入淡出效果而啟動整個 Next。

輸出契約:

  • 一個 .html 檔案:兩個檢視存根和一個切換開關,使用內聯的 <script type="module"> 從 CDN 匯入 motion/React 進行動畫,或使用 document.startViewTransition + ::view-transition-* CSS。
  • 一種凍結已解析的最終狀態的方法——路由轉場是由狀態驅動的(哪個檢視,切換中),因此請對狀態而非時鐘進行截圖。

搜尋測試工具——釘選一個轉場階段。 ?view=b 會掛載目標檢視(或 ?phase=exit 以在動畫中期保持退出中的樹),以便截圖落在一個已穩定的階段:

<script>
  const p = new URLSearchParams(location.search);
  document.documentElement.dataset.view = p.get("view") || "a";   // CSS/React 據此判斷
  // Framer Motion:從 ?view 設定受控的 key/路由;對於 mode="wait",解析的結果就是新頁面
  // View Transitions:對結束狀態截圖(快照是暫時的);或透過模擬慢速動畫來暫停
  window.__ready = true;
</script>

驗證迴圈——渲染 → 設定階段 → 截圖 → 檢查: 開啟來源、中間和目標(?view=a、?phase=exit、?view=b),對每個截圖,然後檢查精確度(退出動畫是否實際執行、方向/交叉淡出是否符合要求、沒有 mode="wait" 的延遲)以及瑕疵(退出中的樹閃現新頁面的內容、雙重掛載、FOUC、畫面跳動)。任何無頭工具都可以:

npx playwright screenshot --wait-for-timeout=500 "file://$PWD/transition.html?view=b" dest.png

在你完成之前:

  1. 獨立開啟——無控制台錯誤,CDN 的 motion/React(如果使用)可解析,startViewTransition 有防護。
  2. ?view=/?phase= 的凍結功能可獲取確定性的轉場階段。
  3. 已在來源/中間/目標處截圖——符合需求,沒有內容閃現或跳過的退出。
  4. 尊重 prefers-reduced-motion——滑動/平移降級為短暫的交叉淡出。
  5. 緩動是有意設計的——進入時 ease-out,退出時 ease-in,持續時間約 0.2-0.4 秒(在 mode="wait" 下不會感覺延遲)。

快速參考

需求方法
每個路由的進入動畫app/template.tsx(每次導航重新掛載)
導航時的退出動畫以 key 驅動的 AnimatePresence mode="wait" + FrozenRouter
退出中的頁面顯示舊內容FrozenRouter(快照 LayoutRouterContext)
偵測路由變更使用 usePathname() 作為 key
簡單的跨頁面淡入/淡出/變形View Transitions API / next-view-transitions
共享元素轉場view-transition-name CSS
跳過首次載入動畫initial={false}

注意事項

  • layout.tsx 會持續存在,並且不會為每個路由重新掛載——請使用 template.tsx 進行每個路由的進入動畫。
  • 省略 FrozenRouter 會導致退出中的頁面在動畫中期渲染新路由的內容(可見的閃現)或完全跳過退出。
  • LayoutRouterContext 的匯入路徑是 Next 的內部實作;它可能隨版本而變化——這是升級後首先要修復的東西。
  • 如果沒有 key={pathname},AnimatePresence 會看到相同的子元素,永遠不會觸發退出。
  • mode="wait" 會等待退出完成後才進入;在較慢的退出上過度使用它會使導航感覺延遲——請調整持續時間(約 0.2-0.4 秒)。
  • View Transitions API 缺乏完整的瀏覽器支援;務必使用 document.startViewTransition 進行防護。
  • 每個轉場檔案都需要 'use client';伺服器元件不能使用 AnimatePresence/usePathname。

參考檔案

  • references/full-examples.md — 方向性/滑動轉場、使用 view-transition-name 的共享元素 View Transitions、非 mode="wait" 的交叉淡出變體、搭配 loading.tsx 的載入狀態轉場,以及一個完整的可運作 App Router 資料夾佈局。

Individual skills in this repo

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

nehakanjamala/personal_website

This skill should be used when the user asks to "fix janky CSS animation", "make animation 60fps", "stop layout thrashing", "animate width/height/top/left smoothly", "convert animation to transform", "animate box-shadow performantly", "animate height auto", "FLIP animation", or "why is my scroll/hover animation choppy".

nehakanjamala/personal_website

This skill should be used when the user asks to "respect prefers-reduced-motion", "honor reduced motion", "make my animations accessible", "fix vestibular / motion-sickness issues", "add a useReducedMotion hook", "gate GSAP / Framer Motion / Lenis for reduced motion", or "meet WCAG 2.3.3 / C39". Provides tiered (not all-or-nothing) reduced-motion patterns in CSS and JS.

nehakanjamala/personal_website

Create, modify, debug, and deploy Apify Actors, and write their input and output schemas. Use when building an Actor from scratch, changing or troubleshooting Actor code, generating or updating .actor schema files, or pushing an Actor to the Apify platform. To wrap an existing non-Actor project, use apify-actorization instead.

nehakanjamala/personal_website

Convert existing projects into Apify Actors - serverless cloud programs. Actorize JavaScript/TypeScript (SDK with Actor.init/exit), Python (async context manager), or any language (CLI wrapper). Use when migrating code to Apify, wrapping CLI tools as Actors, or adding Actor SDK to existing projects.

nehakanjamala/personal_website

Deprecated. Output schema generation moved to the apify-actor-development skill; prefer that skill whenever it is available.

nehakanjamala/personal_website

Design and build an official Apify integration for a company's product - workflow-automation apps (Zapier/n8n-style), AI agent plugins (coding-agent skills+MCP bundles or OpenClaw/Hermes-style harnesses), AI framework packages (LangChain/LlamaIndex-style), or direct application clients via apify-client. Use when planning, creating, or reviewing an integration that exposes Apify Actors, runs, datasets, or key-value stores inside another product.

nehakanjamala/personal_website

Universal AI-powered web scraper for any platform. Scrape data from Instagram, Facebook, TikTok, YouTube, LinkedIn, X/Twitter, Google Maps, Google Search, Google Trends, Reddit, Airbnb, Yelp, and 15+ more platforms. Use for lead generation, brand monitoring, competitor analysis, influencer discovery, trend research, content analytics, audience analysis, review analysis, SEO intelligence, recruitment, or any data extraction task.

nehakanjamala/personal_website

This skill should be used when the user asks to "add a glassmorphism effect", "frosted glass UI", "Apple liquid glass style", "frosted blur card", "translucent glass panel animation", "make a frosted nav bar", "build a glass modal/dialog", "animate a glass card on hover", or "add a refracting liquid-glass hero". Covers frosted translucent panels with backdrop-filter blur, edge/specular highlights, SVG liquid-glass refraction, motion on hover/scroll/enter, and accessible reduced-transparency fallbacks.

nehakanjamala/personal_website

This skill should be used when the user asks to "build a scroll animation", "add ScrollTrigger", "pin a section while scrolling", "scrub an animation to scroll", "create a hero timeline", "do a horizontal scroll section", "split text and animate words", "animate a layout change with GSAP Flip", "sync ScrollTrigger with Lenis", "Lenis smooth scroll jittery", "ScrollTrigger markers drift", "ScrollTrigger pins break with Lenis/Locomotive", or "set up smooth scroll with GSAP pinning". Covers GSAP timelines, ScrollTrigger, SplitText, Flip, and Lenis/Locomotive smooth-scroll sync for code-driven web motion.

nehakanjamala/personal_website

This skill should be used when the user asks to "add a Lottie animation", "play a .lottie or .json animation", "integrate a Bodymovin/After Effects animation on web or mobile", "control Lottie playback / segments", "make a scroll-driven Lottie", "recolor a Lottie at runtime", or "export an AE animation to Lottie". Covers dotLottie/lottie-web integration, playback control, interactivity, theming, and the AE export checklist.

nehakanjamala/personal_website

This skill should be used when the user asks to "add a hover/press effect", "animate a toggle or switch", "build an animated like button", "make a toast/snackbar slide in", "animate a drawer or modal", "animate list reordering or add/remove", "do a shared-element layout transition", or "polish UI feedback". Covers UI motion with Framer Motion (motion/react) and modern CSS.

nehakanjamala/personal_website

Scrape web pages using Scrapling with anti-bot bypass (like Cloudflare Turnstile), stealth headless browsing, spiders framework, adaptive scraping, and JavaScript rendering. Use when asked to scrape, crawl, or extract data from websites; web_fetch fails; the site has anti-bot protections; write Python code to scrape/crawl; or write spiders.

nehakanjamala/personal_website

This skill should be used when the user asks to "animate an SVG", "make a line draw itself on", "do a stroke draw-on / signature animation", "morph one shape into another", "move an element along a path", "animate an icon/logo", or "animate an SVG gradient or filter". Covers stroke-dashoffset draw-on, path morphing, motion-along-path, and animated icons/gradients/filters via CSS, SMIL, and GSAP.

相關技能