# @boxicons/js 中文文档

规范地址：https://npmcn.com/package/%40boxicons/js/

版本：1\.0\.1
npm 元数据更新时间：2026-02-16T00:47:05.471Z
中文译文更新时间：2026-11-08T12:53:46.523Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40boxicons/js
原文对照：https://npmcn.com/package/%40boxicons/js/?lang=original
项目主页：https://boxicons.com/
源码仓库：https://github.com/box-icons/boxicons

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：8.7 MB，13119 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2026-02-12T00:52:27.702Z
- 最新版本发布：2026-02-16T00:47:05.356Z
- 近 30 天下载量：6166（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 2 个）

## 中文简介

有树摇动支持的 Boxicons 香草 JavaScript 库

## 安装

```sh
npm install @boxicons/js
```

也可以使用：`pnpm add @boxicons/js`、`yarn add @boxicons/js`、`bun add @boxicons/js`

## README

# @ boxicons/ js (英语)

为 Boxicons 提供 JavaScript 库,并配有完整的树摇动支持. 灵感来自 [卢西德](https://lucide.dev/guide/packages/lucide)。 。 。 .

## 安装

```bash
npm install @boxicons/js
# or
yarn add @boxicons/js
# or
pnpm add @boxicons/js
```

## 使用量

### 与无害环境管理(建议)

为减少捆绑大小,@boxicons/js被建为可完全树摇. 这个 `getIcons` 函数将搜索带有属性的 HTMLE元素 `data-bx` 并用给定图标名称的 SVG 替换它们.

```html
<!-- Your HTML file -->
<i data-bx="menu"></i>
<i data-bx="arrow-right"></i>
<i data-bx="globe"></i>
```

```javascript
import { getIcons, Menu, ArrowRight, Globe } from '@boxicons/js';

// Recommended way, to include only the icons you need.
getIcons({
  icons: {
    Menu,
    ArrowRight,
    Globe
  }
});
```

### 使用 unpkg (CDN)

```html
<!DOCTYPE html>
<body>
  <i data-bx="menu" class="my-class"></i>
  <i data-bx="home"></i>
  <i data-bx="alarm"></i>

  <script src="https://unpkg.com/@boxicons/js@latest"></script>
  <script>
    // When using CDN, all icons are available
    boxicons.getIcons({ icons: boxicons.icons });
  </script>
</body>
```

### 自定义元素绑定

```javascript
import { createElement, Menu } from '@boxicons/js';

const menuIcon = createElement(Menu); // Returns HTMLElement (svg)

// Append HTMLElement in the DOM
const myApp = document.getElementById('app');
myApp.appendChild(menuIcon);
```

### 自定义元素绑定自定义属性

```javascript
import { createElement, Menu } from '@boxicons/js';

const menuIcon = createElement(Menu, {
  className: 'my-custom-class icon',
  fill: '#333',
  size: 'lg'
}); // Returns HTMLElement (svg)

// Append HTMLElement in the DOM
const myApp = document.getElementById('app');
myApp.appendChild(menuIcon);
```

## 高级使用

### 附加选项

在那个 `getIcons` 函数可以通过一些额外的参数:

-   `nameAttr` - 调整属性名称以查找(默认: `data-bx`(中文(简体) )
-   `attrs` - 向所有图标传递额外的自定义属性
-   `root` - 提供自定义的 DOM 元素或阴影根在内部搜索
-   `inTemplates` - 内部还替换图标 `<template>` 标记

```javascript
import { getIcons, Menu, Home } from '@boxicons/js';

getIcons({
  icons: { Menu, Home },
  attrs: {
    className: 'my-custom-class icon',
    fill: '#333'
  },
  nameAttr: 'data-bx',
  root: document.getElementById('app'),
  inTemplates: true
});
```

### 阴影 DOM 支持

您可以通过将阴影根传递到阴影 DOM 内部的图标 `root` 选项 :

```javascript
import { getIcons, Menu, Home } from '@boxicons/js';

// Create a custom element with shadow DOM
class MyComponent extends HTMLElement {
  constructor() {
    super();
    const shadow = this.attachShadow({ mode: 'open' });
    shadow.innerHTML = `
      <i data-bx="menu"></i>
      <i data-bx="home"></i>
    `;
    
    // Replace icons inside the shadow root
    getIcons({
      icons: { Menu, Home },
      root: shadow
    });
  }
}

customElements.define('my-component', MyComponent);
```

### 模板标签支持

替换内部图标 `<template>` 标签, 设定 `inTemplates: true`数字 :

```javascript
import { getIcons, Menu, Home } from '@boxicons/js';

getIcons({
  icons: { Menu, Home },
  inTemplates: true
});
```

```html
<template id="my-template">
  <i data-bx="menu"></i>
  <i data-bx="home"></i>
</template>
```

### 图标包

每个图标可以有多个变体:

-   **基础** - 大纲/正则样式图标(大多数图标默认)
-   **已填满** - 固态/填充式图标
-   **品牌** - 品牌/标识图标(品牌图标默认)

```html
<i data-bx="alarm"></i>
<i data-bx="alarm" data-bx-pack="filled"></i>
```

### 大小

#### 大小预设

```html
<i data-bx="alarm" data-bx-size="xs"></i>   <!-- 16px -->
<i data-bx="alarm" data-bx-size="sm"></i>   <!-- 20px -->
<i data-bx="alarm" data-bx-size="base"></i> <!-- 24px (default) -->
<i data-bx="alarm" data-bx-size="md"></i>   <!-- 36px -->
<i data-bx="alarm" data-bx-size="lg"></i>   <!-- 48px -->
<i data-bx="alarm" data-bx-size="xl"></i>   <!-- 64px -->
<i data-bx="alarm" data-bx-size="2xl"></i>  <!-- 96px -->
<i data-bx="alarm" data-bx-size="3xl"></i>  <!-- 128px -->
<i data-bx="alarm" data-bx-size="4xl"></i>  <!-- 256px -->
<i data-bx="alarm" data-bx-size="5xl"></i>  <!-- 512px -->
```

#### 自定义大小

```html
<i data-bx="alarm" data-bx-width="32" data-bx-height="32"></i>
```

### 转换

```html
<!-- Flip -->
<i data-bx="alarm" data-bx-flip="horizontal"></i>
<i data-bx="alarm" data-bx-flip="vertical"></i>

<!-- Rotate -->
<i data-bx="alarm" data-bx-rotate="45"></i>
<i data-bx="alarm" data-bx-rotate="90"></i>
```

### 样式

```html
<!-- Custom fill color -->
<i data-bx="alarm" data-bx-fill="#ff0000"></i>

<!-- Opacity -->
<i data-bx="alarm" data-bx-opacity="0.5"></i>

<!-- Remove padding -->
<i data-bx="alarm" data-bx-remove-padding></i>
```

### 程序用途

```javascript
import { createElement, createSvgString, Menu } from '@boxicons/js';

// Get SVG as a string
const svgString = createSvgString(Menu, { size: 'lg', fill: '#ff0000' });

// Get SVG as an element
const svgElement = createElement(Menu, { size: 'lg', fill: '#ff0000' });
document.body.appendChild(svgElement);
```

## 无障碍

默认情况下, 图标会被隐藏在屏幕阅读器中 `aria-hidden="true"`。 。 。 .

您可以使用 aria- 标签添加无障碍属性 :

```html
<i data-bx="home" aria-label="Home icon"></i>
```

## API 参考

### 核心职能

| 函数 | 说明 |
| --- | --- |
| `getIcons(options)` | 扫描 DOM 为 `data-bx` 元素并替换为 SVGs |
| `createElement(icon, options)` | 从图标创建 SVG DOM 元素 |
| `createSvgString(icon, options)` | 从图标创建 SVG 字符串 |

### GetIcons 选项

| 选项 | 类型 | 默认 | 说明 |
| --- | --- | --- | --- |
| `icons` | `IconsRecord` | 所需经费 | 包含要使用的图标的对象 |
| `nameAttr` | `string` | `'data-bx'` | 要查找的属性名称 |
| `attrs` | `IconOptions` | `{}` | 所有图标的默认属性 |
| `root` | `Element | Document | ShadowRoot` | `document` | 要在内部搜索的根元素( 支持阴影 DOM) |
| `inTemplates` | `boolean` | `false` | 同时在内部替换图标 `<template>` 标记 |

### 数据属性

| 属性 | 说明 |
| --- | --- |
| `data-bx` | 图标名称( 需要) |
| `data-bx-pack` | 图标包: “ 基本”、“ 填充”、“ 品牌” |
| `data-bx-size` | 大小预设:"xs","sm","base","md","lg","l","2xl","3xl","4xl","5xl" |
| `data-bx-width` | 自定义宽度 |
| `data-bx-height` | 自定义高度 |
| `data-bx-fill` | 填充颜色 |
| `data-bx-opacity` | 不透明度(0-1) |
| `data-bx-flip` | 翻转方向 : “ 横向 ” 、 “ 垂直 ” |
| `data-bx-rotate` | 学位轮换 |
| `data-bx-remove-padding` | 删除图标补丁( 存在=真实) |

## 类型脚本

导出类型的全类型脚本支持 :

```typescript
import type { 
  IconDefinition, 
  IconOptions, 
  IconPack, 
  IconSize, 
  FlipDirection,
  GetIconsOptions,
  IconsRecord
} from '@boxicons/js';
```

## 树摇

这个图书馆是完全可以树动的。 只有您导入的图标才会包含在您的最后捆绑中 :

```javascript
// ✅ Only Menu and Home are bundled
import { getIcons, Menu, Home } from '@boxicons/js';

getIcons({ icons: { Menu, Home } });

// ❌ This bundles ALL icons - avoid in production
import { getIcons, icons } from '@boxicons/js';
getIcons({ icons });
```

## 许可证

麻省理工学院

## 常见问题

### @boxicons/js 是什么？

有树摇动支持的 Boxicons 香草 JavaScript 库

### 如何安装 @boxicons/js？

在项目目录运行 npm install @boxicons/js，或使用 pnpm add / yarn add / bun add。

### @boxicons/js 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @boxicons/js 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @boxicons/js 可以商用吗？

@boxicons/js 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @boxicons/js 的下载量有多少？

根据 npm 官方下载统计，@boxicons/js 在 2026/10/09 – 2026/11/07 这 30 天内被下载 6,166 次，日均约 206 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @boxicons/js 还在维护吗？

最新版本 1\.0\.1 发布于 2026/02/16（8 个月前）；至今共发布 2 个版本，其中过去 12 个月发布了 2 个；首次发布于 2026/02/12。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

