{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "artifact-panel",
  "title": "Artifact panel",
  "description": "Side panel for what an AI agent made: Preview and Code tabs, a version switcher with a note per version, copy, and code that streams in while the agent writes.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "registry/components/artifact-panel.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport { Check, ChevronLeft, ChevronRight, Copy, FileCode2 } from \"lucide-react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\n/** One saved version of the artifact. */\nexport type ArtifactVersion = {\n  /** Shown in the switcher; defaults to \"v1\", \"v2\", ... */\n  label?: string;\n  /** Source shown on the Code tab. */\n  code: string;\n  /** What the Preview tab renders. Leave unset to show the code only. */\n  preview?: React.ReactNode;\n  /** One line on what changed in this version. */\n  note?: string;\n};\n\nexport type ArtifactTab = \"preview\" | \"code\";\n\nexport type ArtifactPanelProps = {\n  /** File name or title in the header. */\n  title?: string;\n  /** Small chip next to the title, e.g. \"Component\", \"Document\". */\n  kind?: string;\n  versions?: ArtifactVersion[];\n  /** Controlled version index. */\n  version?: number;\n  /** Starting version index; defaults to the newest. */\n  defaultVersion?: number;\n  onVersionChange?: (index: number) => void;\n  tab?: ArtifactTab;\n  defaultTab?: ArtifactTab;\n  onTabChange?: (tab: ArtifactTab) => void;\n  /** True while the newest version's code is still streaming in. */\n  streaming?: boolean;\n  /** Play a scripted edit on a loop while on screen, for demos. */\n  autoPlay?: boolean;\n  /** Fill the parent's height instead of a fixed 256px body, e.g. as a side panel. */\n  fill?: boolean;\n  className?: string;\n};\n\nconst V1 = `export function PricingCard() {\n  return (\n    <div className=\"rounded-xl border p-5\">\n      <p className=\"text-sm text-muted-foreground\">Pro</p>\n      <p className=\"text-3xl font-semibold\">$12</p>\n      <button className=\"mt-4 w-full rounded-md bg-foreground py-2 text-background\">\n        Upgrade\n      </button>\n    </div>\n  );\n}`;\n\nconst V2 = `export function PricingCard() {\n  return (\n    <div className=\"rounded-xl border p-5\">\n      <p className=\"text-sm text-muted-foreground\">Pro</p>\n      <p className=\"text-3xl font-semibold\">\n        $12<span className=\"text-sm text-muted-foreground\">/month</span>\n      </p>\n      <ul className=\"mt-3 space-y-1 text-sm\">\n        <li>Unlimited projects</li>\n        <li>Priority support</li>\n      </ul>\n      <button className=\"mt-4 w-full rounded-md bg-foreground py-2 text-background\">\n        Upgrade\n      </button>\n    </div>\n  );\n}`;\n\nfunction DemoCard({ v2 }: { v2?: boolean }) {\n  return (\n    <div className=\"w-48 rounded-xl border bg-card p-4 text-left shadow-sm\">\n      <p className=\"text-xs text-muted-foreground\">Pro</p>\n      <p className=\"text-2xl font-semibold tracking-tight\">\n        $12{v2 && <span className=\"text-xs font-normal text-muted-foreground\">/month</span>}\n      </p>\n      {v2 && (\n        <ul className=\"mt-2 space-y-1 text-xs\">\n          <li className=\"flex items-center gap-1.5\">\n            <Check className=\"size-3\" aria-hidden /> Unlimited projects\n          </li>\n          <li className=\"flex items-center gap-1.5\">\n            <Check className=\"size-3\" aria-hidden /> Priority support\n          </li>\n        </ul>\n      )}\n      <span className=\"mt-3 block rounded-md bg-foreground py-1.5 text-center text-xs font-medium text-background\">Upgrade</span>\n    </div>\n  );\n}\n\nconst DEMO: ArtifactVersion[] = [\n  { code: V1, preview: <DemoCard />, note: \"First draft\" },\n  { code: V2, preview: <DemoCard v2 />, note: \"Added price period and features\" },\n];\n\n// Demo loop, in ms: stream v2's code, switch to the preview, hold, start over.\nconst START = 600;\nconst CHAR_MS = 7;\nconst TICK = 50;\n\n/** A side panel for what the agent made: preview and code tabs, version history, copy. */\nexport function ArtifactPanel({\n  title = \"pricing-card.tsx\",\n  kind = \"Component\",\n  versions: versionsProp,\n  version: versionProp,\n  defaultVersion,\n  onVersionChange,\n  tab: tabProp,\n  defaultTab = \"preview\",\n  onTabChange,\n  streaming: streamingProp = false,\n  autoPlay = false,\n  fill = false,\n  className,\n}: ArtifactPanelProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const codeRef = useRef<HTMLPreElement>(null);\n  const reduce = useReducedMotion();\n  const versions = versionsProp ?? DEMO;\n  const last = Math.max(0, versions.length - 1);\n\n  const [visible, setVisible] = useState(false);\n  const [tick, setTick] = useState(0);\n  const [innerVersion, setInnerVersion] = useState<number | null>(defaultVersion ?? null);\n  const [innerTab, setInnerTab] = useState<ArtifactTab | null>(autoPlay ? null : defaultTab);\n  const [copied, setCopied] = useState(false);\n\n  const STREAM_END = START + versions[last].code.length * CHAR_MS;\n  const SWITCH = STREAM_END + 700;\n  const LOOP = SWITCH + 3600;\n\n  useEffect(() => {\n    const el = ref.current;\n    if (!el || !autoPlay) return;\n    const io = new IntersectionObserver(([entry]) => setVisible(entry.isIntersecting));\n    io.observe(el);\n    return () => io.disconnect();\n  }, [autoPlay]);\n\n  useEffect(() => {\n    if (!autoPlay || !visible || reduce) return;\n    let t = 0;\n    const id = setInterval(() => {\n      t = (t + TICK) % LOOP;\n      setTick(t);\n      // A viewer's clicks hold until the loop starts over.\n      if (t === 0) {\n        setInnerTab(null);\n        setInnerVersion(null);\n      }\n    }, TICK);\n    return () => clearInterval(id);\n  }, [autoPlay, visible, reduce, LOOP]);\n\n  const playing = autoPlay && !reduce;\n  const t = playing ? tick : LOOP - 1;\n\n  const index = Math.min(last, versionProp ?? innerVersion ?? last);\n  const current = versions[index];\n  const streaming = autoPlay ? playing && index === last && t < STREAM_END : streamingProp && index === last;\n  const scriptedTab: ArtifactTab = t < SWITCH ? \"code\" : \"preview\";\n  const tab: ArtifactTab = tabProp ?? innerTab ?? (autoPlay ? scriptedTab : defaultTab);\n  const code = autoPlay && streaming ? current.code.slice(0, Math.max(0, Math.floor((t - START) / CHAR_MS))) : current.code;\n  const hasPreview = current.preview !== undefined;\n  const shownTab: ArtifactTab = hasPreview ? tab : \"code\";\n\n  // Follow the newest line while code streams in.\n  useEffect(() => {\n    const el = codeRef.current;\n    if (el && streaming) el.scrollTop = el.scrollHeight;\n  }, [code, streaming]);\n\n  function pickVersion(next: number) {\n    const clamped = Math.max(0, Math.min(last, next));\n    if (versionProp === undefined) setInnerVersion(clamped);\n    onVersionChange?.(clamped);\n  }\n\n  function pickTab(next: ArtifactTab) {\n    if (tabProp === undefined) setInnerTab(next);\n    onTabChange?.(next);\n  }\n\n  async function copy() {\n    try {\n      await navigator.clipboard.writeText(current.code);\n      setCopied(true);\n      setTimeout(() => setCopied(false), 1600);\n    } catch {\n      // Clipboard can be blocked; nothing to undo.\n    }\n  }\n\n  const lines = code.split(\"\\n\");\n\n  return (\n    <div ref={ref} className={cn(\"w-full max-w-lg rounded-xl bg-muted p-1.5 text-foreground\", fill && \"flex h-full max-w-none flex-col\", className)}>\n      <div className={cn(\"overflow-hidden rounded-lg border bg-card shadow-xs\", fill && \"flex min-h-0 flex-1 flex-col\")}>\n        <div className=\"flex items-center gap-2 border-b px-3 py-2\">\n          <FileCode2 className=\"size-4 shrink-0 text-muted-foreground\" aria-hidden />\n          <span className=\"min-w-0 truncate font-mono text-[13px] font-medium\">{title}</span>\n          {kind && <span className=\"shrink-0 rounded-md bg-muted px-1.5 py-0.5 text-[11px] text-muted-foreground\">{kind}</span>}\n          <span className=\"flex-1\" />\n\n          {versions.length > 1 && (\n            <div className=\"flex items-center rounded-md border text-xs\">\n              <button\n                type=\"button\"\n                onClick={() => pickVersion(index - 1)}\n                disabled={index === 0}\n                aria-label=\"Previous version\"\n                className=\"grid size-6 place-items-center rounded-l-md text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-40 disabled:hover:bg-transparent\"\n              >\n                <ChevronLeft className=\"size-3.5\" aria-hidden />\n              </button>\n              <span className=\"min-w-12 px-1 text-center font-mono text-[11px] tabular-nums\" aria-live=\"polite\">\n                {current.label ?? `v${index + 1}`}\n                <span className=\"text-muted-foreground\"> / {versions.length}</span>\n              </span>\n              <button\n                type=\"button\"\n                onClick={() => pickVersion(index + 1)}\n                disabled={index === last}\n                aria-label=\"Next version\"\n                className=\"grid size-6 place-items-center rounded-r-md text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-40 disabled:hover:bg-transparent\"\n              >\n                <ChevronRight className=\"size-3.5\" aria-hidden />\n              </button>\n            </div>\n          )}\n\n          <button\n            type=\"button\"\n            onClick={copy}\n            aria-label={copied ? \"Copied\" : \"Copy code\"}\n            className=\"grid size-7 place-items-center rounded-md text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n          >\n            {copied ? <Check className=\"size-3.5\" aria-hidden /> : <Copy className=\"size-3.5\" aria-hidden />}\n          </button>\n        </div>\n\n        {hasPreview && (\n          <div role=\"tablist\" aria-label=\"View\" className=\"flex gap-1 border-b px-2 py-1.5\">\n            {([\"preview\", \"code\"] as const).map((name) => (\n              <button\n                key={name}\n                type=\"button\"\n                role=\"tab\"\n                aria-selected={shownTab === name}\n                onClick={() => pickTab(name)}\n                className={cn(\n                  \"relative h-7 rounded-md px-2.5 text-xs font-medium capitalize outline-none transition-colors focus-visible:ring-2 focus-visible:ring-ring\",\n                  shownTab === name ? \"text-foreground\" : \"text-muted-foreground hover:text-foreground\",\n                )}\n              >\n                {shownTab === name && (\n                  <motion.span\n                    layoutId={reduce ? undefined : `artifact-tab-${title}`}\n                    className=\"absolute inset-0 rounded-md bg-muted\"\n                    transition={{ type: \"spring\", stiffness: 500, damping: 38 }}\n                    aria-hidden\n                  />\n                )}\n                <span className=\"relative\">{name}</span>\n              </button>\n            ))}\n            {streaming && (\n              <span className=\"ml-auto flex items-center gap-1.5 pr-1 text-[11px] text-muted-foreground\">\n                <span className=\"size-1.5 animate-pulse rounded-full bg-foreground\" aria-hidden />\n                Writing {current.label ?? `v${index + 1}`}\n              </span>\n            )}\n          </div>\n        )}\n\n        <div className={cn(\"relative\", fill ? \"min-h-0 flex-1\" : \"h-64\")}>\n          <AnimatePresence mode=\"wait\" initial={false}>\n            {shownTab === \"preview\" ? (\n              <motion.div\n                key={`preview-${index}`}\n                initial={reduce ? false : { opacity: 0, scale: 0.98 }}\n                animate={{ opacity: 1, scale: 1 }}\n                exit={reduce ? undefined : { opacity: 0 }}\n                transition={{ duration: 0.25, ease: [0.22, 1, 0.36, 1] }}\n                className={cn(\n                  \"absolute inset-0 overflow-auto bg-[radial-gradient(var(--border)_1px,transparent_1px)] [background-size:14px_14px] p-4\",\n                  // A tall preview scrolls from its top; centring it would cut off the top.\n                  fill ? \"grid justify-items-center\" : \"grid place-items-center\",\n                )}\n              >\n                {current.preview}\n              </motion.div>\n            ) : (\n              <motion.pre\n                key={`code-${index}`}\n                ref={codeRef}\n                initial={reduce ? false : { opacity: 0 }}\n                animate={{ opacity: 1 }}\n                exit={reduce ? undefined : { opacity: 0 }}\n                transition={{ duration: 0.2 }}\n                className=\"absolute inset-0 overflow-auto py-3 font-mono text-[12px] leading-[1.6] [scrollbar-width:thin]\"\n              >\n                <code className=\"grid grid-cols-[auto_1fr]\">\n                  {lines.map((line, i) => (\n                    <span key={i} className=\"contents\">\n                      <span className=\"select-none pr-4 pl-3 text-right text-muted-foreground/60 tabular-nums\">{i + 1}</span>\n                      <span className=\"pr-4 whitespace-pre\">\n                        {line}\n                        {streaming && i === lines.length - 1 && (\n                          <span aria-hidden className=\"ml-px inline-block h-3.5 w-1.5 translate-y-0.5 animate-pulse bg-foreground/60\" />\n                        )}\n                      </span>\n                    </span>\n                  ))}\n                </code>\n              </motion.pre>\n            )}\n          </AnimatePresence>\n        </div>\n\n        {current.note && (\n          <div className=\"border-t px-3 py-2 text-xs text-muted-foreground\">\n            <span className=\"font-mono text-foreground\">{current.label ?? `v${index + 1}`}</span> · {current.note}\n          </div>\n        )}\n      </div>\n    </div>\n  );\n}\n\nexport default function ArtifactPanelDemo() {\n  return <ArtifactPanel autoPlay />;\n}\n",
      "type": "registry:component",
      "target": "components/nextui/artifact-panel.tsx"
    }
  ],
  "meta": {
    "kind": "component",
    "category": "ai",
    "previewHeight": 460,
    "new": true,
    "free": true,
    "openSource": true,
    "added": "2026-10-31"
  },
  "categories": [
    "ai"
  ],
  "type": "registry:component"
}