บทนำ
AnimatedIcon ห่อหุ้มไอคอนจาก lucide-react และสร้างแอนิเมชันให้เส้นสายดูเหมือนวาดด้วยมือ
การติดตั้ง
ติดตั้งแพ็กเกจควบคู่ไปกับ lucide-react
terminal
npm install @utilityzoo/icons lucide-reactข้อกำหนด
เริ่มต้นอย่างรวดเร็ว
นำเข้าไอคอนจาก Lucide แล้วส่งไปยัง AnimatedIcon
tsx
"use client";
import { Heart } from "lucide-react";
import { AnimatedIcon } from "@utilityzoo/icons";
export default function LikeButton() {
return <AnimatedIcon icon={Heart} trigger="hover" />;
}นี่คือการตั้งค่าทั้งหมด
คู่มือเฟรม워크
Next.js (App Router)
ไฟล์ที่เรนเดอร์ AnimatedIcon จะต้องเป็น client component
components/like-button.tsx
// components/like-button.tsx
"use client";
import { Heart } from "lucide-react";
import { AnimatedIcon } from "@utilityzoo/icons";
export function LikeButton() {
return <AnimatedIcon icon={Heart} trigger="click" color="crimson" />;
}app/page.tsx
// app/page.tsx (a server component is fine here)
import { LikeButton } from "@/components/like-button";
export default function Page() {
return <LikeButton />;
}Vite, Create React App, React Router
ทุกอย่างทำงานในเบราว์เซอร์
tsx
// src/App.tsx (Vite, Create React App, React Router)
// No directive needed outside the Next.js App Router.
import { Rocket } from "lucide-react";
import { AnimatedIcon } from "@utilityzoo/icons";
export default function App() {
return <AnimatedIcon icon={Rocket} trigger="mount" size={64} />;
}คัดลอกไฟล์แทน
คอมโพเนนต์เป็นไฟล์เดียว คัดลอกได้จาก GitHub GitHub.
tsx
// Copy AnimatedIcon.tsx into your project, then import it locally:
import AnimatedIcon from "@/components/animated-icon";ทริกเกอร์
ทริกเกอร์เป็นตัวกำหนดว่าแอนิเมชันจะเล่นเมื่อใด
สนามทดลอง
เลือกไอคอน เลือกทริกเกอร์ และปรับเวลา
tsx (updates as you change settings)
import { Play, Pause } from "lucide-react";
import { AnimatedIcon } from "@utilityzoo/icons";
<AnimatedIcon
icon={Play}
toggleIcon={Pause}
trigger="toggle"
size={72}
/>รายการ Props
ตัวอย่างการใช้งาน
รูปแบบโค้ดที่คัดลอกไปใช้ได้ทันที
หมายเหตุ: ไอคอนจัดการการคลิกด้วยตัวเอง
การจัดสไตล์
Props ทั้งหมดนอกเหนือจากแอนิเมชันจะถูกส่งต่อไปยังไอคอน
tsx
<AnimatedIcon icon={Star} className="text-amber-500" /> {/* Tailwind color */}
<AnimatedIcon icon={Star} color="#f59e0b" size={40} /> {/* explicit color and size */}
<AnimatedIcon icon={Star} strokeWidth={1.25} /> {/* thinner lines */}
<AnimatedIcon icon={Star} absoluteStrokeWidth /> {/* keep line width fixed when resizing */}tsx (other outline icon sets)
import { Home } from "@tabler/icons-react"; // any outline icon component
import { AnimatedIcon } from "@utilityzoo/icons";
<AnimatedIcon icon={Home} trigger="hover" />การเข้าถึง
- การเคลื่อนไหวลดลง: ไอคอนจะนิ่งเมื่อผู้ใช้เปิดใช้งาน
- ไอคอนที่มีความหมาย: เพิ่ม aria-label ให้กับปุ่ม
- ไอคอนตกแต่ง: ไม่ต้องใช้มาร์กอัปเสริม
- ลูป: ใช้แต่น้อย
ประสิทธิภาพ
- ไม่มีการโหลดไลบรารีแอนิเมชันภายนอก
- ไอคอน inView จะเล่นแอนิเมชันเมื่อเลื่อนมาปรากฏ
- ไอคอน loop จะหยุดชั่วคราวเมื่ออยู่นอกจอ
- สำหรับกริดขนาดใหญ่ แนะนำให้ใช้ hover หรือ inView
วิธีการทำงาน
- ไอคอนของคุณเรนเดอร์เป็น SVG ปกติ
- แต่ละรูปร่างได้รับ pathLength="1"
- รูปแบบเส้นประซ่อนเส้นไว้
- ความทึบแสงจะถูกแอนิเมชันที่จุดเริ่มต้นและสิ้นสุด
- รูปร่างจะเริ่มทีละอันตามลำดับ
การแก้ไขปัญหา
You passed an icon from a server component. Icons are components, and components can't cross from server to client. Put the <AnimatedIcon /> usage in a file that starts with "use client" (see the Next.js guide), then import that file where you need it.