Appearance
Lucide Astro
适用于 Lucide 图标的 Astro 组件,与 Astro 的岛屿架构和多框架支持完美兼容。每个图标都是一个 Astro 组件,以内联 SVG 的形式渲染,为静态网站和服务器端渲染场景提供出色的性能。
🌐 Astro components for Lucide icons that work perfectly with Astro's island architecture and multi-framework support. Each icon is an Astro component that renders as an inline SVG, providing excellent performance for static sites and server-side rendering scenarios.
你可以完成的事:
- 将图标用作 Astro 组件,且 JavaScript 运行时开销为零
- 使用优化的 SVG 图标构建快速静态网站
- 与 Astro 的组件岛和部分水合无缝集成
- 创建图标可跨不同 UI 库工作的多框架应用
- 通过直接导入图标和构建时渲染优化性能
安装
🌐 Installation
sh
pnpm add @lucide/astrosh
yarn add @lucide/astrosh
npm install @lucide/astrosh
bun add @lucide/astro如何使用
🌐 How to use
Lucide 基于 ES 模块构建,因此完全支持 tree-shaking。
🌐 Lucide is built with ES Modules, so it's completely tree-shakable.
每个图标都可以作为 Astro 组件导入,它会渲染一个内联 SVG 元素。这样,只有导入到项目中的图标会包含在最终的包中,其余的图标会被 tree-shaked 移除。
🌐 Each icon can be imported as an Astro 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
默认用法:
🌐 Default usage:
astro
---
import { Skull } from '@lucide/astro';
---
<Skull />可以传递其他属性来调整图标:
🌐 Additional props can be passed to adjust the icon:
astro
---
import { Camera } from '@lucide/astro';
---
<Camera color="#ff3e98" />为了更快的构建和加载时间,你可以直接从 @lucide/astro/icons 目录导入图标:
🌐 For faster builds and load times, you can import icons directly from the @lucide/astro/icons directory:
astro
---
import CircleAlert from '@lucide/astro/icons/circle-alert';
---
<CircleAlert color="#ff3e98" />属性
🌐 Props
| 名称 | 类型 | 默认值 |
|---|---|---|
size | 数字 | 24 |
color | 字符串 | currentColor |
stroke-width | 数字 | 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.
astro
---
import { Phone } from '@lucide/astro';
---
<Phone fill="#333" />这将生成一个填充的调用图标。
🌐 This results a filled phone icon.
类型
🌐 Types
该包包含所有图标的类型定义。如果你想动态渲染图标,这将非常有用。
🌐 The package includes type definitions for all icons. This is useful if you want to dynamically render icons.
示例
🌐 Example
astro
---
import { House, Library, Cog, type Icon as IconType } from '@lucide/astro';
type MenuItem = {
name: string;
href: string;
icon: typeof IconType;
};
const menuItems: MenuItem[] = [
{
name: 'Home',
href: '/',
icon: House,
},
{
name: 'Blog',
href: '/blog',
icon: Library,
},
{
name: 'Projects',
href: '/projects',
icon: Cog,
},
];
---
{
menuItems.map((item) => (
<a href={item.href}>
<item.icon />
<span>{item.name}</span>
</a>
))
}使用 Lucide Lab 或自定义图标
🌐 With Lucide Lab or custom icons
Lucide Lab 是一组不属于 Lucide 主库的图标。
可以使用 Icon 组件来使用它们。所有常规 Lucide 图标的属性都可以传递来调整图标的外观。
🌐 They can be used by using the Icon component. All props of 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.
astro
---
import { Icon } from '@lucide/astro';
import { burger, sausage } from '@lucide/lab';
---
<Icon iconNode={burger} />
<Icon iconNode={sausage} color="red"/>一个通用图标组件
🌐 One generic icon component
可以创建一个通用的图标组件来加载图标,但不建议这样做。
🌐 It is possible to create one generic icon component to load icons, but it is not recommended.
DANGER
下面的示例导入了所有的 ES 模块,因此在使用时需谨慎。导入所有图标会显著增加应用的构建体积。如果你在服务器环境中进行 SSG 和 SSR,这可能是可接受的。然而,如果你在无服务器环境中进行 SSR,这可能会对应用的性能产生负面影响,因为更大的包体积会导致冷启动时间增加。
图标组件示例
🌐 Icon Component Example
astro
---
import { icons, type IconProps } from '@lucide/astro';
interface Props extends IconProps {
name: keyof typeof icons;
}
const { name, ...restProps } = Astro.props;
const Icon = icons[name];
---
<Icon {...restProps} />使用图标组件
🌐 Using the Icon Component
astro
---
import LucideIcon from './LucideIcon.astro';
---
<LucideIcon name="Menu" />无障碍
🌐 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/astro';
---
<Check aria-label="Task completed" />有关无障碍最佳实践,请参阅我们的无障碍指南。
🌐 For best practices on accessibility, please see our accessibility guide.