mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-11-16 14:18:29 +00:00
Merge branch 'main' into main
This commit is contained in:
@@ -8,20 +8,10 @@ import Sandpack from '~/.vitepress/theme/components/editors/Sandpack.vue'
|
||||
|
||||
Learn how to use SVG sprites with Lucide icons in your project, including basic usage and inline options.
|
||||
|
||||
:::warning Not recommended for high traffic production use
|
||||
The SVG sprite includes all icons, which can significantly increase your app's bundle size and load time. For production environments, we recommend using a bundler with tree-shaking support to include only the icons you actually use. Consider using one of the framework-specific [packages](../../packages.md).
|
||||
:::
|
||||
|
||||
SVG sprites can be loaded as individual image, or used inline with the `<use>` element.
|
||||
The sprite is used inline: you put it in your page and reference icons with the `<use>` element.
|
||||
You may also need an additional SVG loader to handle node_modules imports in your project. Check out our [codesandbox example](https://codesandbox.io/s/using-the-svg-sprite-lz1kk) for a working example.
|
||||
|
||||
## Basic sprite usage
|
||||
|
||||
SVG sprites can be imported directory in img tags, and select the icon with <br>the `#{icon-name}` syntax:
|
||||
|
||||
```html
|
||||
<img src="lucide-static/sprite.svg#house" />
|
||||
```
|
||||
To use the sprite, add it inline and reference icons with `<use>`, as shown below.
|
||||
|
||||
## Inline usage
|
||||
|
||||
@@ -104,5 +94,5 @@ import sprite from "lucide-static/sprite.svg";
|
||||
|
||||
document.getElementById('sprite').innerHTML = sprite;
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
|
||||
@@ -42,12 +42,13 @@ const aliases = (await Promise.all(
|
||||
return aliases.map(alias => ({
|
||||
...svg,
|
||||
name: typeof alias === 'string' ? alias : alias.name,
|
||||
aliasOf: svgName,
|
||||
}));
|
||||
})
|
||||
)).flat();
|
||||
|
||||
await Promise.all([
|
||||
generateSprite(svgs, PACKAGE_DIR, license),
|
||||
generateSprite(svgs, aliases, PACKAGE_DIR, license),
|
||||
generateIconNodes(svgs, PACKAGE_DIR),
|
||||
copyIcons([...svgs, ...aliases], PACKAGE_DIR, license),
|
||||
]);
|
||||
|
||||
@@ -3,8 +3,14 @@ import { format } from 'prettier';
|
||||
import { appendFile } from '@lucide/helpers';
|
||||
import { type SVGFile } from './readSvgs.mts';
|
||||
|
||||
export type AliasSVGFile = SVGFile & {
|
||||
/** Name of the icon this alias points to. */
|
||||
aliasOf: string;
|
||||
};
|
||||
|
||||
export default async function generateSprite(
|
||||
svgs: SVGFile[],
|
||||
aliases: AliasSVGFile[],
|
||||
packageDir: string,
|
||||
license: string
|
||||
) {
|
||||
@@ -19,6 +25,28 @@ export default async function generateSprite(
|
||||
children: parsedSvg.children,
|
||||
}));
|
||||
|
||||
// Aliases reference the original icon's symbol instead of duplicating its elements.
|
||||
const aliasSymbols = aliases.map<INode>(({ name, aliasOf, parsedSvg }) => ({
|
||||
name: 'symbol',
|
||||
type: 'element',
|
||||
value: '',
|
||||
attributes: {
|
||||
id: name,
|
||||
viewBox: parsedSvg.attributes.viewBox,
|
||||
},
|
||||
children: [
|
||||
{
|
||||
name: 'use',
|
||||
type: 'element',
|
||||
value: '',
|
||||
attributes: {
|
||||
href: `#${aliasOf}`,
|
||||
},
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
}));
|
||||
|
||||
const spriteSvgObject: INode = {
|
||||
name: 'svg',
|
||||
type: 'element',
|
||||
@@ -31,7 +59,7 @@ export default async function generateSprite(
|
||||
{
|
||||
name: 'defs',
|
||||
type: 'element',
|
||||
children: symbols,
|
||||
children: [...symbols, ...aliasSymbols],
|
||||
value: '',
|
||||
} as INode,
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user