Lucide Astro
Implementation of the lucide icon library for Astro applications.
Installation
pnpm add lucide-astro
yarn add lucide-astro
npm install lucide-astro
bun add lucide-astro
How to use
Lucide is built with ES Modules, so it's completely tree-shakable.
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:
---
import { Skull } from 'lucide-astro';
---
<Skull />
Additional props can be passed to adjust the icon:
---
import { Camera } from 'lucide-astro';
---
<Camera color="#ff3e98" />
For faster builds and load times, you can import icons directly from the lucide-astro/icons
directory:
---
import CircleAlert from 'lucide-astro/icons/circle-alert';
---
<CircleAlert color="#ff3e98" />
Props
name | type | default |
---|---|---|
size | number | 24 |
color | string | currentColor |
stroke-width | number | 2 |
absoluteStrokeWidth | boolean | false |
Applying props
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.
---
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
---
import { Home, 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: Home,
},
{
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>
))
}
With Lucide lab or custom 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 of 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.
---
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
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. This may be passable if you're doing SSG and SSR in server environments. However if you're doing SSR in serverless environments, it could negatively affect your app's performance, as a bigger bundle size will translate to an increase in cold starts.
Icon Component Example
---
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
---
import LucideIcon from './LucideIcon.astro';
---
<LucideIcon name="Menu" />