关于 animated-component-libraries
为 React 开发者提供 Magic UI 与 React Bits 动画组件库的使用指导,涵盖组件选择、安装、定制及性能处理。内容结合代码示例和常见问题,适用于构建交互式网页界面。
主要功能
- 01Magic UI 组件安装与定制
- 02React Bits 组件使用模式
- 03动画背景与文字效果
- 04shadcn/ui 集成指导
- 05动画性能与问题排查
适用场景
- 01为营销页面添加文字动画
- 02构建带动画背景的首页
- 03在仪表盘展示动态统计数字
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ freshtechbro/ claudedesignskills --skill animated-component-libraries安装后在 Claude Code 中输入/animated-component-libraries确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/README.mdreferences/customization_guide.mdmagic_ui_components.mdreact_bits_components.mdscripts/component_importer.pyprops_generator.py详细介绍
动画组件库
概述
此技能专注于预构建的动画 React 组件库,特别是 Magic UI 和 React Bits。这些库提供可用于生产环境的动画组件,能够显著加快现代交互式 Web 应用的开发。
Magic UI 提供 150+ 个基于 Tailwind CSS 和 Framer Motion 构建的 TypeScript 组件,专为与 shadcn/ui 无缝集成而设计。组件可直接复制粘贴,并且具有很强的自定义能力。
React Bits 提供 90+ 个依赖项精简的动画 React 组件,专注于视觉效果、背景和微交互。组件强调性能和易于自定义。
这两个库都遵循现代 React 模式,支持 TypeScript,并可与常用设计系统集成。
核心概念
Magic UI 架构
Magic UI 组件基于三项基础技术构建:
- Tailwind CSS:实用工具优先的样式方案,可通过
tailwind.config.js完全自定义 - Framer Motion:基于物理效果的动画和手势识别
- shadcn/ui 集成:遵循 shadcn 的 CLI 安装方式和组件结构规范
安装方式:
# Via shadcn CLI (recommended)
npx shadcn@latest add https://magicui.design/r/animated-beam
# Manual installation
# 1. Copy component code to components/ui/
# 2. Install motion: npm install motion
# 3. Add required CSS animations to globals.css
# 4. Ensure cn() utility exists in lib/utils.ts
组件结构:
// All Magic UI components follow this pattern:
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 注重轻量、独立的组件,且依赖项精简:
- 自包含:每个组件的外部依赖项都很少
- CSS-in-JS 可选:许多组件使用内联样式或 CSS 模块
- 性能优先:针对 60fps 动画进行了优化
- 支持 WebGL:部分组件(Particles、Plasma)使用 WebGL 实现高级效果
安装:
# Manual copy-paste (primary method)
# Copy component files from reactbits.dev to your project
# Key dependencies (install as needed):
npm install framer-motion # For animation-heavy components
npm install ogl # For WebGL components (Particles, Plasma)
组件类别:
- 文本动画:BlurText、CircularText、CountUp、SpinningText
- 交互元素:MagicButton、Magnet、Dock、Stepper
- 背景:Aurora、Plasma、Particles
- 列表和布局:AnimatedList、Bento Grid
常见模式
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">
{/* Static Grid Pattern */}
<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"
)}
/>
{/* Animated Interactive Grid */}
<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">
{/* Word-by-word reveal */}
<BlurText
text="Transform your ideas into reality"
delay={100}
animateBy="words"
direction="top"
className="text-5xl font-bold text-center mb-8"
/>
{/* Character-by-character reveal with custom 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:带效果的按钮组件
创建带有微光和边框光束效果的交互式按钮:
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 with Animated Border */}
<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">
{/* Revenue Counter */}
<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>
{/* Uptime Percentage */}
<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>
{/* Customer Count */}
<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>
{/* Horizontal Marquee */}
<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>
{/* Vertical 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>
)
}
集成模式
与 shadcn/ui 集成
Magic UI 组件专为与 shadcn/ui 无缝协作而设计:
# Install shadcn/ui component
npx shadcn@latest add button card
# Install Magic UI component
npx shadcn@latest add https://magicui.design/r/shimmer-button
# Use together in components
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'
// Combine React Bits Magnet with Framer Motion gestures
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 (
<>
{/* Animated Page Transitions */}
<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>
{/* Persistent Dock Navigation */}
<Dock items={navItems} />
</>
)
}
组合使用 Magic UI 和 React Bits
在一个项目中发挥这两个库各自的优势:
// Magic UI: Patterns and structural components
import { GridPattern } from "@/components/ui/grid-pattern"
import { BorderBeam } from "@/components/ui/border-beam"
import { Marquee } from "@/components/ui/marquee"
// React Bits: Interactive elements and effects
import BlurText from './components/BlurText'
import CountUp from './components/CountUp'
import Particles from './components/Particles'
export default function LandingPage() {
return (
<main>
{/* Hero with React Bits background + Magic UI pattern */}
<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>
{/* Stats with React Bits CountUp */}
<section>
<CountUp end={10000} suffix="+" />
</section>
{/* Testimonials with Magic UI Marquee */}
<section>
<Marquee>
{/* Testimonial cards */}
</Marquee>
</section>
</main>
)
}
性能优化
Magic UI 性能技巧
- 使用 CSS 遮罩而非裁剪:处理大型图案时性能更佳
<GridPattern
className="[mask-image:radial-gradient(400px_circle_at_center,white,transparent)]"
/>
- 降低动画复杂度:在移动设备上减少 AnimatedGridPattern 的
numSquares
const isMobile = window.innerWidth < 768
<AnimatedGridPattern
numSquares={isMobile ? 20 : 50}
duration={isMobile ? 6 : 4}
/>
- 延迟加载组件:对较重的组件使用 React.lazy
const AnimatedGridPattern = React.lazy(() =>
import("@/components/ui/animated-grid-pattern")
)
React Bits 性能技巧
- WebGL 组件:在低端设备上减少粒子数量
const particleCount = navigator.hardwareConcurrency > 4 ? 300 : 150
<Particles
particleCount={particleCount}
speed={0.1}
/>
- 在减少动态效果时禁用动画:
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches
<BlurText
text="Accessible text"
delay={prefersReducedMotion ? 0 : 100}
animateBy={prefersReducedMotion ? "none" : "words"}
/>
- 优化 Marquee 内容:限制项目数量以提升性能
<Marquee repeat={2}> {/* Instead of default 4 */}
{items.slice(0, 10)} {/* Limit items */}
</Marquee>
- 对非关键动画使用 RequestIdleCallback:
useEffect(() => {
if ('requestIdleCallback' in window) {
requestIdleCallback(() => {
// Initialize expensive animations
})
}
}, [])
常见问题
1. 缺少依赖
问题:由于缺少 motion 或实用函数,组件无法正常工作。
解决方案:始终安装所需的依赖和实用函数:
# Magic UI requirements
npm install motion clsx tailwind-merge
# React Bits WebGL components
npm install ogl
# Ensure cn() utility exists
// 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">
{/* Background (z-0 or negative) */}
<GridPattern className="absolute inset-0 -z-10" />
{/* Content (higher z-index) */}
<div className="relative z-10">
<h1>Content appears above pattern</h1>
</div>
</div>
4. 多个动画组件导致性能问题
问题:多个高负载动画同时运行时,页面会卡顿。
解决方案:实现渐进增强和条件渲染:
import { useState, useEffect } from 'react'
export default function OptimizedPage() {
const [enableHeavyEffects, setEnableHeavyEffects] = useState(false)
useEffect(() => {
// Check device capability
const isHighEnd = navigator.hardwareConcurrency > 4 &&
!navigator.userAgent.includes('Mobile')
setEnableHeavyEffects(isHighEnd)
}, [])
return (
<section className="relative">
{enableHeavyEffects ? (
<Particles particleCount={300} />
) : (
<GridPattern /> {/* Lighter alternative */}
)}
<div className="content">
{/* Page content */}
</div>
</section>
)
}
5. TypeScript 类型错误
问题:TypeScript 对组件属性提出报错。
解决方案:扩展适当的基础类型:
// Magic UI pattern
interface CustomComponentProps extends React.ComponentPropsWithoutRef<"div"> {
customProp?: string
className?: string
}
// React Bits pattern
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}", // Include components directory
],
theme: {
extend: {},
},
plugins: [],
}
资源
官方文档
- Magic UI:https://magicui.design
- React Bits:https://reactbits.dev
- shadcn/ui:https://ui.shadcn.com
- Framer Motion:https://motion.dev
关键脚本
scripts/component_importer.py- 从两个库中导入并自定义组件scripts/props_generator.py- 生成组件属性配置
参考资料
references/magic_ui_components.md- 完整的 Magic UI 组件目录及使用示例references/react_bits_components.md- React Bits 组件库参考资料references/customization_guide.md- 两个库基于属性的自定义模式
入门资源
assets/component_showcase/- 所有组件的交互式演示assets/examples/- 落地页版块、仪表盘小组件、微交互示例
相关技能
- motion-framer:了解两个库所使用的底层动画概念
- gsap-scrolltrigger:滚动驱动动画的替代方案
- react-spring-physics:基于物理效果的动画库,可作为替代方案
- threejs-webgl:用于实现 3D 背景效果,替代 Particles/Plasma