# 推出 UI SDK

用行为库构建你自己的组件，为智能体而做。

[Watch the launch video](https://5i7hyknqgxeqd9wd.public.blob.vercel-storage.com/ui-sdk-launch-v91.mp4)

今天我们发布 UI SDK。

UI SDK 是一个 TypeScript 的 UI 行为库，也就是构成组件库的那些层。它为键盘导航、焦点、选择、浮层、拖放，以及这些交互所需的 ARIA 属性提供构建块。

例如，`createRovingFocus` 用方向键移动焦点，`createTypeahead` 在你输入时找到对应的选项，`createPopup` 在按钮旁打开浮层。

我们做了 50 多个。你，或者你的智能体，把它们组合起来构建自己的组件。

## 构建组件
下面是一个小的 popover。你用 `@ui-sdk/core` 定义一次，`createPopup` 负责最麻烦的部分：在按钮旁打开，按 Escape 或点击外部时关闭，并把焦点放回原处。

```ts title="lib/share.ts"
import {
  createPopup,
  definePrimitive,
  popupContent,
  popupState,
  popupTrigger,
} from "@ui-sdk/core"
import { popoverPreset } from "@ui-sdk/core/popover"

const share = definePrimitive({
  name: "share",
  options: { defaultOpen: "boolean" },
  state: (options: { defaultOpen?: boolean }) => popupState(options),
  api: (context) => createPopup(context, { preset: popoverPreset }),
  slots: (slot) => ({
    trigger: popupTrigger(slot),
    content: popupContent(slot),
  }),
})

export { share }
```

别担心，你不用学会写这些。我们教会了智能体怎么做，而且它们非常擅长。向你自己的智能体要你需要的组件就行。

然后在你的框架里把它做成组件：

**React**

```ts title="components/ui/share.ts"
"use client"

import { createParts } from "@ui-sdk/react/primitive"
import { share } from "@/lib/share"

const parts = createParts(share, { trigger: "button", content: "div" })

const Share = parts.Root
const ShareTrigger = parts.Trigger
const ShareContent = parts.Content

export { Share, ShareContent, ShareTrigger }
```

**Vue**

```ts title="components/ui/share.ts"
import { createParts } from "@ui-sdk/vue/primitive"
import { share } from "@/lib/share"

const parts = createParts(share, { trigger: "button", content: "div" })

const Share = parts.Root
const ShareTrigger = parts.Trigger
const ShareContent = parts.Content

export { Share, ShareContent, ShareTrigger }
```

**Svelte**

```ts title="components/ui/share.ts"
import { createParts } from "@ui-sdk/svelte/primitive"
import { share } from "@/lib/share"

const parts = createParts(share, { trigger: "button", content: "div" })

const Share = parts.Root
const ShareTrigger = parts.Trigger
const ShareContent = parts.Content

export { Share, ShareContent, ShareTrigger }
```

**Solid**

```ts title="components/ui/share.ts"
import { createParts } from "@ui-sdk/solid/primitive"
import { share } from "@/lib/share"

const parts = createParts(share, { trigger: "button", content: "div" })

const Share = parts.Root
const ShareTrigger = parts.Trigger
const ShareContent = parts.Content

export { Share, ShareContent, ShareTrigger }
```

**Web Components**

```ts title="components/ui/share.ts"
import { createParts, wrappedButton } from "@ui-sdk/web"
import { share } from "@/lib/share"

createParts(share, {
  root: "share-popover",
  trigger: { tag: "share-trigger", target: wrappedButton },
  content: "share-content",
})
```

然后像其他组件一样使用它：

**React**

```tsx title="pages/index.tsx"
import { Share, ShareContent, ShareTrigger } from "@/components/ui/share"

function Example() {
  return (
    <Share>
      <ShareTrigger>Share</ShareTrigger>
      <ShareContent>Anyone with the link can view.</ShareContent>
    </Share>
  )
}
```

**Vue**

```vue title="pages/index.vue"
<script setup lang="ts">
import { Share, ShareContent, ShareTrigger } from "@/components/ui/share"
</script>

<template>
  <Share>
    <ShareTrigger>Share</ShareTrigger>
    <ShareContent>Anyone with the link can view.</ShareContent>
  </Share>
</template>
```

**Svelte**

```svelte title="pages/index.svelte"
<script lang="ts">
  import { Share, ShareContent, ShareTrigger } from "@/components/ui/share"
</script>

<Share>
  <ShareTrigger>Share</ShareTrigger>
  <ShareContent>Anyone with the link can view.</ShareContent>
</Share>
```

**Solid**

```tsx title="pages/index.tsx"
import { Share, ShareContent, ShareTrigger } from "@/components/ui/share"

function Example() {
  return (
    <Share>
      <ShareTrigger>Share</ShareTrigger>
      <ShareContent>Anyone with the link can view.</ShareContent>
    </Share>
  )
}
```

**Web Components**

```html title="pages/index.html"
<script type="module">
  import "@/components/ui/share"
</script>

<share-popover>
  <share-trigger><button>Share</button></share-trigger>
  <share-content>Anyone with the link can view.</share-content>
</share-popover>
```

要一步步构建一个组件，请看[构建组件](/docs/core/build-a-component)。要组合多个行为，请看[组合行为](/docs/core/compose-behaviors)。

## 组件库
我们用同样的行为为每个框架构建了一个组件库：50 多个现成的组件，从 Select 到 Date Picker，在 React、Vue、Svelte、Solid 和 Web Components 里拥有相同的部件。样式由你决定。

**React**

```tsx
import {
  Select,
  SelectContent,
  SelectItem,
  SelectLabel,
  SelectTrigger,
  SelectValue,
} from "@ui-sdk/react/select"

function Example() {
  return (
    <Select placeholder="Select a role">
      <SelectLabel>Role</SelectLabel>
      <SelectTrigger>
        <SelectValue />
      </SelectTrigger>
      <SelectContent>
        <SelectItem value="viewer">Viewer</SelectItem>
        <SelectItem value="editor">Editor</SelectItem>
      </SelectContent>
    </Select>
  )
}
```

**Vue**

```vue
<script setup lang="ts">
import {
  Select,
  SelectContent,
  SelectItem,
  SelectLabel,
  SelectTrigger,
  SelectValue,
} from "@ui-sdk/vue/select"
</script>

<template>
  <Select placeholder="Select a role">
    <SelectLabel>Role</SelectLabel>
    <SelectTrigger>
      <SelectValue />
    </SelectTrigger>
    <SelectContent>
      <SelectItem value="viewer">Viewer</SelectItem>
      <SelectItem value="editor">Editor</SelectItem>
    </SelectContent>
  </Select>
</template>
```

**Svelte**

```svelte
<script lang="ts">
  import {
    Select,
    SelectContent,
    SelectItem,
    SelectLabel,
    SelectTrigger,
    SelectValue,
  } from "@ui-sdk/svelte/select"
</script>

<Select placeholder="Select a role">
  <SelectLabel>Role</SelectLabel>
  <SelectTrigger>
    <SelectValue />
  </SelectTrigger>
  <SelectContent>
    <SelectItem value="viewer">Viewer</SelectItem>
    <SelectItem value="editor">Editor</SelectItem>
  </SelectContent>
</Select>
```

**Solid**

```tsx
import {
  Select,
  SelectContent,
  SelectItem,
  SelectLabel,
  SelectTrigger,
  SelectValue,
} from "@ui-sdk/solid/select"

function Example() {
  return (
    <Select placeholder="Select a role">
      <SelectLabel>Role</SelectLabel>
      <SelectTrigger>
        <SelectValue />
      </SelectTrigger>
      <SelectContent>
        <SelectItem value="viewer">Viewer</SelectItem>
        <SelectItem value="editor">Editor</SelectItem>
      </SelectContent>
    </Select>
  )
}
```

**Web Components**

```html
<script type="module">
  import "@ui-sdk/web/select"
</script>

<ui-select placeholder="Select a role">
  <ui-select-label>Role</ui-select-label>
  <ui-select-trigger>
    <button>
      <ui-select-value></ui-select-value>
    </button>
  </ui-select-trigger>
  <ui-select-content>
    <ui-select-item value="viewer">Viewer</ui-select-item>
    <ui-select-item value="editor">Editor</ui-select-item>
  </ui-select-content>
</ui-select>
```

## 智能体
如今，智能体在构建我们的界面。越来越多的智能体也在使用这些界面。所以我们为两者构建了 UI SDK。

用 UI SDK 构建的一切都便于智能体使用。智能体可以读取页面上的内容，并像人一样按压、输入和选择，在你设定的权限范围内。

用 `createAgent` 把你的页面交给智能体：

```ts
import { createAgent } from "@ui-sdk/react/agent"

const agent = createAgent({
  allow: ({ component, label, action }) =>
    component === "select" && label === "Region" && action === "setValue",
})

agent.snapshot()

await agent.act({
  steps: [{ ref: "Region", action: "setValue", args: ["north-america"] }],
})
```

WebMCP 让页面把工具交给浏览器内置的智能体。调用 `registerTools`，浏览器里的智能体就能读取你的组件，也能调用它们的动作。要设置它，请看 [WebMCP](/docs/agents/connect-an-agent/webmcp)。

## 开始使用
把安装提示词复制进你的智能体，它就会替你配置好 UI SDK。

**React**

```text prompt
Set up ui-sdk in this React project. It needs React 19 or newer.

1. Install @ui-sdk/react and @ui-sdk/core with the project's package manager. If a release-age policy blocks the install, stop and ask me instead of changing it.
2. Add a note to AGENTS.md, creating it if needed: before using ui-sdk, follow the ui-sdk skill if it's installed, and read node_modules/@ui-sdk/react/docs/llms.txt. To build a new component, also search node_modules/@ui-sdk/react/docs/core.md by heading, and read rendering.md next to it.
3. If you support skills, run: npx skills@latest add vercel-labs/ui --skill ui-sdk
```

**Vue**

```text prompt
Set up ui-sdk in this Vue project. It needs Vue 3.5 or newer.

1. Install @ui-sdk/vue and @ui-sdk/core with the project's package manager. If a release-age policy blocks the install, stop and ask me instead of changing it.
2. If it's a Nuxt 4 or newer app, add "@ui-sdk/vue" to modules in nuxt.config.ts.
3. Add a note to AGENTS.md, creating it if needed: before using ui-sdk, follow the ui-sdk skill if it's installed, and read node_modules/@ui-sdk/vue/docs/llms.txt. To build a new component, also search node_modules/@ui-sdk/vue/docs/core.md by heading, and read rendering.md next to it.
4. If you support skills, run: npx skills@latest add vercel-labs/ui --skill ui-sdk
```

**Svelte**

```text prompt
Set up ui-sdk in this Svelte project. It needs Svelte 5.33 or newer.

1. Install @ui-sdk/svelte and @ui-sdk/core with the project's package manager. If a release-age policy blocks the install, stop and ask me instead of changing it.
2. Add a note to AGENTS.md, creating it if needed: before using ui-sdk, follow the ui-sdk skill if it's installed, and read node_modules/@ui-sdk/svelte/docs/llms.txt. To build a new component, also search node_modules/@ui-sdk/svelte/docs/core.md by heading, and read rendering.md next to it.
3. If you support skills, run: npx skills@latest add vercel-labs/ui --skill ui-sdk
```

**Solid**

```text prompt
Set up ui-sdk in this Solid project. It needs Solid 2: solid-js and @solidjs/web 2.0.0 or newer.

1. Install @ui-sdk/solid and @ui-sdk/core with the project's package manager. If a release-age policy blocks the install, stop and ask me instead of changing it.
2. Add a note to AGENTS.md, creating it if needed: before using ui-sdk, follow the ui-sdk skill if it's installed, and read node_modules/@ui-sdk/solid/docs/llms.txt. To build a new component, also search node_modules/@ui-sdk/solid/docs/core.md by heading, and read rendering.md next to it.
3. If you support skills, run: npx skills@latest add vercel-labs/ui --skill ui-sdk
```

**Web Components**

```text prompt
Set up ui-sdk in this Web Components project. Each component registers its tags when you import its entry, like @ui-sdk/web/dialog.

1. Install @ui-sdk/web and @ui-sdk/core with the project's package manager. If a release-age policy blocks the install, stop and ask me instead of changing it.
2. Add a note to AGENTS.md, creating it if needed: before using ui-sdk, follow the ui-sdk skill if it's installed, and read node_modules/@ui-sdk/web/docs/llms.txt. To build a new component, also search node_modules/@ui-sdk/web/docs/core.md by heading, and read rendering.md next to it.
3. If you support skills, run: npx skills@latest add vercel-labs/ui --skill ui-sdk
```

想了解更多？请读[简介](/docs/core)。

UI SDK 目前处于 beta 阶段。试试它，然后在 [GitHub](https://github.com/vercel-labs/ui) 上告诉我们你的想法。

## 致谢
UI SDK 汇集了我们多年来使用和构建组件库学到的一切。感谢 [Radix](https://www.radix-ui.com)、[Base UI](https://base-ui.com)、[React Aria](https://react-spectrum.adobe.com/react-aria/)、[shadcn/ui](https://ui.shadcn.com)、[Nuxt UI](https://ui.nuxt.com) 和 [Geist](https://vercel.com/geist) 背后的团队，也感谢 [WAI-ARIA Authoring Practices](https://www.w3.org/WAI/ARIA/apg/)。
