⚠️ Alpha内测版本警告:此为早期内部构建版本,尚不完整且可能存在错误,欢迎大家提Issue反馈问题或建议
Skip to content

3.4.3 告别五颜六色的黑——设计系统

一句话破题

设计系统是一套统一的视觉规范(颜色、字体、间距),确保 UI 一致性并简化与 AI 的协作。

核心价值

没有设计系统,每次描述颜色都要说"那种蓝"、"稍微深一点的灰"。有了设计系统,你只需说 primarymuted,AI 就知道确切的值。

设计令牌(Design Tokens)

设计令牌是设计系统的基础,它将设计决策抽象为可复用的变量:

颜色系统

语义化颜色(推荐)

shadcn/ui 使用语义化颜色命名:

变量用途示例
--background页面背景白色/深灰
--foreground主要文字黑色/白色
--primary品牌主色蓝色
--secondary次要元素浅灰
--muted弱化内容灰色文字
--accent强调元素悬停背景
--destructive危险操作红色

在 CSS 中定义

css
/* globals.css */
@layer base {
  :root {
    --background: 0 0% 100%;
    --foreground: 222.2 84% 4.9%;
    --primary: 222.2 47.4% 11.2%;
    --primary-foreground: 210 40% 98%;
    --secondary: 210 40% 96%;
    --muted: 210 40% 96%;
    --muted-foreground: 215.4 16.3% 46.9%;
    --accent: 210 40% 96%;
    --destructive: 0 84.2% 60.2%;
  }

  .dark {
    --background: 222.2 84% 4.9%;
    --foreground: 210 40% 98%;
    /* ... */
  }
}

在 Tailwind 中使用

tsx
<div className="bg-background text-foreground">
  <button className="bg-primary text-primary-foreground">
    主按钮
  </button>
  <p className="text-muted-foreground">
    次要文字
  </p>
</div>

字体系统

字号规范

类名大小用途
text-xs12px辅助信息、标签
text-sm14px次要内容、表单
text-base16px正文
text-lg18px小标题
text-xl20px标题
text-2xl24px大标题
text-3xl30px页面标题

字重规范

类名字重用途
font-normal400正文
font-medium500强调
font-semibold600小标题
font-bold700标题

配置自定义字体

ts
// tailwind.config.ts
import { fontFamily } from "tailwindcss/defaultTheme"

export default {
  theme: {
    extend: {
      fontFamily: {
        sans: ["Inter", ...fontFamily.sans],
        mono: ["JetBrains Mono", ...fontFamily.mono],
      },
    },
  },
}

间距系统

Tailwind 使用 4px 为基础单位:

像素常见用途
14px图标与文字间距
28px紧凑元素间距
312px表单元素内边距
416px卡片内边距、默认间距
624px区块间距
832px大区块间距
1248px页面区域分隔

间距使用原则

  1. 一致性:相同层级的元素使用相同间距
  2. 层次感:父子元素间距 < 兄弟元素间距
  3. 呼吸感:重要内容周围留更多空白

圆角与阴影

圆角规范

类名用途
rounded-sm2px小元素
rounded4px默认
rounded-md6px按钮、输入框
rounded-lg8px卡片
rounded-xl12px大卡片
rounded-full9999px圆形头像、标签

阴影规范

类名用途
shadow-sm悬浮卡片、下拉菜单
shadow普通卡片
shadow-md弹窗、模态框
shadow-lg重要浮层

建立项目设计系统

1. 定义设计变量文件

ts
// lib/design-tokens.ts
export const colors = {
  brand: {
    primary: "#3B82F6",
    secondary: "#10B981",
    accent: "#F59E0B",
  },
  semantic: {
    success: "#22C55E",
    warning: "#EAB308",
    error: "#EF4444",
    info: "#3B82F6",
  },
}

export const spacing = {
  page: "max-w-7xl mx-auto px-4 sm:px-6 lg:px-8",
  section: "py-12 md:py-16 lg:py-20",
  card: "p-4 md:p-6",
}

2. 扩展 Tailwind 配置

ts
// tailwind.config.ts
import { colors } from "./lib/design-tokens"

export default {
  theme: {
    extend: {
      colors: {
        brand: colors.brand,
      },
    },
  },
}

AI 协作指南

核心意图:让 AI 使用统一的设计语言。

需求定义公式

  • 项目使用 [设计系统名/自定义规范]
  • 颜色使用语义化变量:primarymuteddestructive
  • 间距遵循 4px 基础单位

关键术语设计令牌语义化颜色CSS 变量主题切换

交互策略

  1. 项目初期向 AI 说明设计系统规范
  2. 在 Prompt 中使用设计系统术语
  3. 要求 AI 使用已定义的颜色/间距变量

验收清单

  • [ ] 颜色使用语义化变量,不是硬编码值
  • [ ] 字号使用 Tailwind 预设类
  • [ ] 间距遵循 4px 倍数规律
  • [ ] 暗色模式颜色正确切换