animated-component-libraries

animated-component-libraries

熱門

整合 Magic UI(150+ 個 TypeScript/Tailwind/Motion 元件)與 React Bits(90+ 個極簡依賴動畫元件)的預製 React 動畫元件庫集合。當開發 Landing Page、行銷網站、儀表板或需要預製動畫元件而非手寫動畫的互動式 UI 時,可以使用此 Skill。觸發時機包含涉及動畫 UI 元件、Magic UI、React Bits、shadcn/ui 整合、Tailwind CSS 元件或元件庫選型的任務。此工具可作為使用 Framer Motion 或 GSAP 手動實現動畫的替代方案。

728星標
121分支
更新於 2025/11/20
SKILL.md
唯讀
名稱
animated-component-libraries
描述

整合 Magic UI(150+ 個 TypeScript/Tailwind/Motion 元件)與 React Bits(90+ 個極簡依賴動畫元件)的預製 React 動畫元件庫集合。當開發 Landing Page、行銷網站、儀表板或需要預製動畫元件而非手寫動畫的互動式 UI 時,可以使用此 Skill。觸發時機包含涉及動畫 UI 元件、Magic UI、React Bits、shadcn/ui 整合、Tailwind CSS 元件或元件庫選型的任務。此工具可作為使用 Framer Motion 或 GSAP 手動實現動畫的替代方案。

動畫元件庫 (Animated Component Libraries)

概觀 (Overview)

此 Skill 提供預製 React 動畫元件庫的專業知識,特別是 Magic UI 與 React Bits。這些元件庫提供可直接用於生產環境的動畫元件,能大幅加速現代互動式 Web 應用程式的開發。

Magic UI 提供 150+ 個基於 Tailwind CSS 與 Framer Motion 建置的 TypeScript 元件,專為與 shadcn/ui 無縫整合而設計。元件支援複製貼上,且具備高度可客製性。

React Bits 提供 90+ 個極簡依賴的動畫 React 元件,專注於視覺效果、背景與微互動 (micro-interactions)。這些元件強調效能與易客製性。

這兩個元件庫均遵循現代 React 模式、支援 TypeScript,並可整合至熱門的設計系統中。

核心概念 (Core Concepts)

Magic UI 架構

Magic UI 元件建立於三個核心技術之上:

  1. Tailwind CSS:Utility-first 樣式,可透過 tailwind.config.js 進行完全客製化
  2. Framer Motion:基於物理特性的動畫與手勢識別
  3. shadcn/ui 整合:遵循 shadcn 規範進行 CLI 安裝與元件結構設定

安裝方法:

# 透過 shadcn CLI(推薦)
npx shadcn@latest add https://magicui.design/r/animated-beam

# 手動安裝
# 1. 將元件程式碼複製到 components/ui/
# 2. 安裝 motion: npm install motion
# 3. 將所需的 CSS 動畫新增至 globals.css
# 4. 確保 cn() 工具函式已存在於 lib/utils.ts

元件結構:

// 所有 Magic UI 元件均遵循此模式:
import { cn } from "@/lib/utils"
import { motion } from "motion/react"

interface ComponentProps extends React.ComponentPropsWithoutRef<"div"> {
  customProp?: string
  className?: string
}

export function MagicComponent({ className, customProp, ...props }: ComponentProps) {
  return (
    <motion.div
      className={cn("base-styles", className)}
      {...props}
    >
      {/* Component content */}
    </motion.div>
  )
}

React Bits 架構

React Bits 強調輕量、獨立且具極簡依賴的元件:

  1. 自包含 (Self-Contained):每個元件的外部依賴極少
  2. CSS-in-JS 可選:許多元件使用 inline 樣式或 CSS Modules
  3. 效能優先:針對 60fps 動畫進行最佳化
  4. WebGL 支援:部分元件(如 Particles、Plasma)使用 WebGL 實現進階特效

安裝方式:

