はじめに
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をレンダリングするファイルはクライアントコンポーネントである必要があります。
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} />;
}代わりにファイルをコピー
コンポーネントは1つのファイルです。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}
/>プロップス一覧
レシピ
よく使われる構築パターン。
注意:アイコンは自身のクリックを処理します。
スタイリング
アニメーション以外のすべてのプロップはアイコンに渡されます。
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アイコンは画面外で自動的に一時停止します。
- 大きなグリッドでは、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.