@heroicons/react
是 Tailwind CSS 团队(由 Tailwind Labs 维护)提供的 React 图标库,包含一套 MIT 许可的免费 SVG 图标,主要用于现代 Web 项目。
它提供了 轮廓版(Outline) 和 实心版(Solid) 两套风格,并且每个图标都是 SVG 组件化,在 React 中直接使用。
两种风格
Outline
(线框风格)Solid
(实心风格)SVG React 组件
import
使用,不需要手动拷贝 SVG免费开源
Tailwind CSS 友好
轻量化
npm install @heroicons/react
或 Yarn:
yarn add @heroicons/react
import { HomeIcon } from '@heroicons/react/24/outline'
function App() {
return <HomeIcon className="h-6 w-6 text-blue-500" />
}
export default App
h-6 w-6
使用 Tailwind 设置宽高text-blue-500
设置颜色import { HomeIcon } from '@heroicons/react/24/solid'
function App() {
return <HomeIcon className="h-6 w-6 text-green-500" />
}
import { MoonIcon as MoonOutline } from '@heroicons/react/24/outline'
import { MoonIcon as MoonSolid } from '@heroicons/react/24/solid'
function ThemeToggle({ isDark }) {
return isDark ? <MoonSolid className="h-6 w-6" /> : <MoonOutline className="h-6 w-6" />
}
安装后,你可以看到支持以下路径:
@heroicons/react/20/solid
@heroicons/react/24/solid
@heroicons/react/24/outline
20
/ 24
表示图标尺寸(px)solid
/ outline
表示图标风格Heroicons 的 React 组件其实是一个 <svg>
,支持标准 SVG 属性:
Prop | 说明 |
---|---|
className |
设置宽高、颜色、margin(通常用 Tailwind) |
strokeWidth |
控制线条宽度(Outline 图标可用) |
aria-hidden |
可访问性属性,默认 true |
role |
设置为 "img" 以支持屏幕阅读器 |
导航栏
按钮图标
状态或反馈
结合 Tailwind 组件库
shadcn/ui
、Headless UI
做现代化界面优点:
缺点:
@heroicons/react
= Tailwind 官方免费 React 图标库