| name | web-icon-library |
| description | 网站图标库集成指南,提供 Lucide Icons 和 Heroicons 的使用规范和最佳实践 |
Web Icon Library Integration
推荐图标库
1. Lucide Icons(主推荐)
特点:
- 1000+ 精美图标
- 一致的设计语言
- 支持 React、Vue、Svelte、纯 HTML
- 完全开源(ISC License)
- 24x24 基础尺寸
- 可自定义颜色、大小、描边宽度
官网:https://lucide.dev
安装:
npm install lucide-react
npm install lucide-vue-next
<script src="https://unpkg.com/lucide@latest"></script>
2. Heroicons(备选)
特点:
- Tailwind CSS 官方图标库
- 292 个精选图标
- Outline 和 Solid 两种风格
- MIT License
- 24x24 基础尺寸
官网:https://heroicons.com
安装:
npm install @heroicons/react
直接使用 SVG 代码
Lucide Icons 使用指南
React 使用方式
import { Home, User, Settings, ChevronRight, Check } from 'lucide-react';
function MyComponent() {
return (
<div>
{/* 基础使用 */}
<Home />
{/* 自定义大小和颜色 */}
<User size={32} color="#3B82F6" />
{/* 自定义描边宽度 */}
<Settings size={24} strokeWidth={1.5} />
{/* 使用 CSS 类 */}
<ChevronRight className="w-6 h-6 text-blue-500" />
{/* 绝对像素大小 */}
<Check absoluteStrokeWidth size={20} />
</div>
);
}
纯 HTML/CDN 使用方式
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/lucide@latest"></script>
</head>
<body>
<i data-lucide="home"></i>
<i data-lucide="user" class="w-8 h-8 text-blue-500"></i>
<i data-lucide="settings"></i>
<script>
lucide.createIcons();
</script>
</body>
</html>
内联 SVG 方式(推荐用于静态网站)
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="m3 9 9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/>
<polyline points="9 22 9 12 15 12 15 22"/>
</svg>
常用图标分类
导航类
import {
Home,
Menu,
X,
ChevronDown,
ChevronRight,
ArrowRight,
ExternalLink
} from 'lucide-react';
功能类
import {
Search,
Settings,
User,
Bell,
Mail,
Phone,
Calendar,
Clock,
Download,
Upload,
Share2,
Heart,
Star,
Bookmark
} from 'lucide-react';
状态类
import {
Check,
X,
AlertCircle,
Info,
HelpCircle,
Loader,
CheckCircle,
XCircle,
AlertTriangle
} from 'lucide-react';
社交媒体类
import {
Twitter,
Facebook,
Instagram,
Linkedin,
Github,
Youtube,
Mail
} from 'lucide-react';
商业类
import {
ShoppingCart,
CreditCard,
DollarSign,
TrendingUp,
BarChart,
PieChart,
Package,
Truck
} from 'lucide-react';
文件/编辑类
import {
File,
FileText,
Folder,
Image,
Video,
Music,
Edit,
Trash,
Copy,
Save
} from 'lucide-react';
图标使用规范
尺寸规范
<Icon size={16} />
<Icon size={20} />
<Icon size={24} />
<Icon size={32} />
<Icon size={48} />
<Icon size={64} />
<Icon size={96} />
颜色规范
<Icon color="var(--color-primary)" />
<Icon color="var(--color-secondary)" />
<Icon className="text-blue-500" />
<Icon className="text-gray-600" />
<Icon color="currentColor" />
描边宽度
<Icon strokeWidth={1} />
<Icon strokeWidth={1.5} />
<Icon strokeWidth={2} />
<Icon strokeWidth={2.5} />
<Icon strokeWidth={3} />
响应式图标
<Icon className="w-4 h-4 sm:w-5 sm:h-5 md:w-6 md:h-6" />
<Icon size={isMobile ? 20 : 24} />
动画效果
旋转动画(加载中)
import { Loader } from 'lucide-react';
<Loader className="animate-spin" />
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.animate-spin {
animation: spin 1s linear infinite;
}
悬停效果
<Icon className="transition-colors hover:text-blue-500" />
<Icon className="transition-transform hover:scale-110" />
可访问性
添加 aria-label
<Home aria-label="返回首页" />
<i data-lucide="home" aria-label="返回首页"></i>
装饰性图标
<Icon aria-hidden="true" />
按钮中的图标
<button>
<Home className="mr-2" />
返回首页
</button>
<button aria-label="返回首页">
<Home />
</button>
图标组件封装(推荐)
import * as LucideIcons from 'lucide-react';
export function Icon({ name, size = 24, color = 'currentColor', className = '', ...props }) {
const IconComponent = LucideIcons[name];
if (!IconComponent) {
console.warn(`Icon "${name}" not found`);
return null;
}
return (
<IconComponent
size={size}
color={color}
className={className}
{...props}
/>
);
}
<Icon name="Home" size={24} />
<Icon name="User" className="text-blue-500" />
图标搜索工具
在线搜索
命令行搜索(如果有本地副本)
grep -r "home" node_modules/lucide-react/dist/esm/icons/
ls node_modules/lucide-react/dist/esm/icons/
性能优化
Tree Shaking(按需导入)
import { Home, User } from 'lucide-react';
import * as Icons from 'lucide-react';
懒加载
import { lazy, Suspense } from 'react';
const Icon = lazy(() => import('./Icon'));
<Suspense fallback={<div className="w-6 h-6" />}>
<Icon name="Home" />
</Suspense>
SVG Sprite(大量图标场景)
<svg style="display: none;">
<symbol id="icon-home" viewBox="0 0 24 24">
<path d="..."/>
</symbol>
</svg>
<svg class="w-6 h-6">
<use href="#icon-home"/>
</svg>
与设计系统集成
CSS 变量
:root {
--icon-size-sm: 16px;
--icon-size-md: 24px;
--icon-size-lg: 32px;
--icon-color-primary: #3B82F6;
--icon-color-secondary: #6B7280;
}
.icon {
width: var(--icon-size-md);
height: var(--icon-size-md);
color: var(--icon-color-primary);
}
Tailwind 配置
module.exports = {
theme: {
extend: {
spacing: {
'icon-sm': '16px',
'icon-md': '24px',
'icon-lg': '32px',
}
}
}
}
常见使用场景
导航栏
<nav>
<a href="/"><Home size={20} /> 首页</a>
<a href="/about"><Info size={20} /> 关于</a>
<a href="/contact"><Mail size={20} /> 联系</a>
</nav>
功能卡片
<div className="feature-card">
<div className="icon-wrapper">
<Zap size={48} color="#3B82F6" />
</div>
<h3>快速高效</h3>
<p>闪电般的加载速度</p>
</div>
按钮
<button className="btn-primary">
<Download size={20} />
下载应用
</button>
<button className="btn-icon" aria-label="搜索">
<Search size={20} />
</button>
列表项
<ul>
<li><Check size={16} className="text-green-500" /> 功能一</li>
<li><Check size={16} className="text-green-500" /> 功能二</li>
<li><Check size={16} className="text-green-500" /> 功能三</li>
</ul>
状态提示
<div className="alert alert-success">
<CheckCircle size={20} />
操作成功!
</div>
<div className="alert alert-error">
<XCircle size={20} />
操作失败!
</div>
图标替换映射表
从其他图标库迁移到 Lucide:
| Font Awesome | Heroicons | Lucide |
|---|
| fa-home | HomeIcon | Home |
| fa-user | UserIcon | User |
| fa-cog | CogIcon | Settings |
| fa-search | MagnifyingGlassIcon | Search |
| fa-bars | Bars3Icon | Menu |
| fa-times | XMarkIcon | X |
| fa-check | CheckIcon | Check |
| fa-chevron-down | ChevronDownIcon | ChevronDown |
质量检查清单
使用图标时确保:
与 Website Generation Workflow 集成
在生成网站代码时:
- 自动导入常用图标
- 根据 section 类型选择合适图标
- 确保图标与设计系统颜色一致
- 添加必要的可访问性属性
示例:
<section className="hero">
<h1>快速构建现代网站</h1>
<button>
<Rocket size={20} />
立即开始
</button>
</section>
<div className="features">
<div className="feature">
<Zap size={32} color="var(--color-primary)" />
<h3>快速</h3>
</div>
<div className="feature">
<Shield size={32} color="var(--color-primary)" />
<h3>安全</h3>
</div>
<div className="feature">
<Sparkles size={32} color="var(--color-primary)" />
<h3>优雅</h3>
</div>
</div>
参考资源