# 手動複製貼上(主要方法)
# 從 reactbits.dev 將元件檔案複製到您的專案中

# 關鍵依賴(視需要安裝):
npm install framer-motion  # 用於重度動畫元件
npm install ogl           # 用於 WebGL 元件(Particles、Plasma)

元件類別:

  • 文字動畫 (Text Animations):BlurText、CircularText、CountUp、SpinningText
  • 互動元素 (Interactive Elements):MagicButton、Magnet、Dock、Stepper
  • 背景特效 (Backgrounds):Aurora、Plasma、Particles
  • 列表與佈局 (Lists & Layouts):AnimatedList、Bento Grid

常見模式 (Common Patterns)

1. Magic UI:動畫背景圖案

使用 SVG 圖案建立動態背景效果:

import { GridPattern } from "@/components/ui/grid-pattern"
import { AnimatedGridPattern } from "@/components/ui/animated-grid-pattern"
import { cn } from "@/lib/utils"

export default function HeroSection() {
  return (
    <div className="relative flex h-[500px] w-full items-center justify-center overflow-hidden rounded-lg border">
      {/* 靜態網格圖案 */}
      <GridPattern
        squares={[
          [4, 4], [5, 1], [8, 2], [5, 3], [10, 10], [12, 15]
        ]}
        className={cn(
          "[mask-image:radial-gradient(400px_circle_at_center,white,transparent)]",
          "fill-gray-400/30 stroke-gray-400/30"
        )}
      />

      {/* 動畫互動網格 */}
      <AnimatedGridPattern
        numSquares={50}
        maxOpacity={0.5}
        duration={4}
        repeatDelay={0.5}
        className={cn(
          "[mask-image:radial-gradient(500px_circle_at_center,white,transparent)]",
          "inset-x-0 inset-y-[-30%] h-[200%] skew-y-12"
        )}
      />

      <h1 className="relative z-10 text-6xl font-bold">
        Your Content Here
      </h1>
    </div>
  )
}

2. React Bits:文字漸顯動畫

使用 BlurText 實作由捲動觸發的文字漸顯效果:

import BlurText from './components/BlurText'

export default function MarketingSection() {
  return (
    <section className="py-20">
      {/* 逐字漸顯 */}
      <BlurText
        text="Transform your ideas into reality"
        delay={100}
        animateBy="words"
        direction="top"
        className="text-5xl font-bold text-center mb-8"
      />

      {/* 逐字元漸顯,支援自訂Easing */}
      <BlurText
        text="Pixel-perfect animations at your fingertips"
        delay={50}
        animateBy="characters"
        direction="bottom"
        threshold={0.3}
        stepDuration={0.4}
        animationFrom={{ filter: 'blur(20px)', opacity: 0, y: 50 }}
        animationTo={{ filter: 'blur(0px)', opacity: 1, y: 0 }}
        className="text-2xl text-gray-600 text-center"
      />
    </section>
  )
}

3. Magic UI:帶有特效的按鈕元件

建立帶有微光 (Shimmer) 與邊框光束 (Border Beam) 效果的互動式按鈕:

import { ShimmerButton } from "@/components/ui/shimmer-button"
import { BorderBeam } from "@/components/ui/border-beam"
import { Button } from "@/components/ui/button"
import { Card } from "@/components/ui/card"

export default function CTASection() {
  return (
    <div className="flex gap-4 items-center">
      {/* Shimmer Button */}
      <ShimmerButton
        shimmerColor="#ffffff"
        shimmerSize="0.05em"
        shimmerDuration="3s"
        borderRadius="100px"
        background="rgba(0, 0, 0, 1)"
        className="px-8 py-3"
      >
        Get Started
      </ShimmerButton>

      {/* 帶有動畫邊框的卡片 */}
      <Card className="relative w-[350px] overflow-hidden">
        <div className="p-6">
          <h3 className="text-2xl font-bold">Premium Plan</h3>
          <p className="text-gray-600">Unlock all features</p>
          <Button className="mt-4">Subscribe</Button>
        </div>
        <BorderBeam duration={8} size={100} />
      </Card>
    </div>
  )
}

