简介
AnimatedIcon 封装了来自 lucide-react 的图标,并为其描边添加动画效果,使图标看起来像是手绘出来的。它本身不包含任何图标。您仍然需要从 Lucide 导入图标,然后将其传递给 AnimatedIcon。
安装
将此软件包与 lucide-react 一起安装。您的应用已经提供 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" />;
}以上就是全部配置。更改 trigger 即可控制动画的播放时机,有关速度、尺寸和颜色的设置,请参阅属性参考。
框架指南
Next.js(App Router)
渲染 AnimatedIcon 的文件必须是客户端组件,因为您需要将一个图标(组件)作为属性传递。创建一个简单的客户端包装组件,然后在服务端页面中使用它。
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}
/>属性参考
实用示例
适用于常见开发场景的即用型复制粘贴代码示例。
注意:图标会自行处理点击事件。如果将其放在按钮内部,请移除按钮的内边距(或让图标占据整个可点击区域),以确保点击事件能够触发图标。
样式设置
除了动画相关属性之外,其他所有属性都会传递给图标,因此样式设置与普通 Lucide 图标完全一致。颜色使用 currentColor,因此会自动继承父元素的文本颜色。
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="Toggle menu"。
- 装饰性图标:当图标与可见文本一起出现时,无需添加额外标记。
- 循环动画:请谨慎使用。持续运动可能分散注意力,因此建议保持动画缓慢且幅度较小。
性能
- 无需加载动画库。组件直接使用浏览器内置的动画引擎。
- 使用 inView 的图标仅在进入屏幕可视区域时播放动画,之后停止监听。
- 使用 loop 的图标在滚动到屏幕之外时会自动暂停。
- 对于大型图标网格,建议优先使用 hover 或 inView,而不是 loop。
工作原理
- 图标会渲染为普通 SVG,由 path、circle、line 等类似图形元素组成。
- 每个图形都会设置 pathLength="1",使所有线条具有相同的标准化长度,无需进行额外计算。
- 通过长度为一个单位的虚线样式隐藏线条,再将 dash offset 从 1 动画到 0,即可逐渐绘制出线条。
- 在动画开始和结束时调整透明度,避免圆形线条端点留下多余的小点。
- 各个图形会依次开始动画(即交错效果),最终状态会保存在元素上。
故障排除
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.