Lucide Static
此包包含以下 Lucide 图标的实现:
- 单个 SVG 文件
- SVG 精灵
- 图标字体文件
- 导出 SVG 字符串的 JavaScript 库
这是为谁准备的?
lucide-static
适用于 非常特定的用例,其中您希望在不依赖 JavaScript 框架或组件系统的情况下使用 Lucide 图标。它非常适合:
- 使用纯 CSS 或实用优先框架的图标字体项目
- 直接在 HTML 中嵌入原始 SVG 文件或精灵
- 将 SVG 用作 CSS 背景图像
- 将 SVG 字符串导入 Node.js (CommonJS) 环境
DANGER
不推荐用于生产环境
SVG 精灵和图标字体包含 所有图标,这可能会显著增加您的应用的捆绑包大小和加载时间。
对于生产环境,我们推荐使用支持 tree-shaking 的打包器,仅包含您实际使用的图标。请考虑使用:
安装
sh
pnpm install lucide-static
sh
yarn add lucide-static
sh
npm install lucide-static
sh
bun add lucide-static
SVG 文件
您可以以多种方式使用独立的 SVG 文件或 SVG 精灵。
查看我们的 codesandbox 示例。
SVG 文件作为图像
在 HTML 中:
html
<!-- SVG 文件用于单个图标 -->
<img src="~lucide-static/icons/house.svg" />
html
<!-- SVG 文件用于单个图标 -->
<img src="https://unpkg.com/lucide-static@latest/icons/house.svg" />
在 CSS 中:
css
.house-icon {
background-image: url(~lucide-static/icons/house.svg);
}
css
.house-icon {
background-image: url(https://unpkg.com/lucide-static@latest/icons/house.svg);
}
确保您已正确配置 Webpack 加载器,例如 url-loader
。
SVG 文件作为字符串
要将 SVG 导入为字符串(例如,用于模板):
js
import arrowRightIcon from 'lucide-static/icons/arrow-right';
js
import arrowRightIcon from 'lucide-static/icons/arrow-right.svg?raw';
您需要一个 SVG 加载器,例如 svg-inline-loader
。
使用 SVG 精灵
DANGER
您可能还需要一个额外的 SVG 加载器来处理此操作。
基本精灵用法(非生产优化):
html
<img src="lucide-static/sprite.svg#house" />
内联用法:
html
<svg
width="24"
height="24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<use href="#alert-triangle" />
</svg>
<!-- sprite SVG -->
<svg>...</svg>
使用 CSS 辅助类进行内联
如果您愿意,您可以使用 CSS 来保留基础 SVG 属性:
css
.lucide-icon {
width: 24px;
height: 24px;
stroke: currentColor;
fill: none;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
...并相应更新 SVG:
xml
<svg
xmlns="http://www.w3.org/2000/svg"
class="lucide-icon"
>
<use href="#triangle-alert" />
</svg>
<!-- sprite SVG -->
<svg>...</svg>
图标字体
DANGER
Lucide 图标也可以作为 Web 字体使用。要使用它们,首先需要包含相应的样式表:
css
@import 'lucide-static/font/lucide.css';
css
@import ('~lucide-static/font/lucide.css');
html
<link rel="stylesheet" href="https://unpkg.com/lucide-static@latest/font/lucide.css" />
html
<link rel="stylesheet" href="/your/path/to/lucide.css" />
包含后,使用格式 icon-{kebab-case-name}
。您可以从 Lucide Icons 页面 复制图标名称。
html
<div class="icon-house"></div>
如果您未使用包管理器,可以直接从 最新发布 下载字体文件。
Node.js
您也可以在 Node.js 项目中导入 Lucide 图标:
js
import {MessageSquare} from 'lucide-static';
js
const {MessageSquare} = require('lucide-static');
注意:每个图标名称均为 PascalCase。
Node.js 中的 Express 应用示例
js
import express from 'express';
import {MessageSquare} from 'lucide-static';
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>Lucide Icons</h1>
<p>This is a Lucide icon ${MessageSquare}</p>
</body>
</html>
`);
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});