4. React Bits:互動式 Dock 導覽列

實作類似 macOS 的 Dock 導覽列與放大效果:

import Dock from './components/Dock'
import { VscHome, VscArchive, VscAccount, VscSettingsGear } from 'react-icons/vsc'
import { useNavigate } from 'react-router-dom'

export default function AppNavigation() {
  const navigate = useNavigate()

  const dockItems = [
    {
      icon: <VscHome size={24} />,
      label: 'Dashboard',
      onClick: () => navigate('/dashboard')
    },
    {
      icon: <VscArchive size={24} />,
      label: 'Projects',
      onClick: () => navigate('/projects')
    },
    {
      icon: <VscAccount size={24} />,
      label: 'Profile',
      onClick: () => navigate('/profile')
    },
    {
      icon: <VscSettingsGear size={24} />,
      label: 'Settings',
      onClick: () => navigate('/settings')
    }
  ]

  return (
    <div className="fixed bottom-4 left-1/2 -translate-x-1/2">
      <Dock
        items={dockItems}
        spring={{ mass: 0.15, stiffness: 200, damping: 15 }}
        magnification={80}
        distance={250}
        panelHeight={70}
        baseItemSize={55}
      />
    </div>
  )
}

5. React Bits:使用 CountUp 的數字動畫

在儀表板或行銷頁面中顯示數字累加動畫:

import CountUp from './components/CountUp'

export default function Statistics() {
  return (
    <div className="grid grid-cols-1 md:grid-cols-3 gap-8 py-16">
      {/* 營收計數器 */}
      <div className="stat-card text-center">
        <CountUp
          start={0}
          end={1000000}
          duration={3}
          separator=","
          prefix="$"
          className="text-6xl font-bold text-blue-600"
        />
        <p className="text-xl text-gray-600 mt-2">Revenue Generated</p>
      </div>

      {/* 正常運作時間百分比 */}
      <div className="stat-card text-center">
        <CountUp
          end={99.9}
          duration={2.5}
          decimals={1}
          suffix="%"
          className="text-6xl font-bold text-green-600"
        />
        <p className="text-xl text-gray-600 mt-2">Uptime</p>
      </div>

      {/* 客戶數量 */}
      <div className="stat-card text-center">
        <CountUp
          end={10000}
          duration={2}
          separator=","
          className="text-6xl font-bold text-purple-600"
        />
        <p className="text-xl text-gray-600 mt-2">Happy Customers</p>
      </div>
    </div>
  )
}

6. Magic UI:跑馬燈元件 (Marquee)

建立無限捲動的內容展示:

import { Marquee } from "@/components/ui/marquee"

const testimonials = [
  { name: "John Doe", text: "Amazing product!", avatar: "/avatar1.jpg" },
  { name: "Jane Smith", text: "Exceeded expectations", avatar: "/avatar2.jpg" },
  { name: "Bob Johnson", text: "Highly recommend", avatar: "/avatar3.jpg" }
]

export default function Testimonials() {
  return (
    <section className="py-20">
      <h2 className="text-4xl font-bold text-center mb-12">
        What Our Customers Say
      </h2>

      {/* 水平跑馬燈 */}
      <Marquee pauseOnHover className="[--duration:40s]">
        {testimonials.map((item, idx) => (
          <div key={idx} className="mx-4 w-[350px] rounded-lg border p-6">
            <p className="text-lg mb-4">"{item.text}"</p>
            <div className="flex items-center gap-3">
              <img src={item.avatar} alt={item.name} className="w-10 h-10 rounded-full" />
              <p className="font-semibold">{item.name}</p>
            </div>
          </div>
        ))}
      </Marquee>

      {/* 垂直跑馬燈 */}
      <Marquee vertical reverse className="h-[400px] mt-8">
        {testimonials.map((item, idx) => (
          <div key={idx} className="my-4 w-full max-w-md rounded-lg border p-6">
            <p>{item.text}</p>
          </div>
        ))}
      </Marquee>
    </section>
  )
}

