Appearance
Lucide Preact
用于 Lucide 图标的 Preact 组件,提供类似 React 的开发体验,同时占用更小的空间。每个图标都是一个轻量级的 Preact 组件,呈现为内联 SVG,非常适合需要 React 兼容性且打包体积最小的应用。
🌐 Preact components for Lucide icons that provide React-like development experience with a smaller footprint. Each icon is a lightweight Preact component that renders as an inline SVG, perfect for applications that need React compatibility with minimal bundle size.
你可以完成的事:
- 将图标用作 Preact 组件,并采用类似 React 的语法和模式
- 使用 Preact 更小的运行时构建轻量级应用
- 以最小的 JavaScript 开销创建快速响应的界面
- 保持 React 兼容性,同时减少包大小
- 与现有的 Preact 应用和组件库集成
安装
🌐 Installation
sh
pnpm add lucide-preactsh
yarn add lucide-preactsh
npm install lucide-preactsh
bun add lucide-preact如何使用
🌐 How to use
Lucide 基于 ES 模块构建,因此完全支持 tree-shaking。
🌐 Lucide is built with ES Modules, so it's completely tree-shakable.
每个图标都可以作为 Preact 组件导入,该组件会渲染一个内联 SVG 元素。这样,只有导入到项目中的图标会包含在最终的打包文件中,其余的图标会被进行摇树优化。
🌐 Each icon can be imported as a Preact 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-preact';
// 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" />;
};在 Preact 中,SVG 属性不会被转换,因此如果你想更改例如
stroke-linejoin,你需要使用连字符命名方式传递。基本上就是 SVG 规范要求你如何书写它。参见 Preact 文档 中的这个主题。
使用 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 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-preact';
import { coconut } from '@lucide/lab';
const App = () => (
<Icon iconNode={coconut} />
);一个通用图标组件
🌐 One generic icon component
可以创建一个通用的图标组件来加载图标,但不建议这样做。
🌐 It is possible to create one generic icon component to load icons, but it is not recommended.
DANGER
下面的示例导入了所有的 ES 模块,所以在使用时请谨慎。导入所有图标会显著增加应用的构建大小,从而对性能产生负面影响。在使用像 Webpack、Rollup 或 Vite 这样的打包工具时,这一点尤为重要。
图标组件示例
🌐 Icon Component Example
jsx
import { icons } from 'lucide-preact';
const Icon = ({ name, color, size }) => {
const LucideIcon = icons[name];
return <LucideIcon color={color} size={size} />;
};
export default Icon;使用图标组件
🌐 Using the Icon Component
jsx
import Icon from './Icon';
const App = () => {
return <Icon name="house" />;
};
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-preact';
const App = () => {
return <Check aria-label="Task completed" />;
};有关无障碍最佳实践,请参阅我们的无障碍指南。
🌐 For best practices on accessibility, please see our accessibility guide.