Claude Artifacts

给 Claude 一小段指令,让它构建的 artifact 使用 UI SDK Web Components。

让 Claude 用 UI SDK 来构建 artifact 吧。artifact 是 Claude 在聊天里为你做的一个页面,比如原型、表单或小工具。

我们只需给 Claude 一小段指令,一次就够。之后,当 artifact 需要对话框、选择框或日期选择器时,Claude 就会用 @ui-sdk/web 的 Web Components 来构建它。

复制指令

先复制下面这段内容。它告诉 Claude 用哪些组件、到哪里读它们的说明,以及如何加载它们:

提示词
Whenever I need UI components (in artifacts, HTML pages or prototypes), use UI SDK web components (@ui-sdk/web). Don't use another component library unless I ask.

When building with UI SDK web:
- Fetch https://data.jsdelivr.com/v1/packages/npm/@ui-sdk/web/resolved?specifier=latest to get the latest version.
- Before building, read https://cdn.jsdelivr.net/npm/@ui-sdk/web@<version>/docs/components.md. It has every component's composition, attributes and events in one file. Read docs/components/<name>.md at the same path only when you need a component's examples. If components.md is missing, read docs/llms.txt at the same path instead.
- Load the components with <script type="module" src="https://cdn.jsdelivr.net/npm/@ui-sdk/web@<version>/browser/web.js"></script>, pinned to that exact version.
- Fetch https://cdn.jsdelivr.net/npm/@ui-sdk/web@<version>/browser/web.css, publish it as one of the artifact's own files, and link it with <link rel="stylesheet" href="web.css" />. If the artifact can only be a single HTML file, put the contents of web.css in a <style> tag instead. Don't link it from the CDN; artifact pages block outside stylesheets.
- Style with the --ui-* tokens and data-slot selectors from web.css rather than your own design tokens.

把指令加到 Claude

把这段内容粘贴到 Claude 存放你指令的地方:

  • 网页版或桌面版 Claude。 打开 Settings,选择 Your Claude,再粘贴到 Instructions。
  • Claude Code。 粘贴到 ~/.claude/CLAUDE.md。如果文件还不存在,就创建一个。

Claude Code 会在会话开始时读取 CLAUDE.md。新开一个会话,然后运行 /memory,确认文件已经加载。如果只想对单个项目生效,改用该项目文件夹里的 CLAUDE.md。

要一个 artifact

现在让它做点带几个组件的东西:

提示词
Build an artifact for booking a table: a date picker, a select for the time, a number input for the party size and a dialog that confirms the booking.

Claude 会查询最新版本、阅读文档并构建页面。它生成的 HTML 用两个标签加载 UI SDK:

<script
  type="module"
  src="https://cdn.jsdelivr.net/npm/@ui-sdk/web@0.4.1/browser/web.js"
></script>
<link rel="stylesheet" href="web.css" />

用键盘试一试。打开对话框,按 Escape。它会关闭,焦点回到打开它的按钮上。

下一步