7. React Bits:WebGL 背景特效

加入高效能的動畫背景:

import Particles from './components/Particles'
import Plasma from './components/Plasma'
import Aurora from './components/Aurora'

// 粒子效果 (Particles Effect)
export default function ParticlesHero() {
  return (
    <section style={{ position: 'relative', height: '100vh' }}>
      <Particles
        particleCount={200}
        particleColors={['#FF6B6B', '#4ECDC4', '#45B7D1']}
        particleSpread={10}
        speed={0.12}
        moveParticlesOnHover={true}
        particleHoverFactor={2}
        particleBaseSize={100}
        sizeRandomness={1.2}
        alphaParticles={true}
        cameraDistance={20}
        className="particles-bg"
      />
      <div className="relative z-10 flex items-center justify-center h-full">
        <h1 className="text-7xl font-bold text-white">
          Welcome to the Future
        </h1>
      </div>
    </section>
  )
}

// 電漿效果 (Plasma Effect)
export default function PlasmaBackground() {
  return (
    <div className="relative min-h-screen">
      <Plasma
        color1="#FF0080"
        color2="#7928CA"
        color3="#00DFD8"
        speed={0.8}
        blur={30}
        className="plasma-bg"
      />
      <div className="relative z-10 p-8">
        <h1>Content with Plasma Background</h1>
      </div>
    </div>
  )
}

// 極光效果 (Aurora Effect)
export default function AuroraHero() {
  return (
    <div className="relative min-h-screen">
      <Aurora
        colors={['#FF00FF', '#00FFFF', '#FFFF00']}
        speed={0.5}
        blur={80}
      />
      <main className="relative z-10">
        <h1>Cyberpunk Aurora Effect</h1>
      </main>
    </div>
  )
}

整合模式 (Integration Patterns)

與 shadcn/ui 整合

Magic UI 元件設計為可與 shadcn/ui 無縫運作:

# 安裝 shadcn/ui 元件
npx shadcn@latest add button card

# 安裝 Magic UI 元件
npx shadcn@latest add https://magicui.design/r/shimmer-button

# 在元件中混合使用
import { Button } from "@/components/ui/button"
import { Card } from "@/components/ui/card"
import { ShimmerButton } from "@/components/ui/shimmer-button"
import { BorderBeam } from "@/components/ui/border-beam"

必需的工具函式 (lib/utils.ts):

