新增工具! 探索最新内容

简介

AnimatedIcon 封装了来自 lucide-react 的图标,并为其描边添加动画效果,使图标看起来像是手绘出来的。它本身不包含任何图标。您仍然需要从 Lucide 导入图标,然后将其传递给 AnimatedIcon。

描边,而非填充

Lucide 图标由轮廓线构成,而这些轮廓线可以逐渐绘制出来。

虚线动画技巧

为每条线设置与其长度相同的虚线,将其移出可视区域,然后通过动画将其滑回视野中。

零额外依赖

使用浏览器内置的 Web Animations API,无需加载任何额外依赖。

安装

将此软件包与 lucide-react 一起安装。您的应用已经提供 react 和 lucide-react,因此不会产生重复依赖。

terminal
npm install @utilityzoo/icons lucide-react

环境要求

要求版本
react18 or 19
lucide-react0.300 or newer
BrowserAny current browser (needs the Web Animations API)

快速开始

从 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";

触发方式

触发方式决定动画何时播放。逐一尝试每种方式,然后复制下方对应的代码行。

inView

The default. Draws once when scrolled into view.

tsx
<AnimatedIcon icon={Heart} />

mount

Draws once as soon as the page loads.

tsx
<AnimatedIcon icon={Rocket} trigger="mount" />

hover

Draws each time the mouse enters. Hover it.

tsx
<AnimatedIcon icon={Bell} trigger="hover" />

click

Draws on every click. Click it.

tsx
<AnimatedIcon icon={Star} trigger="click" />

toggle

One icon that erases, then draws back, on each click.

tsx
<AnimatedIcon icon={Bookmark} trigger="toggle" />

toggle + toggleIcon

Two icons: the first erases, then the second draws.

tsx
<AnimatedIcon icon={Play} toggleIcon={Pause} trigger="toggle" />

loop

Endless draw and erase. Pauses when off screen.

tsx
<AnimatedIcon icon={Zap} trigger="loop" loopDelay={1000} />

在线体验

选择图标、设置触发方式并调整动画时间。下方代码会随着您的操作自动更新,可直接复制使用。

Click the icon to erase and draw (or swap with the second icon).

Color
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 图标,而不仅仅是这个简短列表吗?

属性参考

属性类型默认值功能说明
iconLucideIconrequiredThe Lucide icon component to animate.
trigger"inView" | "mount" | "hover" | "click" | "toggle" | "loop""inView"What starts the animation.
toggleIconLucideIconnoneSecond icon for trigger="toggle". The first erases, then this one draws.
durationnumber800Milliseconds each stroke takes.
staggernumber120Milliseconds between strokes. 0 draws them all together.
easingstring"ease-in-out"Any CSS easing, e.g. linear or cubic-bezier(...).
oncebooleantrueinView only. Animate the first time only.
replayOnClickbooleanfalseinView, mount, hover. Clicking replays the animation.
loopDelaynumber600loop only. Pause in ms between draw and erase.
size, color, strokeWidth, className…LucidePropsLucide defaultsEverything else goes straight to the icon.

实用示例

适用于常见开发场景的即用型复制粘贴代码示例。

Menu button

Menu turns into a close icon. Fast timing feels snappy in a navbar.

tsx
<button aria-label="Toggle menu" className="p-0">
  <AnimatedIcon
    icon={Menu} toggleIcon={X} trigger="toggle"
    duration={350} stagger={0} size={28}
  />
</button>

Play / pause

Swap two icons on click. Keep your own state in sync with onClick on the button.

tsx
<AnimatedIcon icon={Play} toggleIcon={Pause} trigger="toggle" size={32} />

Attention bell

A slow, quiet loop draws the eye without being annoying.

tsx
<AnimatedIcon icon={Bell} trigger="loop" loopDelay={2000} size={24} />

Feature card icon

Draws once as the card scrolls into view. Color comes from Tailwind.

tsx
<AnimatedIcon
  icon={Rocket} trigger="inView" size={40}
  className="text-primary" strokeWidth={1.5}
/>

注意:图标会自行处理点击事件。如果将其放在按钮内部,请移除按钮的内边距(或让图标占据整个可点击区域),以确保点击事件能够触发图标。

样式设置

除了动画相关属性之外,其他所有属性都会传递给图标,因此样式设置与普通 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。

工作原理

  1. 图标会渲染为普通 SVG,由 path、circle、line 等类似图形元素组成。
  2. 每个图形都会设置 pathLength="1",使所有线条具有相同的标准化长度,无需进行额外计算。
  3. 通过长度为一个单位的虚线样式隐藏线条,再将 dash offset 从 1 动画到 0,即可逐渐绘制出线条。
  4. 在动画开始和结束时调整透明度,避免圆形线条端点留下多余的小点。
  5. 各个图形会依次开始动画(即交错效果),最终状态会保存在元素上。

故障排除

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.

更新日志

VersionTitleChanges
0.1.2Initial releaseTriggers: inView, mount, hover, click, toggle, loop. Two-icon toggle. Reduced-motion support.

准备好让图标焕发生机了吗?

浏览所有 Lucide 图标,调整效果并复制代码。