@cdx-ui/styles

中文文档
0.3.12发布于 2026/11/04MITTypeScriptESM + CJS近 30 天下载 2916

Forge UI design tokens, generated CSS, color utilities, and runtime theme overrides.

原始使用文档
文档目录8 节
  1. Install
  2. CSS entry points
  3. Runtime overrides
  4. Web fonts
  5. Font weight and style
  6. Target adapters
  7. Token pipeline
  8. Development

@cdx-ui/styles

Forge UI design tokens, generated CSS, color utilities, and runtime theme overrides.

Install

pnpm add @cdx-ui/styles

CSS entry points

Import the files needed by the target application:

@import '@cdx-ui/styles/theme.css';
@import '@cdx-ui/styles/utilities.css';
  • theme.css registers Forge tokens for Tailwind and Uniwind.
  • utilities.css provides generated typography utilities.
  • web.css provides unlayered .light and .dark overrides for web applications.
  • vanilla.css provides framework-independent custom properties and typography classes.
  • mui.css and cds.css bridge Forge semantic tokens to MUI and CDS variables.

The generated source theme is also exported:

import baseTheme from '@cdx-ui/styles/tokens/base.json';

Runtime overrides

A ThemeOverride stores FI intent. Its metadata contains only the override schema version:

import { applyThemeOverride, type ThemeOverride } from '@cdx-ui/styles';

const override = {
  $extensions: {
    'com.forge.ui.themeOverride': {
      schemaVersion: '1.0.0',
    },
  },
  inputs: {
    brandPrimary: '#0052cc',
    displayFont: 'Bitter',
  },
  selections: {
    actionSurface: {
      mode: 'linked',
      source: { ref: 'color.brand.700' },
    },
  },
  overrides: {
    'color.content.link': {
      light: { value: '#0052cc' },
      dark: { value: '#80bfff' },
    },
  },
} satisfies ThemeOverride;

const result = applyThemeOverride(override);

Expansion starts from the generated base theme, applies input generation, then selections, then direct token overrides. Runtime writes are sparse and concrete. Unknown additive fields are preserved by the parser for forward-compatible editing.

The public display-font catalog is generated from the base theme and Figma configuration:

import { displayFontCatalog } from '@cdx-ui/styles';

displayFontCatalog.defaultFamily;
displayFontCatalog.families;

Each family entry contains family, normalWeights, and italicWeights.

Web fonts

@cdx-ui/styles/fonts states what a browser has to register for the font tokens to resolve. The spec is generated from the token pipeline, so it cannot drift from theme.css.

import { buildWebFontProviderUrl, forgeWebFontSpec } from '@cdx-ui/styles/fonts';

forgeWebFontSpec.platform; // the `--font-sans` / `--font-mono` families
forgeWebFontSpec.display; // every Display family a ThemeOverride can select

buildWebFontProviderUrl([...forgeWebFontSpec.platform, forgeWebFontSpec.display[0]]);

Entries are base families with the weights and italics they ship. Register each family once per weight and let font-weight select the face — that is how a browser resolves weight, and it is what makes font-medium, font-semibold and italic reach the right file. Availability differs per family, and a provider rejects the entire request if asked for a weight a family does not have, which is why the weights travel with the family name.

Register statically where you can, so the preload scanner finds the fonts while the HTML parses: paste the built URL into a <link>, or write your own @font-face rules against self-hosted files (one rule per face, under the base family, with font-weight and font-style descriptors). loadForgeWebFonts injects the link at runtime and is for families not known until then, such as an FI font picker.

Font weight and style

Font tokens name one family per role — --font-sans, --font-body, --font-mono, --font-display. Set weight and style with font-medium / font-semibold / font-bold / italic over the family a composite typography utility already applies; every platform resolves them inside a family registered with all of its faces. Native builds get that registration from the @cdx-ui/native config plugin.

A family that ships no italic faces renders italic text upright rather than slanted. The Display family can change at runtime, so its support travels with it as --font-display-italic-style (italic or normal); style italic Display text with [font-style:var(--font-display-italic-style)] instead of italic. The sans and mono families always ship italics, so italic is correct for body and mono text.

Target adapters

The package includes adapters for Uniwind and MUI:

import { expandThemeOverride, toMuiThemeOptions, toUniwindMaps } from '@cdx-ui/styles';

Covering Uniwind sessions are available when an editor must clear values written by earlier previews without touching unrelated CSS variables.

Token pipeline

tokens/base.json is the token source for the build. Most tokens mirror Figma Variables and are written by the fetch; figma.config.json maps stable collection roles to Figma collection names and sets the outputFile.

Tokens that exist only in code are added to tokens/base.json by hand and marked code-owned:

"ai-start": {
  "$type": "color",
  "$value": "{color.brand.400}",
  "$extensions": { "com.forge.ui.token": { "source": "code" } }
}

The fetch keeps every marked token unchanged and drops unmarked tokens that Figma no longer defines. When Figma defines a variable at a code-owned path, the fetch fails so a person decides which value wins and removes the marker. Mark each mode (modes.light, modes.dark) separately.

# Fetch and atomically write tokens/base.json
FIGMA_VARIABLES_TOKEN=... pnpm --filter @cdx-ui/styles tokens:fetch

# Validate the complete result without writing
FIGMA_VARIABLES_TOKEN=... pnpm --filter @cdx-ui/styles tokens:fetch -- --dry-run

# Inspect one configured role without writing
FIGMA_VARIABLES_TOKEN=... pnpm --filter @cdx-ui/styles tokens:fetch -- --dry-run --role semantics

# Generate CSS and runtime artifacts
pnpm --filter @cdx-ui/styles tokens:build

# Verify generated artifacts are current and deterministic
pnpm --filter @cdx-ui/styles tokens:check

Supported inspection roles are primitives, fiPrimitives, semantics, and platform. Full fetches fail when a configured collection or required mode is missing.

Generated outputs:

  • css/theme.css
  • css/utilities.css
  • css/web.css
  • css/vanilla.css
  • css/mui.css
  • css/cds.css
  • runtime/token-to-css-var.json
  • runtime/override-artifacts.json

Do not edit generated outputs by hand. In tokens/base.json, hand-edit only code-owned tokens; the next fetch overwrites edits to Figma-owned tokens.

Development

From the workspace root:

pnpm --filter @cdx-ui/styles test
pnpm --filter @cdx-ui/styles lint
pnpm --filter @cdx-ui/styles build

版本与发布记录

@cdx-ui/styles 共发布 223 个版本,首次发布于 2026/04/21;过去 12 个月发布了 223 个版本。

版本发布日期距今
0.3.12latest2026/11/044 天前
0.3.112026/10/309 天前
0.3.102026/10/309 天前
0.3.92026/10/3010 天前
0.3.82026/10/2811 天前
0.3.72026/10/2713 天前

其他发布通道:staging → 0.1.0。

关于 @cdx-ui/styles 的常见问题

@cdx-ui/styles 是什么?

Forge UI design tokens, generated CSS, color utilities, and runtime theme overrides.

如何安装 @cdx-ui/styles?

在项目目录运行 npm install @cdx-ui/styles,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.2.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cdx-ui/styles 支持 TypeScript 吗?

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

@cdx-ui/styles 支持 ESM 还是 CommonJS?

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

@cdx-ui/styles 可以商用吗?

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

@cdx-ui/styles 的下载量有多少?

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

@cdx-ui/styles 还在维护吗?

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