@carbon/web-components

中文文档
2.67.0发布于 2026/11/04Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 10.9万

Web components for the Carbon Design System

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. CDN
  4. Preparing for v3
  5. Documentation
  6. Contributing
  7. License
  8. IBM Telemetry

@carbon/web-components

Web components for the Carbon Design System.

Installation

npm install @carbon/web-components

Usage

The @carbon/web-components package provides components for the Carbon Design System.

To use a component, you can import it directly from the package:

import '@carbon/web-components/es/components/dropdown/dropdown.js';
import '@carbon/web-components/es/components/dropdown/dropdown-item.js';

Once you've imported the component modules, you can use the components in the same manner as native HTML tags:

<cds-dropdown trigger-content="Select an item">
  <cds-dropdown-item value="all">Option 1</cds-dropdown-item>
  <cds-dropdown-item value="cloudFoundry">Option 2</cds-dropdown-item>
  <cds-dropdown-item value="staging">Option 3</cds-dropdown-item>
  <cds-dropdown-item value="dea">Option 4</cds-dropdown-item>
  <cds-dropdown-item value="router">Option 5</cds-dropdown-item>
</cds-dropdown>

CDN

CDN artifacts are also available and can be added directly to the page (starting at version v1.16.0):

<!doctype html>
<html>
  <head>
    <script
      type="module"
      src="https://1.www.s81c.com/common/carbon/web-components/version/v2.65.0/dropdown.min.js"></script>
    <style type="text/css">
      <!-- hide custom element until it has been defined //-->
      cds-dropdown:not(:defined),
      cds-dropdown-item:not(:defined) {
        visibility: hidden;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <cds-dropdown trigger-content="Select an item">
        <cds-dropdown-item value="all">Option 1</cds-dropdown-item>
        <cds-dropdown-item value="cloudFoundry">Option 2</cds-dropdown-item>
        <cds-dropdown-item value="staging">Option 3</cds-dropdown-item>
        <cds-dropdown-item value="dea">Option 4</cds-dropdown-item>
        <cds-dropdown-item value="router">Option 5</cds-dropdown-item>
      </cds-dropdown>
    </div>
  </body>
</html>

Preparing for v3

The next major release (v3) changes how components register. See the v3 migration guide to prepare.

Available now. Both the opt-in registration API — defineCustomElement (es/globals/register.js) and the static is tag field — are available in v2, so you can write v3-ready code today.

Deprecated (will be removed in v3):

  • The carbonElement decorator (es/globals/decorators/carbon-element.js), used to author your own custom elements — migrate to Lit's customElement (import { customElement } from 'lit/decorators.js').
  • The es-custom build (cds-custom-* elements), replaced by custom tag names, a prefixed build, and scoped registries.
  • The custom-elements.json WCA manifest, replaced by the standard Custom Elements Manifest (CEM).

Breaking in v3: In v3, importing a component's class file no longer registers its element, e.g. importing button/button.js instead of button/index.js. Test using the v3 tag — built from the feat/web-components-v3 branch.

Documentation

For component documentation and usage guides, see:

Contributing

See the Contributing Guide.

License

Licensed under the Apache 2.0 License.

IBM Telemetry IBM Telemetry

This package uses IBM Telemetry to collect de-identified and anonymized metrics data. By installing this package as a dependency you are agreeing to telemetry collection. To opt out, see Opting out of IBM Telemetry data collection. For more information on the data being collected, please see the IBM Telemetry documentation.

版本与发布记录

@carbon/web-components 共发布 1,219 个版本,首次发布于 2022/12/07;过去 12 个月发布了 58 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.67.0latest2026/11/044 天前
2.67.0-rc.0next2026/11/035 天前
3.0.0-alpha.5v12-alpha2026/10/2812 天前
3.0.0-alpha.42026/10/2218 天前
2.66.02026/10/2119 天前
2.66.0-rc.02026/10/2020 天前

其他发布通道:v1-latest → 1.47.1,v12-alpha → 3.0.0-alpha.5,next → 2.67.0-rc.0。

关于 @carbon/web-components 的常见问题

@carbon/web-components 是什么?

Web components for the Carbon Design System

如何安装 @carbon/web-components?

在项目目录运行 npm install @carbon/web-components,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 create-prefixed-build,可以用 npx create-prefixed-build 直接运行。

@carbon/web-components 支持 TypeScript 吗?

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

@carbon/web-components 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@carbon/web-components 可以商用吗?

@carbon/web-components 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@carbon/web-components 的下载量有多少?

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

@carbon/web-components 还在维护吗?

最新版本 2.67.0 发布于 2026/11/04(4 天前);至今共发布 1219 个版本,其中过去 12 个月发布了 58 个;首次发布于 2022/12/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。