AI Artifact

Things an agent makes: a file card that builds itself, a preview pane with a code toggle, and a version strip of generated results.

Installation

Installation

One command adds ai-artifact and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.

$npx shadcn@latest add https://tweenui.dev/r/ai-artifact.json
ai-artifact-demo
components/ui/ai-artifact-demo.tsx
"use client"

import * as React from "react"
import { Check } from "lucide-react"

import { AiDemo } from "@/lib/ai-demo"
import { AiStyle, useAiLoop } from "@/lib/ai-kit"
import {
  AiArtifactFile,
  AiArtifactPreview,
  AiArtifactVersions,
  type AiArtifactVersion,
  type AiArtifactView,
} from "@/registry/tween/ai-artifact"

const CSS = `
.aik-ard-price{display:grid;gap:10px;width:min(100%,228px);padding:14px;border-radius:14px;background:var(--aik-card);box-shadow:var(--aik-sh-pop);text-align:left}
.aik-ard-price header{display:flex;align-items:center;justify-content:space-between;gap:8px}
.aik-ard-price header b{font-size:12.5px;font-weight:600}
.aik-ard-price header span{height:18px;padding:0 6px;border-radius:6px;font:500 10px/18px var(--aik-mono);letter-spacing:.04em;text-transform:uppercase;color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 12%,transparent)}
.aik-ard-amt{display:flex;align-items:baseline;gap:4px}
.aik-ard-amt b{font-size:28px;line-height:30px;font-weight:600;letter-spacing:-.03em}
.aik-ard-amt span{font-size:11.5px;color:var(--aik-text-3)}
.aik-ard-price ul{display:grid;gap:5px}
.aik-ard-price li{display:flex;align-items:center;gap:6px;font-size:11.5px;line-height:16px;color:var(--aik-text-2)}
.aik-ard-price li svg{color:var(--aik-green)}
.aik-ard-cta{display:grid;place-items:center;height:28px;border-radius:9px;background:var(--aik-pri);color:var(--aik-pri-ink);font-size:12px;font-weight:500}

.aik-ard-art{position:relative;display:block;overflow:hidden}
.aik-ard-art svg{position:absolute;inset:0;width:100%;height:100%}
.aik-ard-art[data-v="dawn"]{background:radial-gradient(90% 120% at 12% 100%,#ff9a5a 0%,transparent 58%),radial-gradient(80% 110% at 92% 8%,#ff5f8f 0%,transparent 60%),linear-gradient(160deg,#3a1d6e,#7a3fd0)}
.aik-ard-art[data-v="tide"]{background:radial-gradient(80% 100% at 80% 0%,#7fe3d4 0%,transparent 62%),linear-gradient(170deg,#0b3a66,#1273a8 60%,#18a7a7)}
.aik-ard-art[data-v="grid"]{background:radial-gradient(60% 80% at 50% 110%,#5b7cff 0%,transparent 64%),#0c0d16;background-blend-mode:screen}
.aik-ard-art[data-v="grid"]::before{content:"";position:absolute;inset:0;background:linear-gradient(#ffffff14 1px,transparent 1px) 0 0/100% 12.5%,linear-gradient(90deg,#ffffff14 1px,transparent 1px) 0 0/8.33% 100%}
.aik-ard-art[data-v="paper"]{background:radial-gradient(70% 90% at 18% 22%,#ffd9a8 0%,transparent 60%),#f6efe4}
.aik-ard-art[data-v="moss"]{background:radial-gradient(80% 110% at 100% 100%,#c9f27a 0%,transparent 60%),linear-gradient(150deg,#0f3d2a,#1f7a4c)}
`

const Bars = ({ ink }: { ink: string }) => (
  <svg viewBox="0 0 160 100" preserveAspectRatio="none" aria-hidden="true">
    <rect x="14" y="58" width="74" height="9" rx="4.5" fill={ink} />
    <rect
      x="14"
      y="72"
      width="48"
      height="5"
      rx="2.5"
      fill={ink}
      opacity=".55"
    />
    <rect x="14" y="83" width="26" height="8" rx="4" fill={ink} opacity=".9" />
  </svg>
)

