Lucide Svelte
用于 Lucide 图标的 Svelte 组件,可与 Svelte 4 和 Svelte 5 无缝协作。每个图标都是一个响应式 Svelte 组件,可渲染为内联 SVG,提供卓越的性能并与 Svelte 的响应式系统和现代功能集成。
¥Svelte components for Lucide icons that work seamlessly with both Svelte 4 and Svelte 5. Each icon is a reactive Svelte component that renders as an inline SVG, providing excellent performance and integration with Svelte's reactive system and modern features.
你可以实现的目标:
¥What you can accomplish:
将图标用作 Svelte 组件,并实现完全的响应式和 TypeScript 支持
¥Use icons as Svelte components with full reactivity and TypeScript support
将图标属性绑定到响应式变量和 store
¥Bind icon properties to reactive variables and stores
创建响应应用状态的动态图标系统
¥Create dynamic icon systems that respond to application state
使用全面的 TypeScript 定义构建类型安全的接口
¥Build type-safe interfaces with comprehensive TypeScript definitions
通过直接导入图标和 tree-shaking 优化包大小
¥Optimize bundle sizes with direct icon imports and tree-shaking
安装
¥Installation
pnpm add @lucide/svelteyarn add @lucide/sveltenpm install @lucide/sveltebun add @lucide/svelte
@lucide/svelte仅适用于 Svelte 5,对于 Svelte 4,请使用lucide-svelte软件包。¥
@lucide/svelteis only for Svelte 5, for Svelte 4 use thelucide-sveltepackage.
如何使用
¥How to use
Lucide 基于 ES 模块构建,因此完全支持 tree-shaking。
¥Lucide is built with ES Modules, so it's completely tree-shakable.
每个图标都可以作为 Svelte 组件导入,该组件会渲染一个内联 SVG 元素。这样,只有导入到项目中的图标才会包含在最终的软件包中。其余图标已通过 tree-shaking 移除。
¥Each icon can be imported as a Svelte 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:
<script>
import { Skull } from '@lucide/svelte';
</script>
<Skull />可以传递其他属性来调整图标:
¥Additional props can be passed to adjust the icon:
<script>
import { Camera } from '@lucide/svelte';
</script>
<Camera color="#ff3e98" />为了加快构建和加载速度,你可以直接从 @lucide/svelte/icons 目录导入图标:
¥For faster builds and load times, you can import icons directly from the @lucide/svelte/icons directory:
<script>
import CircleAlert from '@lucide/svelte/icons/circle-alert';
</script>
<CircleAlert color="#ff3e98" />属性
¥Props
| name | type | default |
|---|---|---|
size | number | 24 |
color | string | currentColor |
strokeWidth | number | 2 |
absoluteStrokeWidth | 布尔值 | false |
应用属性
¥Applying props
要自定义图标的外观,你可以将自定义属性作为 props 直接传递给组件。该组件接受所有 SVG 属性作为属性,从而允许灵活地设置 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.
<script>
import { Phone } from '@lucide/svelte';
</script>
<Phone fill="#333" />这将生成一个填充的调用图标。
¥This results a filled phone icon.
类型
¥Types
该软件包包含所有图标的类型定义。无论你使用的是 TypeScript 还是 JSDoc,如果你想使用 svelte:component 指令动态加载图标,这都非常有用。
¥The package includes type definitions for all icons. This is useful if you want to dynamically load icons with the svelte:component directive whether you are using TypeScript or JSDoc.
TypeScript 示例
¥TypeScript Example
<script lang="ts">
import { Home, Library, Cog, type Icon as IconType } from '@lucide/svelte';
type MenuItem = {
name: string;
href: string;
icon: typeof IconType;
};
const menuItems: MenuItem[] = [
{
name: 'Home',
href: '/',
icon: Home
},
{
name: 'Blog',
href: '/blog',
icon: Library
},
{
name: 'Projects',
href: '/projects',
icon: Cog
}
];
</script>
{#each menuItems as item}
{@const Icon = item.icon}
<a href={item.href}>
<Icon />
<span>{item.name}</span>
</a>
{/each}<script lang="ts">
import { Home, Library, Cog, type Icon } from '@lucide/svelte';
import type { ComponentType } from 'svelte';
type MenuItem = {
name: string;
href: string;
icon: ComponentType<Icon>;
};
const menuItems: MenuItem[] = [
{
name: 'Home',
href: '/',
icon: Home
},
{
name: 'Blog',
href: '/blog',
icon: Library
},
{
name: 'Projects',
href: '/projects',
icon: Cog
}
];
</script>
{#each menuItems as item}
{@const Icon = item.icon}
<a href={item.href}>
<Icon />
<span>{item.name}</span>
</a>
{/each}JSDoc 示例
¥JSDoc Example
<script>
import { Home, Library, Cog } from '@lucide/svelte';
/**
* @typedef {Object} MenuItem
* @property {string} name
* @property {string} href
* @property {typeof import('@lucide/svelte').Icon} icon
*/
/** @type {MenuItem[]} */
const menuItems = [
{
name: 'Home',
href: '/',
icon: Home
},
{
name: 'Blog',
href: '/blog',
icon: Library
},
{
name: 'Projects',
href: '/projects',
icon: Cog
}
];
</script>
{#each menuItems as item}
{@const Icon = item.icon}
<a href={item.href}>
<Icon />
<span>{item.name}</span>
</a>
{/each}<script>
import { Home, Library, Cog } from '@lucide/svelte';
/**
* @typedef {Object} MenuItem
* @property {string} name
* @property {string} href
* @property {import('svelte').ComponentType<import('@lucide/svelte').Icon>} icon
*/
/** @type {MenuItem[]} */
const menuItems = [
{
name: 'Home',
href: '/',
icon: Home,
},
{
name: 'Blog',
href: '/blog',
icon: Library,
},
{
name: 'Projects',
href: '/projects',
icon: Cog,
}
];
</script>
{#each menuItems as item}
{@const Icon = item.icon}
<a href={item.href}>
<Icon />
<span>{item.name}</span>
</a>
{/each}有关键入 svelte:component 指令的更多详细信息,请参阅 Svelte 文档。
¥For more details about typing the svelte:component directive, see the Svelte documentation.
使用 Lucide Lab 或自定义图标
¥With Lucide lab or custom icons
Lucide Lab 是一个图标集合,不属于 Lucide 主库。
¥Lucide lab is a collection of icons that are not part of the Lucide main library.
它们可以通过 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.
<script>
import { Icon } from '@lucide/svelte';
import { pear, sausage } from '@lucide/lab';
</script>
<Icon iconNode={pear} />
<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.
危险
以下示例导入了所有 ES 模块,因此使用时请谨慎。导入所有图标将显著增加应用的构建大小,从而对其性能产生负面影响。在使用 Webpack、Rollup 或 Vite 等打包器时,这一点尤为重要。
¥The example below imports all ES Modules, so exercise caution when using it. Importing all icons will significantly increase the build size of the application, negatively affecting its performance. This is especially important when using bundlers like Webpack, Rollup, or Vite.
图标组件示例
¥Icon Component Example
<script>
import * as icons from '@lucide/svelte';
let { name, ...props } = $props();
const Icon = icons[name];
</script>
<Icon {...props} /><script>
import * as icons from '@lucide/svelte';
export let name;
</script>
<svelte:component this={icons[name]} {...$$props} />使用图标组件
¥Using the Icon Component
<script>
import LucideIcon from './LucideIcon';
</script>
<LucideIcon name="Menu" />