# 主题、密度与设计令牌

在应用入口引入 `@ancrel/tokens/css`。页面默认使用暗色主题与舒适密度。面板背景使用 `--ac-sys-surface-panel`，正文颜色使用 `sys.ink.primary`。切换预览主题时，两者会一起更新。

| `Context` | `--ac-sys-surface-panel` |
| - | - |
| `dark/compact` | `#16161a` |
| `dark/comfortable` | `#16161a` |
| `light/compact` | `#ffffff` |
| `light/comfortable` | `#ffffff` |

```tsx
'use client'

import { useState } from 'react'
import { cssVar, type Theme } from '@ancrel/tokens'
import '@ancrel/tokens/css'

const panel = {
  backgroundColor: cssVar('sys.surface.panel'),
  color: cssVar('sys.ink.primary'),
  padding: cssVar('sys.space.inset'),
  fontFamily: cssVar('sys.type.family.sans'),
}

export default function App() {
  const [theme, setTheme] = useState<Theme>('dark')
  return (
    <section aria-label="Workspace" data-theme="light" data-density="comfortable" style={panel}>
      <h2>Workspace</h2>
      <section aria-label="Local preview" data-theme={theme} data-density="compact" style={panel}>
        <h3>Local preview</h3>
        <p>Preview theme: {theme}</p>
        <button type="button" onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>
          Switch preview theme
        </button>
      </section>
    </section>
  )
}
```

Expected:

### Nested theme preview

The workspace has a white panel with comfortable padding. Its compact preview starts dark. Select **Switch preview theme** to turn the inner panel light, then select it again to restore dark. Its text changes with the panel and its padding stays compact. The outer workspace stays light throughout.

From the workspace root, run `pnpm --filter @ancrel/tokens build` and `pnpm --filter @ancrel/example-tokens test`.

## 在局部切换外观

```tsx
import '@ancrel/tokens/css'
import { cssVar } from '@ancrel/tokens'

export function Preview() {
  return (
    <section data-theme="light" data-density="compact" style={{
      backgroundColor: cssVar('sys.surface.panel'),
      color: cssVar('sys.ink.primary'),
      padding: cssVar('sys.space.inset'),
    }}>
      Preview
    </section>
  )
}
```

在任意容器设置 `data-theme="dark|light"` 或 `data-density="compact|comfortable"` 即可切换其子树。两条轴独立，因此一个紧凑面板可以继承外层主题。密度决定控件行高、留白和命中区域。粗指针设备的命中区域扩大至 44px（按默认根字号换算）。系统偏好减少动画时，三个过渡时长均为 0ms。

组件槽 `comp.split.handle.size`、`comp.split.handle.hitArea` 和 `comp.rail.width` 可在目标组件上覆盖。活动栏默认宽度为 4.375rem。私有的 `sys.internal.railWidth` 为这一槽提供语义别名，公共类型和 CSS 只暴露组件槽。数字列使用 `font-variant-numeric: var(--ac-sys-type-numeric)`。DTCG 用数值 1 表示启用 OpenType 的 `tnum` 特性，CSS 投影为 `tabular-nums`。

## 接入已有应用

使用根元素 `.dark` 的应用，可在主样式之后引入 `@ancrel/tokens/css/compat-dark-class.css`。此时根元素默认亮色，添加 `.dark` 切换为暗色，显式 `data-theme` 优先。主题持久化由应用负责。

Tailwind v4 在样式入口引入 `@ancrel/tokens/tailwind-v4.css`，Tailwind v3 在配置的 `presets` 数组中加入 `require('@ancrel/tokens/tailwind-v3')`。两者都可在应用中使用 `bg-ac-panel`、`text-ac-ink-primary` 和 `p-ac-inset`。v3 的变量颜色不支持 `bg-ac-panel/50`，v4 支持透明度修饰符。组件本身使用普通 CSS。

## 对比与偏离提案

普通文字最低对比度为 4.5∶1，大字和必要的非文本边界为 3∶1。控件边界使用 `sys.border.control`，装饰性分隔线使用 `sys.border.subtle`。状态使用可见文字或图形辅以颜色。强制颜色模式下，使用阴影表示抬升的组件需提供 1px 的 `CanvasText` 边界。

| 提案范围 | 四个上下文的检查结果 | 发布值 |
| - | - | - |
| RFC §2.2 给定的表面、文字、边界、强调色与状态前景 | 所有列出的颜色对通过 | 保留提案色值 |
| RFC 留待选定的悬停、按下、选中及状态底色 | 在实际不透明背景上检查对应前景 | 各主题的配对写入 DTCG |

运行 `pnpm --filter @ancrel/tokens build` 可在包目录下的 `dist/reports/contrast.json` 查阅每一对的实测比值、阈值与上下文。对比计算遵循 sRGB 相对亮度，先合成透明图层，再比较亮度。[WCAG 2.2 对比度](https://www.w3.org/WAI/WCAG22/Understanding/contrast-minimum.html)

## 读取交换文件

`@ancrel/tokens/dtcg/index.json` 列出默认上下文与四个文件名。每个文件都包含已解析的类型和值，可单独交给设计工具。`tokenIndex` 按公开路径列出 CSS 变量及四个上下文的 CSS 值。原始值由设计系统维护者在 `ref.tokens.json` 中编辑。[DTCG 2025.10 格式](https://www.designtokens.org/tr/2025.10/format/)