function Art({ kind }: { kind: "dawn" | "tide" | "grid" | "paper" | "moss" }) {
  return (
    <span className="aik-ard-art" data-v={kind}>
      {kind === "tide" ? (
        <svg
          viewBox="0 0 160 100"
          preserveAspectRatio="none"
          aria-hidden="true"
        >
          <path
            d="M0 44 C30 30 50 56 82 42 S134 28 160 40 V100 H0Z"
            fill="#ffffff"
            opacity=".12"
          />
          <path
            d="M0 56 C34 44 58 66 92 54 S138 44 160 52 V100 H0Z"
            fill="#ffffff"
            opacity=".1"
          />
        </svg>
      ) : null}
      {kind === "paper" ? (
        <svg
          viewBox="0 0 160 100"
          preserveAspectRatio="xMidYMid slice"
          aria-hidden="true"
        >
          <circle cx="122" cy="34" r="22" fill="#ff7a45" />
          <circle cx="104" cy="50" r="13" fill="#1d1d24" />
        </svg>
      ) : null}
      {kind === "grid" ? (
        <svg
          viewBox="0 0 160 100"
          preserveAspectRatio="xMidYMid slice"
          aria-hidden="true"
        >
          <circle cx="118" cy="30" r="12" fill="#9db1ff" opacity=".9" />
        </svg>
      ) : null}
      {kind === "moss" ? (
        <svg
          viewBox="0 0 160 100"
          preserveAspectRatio="xMidYMid slice"
          aria-hidden="true"
        >
          <rect
            x="100"
            y="14"
            width="40"
            height="40"
            rx="13"
            fill="#eaffc2"
            opacity=".9"
          />
        </svg>
      ) : null}
      <Bars ink={kind === "paper" ? "#1d1d24" : "#ffffff"} />
    </span>
  )
}

function File() {
  const root = React.useRef<HTMLDivElement>(null)
  const [lines, setLines] = React.useState(148)
  const [ready, setReady] = React.useState(true)
  const total = 148

  useAiLoop(
    root,
    async ({ sleep }) => {
      setReady(false)
      setLines(0)
      await sleep(600)
      for (let count = 0; count <= total; count += 4) {
        setLines(count)
        await sleep(count % 36 === 0 ? 240 : 46)
      }
      setLines(total)
      await sleep(350)
      setReady(true)
      await sleep(2200)
    },
    { gap: 700 }
  )

  return (
    <div ref={root} style={{ display: "grid", width: "min(100%, 380px)" }}>
      <AiArtifactFile
        name="pricing-section.tsx"
        detail="React"
        lines={lines}
        progress={lines / total}
        status={ready ? "ready" : "building"}
      />
    </div>
  )
}

const CODE = `export function PricingCard() {
  const [yearly, setYearly] = useState(false)
  return (
    <Card className="w-56">
      <Badge>Popular</Badge>
      <Price value={yearly ? 19 : 24} />
      <FeatureList items={FEATURES} />
      <Button>Start free</Button>
    </Card>
  )
}`

function Preview() {
  const root = React.useRef<HTMLDivElement>(null)
  const [view, setView] = React.useState<AiArtifactView>("preview")
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setView("preview")
      await sleep(2400)
      setView("code")
      await sleep(2600)
      setView("preview")
    },
    { gap: 600, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 420px)" }}
      onPointerDown={() => setHands(true)}
      onKeyDown={() => setHands(true)}
    >
      <AiArtifactPreview
        title="pricing-card.tsx"
        view={view}
        onViewChange={setView}
        code={CODE}
      >
        <div className="aik-ard-price">
          <header>
            <b>Pro</b>
            <span>Popular</span>
          </header>
          <div className="aik-ard-amt">
            <b>$24</b>
            <span>per month</span>
          </div>
          <ul>
            <li>
              <Check size={12} />
              Every component
            </li>
            <li>
              <Check size={12} />
              Templates and updates
            </li>
          </ul>
          <span className="aik-ard-cta">Start free</span>
        </div>
      </AiArtifactPreview>
    </div>
  )
}

const BASE: AiArtifactVersion[] = [
  { id: "dawn", label: "Dawn gradient", art: <Art kind="dawn" /> },
  { id: "tide", label: "Tide", art: <Art kind="tide" /> },
  { id: "grid", label: "Night grid", art: <Art kind="grid" /> },
]
const EXTRA: AiArtifactVersion[] = [
  { id: "paper", label: "Paper cut", art: <Art kind="paper" /> },
  { id: "moss", label: "Moss", art: <Art kind="moss" /> },
]

