framework
framework/react/theming/css-token-system.md
medium
react-css-token-system
React CSS 变量 Token 单一可信源 — tokens.css ↔ antd theme.token ↔ GSAP duration 三者镜像。Use when 配置 antd theme / 新增颜色或圆角 / 接入暗色模式 / 评审硬编码颜色 PR。
css variablecss 变量design token设计 tokentokens.cssconfigproviderantd theme单一可信源single source of truth
paths
frontend/src/styles/**/*.cssfrontend/src/**/*.tsxfrontend/src/main.tsx
React · CSS Token 系统
单一可信源原则
颜色 / 圆角 / 字号 / 字重 / 间距 / 动画时长 只来自一处:项目根的 tokens.css。所有消费方都镜像它,不重新定义:
tokens.css (唯一定义点)
│
├──→ antd ConfigProvider.theme.token (main.tsx 启动时读 CSS var 注入)
├──→ 业务组件 .tsx / .css (直接用 var(--accent) 等)
└──→ GSAP animations/tokens.ts (镜像同样的数值)
规则
- 只在
tokens.css定义实际值(hex / px / 秒),其他地方一律var(--xxx)。 - antd
theme.token在main.tsx启动时读取 CSS 变量注入(用getComputedStyle),不要硬编码同样的颜色。 - GSAP
D = {micro, base, slow, hero}时长数值必须与 CSS--duration-fast/mid/slow一致,不要写两份不同的。 - 新增 token 类别(如 shadow / opacity)先加到
tokens.css,再同步到 antd theme + animations。 - 暗色模式用
[data-theme="dark"] :root覆盖变量,组件代码零改动。
命名规约
| 类别 | 前缀 | 例 |
|---|---|---|
| 品牌色 | --accent / --color-* |
--accent, --color-danger |
| 文字 | --text-* |
--text-primary, --text-secondary |
| 背景 | --bg-* |
--bg-base, --bg-hover |
| 边框 | --border* |
--border, --border-strong |
| 圆角 | --radius-* |
--radius-sm, --radius-lg |
| 间距 | --space-*(8px 网格) |
--space-md = 16px |
| 字号 | --text-xs/sm/md/lg/xl/2xl |
--text-lg = 14px |
| 时长 | --duration-fast/mid/slow |
--duration-mid = 0.24s |
自检
- [ ] tokens.css 是颜色 / 圆角 / 字号 / 间距 / 时长 / 字体的唯一来源?
- [ ] antd
theme.token通过getComputedStyle镜像 CSS 变量,不硬编码? - [ ] 业务组件全程用
var(--xxx),找不到硬编码#3b82f6/0.24s之类? - [ ] GSAP
D.micro/base/slow数值与--duration-*完全一致? - [ ] 新加颜色 / 圆角 / 字号必经 tokens.css,不在组件里直写?
详细参考
- 完整 tokens.css 模板 + antd 注入 + GSAP 镜像 + 暗色模式片段:
./css-token-system.examples.md
相关
- 父:
./index.md - 模型权威(框架无关三层 token / 换肤 / light-dark):
../../../design/design-language/tokens-and-theming.md - 兄弟:
palette-principles.md·palette-types.md - 配套:
../../antd/setup/config-provider.md