元件規範
components 區塊將具名的 UI 元件對應至以 design tokens 為基礎的具體樣式屬性。
元件屬性
在 DESIGN.md 規範中,每個元件項目接受以下規範性屬性:
| 屬性 | 預期數值 |
|---|---|
backgroundColor | 色彩字面值或 {colors.*} 引用 |
textColor | 色彩字面值或 {colors.*} 引用 |
typography | 複合 {typography.*} 引用 |
rounded | 尺寸字面值或 {rounded.*} 引用 |
padding | 尺寸字面值或 {spacing.*} 引用 |
size | 尺寸字面值或 {spacing.*} 引用 |
height | 尺寸字面值或 {spacing.*} 引用 |
width | 尺寸字面值或 {spacing.*} 引用 |
components:
button-primary:
backgroundColor: "{colors.primary}"
textColor: "{colors.on-primary}"
typography: "{typography.label-md}"
rounded: "{rounded.md}"
padding: 12px與全域 token 類別不同,components 允許同時引用複合 {typography.*} tokens 與基礎 {colors.*}、{rounded.*}、{spacing.*} tokens。
將互動狀態定義為同層項目
懸停(hover)、啟用(active)、聚焦(focus)與停用(disabled)等狀態應以扁平同層鍵值表達,而非巢狀在元件底下的物件:
components:
button-primary:
backgroundColor: "{colors.primary}"
textColor: "{colors.on-primary}"
rounded: "{rounded.md}"
padding: 12px
button-primary-hover:
backgroundColor: "#3730a3"
textColor: "{colors.on-primary}"
rounded: "{rounded.md}"
padding: 12px
button-primary-disabled:
backgroundColor: "{colors.surface}"
textColor: "{colors.text-muted}"
rounded: "{rounded.md}"
padding: 12px維持狀態扁平能確保每個項目在符號表中一致解析(components.button-primary-hover.backgroundColor),並在各工具之間乾淨匯出。
哪些規則應改寫在內文中
邊框、盒陰影、內部 flex/grid 間距、圖示尺寸規則與響應式堆疊方式在 YAML components 結構中沒有專屬屬性。請將這些規則寫在 ## Components Markdown 章節中,讓 Agent 獲得結構指引而不會讓 YAML 結構過度膨脹:
## Components
- **Primary button (`button-primary`):** Minimum width `96px`, `1px` transparent border to prevent layout shift when paired with outlined secondary buttons, and `8px` gap between icon and label.
- **Card surface (`card-surface`):** Use a `1px solid` `{colors.border}` hairline instead of drop shadows on desktop grids.外部範本引用
元件可透過行內程式碼引用指向參考 HTML 或元件片段,例如:
@components/button.html@components/card.html#card-elevated
路徑會相對於 DESIGN.md 所在的目錄解析。
自動元件對比度檢查
將 DESIGN.md 貼入 DesignMD Pro 驗證器時,linter 會自動檢查每個元件的前景屬性(color、text、textColor、foreground)與背景屬性(background、backgroundColor、bg、surface、fill):
- 對比度
< 3:1→ 錯誤(low-contrast) - 對比度
3:1至4.49:1→ 警告(low-contrast) - 對比度
>= 4.5:1→ 通過一般文字的 WCAG AA 標準
以上方的 button-primary-disabled 為例,驗證 {colors.text-muted}(#52525b)搭配 {colors.surface}(#f6f5f4)可確保停用狀態標籤依然清晰可讀。
下一步
- 內文章節 —— 搭配其他固定順序章節撰寫
## Components說明 - 完整範例 —— 檢視包含多個元件的完整
DESIGN.md - 驗證器與 Lint 規則 —— 查閱完整的引用與對比度檢查規則