@ch-ui/tokens

中文文档
3.0.1发布于 2025/10/30Ch Public LicenseTypeScriptESM + CJS近 30 天下载 6513

A tokens system for a more malleable web.

原始使用文档
文档目录10 节
  1. Getting started
  2. Use with PostCSS
  3. Background
  4. Physical series
  5. Layers
  6. Semantic layers
  7. Semantic expressions
  8. Facets
  9. Render all tokens
  10. Auditing

@ch-ui/tokens

The tokens package renders sets of CSS custom properties, a.k.a. CSS variables, that you can put upstream of a utility system like Tailwind or a web component library (or use on their own of course).

This package generates tokens using a principled approach that aims to make it easier for platform developers, app developers, and end-users alike to maintain and apply adjustments to an app’s design system tokens.

Getting started

pnpm add -D @ch-ui/tokens

Then, use any of the render functions as you like. If you’re using Vite, there’s already @ch-ui/vite-plugin-tokens if you like.

Use with PostCSS

To add to your PostCSS setup:

import chTokens from '@ch-ui/tokens/postcss';
import myTokenSet from './config';
//...
plugins: [
  // ...
  chTokens((params: string) => myTokenSet),
  // ...
]
//...

In your PostCSS:

@layer tokens {
  @tokens myTokens
}

Easy.

If you like, you can resolve different sets of tokens using the params provided after @tokens.

A “default token set” is provided if you suffer from blank canvas syndrome:

import chTokens from '@ch-ui/tokens/postcss';
import { defaultTokenSet } from '@ch-ui/tokens';
import myTokenSet from './config';
//...
plugins: [
  // ...
  chTokens(
    (params: string) => params === 'myProductionApp'
      ? myTokenSet
      : defaultTokenSet
  ),
  // ...
]
//...

Background

Design systems often maintain an intentionally limited set of design tokens, such as:

  • specific colors arranged in “palettes”,
  • font sizes arranged in a “type scale” or “font ramp”.

These physical tokens are the most fundamental layer, naming specific scalar (numeric) values to use. The same tokens can be redefined within conditional at-rules e.g. to support broader gamuts like rec2020.

The base layer of physical tokens can then be built upon with a layer of semantic tokens, which map physical values guarded by conditional at-rules to one coherent meaningful name, for example --fg-description (for “foreground color: descriptions”) can map to one physical color by default or a different one when prefers-color-palette: dark.

Physical series

All design tokens ultimately resolve to physical values, which in @ch-ui/tokens are generated by a series of inputs along a continuum.

@ch-ui/tokens implements three kinds of series:

  • linear,
  • exponential, and
  • helical arc for color palettes

For example, the package comes with default font sizes in an exponential series:

const defaultSizes = {
  initial: 1,
  unit: 'rem',
  base: 1.2,
  naming: {
    '2xs': -3,
    xs: -2,
    s: -1,
    base: 0,
    lg: 1,
    xl: 2,
    '2xl': 3,
    '3xl': 4,
    '4xl': 5,
  },
} satisfies ExponentialSeries;

This will output physical tokens in rem for the input values [-3, -2, -1, 0, 1, 2, 3, 4, 5] using the equation initial * Math.pow(base, input).

Layers

Rendered by:

  • renderPhysicalLayer(physicalLayer: PhysicalLayer, semanticValues?: SemanticValues)
  • renderSemanticLayer(semanticLayer: SemanticLayer)

Physical and semantic configurations are defined in a layer which lets you redefine slates of tokens based on conditions, which are just nested CSS statements (at-rules or selectors). Additionally, layers are where you can set a namespace.

For example, the package comes with default colors that use conditions to redefine the tokens when the browser supports broader gamuts:

export const defaultPhysicalColors = {
  conditions: {
    srgb: [':root'],
    p3: ['@media (color-gamut: p3)', ':root'],
    rec2020: ['@media (color-gamut: rec2020)', ':root'],
  },
  series: {
    neutral: {
      srgb: neutralArc,
      p3: neutralArc,
      rec2020: neutralArc,
    },
    accent: {
      srgb: accentArc,
      p3: accentArc,
      rec2020: accentArc,
    },
  },
  namespace: 'ch-',
} satisfies ColorsPhysicalLayer;

Note that neutralArc and accentArc are helical arc series which are repeated; the repetition is incidental, since you may want to use a different series in different conditions. How this applies to colors in particular is described in more detail in @ch-ui/colors.

Semantic layers

Using semantic layers, you can define names which map to different physical tokens in different conditions. Where physical layers describe their values as a series, semantic layers describe their values as sememes (/ˈsɛmiːms/), which map meaningful names to a physical value for each condition in the layer.

For example, the package comes with a default semantic layer for colors to apply between light and dark modes:

export const defaultSemanticColors = {
  conditions: {
    light: [':root'],
    dark: ['@media (prefers-color-scheme: dark)', ':root'],
  },
  sememes: {
    'bg-base': {
      light: ['neutral', 975],
      dark: ['neutral', 150],
    },
    'fg-base': {
      light: ['neutral', 0],
      dark: ['neutral', 900],
    },
    // etc
  },
  namespace: 'ch-',
} satisfies SemanticLayer;

Semantic expressions

Semantic layers support expressions where, instead of a literal value, you can specify some operation on either a literal value or a reference to another semantic token, for example:

export const defaultSemanticColors = {
  // ...
  sememes: {
    'bg-base': {
      // ...
    },
    'fg-base': {
      light: ['neutral', '88f:bg-base'],
      dark: ['neutral', '-88f:bg-base'],
    },
    // ...
  },
  // ...
} satisfies SemanticLayer;

With this configuration, fg-base’s light and dark values will be computed to be a value that should each result in an APCA Lc value of ±88 when fg-base is used as a foreground against bg-base.

Note when referring to other tokens in expressions that they must not cycle and must ultimately resolve to a literal value.

Valid operations depend on the type of series the semantic layer configures. Currently only f and b operations are supported, and f and b only apply to color series a.k.a. HelicalArcSeries.

Facets

Rendered by renderFacet(facet: Facet).

For better ergonomics, physical and semantic layers can be combined into a facet. When rendered together like this, @ch-ui/tokens will automatically infer that any values mentioned in the semantic layer should be added to the inputs of the physical layer. If you will only use semantic values, this means you don’t need to define any values in the physical layer.

Render all tokens

All tokens can be rendered using renderTokenSet(tokenSet: TokenSet).

A TokenSet is just a Record<string, Facet> containing all the facets you’d like to render, keyed by ids you provide.

Auditing

You can audit tokens (one facet at a time) if you’d like more information about why the system is rendering the physical tokens it’s rendering.

import { auditFacet } from '@ch-ui/tokens';
const colorsAudit = auditFacet(tokenSet.colors, { condition: 'p3' });

版本与发布记录

@ch-ui/tokens 共发布 24 个版本,首次发布于 2024/09/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.1latest2025/10/301 年前
3.0.02025/10/301 年前
2.4.02025/05/281.5 年前
2.3.02025/05/281.5 年前
2.2.02025/05/281.5 年前
2.1.12025/05/281.5 年前

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

@ch-ui/tokens 是什么?

A tokens system for a more malleable web.

如何安装 @ch-ui/tokens?

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

@ch-ui/tokens 支持 TypeScript 吗?

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

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

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

@ch-ui/tokens 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

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

@ch-ui/tokens 使用 Ch Public License 许可。声明的许可为 Ch Public License,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

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

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

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

最新版本 3.0.1 发布于 2025/10/30(1 年前);至今共发布 24 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/09/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。