例如:
<Bars3Icon className="h-4 w-4 text-gray-500" />
这是 React 中使用 @heroicons/react 库的一个图标组件,具体作用是:
className
属性使用 Tailwind CSS 类名设置外观:h-4
:高度 4pxw-4
:宽度 4pxtext-gray-500
:颜色为中度灰色渲染后,它会在页面上显示一个小的灰色三横线图标。
Bars3Icon 是一个灵活的组件,可以通过调整 props 或上下文有多种用法:
调整大小
<Bars3Icon className="h-6 w-6" /> {/* 6px × 6px */}
<Bars3Icon className="h-8 w-8" /> {/* 8px × 8px */}
<Bars3Icon className="h-12 w-12" /> {/* 12px × 12px */}
改变颜色
<Bars3Icon className="h-4 w-4 text-blue-500" /> {/* 蓝色 */}
<Bars3Icon className="h-4 w-4 text-red-600" /> {/* 红色 */}
<Bars3Icon className="h-4 w-4 text-green-400" /> {/* 绿色 */}
添加交互效果
<Bars3Icon className="h-4 w-4 text-gray-500 hover:text-gray-700" /> {/* 鼠标悬停变深 */}
<Bars3Icon className="h-4 w-4 text-gray-500 focus:text-blue-500" /> {/* 点击时变蓝 */}
作为按钮的一部分
<button className="p-2 rounded-md hover:bg-gray-100">
<Bars3Icon className="h-4 w-4 text-gray-500" />
</button>
动态样式
const isActive = true;
<Bars3Icon className={`h-4 w-4 ${isActive ? 'text-blue-500' : 'text-gray-500'}`} />
搭配其他属性
<Bars3Icon
className="h-4 w-4 text-gray-500"
aria-hidden="true" // 隐藏给屏幕阅读器
title="Menu" // 鼠标悬停提示
/>
不同风格
import { Bars3Icon } from '@heroicons/react/24/outline'
<Bars3Icon className="h-4 w-4 text-gray-500" /> {/* 描边风格 */}
动画效果
<Bars3Icon className="h-4 w-4 text-gray-500 animate-spin" /> {/* 旋转动画 */}
<Bars3Icon className="h-4 w-4 text-gray-500 transition-transform hover:rotate-90" /> {/* 悬停旋转90度 */}
使用 pnpm 安装特定版本的命令如下:
安装特定版本:
pnpm add @heroicons/react@2.0.16
使用方式保持一致:
import { Bars3Icon } from '@heroicons/react/20/solid'
function MyComponent() {
return (
<Bars3Icon className="h-6 w-6" />
)
}