跳到主要内容

Icon

用于渲染 Lucide 图标组件的包装组件,可保持统一的尺寸和样式。

VEF 专属组件。 不属于 Ant Design 的一部分。

何时使用

  • 渲染通过属性传入的 Lucide 图标组件。
  • 在整个应用中为图标应用统一的尺寸和颜色。

基础用法

import { Icon } from '@vef-framework-react/components';
import { Settings } from 'lucide-react';

export default function Demo() {
return <Icon component={Settings} />;
}

动态图标组件

import { Icon } from '@vef-framework-react/components';
import type { IconProps } from '@vef-framework-react/components';
import { User, Settings, Bell } from 'lucide-react';
import { LucideIcon } from 'lucide-react';

interface NavItem {
label: string;
IconComponent: LucideIcon;
}

const navItems: NavItem[] = [
{ label: 'Profile', IconComponent: User },
{ label: 'Settings', IconComponent: Settings },
{ label: 'Notifications', IconComponent: Bell },
];

export default function Nav() {
return (
<ul>
{navItems.map((item) => (
<li key={item.label}>
<Icon component={item.IconComponent} />
<span>{item.label}</span>
</li>
))}
</ul>
);
}

API

Icon 继承了 Lucide 的全部 LucideProps 属性(sizecolorchildren 除外),并新增以下属性:

PropTypeDefault说明
componentComponentType<LucideProps>要渲染的 Lucide 图标组件,与 iconNode 互斥,二者需二选一
iconNodeIconNode要渲染的原始 Lucide 图标节点数据,与 component 互斥,二者需二选一
strokeWidthnumber2描边宽度
classNamestringCSS 类名
styleCSSProperties行内样式

图标的尺寸和颜色不可通过 props 配置——图标始终以 1.2em 渲染,并继承父级的 color

最佳实践

  • 当图标组件是通过属性传入时使用 Icon。对于静态图标,直接导入并使用 Lucide 组件即可。
  • 对于以字符串形式存储的图标名称,请改用 DynamicIcon