Skip to content

Lucide Solid

用于 Lucide 图标的 SolidJS 组件,利用 Solid 的细粒度响应系统。每个图标都是一个响应式 Solid 组件,以内联 SVG 的形式渲染,通过 Solid 的编译时优化和响应式原语提供卓越的性能。

🌐 SolidJS components for Lucide icons that leverage Solid's fine-grained reactivity system. Each icon is a reactive Solid component that renders as an inline SVG, providing exceptional performance through Solid's compile-time optimizations and reactive primitives.

你可以完成的事:

  • 将图标用作 SolidJS 组件,并实现细粒度的响应式
  • 使用 Solid 的响应式系统创建高性能界面
  • 构建响应信号和 store 的动态图标组件
  • 与 Solid 的 JSX 和组件模式无缝集成
  • 通过直接导入图标和最小化运行时开销优化性能

安装

🌐 Installation

sh
pnpm add lucide-solid
sh
yarn add lucide-solid
sh
npm install lucide-solid
sh
bun add lucide-solid

如何使用

🌐 How to use

Lucide 基于 ES 模块构建,因此完全支持 tree-shaking。

🌐 Lucide is built with ES Modules, so it's completely tree-shakable.

每个图标都可以作为 Solid 组件导入,它会渲染一个内联 SVG 元素。这样,只有导入到项目中的图标会包含在最终的包中,其余的图标会被 tree-shake 掉。

🌐 Each icon can be imported as a Solid component, which renders an inline SVG element. This way, only the icons that are imported into your project are included in the final bundle. The rest of the icons are tree-shaken away.

示例

🌐 Example

可以传递其他属性来调整图标:

🌐 Additional props can be passed to adjust the icon:

jsx
import { Camera } from 'lucide-solid';

// Usage
const App = () => {
  return <Camera color="red" size={48} />;
};

export default App;

Vite 在开发服务器上的加载/执行问题可以通过直接从 lucide-solid/icons 目录导入图标来解决:

🌐 Vite loading/performing issues with the dev server can be resolved by import icons directly from the lucide-solid/icons directory:

jsx
import Camera from 'lucide-solid/icons/camera';

// Usage
const App = () => {
  return <Camera color="red" size={48} />;
};

export default App;

属性

🌐 Props

名称类型默认值
size数字24
color字符串currentColor
strokeWidth数字2
absoluteStrokeWidth布尔值false

应用属性

🌐 Applying props

要自定义图标的外观,你可以将自定义属性作为 props 直接传递给组件。该组件接受所有 SVG 属性作为 props,这允许对 SVG 元素进行灵活的样式设置。请参阅 MDN 上的 SVG 展示属性列表。

🌐 To customize the appearance of an icon, you can pass custom properties as props directly to the component. The component accepts all SVG attributes as props, which allows flexible styling of the SVG elements. See the list of SVG Presentation Attributes on MDN.

jsx
// Usage
const App = () => {
  return <Camera fill="red" stroke-linejoin="bevel" />;
};

使用 Lucide Lab 或自定义图标

🌐 With Lucide Lab or custom icons

Lucide Lab 是一组不属于 Lucide 主库的图标。

可以使用 Icon 组件来使用它们。所有像常规 Lucide 图标一样的属性都可以传递来调整图标的外观。

🌐 They can be used by using the Icon component. All props like the regular Lucide icons can be passed to adjust the icon appearance.

使用 Icon 组件

🌐 Using the Icon component

这将根据传递的 iconNode 创建一个图标,并渲染一个 Lucide 图标组件。

🌐 This creates a single icon based on the iconNode passed and renders a Lucide icon component.

jsx
import { Icon } from 'lucide-solid';
import { sausage } from '@lucide/lab';

const App = () => (
  <Icon iconNode={sausage} color="red"/>
);

一个通用图标组件

🌐 One generic icon component

可以创建一个通用的图标组件来加载图标。但不推荐这样做。

🌐 It is possible to create one generic icon component to load icons. It's not recommended.

DANGER

下面的示例导入了所有的 ES 模块,因此在使用时请谨慎。导入所有图标将显著增加应用的构建大小,从而对其性能产生负面影响。在使用像 WebpackRollupVite 这样的打包工具时,这一点尤其需要注意。

图标组件示例

🌐 Icon Component Example

tsx
import { icons, type LucideProps } from 'lucide-solid';
import { splitProps } from 'solid-js';
import { Dynamic } from 'solid-js/web';

interface IconProps extends LucideProps {
  name: keyof typeof icons;
}

const Icon = (props: IconProps) => {
  const [local, others] = splitProps(props, ["name"]);

  return <Dynamic component={icons[local.name]} {...others} />
};

export default Icon;

使用图标组件

🌐 Using the Icon Component

tsx
import Icon from './Icon';

const App = () => {
  return <Icon name="home" />;
};

export default App;

无障碍

🌐 Accessibility

默认情况下,我们使用 aria-hidden="true" 从屏幕阅读器中隐藏图标。

🌐 By default, we hide icons from screen readers using aria-hidden="true".

你可以使用 aria-label 添加可访问性属性。

🌐 You can add accessibility attributes using aria-labels.

jsx
import { Check } from 'lucide-solid';

const App = () => {
  return <Check aria-label="Task completed" />;
};

有关无障碍最佳实践,请参阅我们的无障碍指南

🌐 For best practices on accessibility, please see our accessibility guide.