function Versions() {
  const root = React.useRef<HTMLDivElement>(null)
  const [versions, setVersions] = React.useState<AiArtifactVersion[]>([
    ...BASE,
    EXTRA[0]!,
  ])
  const [value, setValue] = React.useState("tide")
  const [busy, setBusy] = React.useState(false)
  const [hands, setHands] = React.useState(false)
  const timer = React.useRef<number | undefined>(undefined)
  React.useEffect(() => () => window.clearTimeout(timer.current), [])

  useAiLoop(
    root,
    async ({ sleep }) => {
      setVersions(BASE)
      setBusy(false)
      setValue("dawn")
      await sleep(1500)
      setValue("tide")
      await sleep(1400)
      setValue("grid")
      await sleep(1400)
      setBusy(true)
      await sleep(1700)
      setVersions([...BASE, EXTRA[0]!])
      setValue("paper")
      setBusy(false)
      await sleep(2400)
    },
    { gap: 600, enabled: !hands }
  )

  const regenerate = () => {
    setBusy(true)
    window.clearTimeout(timer.current)
    timer.current = window.setTimeout(() => {
      setVersions((list) => {
        const next = [...BASE, ...EXTRA].find(
          (item) => !list.some((have) => have.id === item.id)
        )
        if (!next) return list
        setValue(next.id)
        return [...list, next]
      })
      setBusy(false)
    }, 1500)
  }

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 400px)" }}
      onPointerDown={() => setHands(true)}
      onKeyDown={() => setHands(true)}
    >
      <AiArtifactVersions
        versions={versions}
        value={value}
        onValueChange={setValue}
        regenerating={busy}
        onRegenerate={regenerate}
      />
    </div>
  )
}

export function AiArtifactDemo({ only }: { only?: string }) {
  return (
    <>
      <AiStyle id="ai-artifact-demo" css={CSS} />
      <AiDemo
        only={only}
        tabs={[
          { name: "File", node: <File /> },
          { name: "Preview", node: <Preview /> },
          { name: "Versions", node: <Versions /> },
        ]}
      />
    </>
  )
}

Installation

pnpm dlx shadcn@latest add https://tweenui.dev/r/ai-artifact.json

Usage

import {
  AiArtifactFile,
  AiArtifactPreview,
  AiArtifactVersions,
} from "@/components/ui/ai-artifact"
const [view, setView] = useState<"preview" | "code">("preview")
 
<AiArtifactFile
  name="pricing-section.tsx"
  lines={96}
  progress={0.65}
  status="building"
/>
 
<AiArtifactPreview
  title="pricing-card.tsx"
  view={view}
  onViewChange={setView}
  code={source}
>
  <PricingCard />
</AiArtifactPreview>

Versions

  • AiArtifactFile is a file being written. The line count climbs, a progress line fills along the bottom edge, and when status is ready the building pill gives way to an Open button.
  • AiArtifactPreview shows a result with a Preview and Code toggle. The thumb slides between the two, the arrow keys switch sides, and the panes cross over in place so the frame never changes size.
  • AiArtifactVersions is a strip of generated results. Pick a thumbnail to see it large, or regenerate: the stage shimmers while regenerating is true and the new version pops into the strip.

The demo's pictures are drawn in CSS and inline SVG. art and children accept any node, so pass real images or a live component.

Props

AiArtifactFile

PropTypeDefaultDescription
namestringThe file name.
linesnumberLines written so far.
progressnumberBuild progress from 0 to 1.
status"building" | "ready""building"Ready swaps the pill for an Open button.
detailstringA qualifier after the line count, such as a language.
iconReactNodeReplaces the default file icon.
onOpen() => voidCalled by the Open button.

AiArtifactPreview

PropTypeDefaultDescription
titlestringThe artifact's name in the header.
view"preview" | "code"Which side is showing.
onViewChange(view) => voidCalled by the toggle.
codestringThe source, one line per row.
childrenReactNodeThe rendered result.

AiArtifactVersions

PropTypeDefaultDescription
versions{ id, label, art }[]The results. art fills its frame.
valuestringThe selected version's id.
onValueChange(id: string) => voidCalled when a thumbnail is picked.
onRegenerate() => voidCalled by the regenerate button.
regeneratingbooleanfalseShimmers the stage and spins the button.