@c2n/theme

中文文档
1.0.23发布于 2026/11/08MITTypeScriptES Module零依赖近 30 天下载 3009

Design tokens (--c2-theme--*) and a generated base theme that maps every @c2n component CSS variable onto them

原始使用文档
文档目录5 节
  1. Files
  2. Dark mode
  3. Cascade layer
  4. Layers
  5. Development

@c2n/theme

Design tokens for the @c2n/* web components, plus a generated base theme that maps every component CSS variable onto them.

Components are themed through their own CSS custom properties (--c2-button__container--background-color, …, about 700 in total). @c2n/theme adds a convenience layer on top: ~35 shared tokens named --c2-theme--<name>. The base theme sets each component variable to var(--c2-theme--<token>, <component default>) at :root, so an application only has to define the tokens. Any component variable you set yourself still wins.

npm install @c2n/theme
import '@c2n/theme/theme.css' // tokens + base theme
/* Your app: override a few tokens, every component follows. */
:root {
  --c2-theme--color-primary: #7c3aed;
  --c2-theme--radius-md: 10px;
  --c2-theme--font-family: 'Inter', system-ui, sans-serif;
}

Files

ExportContent
@c2n/theme/theme.csstokens.css + base.css
@c2n/theme/tokens.cssToken defaults: light on :root, dark under [data-theme='dark'] / .c2-dark and as the prefers-color-scheme: dark fallback
@c2n/theme/base.css--c2-<component>…: var(--c2-theme--…, <default>) for every mapped component variable. Use it alone if your app already defines the tokens
@c2n/theme/tokens.jsThe token table as TypeScript (tokens, TokenDef)
@c2n/theme/tokens.jsonTokens + the component variable → token mapping
@c2n/theme/report.jsonCoverage per package and the list of variables left unmapped

Dark mode

Set data-theme="dark" (or the c2-dark class) on <html> or on any subtree; without it the OS preference applies. data-theme="light" / c2-light opts a subtree out.

Cascade layer

tokens.css and base.css put every rule in the c2n-theme cascade layer. An unlayered rule beats a layered one whatever its specificity or source order, so a plain :root { --c2-theme--color-primary: … } overrides the token in light mode, under data-theme="dark" and under the OS dark fallback alike, and so does a component variable you set yourself. If your app puts its own styles in layers, declare c2n-theme first, in a @layer statement before any import, so it stays the lowest: @layer c2n-theme, base, components;. A layered framework imported before the theme otherwise leaves c2n-theme on top of its layers.

Layers

  1. Tokens — --c2-theme--*, set by the app.
  2. Base theme — generated mapping, ships here.
  3. Component variables — --c2-<component>__<part>[__<state>]--<property>, the fine-grained escape hatch.
  4. App variant components — repeated looks wrapped in your own components (see the docs site guide).

Development

npm run build -w packages/tools/theme compiles src/tokens.ts and runs scripts/theme-generator, which reads every @c2n/*/custom-elements.json, classifies each documented CSS variable (scripts/theme-generator/classify.ts, exceptions in overrides.ts) and writes dist/. The build depends on every component build so the manifests are fresh; the console prints a coverage table and dist/report.json lists what stayed unmapped and why.

A component may document a shorthand variable behind its per-side ones (--c2-details--border behind --c2-details--border-top and the other three, compiled as var(--c2-details--border-top, var(--c2-details--border, …))). The generator reads those fallbacks from the package's built dist/ and, when a per-side variable maps to exactly the value its shorthand maps to, writes only the shorthand to base.css (scripts/theme-generator/shorthands.ts): writing the sides would make the shorthand unreachable under the theme. A per-side variable you set still wins; dist/report.json lists the omitted sides under coveredByShorthand. npm test -w packages/tools/theme runs the generator's unit tests.

npm run dev -w packages/tools/theme opens a Vite harness (index.html) with themed components, a dark-mode toggle and token overrides.

版本与发布记录

@c2n/theme 共发布 43 个版本,首次发布于 2026/09/11;过去 12 个月发布了 43 个版本。

版本发布日期距今
1.0.23latest2026/11/081 天前
1.0.222026/11/072 天前
1.0.212026/11/054 天前
1.0.202026/11/035 天前
1.0.192026/11/035 天前
1.0.182026/10/2811 天前

关于 @c2n/theme 的常见问题

@c2n/theme 是什么?

Design tokens (--c2-theme--*) and a generated base theme that maps every @c2n component CSS variable onto them

如何安装 @c2n/theme?

在项目目录运行 npm install @c2n/theme,或使用 pnpm add / yarn add / bun add。

@c2n/theme 支持 TypeScript 吗?

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

@c2n/theme 支持 ESM 还是 CommonJS?

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

@c2n/theme 可以商用吗?

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

@c2n/theme 的下载量有多少?

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

@c2n/theme 还在维护吗?

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