2022-02-17 17:46:55 +01:00
# Lucide Svelte
2025-10-24 05:18:51 -07:00
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:**
- Use icons as Svelte components with full reactivity and TypeScript support
- Bind icon properties to reactive variables and stores
- Create dynamic icon systems that respond to application state
- Build type-safe interfaces with comprehensive TypeScript definitions
- Optimize bundle sizes with direct icon imports and tree-shaking
2022-02-17 17:46:55 +01:00
## Installation
2023-06-12 22:10:15 +02:00
::: code-group
```sh [pnpm]
2025-03-07 13:44:09 +01:00
pnpm add @lucide/svelte
2022-10-27 08:19:45 +02:00
```
2022-02-17 17:46:55 +01:00
2023-06-12 22:10:15 +02:00
```sh [yarn]
2025-03-07 13:44:09 +01:00
yarn add @lucide/svelte
2023-06-12 22:10:15 +02:00
```
2022-02-17 17:46:55 +01:00
2023-06-12 22:10:15 +02:00
```sh [npm]
2025-03-07 13:44:09 +01:00
npm install @lucide/svelte
2022-02-17 17:46:55 +01:00
```
2024-11-29 09:11:30 +01:00
```sh [bun]
2025-03-07 13:44:09 +01:00
bun add @lucide/svelte
2024-11-29 09:11:30 +01:00
```
2023-06-12 22:10:15 +02:00
:::
2025-03-07 13:44:09 +01:00
> `@lucide/svelte` is only for Svelte 5, for Svelte 4 use the `lucide-svelte` package.
2023-06-12 22:10:15 +02:00
2022-02-17 17:46:55 +01:00
## How to use
2023-09-02 13:48:45 -06:00
Lucide is built with ES Modules, so it's completely tree-shakable.
2023-06-12 22:10:15 +02:00
2023-09-02 13:48:45 -06:00
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.
2022-02-17 17:46:55 +01:00
### Example
Default usage:
2023-06-12 22:10:15 +02:00
```svelte
2022-02-17 17:46:55 +01:00
<script>
2025-03-07 13:44:09 +01:00
import { Skull } from '@lucide/svelte ';
2022-02-17 17:46:55 +01:00
</script>
2022-10-27 08:19:45 +02:00
<Skull />
2022-02-17 17:46:55 +01:00
```
2023-06-12 22:10:15 +02:00
Additional props can be passed to adjust the icon:
2022-02-17 17:46:55 +01:00
2023-06-12 22:10:15 +02:00
```svelte
2022-02-17 17:46:55 +01:00
<script>
2025-03-07 13:44:09 +01:00
import { Camera } from '@lucide/svelte ';
2022-02-17 17:46:55 +01:00
</script>
2022-08-08 13:34:44 -05:00
<Camera color="#ff3e98 " />
2022-02-17 17:46:55 +01:00
```
2025-03-07 13:44:09 +01:00
For faster builds and load times, you can import icons directly from the `@lucide/svelte/icons` directory:
2024-01-03 12:39:42 +02:00
```svelte
<script>
2025-03-07 13:44:09 +01:00
import CircleAlert from '@lucide/svelte/icons/circle -alert';
2024-01-03 12:39:42 +02:00
</script>
2024-11-08 08:45:19 +01:00
<CircleAlert color="#ff3e98 " />
2024-01-03 12:39:42 +02:00
```
2023-06-12 22:10:15 +02:00
## Props
2022-02-17 17:46:55 +01:00
2023-04-20 16:08:34 +02:00
| name | type | default |
| --------------------- | --------- | ------------ |
2024-01-03 12:39:42 +02:00
| `size` | _ number _ | 24 |
| `color` | _ string _ | currentColor |
| `strokeWidth` | _ number _ | 2 |
| `absoluteStrokeWidth` | _ boolean _ | false |
2023-04-20 16:08:34 +02:00
2023-06-12 22:10:15 +02:00
### Applying props
2022-02-17 17:46:55 +01:00
2023-09-02 13:48:45 -06:00
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 ](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/Presentation ).
2022-02-17 17:46:55 +01:00
2023-06-12 22:10:15 +02:00
```svelte
2022-02-17 17:46:55 +01:00
<script>
2025-03-07 13:44:09 +01:00
import { Phone } from '@lucide/svelte ';
2022-02-17 17:46:55 +01:00
</script>
2022-10-27 08:19:45 +02:00
<Phone fill="#333 " />
2022-02-17 17:46:55 +01:00
```
This results a filled phone icon.
2024-01-03 12:39:42 +02:00
## Types
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 Example
2025-03-07 13:44:09 +01:00
::: code-group
2024-11-22 10:07:48 +01:00
2025-03-07 13:44:09 +01:00
```svelte [Svelte 5]
2024-01-03 12:39:42 +02:00
<script lang="ts">
2025-03-07 13:44:09 +01:00
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
}
];
2024-01-03 12:39:42 +02:00
</script>
{#each menuItems as item}
2025-03-07 13:44:09 +01:00
{@const Icon = item.icon}
<a href={item.href}>
<Icon />
<span>{item.name}</span>
</a>
2024-11-22 10:07:48 +01:00
{/each}
```
2025-03-07 13:44:09 +01:00
```svelte [Svelte 4]
<script lang="ts">
import { Home, Library, Cog, type Icon } from '@lucide/svelte ';
import type { ComponentType } from 'svelte';
2024-11-22 10:07:48 +01:00
2025-03-07 13:44:09 +01:00
type MenuItem = {
name: string;
href: string;
icon: ComponentType<Icon>;
};
2024-11-22 10:07:48 +01:00
2025-03-07 13:44:09 +01:00
const menuItems: MenuItem[] = [
{
name: 'Home',
href: '/',
icon: Home
},
{
name: 'Blog',
href: '/blog',
icon: Library
},
{
name: 'Projects',
href: '/projects',
icon: Cog
}
];
2024-11-22 10:07:48 +01:00
</script>
{#each menuItems as item}
2025-05-02 03:39:05 -04:00
{@const Icon = item.icon}
2025-03-07 13:44:09 +01:00
<a href={item.href}>
2025-05-02 03:39:05 -04:00
<Icon />
2025-03-07 13:44:09 +01:00
<span>{item.name}</span>
</a>
2024-01-03 12:39:42 +02:00
{/each}
2025-05-02 03:39:05 -04:00
2024-01-03 12:39:42 +02:00
```
2025-03-07 13:44:09 +01:00
:::
2024-01-03 12:39:42 +02:00
### JSDoc Example
2025-03-07 13:44:09 +01:00
::: code-group
2024-11-22 10:07:48 +01:00
2025-03-07 13:44:09 +01:00
```svelte [Svelte 5]
2024-01-03 12:39:42 +02:00
<script>
2025-03-07 13:44:09 +01:00
import { Home, Library, Cog } from '@lucide/svelte ';
2024-01-03 12:39:42 +02:00
/**
* @typedef {Object} MenuItem
* @property {string} name
* @property {string} href
2025-03-07 13:44:09 +01:00
* @property {typeof import('@lucide/svelte ').Icon} icon
2024-01-03 12:39:42 +02:00
*/
/** @type {MenuItem[]} */
const menuItems = [
{
name: 'Home',
href: '/',
2025-03-07 13:44:09 +01:00
icon: Home
2024-01-03 12:39:42 +02:00
},
{
name: 'Blog',
href: '/blog',
2025-03-07 13:44:09 +01:00
icon: Library
2024-01-03 12:39:42 +02:00
},
{
name: 'Projects',
href: '/projects',
2025-03-07 13:44:09 +01:00
icon: Cog
2024-01-03 12:39:42 +02:00
}
];
</script>
2024-11-22 10:07:48 +01:00
{#each menuItems as item}
2025-03-07 13:44:09 +01:00
{@const Icon = item.icon}
2024-11-22 10:07:48 +01:00
<a href={item.href}>
2025-03-07 13:44:09 +01:00
<Icon />
2024-11-22 10:07:48 +01:00
<span>{item.name}</span>
</a>
{/each}
```
2025-03-07 13:44:09 +01:00
```svelte [Svelte 4]
<script>
import { Home, Library, Cog } from '@lucide/svelte ';
2024-11-22 10:07:48 +01:00
2025-03-07 13:44:09 +01:00
/**
* @typedef {Object} MenuItem
* @property {string} name
* @property {string} href
* @property {import('svelte').ComponentType<import('@lucide/svelte ').Icon>} icon
*/
2024-11-22 10:07:48 +01:00
2025-03-07 13:44:09 +01:00
/** @type {MenuItem[]} */
const menuItems = [
{
name: 'Home',
href: '/',
icon: Home,
},
{
name: 'Blog',
href: '/blog',
icon: Library,
},
{
name: 'Projects',
href: '/projects',
icon: Cog,
}
];
2024-11-22 10:07:48 +01:00
</script>
{#each menuItems as item}
2025-05-02 03:39:05 -04:00
{@const Icon = item.icon}
2025-03-07 13:44:09 +01:00
<a href={item.href}>
2025-05-02 03:39:05 -04:00
<Icon />
2025-03-07 13:44:09 +01:00
<span>{item.name}</span>
</a>
2024-11-22 10:07:48 +01:00
{/each}
2024-01-03 12:39:42 +02:00
```
2025-03-07 13:44:09 +01:00
:::
2024-01-03 12:39:42 +02:00
For more details about typing the `svelte:component` directive, see the [Svelte documentation ](https://svelte.dev/docs/typescript#types-componenttype ).
2024-04-26 17:59:04 +02:00
## With Lucide lab or custom icons
[Lucide lab ](https://github.com/lucide-icons/lucide-lab ) is a collection of icons that are not part of the Lucide main library.
They can be used by using the `Icon` component.
All props like the regular Lucide icons can be passed to adjust the icon appearance.
### Using the `Icon` component
This creates a single icon based on the iconNode passed and renders a Lucide icon component.
```svelte
<script>
2025-03-07 13:44:09 +01:00
import { Icon } from '@lucide/svelte ';
2025-05-03 09:28:29 +02:00
import { pear, sausage } from '@lucide/lab ';
2024-04-26 17:59:04 +02:00
</script>
2025-05-03 09:28:29 +02:00
<Icon iconNode={pear} />
2024-04-26 17:59:04 +02:00
<Icon iconNode={sausage} color="red"/>
```
2023-06-12 22:10:15 +02:00
## One generic icon component
2022-02-17 17:46:55 +01:00
2023-09-02 13:48:45 -06:00
It is possible to create one generic icon component to load icons, but it is not recommended.
2022-02-17 17:46:55 +01:00
2023-06-12 22:10:15 +02:00
::: danger
2023-09-02 13:48:45 -06:00
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` .
2023-06-12 22:10:15 +02:00
:::
2022-02-17 17:46:55 +01:00
2023-06-12 22:10:15 +02:00
### Icon Component Example
2022-02-17 17:46:55 +01:00
2025-03-07 13:44:09 +01:00
::: code-group
2024-11-22 10:07:48 +01:00
2025-03-07 13:44:09 +01:00
```svelte [Svelte 5]
2022-02-17 17:46:55 +01:00
<script>
2025-03-07 13:44:09 +01:00
import * as icons from '@lucide/svelte ';
2025-07-28 15:58:31 +02:00
let { name, ...props } = $props();
2025-03-07 13:44:09 +01:00
const Icon = icons[name];
2022-02-17 17:46:55 +01:00
</script>
2025-03-07 13:44:09 +01:00
<Icon {...props} />
2024-11-22 10:07:48 +01:00
```
2025-03-07 13:44:09 +01:00
```svelte [Svelte 4]
2024-11-22 10:07:48 +01:00
<script>
2025-03-07 13:44:09 +01:00
import * as icons from '@lucide/svelte ';
export let name;
2024-11-22 10:07:48 +01:00
</script>
2025-03-07 13:44:09 +01:00
<svelte:component this={icons[name]} {...$$props} />
2022-02-17 17:46:55 +01:00
```
2025-03-07 13:44:09 +01:00
:::
2023-06-12 22:10:15 +02:00
#### Using the Icon Component
2022-02-17 17:46:55 +01:00
2023-06-12 22:10:15 +02:00
```svelte
2022-02-17 17:46:55 +01:00
<script>
2022-10-27 08:19:45 +02:00
import LucideIcon from './LucideIcon';
2022-02-17 17:46:55 +01:00
</script>
<LucideIcon name="Menu" />
```