Installation
Usage
"use client"
import * as React from "react"
import { ArtifactCanvas, ArtifactCard, type Artifact } from "@/components/artifact-canvas"
const FIRST = `<div class="card">
<h3>Team</h3>
<p>$24 / month</p>
<button>Start free trial</button>
</div>`
export function Workspace() {
const [artifacts, setArtifacts] = React.useState<Artifact[]>([
{ id: "card", title: "Team plan card", kind: "html", versions: [{ id: "v1", content: FIRST, note: "First version" }] },
])
const [active, setActive] = React.useState<string | null>("card")
// Your model's stream: add a version, grow its content as tokens arrive, then clear `streaming`.
function revise(next: string, note: string) {
setArtifacts((all) =>
all.map((a) => ({ ...a, versions: [...a.versions, { id: `v${a.versions.length + 1}`, content: next, note }] })),
)
}
return (
<ArtifactCanvas
artifacts={artifacts}
active={active}
onActiveChange={setActive}
onAsk={(selection) => console.log("ask about", selection.text)}
onRestore={(id, versionId) => {
const old = artifacts[0].versions.find((v) => v.id === versionId)
if (old) revise(old.content, "Restored")
}}
>
<div className="p-6">
<p className="mb-3 text-sm">Here is a first version of the card.</p>
<ArtifactCard artifact={artifacts[0]} current={active === "card"} onClick={() => setActive("card")} />
<button type="button" className="mt-4 text-sm underline" onClick={() => revise(FIRST.replace("<button>", "<button style=\"background:#f2602a;color:#fff\">"), "Orange button")}>
Make the button orange
</button>
</div>
</ArtifactCanvas>
)
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | — | The conversation, shown beside the canvas. Put an ArtifactCard in a message to open its artifact. |
| artifacts | Artifact[] | — | Each has id, title, kind ("html", "code" or "markdown"), optional language for code, versions (oldest first: id, content, optional note and at) and streaming while the newest version is still being written. |
| active / defaultActive | string | null | null | The open artifact, kept by you or by the component. null closes the canvas. |
| onActiveChange | (id) => void | — | Called when an artifact opens or the canvas closes. |
| onAsk | (selection) => void | — | Called with the selected lines (text, startLine, endLine, artifactId, versionId) when someone chooses Ask about this. Without it the button never shows. |
| onRestore | (artifactId, versionId) => void | — | Called when someone restores an older version: add it again as the newest. Without it there is no Restore button. |
| previewSandbox | string | "" | Extra sandbox permissions for the HTML preview, which runs with none by default. Never pair allow-scripts with allow-same-origin. |
| previewHead | string | "" | Markup put before every HTML preview, e.g. a <style> with your base styles. |
| defaultSplit | number | 0.42 | Share of the width the conversation takes at first, 0.28 to 0.68. The divider can be dragged or moved with the arrow keys. |
| label | string | "Canvas" | Accessible name of the canvas. |
Accessibility
- The canvas is a labelled region; when it opens, focus moves to its title, and when it closes, focus goes back to whatever opened it. Escape closes it.
- Preview, Code and Changes are tabs: Left and Right move between them.
- The divider is a separator with its value: Left and Right resize, Home and End jump to the limits.
- Changes say "added" and "removed" to screen readers, not only in colour.
- Writing and each new version are announced through a polite live region.
- The version list is a menu: Up, Down, Home and End move, Enter picks, Escape closes it.
- HTML previews run in a sandboxed frame with no permissions unless you add them.
- Under prefers-reduced-motion nothing slides, pulses or blinks.
Changelog
- v1.0.0Latest
First release.