import clsx, { ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

與 Framer Motion 整合

這兩個元件庫均利用 Framer Motion 來實現動畫:

import { motion } from "framer-motion"
import { Magnet } from './components/Magnet'

// 將 React Bits Magnet 與 Framer Motion 手勢結合
export default function InteractiveCard() {
  return (
    <Magnet magnitude={0.4} maxDistance={180}>
      <motion.div
        whileHover={{ scale: 1.05 }}
        whileTap={{ scale: 0.95 }}
        className="card p-6 rounded-xl shadow-lg"
      >
        <h3>Interactive Card</h3>
        <p>Combines magnetic pull with scale animation</p>
      </motion.div>
    </Magnet>
  )
}

與 React Router 整合

將動畫元件與路由結合:

import { AnimatePresence, motion } from "framer-motion"
import { useLocation, Routes, Route } from "react-router-dom"
import { Dock } from './components/Dock'

export default function App() {
  const location = useLocation()

  return (
    <>
      {/* 帶有動畫的頁面轉場 */}
      <AnimatePresence mode="wait">
        <Routes location={location} key={location.pathname}>
          <Route path="/" element={
            <motion.div
              initial={{ opacity: 0, y: 20 }}
              animate={{ opacity: 1, y: 0 }}
              exit={{ opacity: 0, y: -20 }}
            >
              <HomePage />
            </motion.div>
          } />
        </Routes>
      </AnimatePresence>

      {/* 常駐的 Dock 導覽列 */}
      <Dock items={navItems} />
    </>
  )
}

混合使用 Magic UI 與 React Bits

在單一專案中發揮兩個元件庫的優勢:

// Magic UI:背景圖案與結構性元件
import { GridPattern } from "@/components/ui/grid-pattern"
import { BorderBeam } from "@/components/ui/border-beam"
import { Marquee } from "@/components/ui/marquee"

// React Bits:互動式元素與視覺效果
import BlurText from './components/BlurText'
import CountUp from './components/CountUp'
import Particles from './components/Particles'

export default function LandingPage() {
  return (
    <main>
      {/* Hero 區域:React Bits 背景 + Magic UI 圖案 */}
      <section className="relative h-screen">
        <Particles particleCount={150} />
        <GridPattern
          squares={[[4,4], [8,2], [12,6]]}
          className="opacity-30"
        />
        <BlurText
          text="Next-Generation Platform"
          className="text-7xl font-bold"
        />
      </section>

      {/* 統計數據:使用 React Bits CountUp */}
      <section>
        <CountUp end={10000} suffix="+" />
      </section>

      {/* 推薦評語:使用 Magic UI Marquee */}
      <section>
        <Marquee>
          {/* Testimonial cards */}
        </Marquee>
      </section>
    </main>
  )
}

效能最佳化 (Performance Optimization)

Magic UI 效能技巧

  1. 使用 CSS Mask 替代 Clipping:在大型圖案上具備更高的渲染效能
<GridPattern
  className="[mask-image:radial-gradient(400px_circle_at_center,white,transparent)]"
/>
  1. 降低動畫複雜度:在行動裝置上為 AnimatedGridPattern 降低 numSquares
const isMobile = window.innerWidth < 768
<AnimatedGridPattern
  numSquares={isMobile ? 20 : 50}
  duration={isMobile ? 6 : 4}
/>
  1. 懶載入元件 (Lazy Load):對重度元件使用 React.lazy
const AnimatedGridPattern = React.lazy(() =>
  import("@/components/ui/animated-grid-pattern")
)

React Bits 效能技巧

  1. WebGL 元件:在低階裝置上減少粒子數量
const particleCount = navigator.hardwareConcurrency > 4 ? 300 : 150

<Particles
  particleCount={particleCount}
  speed={0.1}
/>
  1. 根據「減少動態效果 (Reduced Motion)」停用動畫:
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches

<BlurText
  text="Accessible text"
  delay={prefersReducedMotion ? 0 : 100}
  animateBy={prefersReducedMotion ? "none" : "words"}
/>
  1. 最佳化跑馬燈內容:限制項目數量以獲得更好的效能
<Marquee repeat={2}> {/* 替代預設的 4 */}
  {items.slice(0, 10)} {/* 限制項目數量 */}
</Marquee>
  1. 針對非關鍵動畫使用 RequestIdleCallback:
useEffect(() => {
  if ('requestIdleCallback' in window) {
    requestIdleCallback(() => {
      // 初始化開銷較大的動畫
    })
  }
}, [])

常見陷阱 (Common Pitfalls)

1. 遺漏套件依賴

問題:因缺少 motion 或工具函式導致元件崩潰。

解決方案:務必安裝所需的依賴套件與工具函式:

# Magic UI 需求
npm install motion clsx tailwind-merge

# React Bits WebGL 元件需求
npm install ogl

# 確保 cn() 工具函式已存在
// lib/utils.ts
import clsx, { ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

2. CSS 動畫未套用

問題:手動安裝後,Magic UI 動畫無法正常運作。

解決方案:將所需的 CSS 動畫新增至 globals.css:

/* app/globals.css */
@theme inline {
  --animate-ripple: ripple var(--duration, 2s) ease calc(var(--i, 0) * 0.2s) infinite;
  --animate-shimmer-slide: shimmer-slide var(--speed) ease-in-out infinite alternate;
  --animate-marquee: marquee var(--duration) linear infinite;
  --animate-marquee-vertical: marquee-vertical var(--duration) linear infinite;
}

@keyframes ripple {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(0.9); }
}

@keyframes shimmer-slide {
  to { transform: translate(calc(100cqw - 100%), 0); }
}

@keyframes marquee {
  from { transform: translateX(0); }
  to { transform: translateX(calc(-100% - var(--gap))); }
}

@keyframes marquee-vertical {
  from { transform: translateY(0); }
  to { transform: translateY(calc(-100% - var(--gap))); }
}

3. Z-Index 圖層衝突

問題:背景圖案或特效遮蓋了前景內容。

解決方案:使用正確的 z-index 分層:

<div className="relative">
  {/* 背景 (z-0 或負數) */}
  <GridPattern className="absolute inset-0 -z-10" />

  {/* 內容 (較高 z-index) */}
  <div className="relative z-10">
    <h1>Content appears above pattern</h1>
  </div>
</div>

4. 同時執行多個動畫元件導致效能問題

問題:當多個重度動畫同時執行時,頁面出現卡頓。

解決方案:實作漸進式增強 (progressive enhancement) 與條件式渲染:

import { useState, useEffect } from 'react'

export default function OptimizedPage() {
  const [enableHeavyEffects, setEnableHeavyEffects] = useState(false)

  useEffect(() => {
    // 檢查裝置能力
    const isHighEnd = navigator.hardwareConcurrency > 4 &&
                     !navigator.userAgent.includes('Mobile')
    setEnableHeavyEffects(isHighEnd)
  }, [])

  return (
    <section className="relative">
      {enableHeavyEffects ? (
        <Particles particleCount={300} />
      ) : (
        <GridPattern /> {/* 較輕量的替代方案 */}
      )}

      <div className="content">
        {/* Page content */}
      </div>
    </section>
  )
}

5. TypeScript 型別錯誤

問題:TypeScript 針對元件 Props 拋出型別錯誤。

解決方案:正確擴充基底型別:

// Magic UI 模式
interface CustomComponentProps extends React.ComponentPropsWithoutRef<"div"> {
  customProp?: string
  className?: string
}

// React Bits 模式
interface CustomProps extends React.HTMLAttributes<HTMLDivElement> {
  customProp?: string
}

6. Tailwind 類別未套用

問題:Magic UI 元件中的自訂 Tailwind 類別沒有生效。

解決方案:確保內容路徑包含元件目錄:

// tailwind.config.js
module.exports = {
  content: [
    "./src/**/*.{js,jsx,ts,tsx}",
    "./components/**/*.{js,jsx,ts,tsx}", // 包含元件目錄
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

相關資源 (Resources)

官方文件

核心腳本

  • scripts/component_importer.py - 從兩個元件庫匯入並客製化元件
  • scripts/props_generator.py - 生成元件 Prop 設定

參考檔案

  • references/magic_ui_components.md - 完整的 Magic UI 元件目錄與使用範例
  • references/react_bits_components.md - React Bits 元件庫參考指南
  • references/customization_guide.md - 適用於這兩個元件庫的 Prop 客製化模式

入門資源

  • assets/component_showcase/ - 所有元件的互動式展示範例
  • assets/examples/ - Landing Page 區塊、儀表板小工具、微互動範例

相關 Skill (Related Skills)

  • motion-framer:用於理解兩個元件庫背後所使用的動畫概念
  • gsap-scrolltrigger:捲動驅動動畫的替代解決方案
  • react-spring-physics:另一種基於物理特性的動畫庫
  • threejs-webgl:作為 Particles/Plasma 替代方案的 3D 背景特效