@chai-ui/tokens

中文文档
1.4.1发布于 2026/10/31MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 4256

Chai UI design tokens — single source of truth, built to CSS variables, TypeScript, and a Figma Tokens/Variables-plugin JSON.

原始使用文档
文档目录3 节
  1. Editing tokens
  2. Using the tokens
  3. Dark theme

Chai UI

npm version MIT license CI Coverage Docs

@chai-ui/tokens

The single source of truth for color, space, radius, type, and motion values across Chai UI. One JSON file in, three consumers out:

src/tokens.json  →  dist/css/tokens.css        (CSS custom properties)
                 →  dist/js/index.js/.d.ts      (typed JS/TS exports)
                 →  dist/figma/tokens.figma.json (Figma token set)

Editing tokens

Edit src/tokens.json only. It's a DTCG-flavored format: every leaf is { "$value": ... }, and a value can reference another token with {color.root.green-50} — see color.semantic.* for examples of referencing color.root.*. Always add new components against color.semantic.* (or add a new semantic alias) rather than color.root.* directly, so a future re-theme only touches the root palette. A new semantic role also needs its dark theme value in color.dark.* (see below); the build fails without one.

Run pnpm build (from here, or pnpm --filter @chai-ui/tokens build from the repo root) to regenerate dist/.

Using the tokens

CSS

@import "@chai-ui/tokens/css";

.my-thing {
  background: var(--chai-color-semantic-control-surface);
  padding: var(--chai-space-4);
}

JS / TS

import { tokens, flatTokens } from "@chai-ui/tokens";

tokens.color.semantic.accent; // "#009747"
flatTokens["color.semantic.accent"]; // same value, flat lookup

Figma

dist/figma/tokens.figma.json is shaped for the Tokens Studio for Figma plugin (Import → JSON). It's also a reasonable starting point for a manual Figma Variables import if you'd rather not add the plugin — the values are already resolved, so no reference-following is required at the Figma end.

This is a design-tokens pipeline only: it does not keep Figma components in sync with the code components.

Dark theme

The dark theme is opt-in: set data-chai-theme="dark" on <html>, or on any element, to switch every Chai component inside it. Without the attribute nothing changes.

<html data-chai-theme="dark">

Chai doesn't follow the system setting on its own, because your app decides its theme. To follow it:

const dark = matchMedia("(prefers-color-scheme: dark)");
const sync = () =>
  dark.matches
    ? document.documentElement.setAttribute("data-chai-theme", "dark")
    : document.documentElement.removeAttribute("data-chai-theme");
sync();
dark.addEventListener("change", sync);

In src/tokens.json, color.dark holds what each semantic role becomes in the dark theme, using the same root palette. The roles that sit on images and video (the overlays, media-backdrop and the edit-region colors) look the same in both themes. Every text pair in the dark theme meets WCAG 2.2 AA contrast. The brand green moves to green-60, because green-50 is too dark for text on dark surfaces.

The build emits the dark values under [data-chai-theme="dark"] in @chai-ui/tokens/css, along with the Material color roles again, so the switch, submit button and text field follow the theme too. To adjust one role in the dark theme only, override it under the same selector:

[data-chai-theme="dark"] {
  --chai-color-semantic-control-surface: #202124;
}

版本与发布记录

@chai-ui/tokens 共发布 32 个版本,首次发布于 2026/10/02;过去 12 个月发布了 32 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.4.1latest2026/10/319 天前
1.4.02026/10/319 天前
1.3.02026/10/319 天前
1.2.02026/10/3010 天前
1.1.22026/10/2812 天前
1.1.12026/10/2713 天前

关于 @chai-ui/tokens 的常见问题

@chai-ui/tokens 是什么?

Chai UI design tokens — single source of truth, built to CSS variables, TypeScript, and a Figma Tokens/Variables-plugin JSON.

如何安装 @chai-ui/tokens?

在项目目录运行 npm install @chai-ui/tokens,或使用 pnpm add / yarn add / bun add。

@chai-ui/tokens 支持 TypeScript 吗?

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

@chai-ui/tokens 支持 ESM 还是 CommonJS?

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

@chai-ui/tokens 可以商用吗?

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

@chai-ui/tokens 的下载量有多少?

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

@chai-ui/tokens 还在维护吗?

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