设计

内置四套设计。也可以用你自己的 DESIGN.md、别的组件库的,或者一份主题样式表。

一套设计就是一份 DESIGN.md:先是颜色、字体、圆角、间距和动效的 token,后面是说明怎么用这些 token 的文字。渲染器把 token 变成这块界面的 CSS 变量。提示词把同样的 token 和这段文字交给模型。一个文件决定每条回复的样子。

包里自带的设计

包里带了四套完整的设计,每套都有浅色和深色两组颜色。在下面的例子上切换看看,标记不用改。

实时渲染

正在加载渲染器…

<state range="month" rows={[
  { week: "W1", orders: 124 }, { week: "W2", orders: 151 }, { week: "W3", orders: 139 }, { week: "W4", orders: 182 }
]} />

<row gap={3} wrap>
  <title level={2}>Orders</title>
  <spacer />
  <SegmentedControl aria-label="Range" bind:value={range}>
    <SegmentedControlIndicator />
    <SegmentedControlItem value="week">Week</SegmentedControlItem>
    <SegmentedControlItem value="month">Month</SegmentedControlItem>
  </SegmentedControl>
</row>

<grid columns={3} gap={3}>
  <Stat label="Orders" value={sumBy(rows, "orders")} delta={0.12} caption="vs last month" />
  <Stat label="Average order" value={84.5} format="currency" delta={-0.03} />
  <card padding={4} gap={2}>
    <text size="sm" color="muted">Status</text>
    <row gap={2} wrap><badge color="success">On track</badge><badge color="warning">2 late</badge><badge color="danger">1 failed</badge></row>
    <row gap={2}><Button variant="solid">Review</Button><Button>Export</Button></row>
  </card>
</grid>

<card padding={4}>
  <Chart type="area" data={rows} x="week" y="orders" height={160} aria-label="Orders per week" />
</card>
设计特点
paper默认设计。安静、中性:内容优先,一个强调色,柔和的层次
noir深色优先:深炭灰、发亮的蓝色、大圆角
heritage杂志风格:衬线标题、米色纸张、一个陶土色强调色、直角
signal紧凑、偏技术,适合运维类界面:等宽字体的标签、青柠色的信号色
import { noir } from "@open-intelligent-ui/core/designs"

<IntelligentUI source={text} design={noir} scheme="auto" />

buildSystemPrompt({ design: noir })

不传 design 时用 Paper。scheme 可以是 light、dark 或 auto,auto 跟随系统。

你自己的 DESIGN.md

在文件开头用 YAML 写 token,下面用 Markdown 写使用说明。

---
name: Acme
colors:
  light:
    background: "#fbfbfa"
    surface: "#ffffff"
    foreground: "#1c1c1a"
    muted: "#f1f1ef"
    muted-foreground: "#6b6b66"
    border: "#e4e4e0"
    primary: "#1c1c1a"
    on-primary: "#ffffff"
    accent: "#2f6feb"
    success: "#17834a"
    danger: "#d1343b"
    chart-1: "#2f6feb"
  dark:
    background: "#131312"
    # …
typography:
  headline-md: { fontFamily: Inter, fontSize: 1.3125rem, fontWeight: 600, lineHeight: 1.3 }
  body-md: { fontFamily: Inter, fontSize: 0.9375rem, fontWeight: 400, lineHeight: 1.6 }
rounded: { sm: 6px, md: 8px, lg: 12px }
spacing: { unit: 4px }
motion:
  surface-enter: { duration: 260ms, timingFunction: "cubic-bezier(0.16, 1, 0.3, 1)" }
  emphasis: { duration: 520ms, timingFunction: "cubic-bezier(0.34, 1.4, 0.64, 1)" }
---

## Colors

Accent is for the one action that matters on a surface. Status colors are only for status.
import design from "./acme.DESIGN.md?raw"

<IntelligentUI source={text} design={design} />

渲染器读取的内容:

Token变成
colors--iui-color-<name>。样式表用到的角色有 background、surface、foreground、muted、muted-foreground、border、primary、accent、success、warning、danger、info,以及 chart-1 到 chart-6,每个角色都有一个配对的 on- 颜色
typography--iui-font-<role>-family、-size、-weight、-line 和 -tracking,供 <title>、<text> 和 variant="<name>" 使用
rounded、spacing--iui-radius-*、--iui-space-*
motion--iui-motion-<name>-duration 和 -easing,所有入场、退场和过渡都用它们
components--iui-c-<component>-*,例如主按钮的颜色

设计里写到的字体,页面上有就会用。字体要你自己加载,渲染器不会去下载字体。在仓库里运行 pnpm design:lint,可以用 DESIGN.md CLI 检查一个文件。

为别的组件库写的 DESIGN.md

公司的设计系统往往已经有一份 DESIGN.md,是为它自己的组件库写的。这种文件通常没有 token YAML,颜色名字的含义和这里不一样,文字部分讲的也是另一个组件库的 class。

原样传入。文件里没有 token YAML 时,token 从它记录的 CSS 变量里读取:带浅色和深色取值的变量表格、行内的 --name: value,以及 css 代码块。HeroUI 公开发布的 DESIGN.md 就是这样不加改动读进来的,演示站的设计菜单里有它。

import heroui from "./heroui.DESIGN.md?raw"

defineLibrary({ design: heroui })

调整它没定下来的部分。parseDesign 接受选项,返回一个设计,你把它传进去,代替原来的文本。

import { parseDesign } from "@open-intelligent-ui/core"

const design = parseDesign(heroui, {
  // 叠加在文件的 token 之上:补上它没写的,或者它只提了名字的颜色。
  tokens: {
    colors: { snow: "oklch(0.9911 0 0)" },
    motion: { emphasis: { duration: "420ms", timingFunction: "cubic-bezier(0.34, 1.56, 0.64, 1)" } },
  },
  // 名字对不上时,指定设计里的哪个颜色填哪个角色。
  roles: { primary: "accent", "muted-foreground": "muted" },
  // 不该让模型读的章节:另一个组件库的安装步骤。
  prose: { omit: ["Setup", "Source of Truth"] },
})
  • 角色:token 的名字和某个角色相同,就填这个角色。别的体系里的叫法也能识别:shadcn 的 card 和 destructive,Material 的 on-surface 和 on-primary。在某套设计里 muted 是文字颜色还是背景色,根据它和页面背景的对比度来判断。colorSources(design) 返回每个角色最终用了哪个 token。
  • 文字说明:对于为别的组件库写的设计,提示词会告诉模型:采纳它在层级、颜色、形状和语气上的方向,它的 class 名和安装步骤在这里不适用。
  • 问题:不会抛异常。读不出来的值,或者写进 CSS 不安全的值,会被略过,并列在 design.issues 里。validateLibrary 也会报告这些问题。

只有样式表

只有主题样式表、没有 DESIGN.md 时(shadcn/ui、HeroUI、Tailwind @theme、你自己的变量),用 designFromCss 读取其中的变量。

import { designFromCss } from "@open-intelligent-ui/core"
import theme from "./theme.css?raw"

const design = designFromCss(theme, {
  name: "Acme",
  prose: "Primary is for the one action that matters on a surface.",
})

:root、html 和 @theme 里的变量是浅色。.dark、[data-theme="dark"] 和 @media (prefers-color-scheme: dark) 里的是深色。oklch()、hsl()、color-mix()、var() 引用,以及 shadcn 那种不带函数名的 HSL 三元组,都能识别。

设计放在哪里

把设计和你的产品添加的其他东西一起放进组件库,这样渲染器和提示词就不会不一致:

export const library = defineLibrary({ design, components, charts, motion })

提示词会让模型用设计变量,不用写死的颜色。模型写的 CSS 用的也是同一批变量:var(--iui-color-accent)、var(--iui-radius-md)、var(--iui-space-lg)、var(--iui-motion-layout-duration)。在一套设计下写的回复,换一套设计也能正确地重新渲染。