Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Sandbox Run for UI SDK

Code an AI agent ran in a sandbox, as a card with a status badge over tabs for its code, console and files. A React component built on UI SDK.

On this page11 sections

Sandbox Run shows code an agent ran in a sandbox as a card: a header with the file's name and a status badge, Running, Completed or Error, over tabs for the code, what it printed and the files it wrote. The header folds the card, so a finished run can step aside for the reply under it.

It only displays a run that happened elsewhere. To let people edit code and run it in the page, use Sandbox, and for a shell command's streaming output, use Terminal.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Sandbox Run anatomy: root, header, tabs list, tabs bar, tab content

Usage

import {
  SandboxRun, SandboxRunContent, SandboxRunHeader, SandboxRunTabContent,
  SandboxRunTabs, SandboxRunTabsBar, SandboxRunTabsList,
  SandboxRunTabsTrigger,
} from "@/components/ui/sandbox-run";

SandboxRunHeader needs a state, and each SandboxRunTabsTrigger shows the SandboxRunTabContent with the same value. The run starts open.

<SandboxRun>
  <SandboxRunHeader state="completed" title="Code execution" />
  <SandboxRunContent>
    <SandboxRunTabs defaultValue="code">
      <SandboxRunTabsBar>
        <SandboxRunTabsList>
          <SandboxRunTabsTrigger value="code">Code</SandboxRunTabsTrigger>
          <SandboxRunTabsTrigger value="console">Console</SandboxRunTabsTrigger>
        </SandboxRunTabsList>
      </SandboxRunTabsBar>
      <SandboxRunTabContent value="code">
        <pre className="p-3 font-mono text-xs">console.log(fibonacci(10));</pre>
      </SandboxRunTabContent>
      <SandboxRunTabContent value="console">
        <pre className="p-3 font-mono text-xs">55</pre>
      </SandboxRunTabContent>
    </SandboxRunTabs>
  </SandboxRunContent>
</SandboxRun>

Accessibility

The header is a <button> with aria-expanded, named by its title and then the badge's word, analyze.py Completed, since the badge's icon and pulsing dot are hidden from screen readers. Nothing announces a change of state, so a reader finds out a run finished by going back to the header. SandboxRunTabsList is a tablist, each trigger a tab and each panel a tabpanel, with only the selected tab in the tab order. Give an icon button in SandboxRunTabsBar an aria-label, as the Files demo does for its copy button.

Tab
Moves focus from the header to the selected tab, then to any button after the tabs in the bar, then into the open panel.
Shift+Tab
Moves focus back the same way.
Enter
When focus is on the header, opens or closes the run. When focus is on a tab, shows its panel.
Space
When focus is on the header, opens or closes the run. When focus is on a tab, shows its panel.
Arrow Right
When focus is on a tab, moves focus to the next tab and shows its panel, wrapping from the last to the first.
Arrow Left
When focus is on a tab, moves focus to the previous tab and shows its panel, wrapping from the first to the last.
Home
When focus is on a tab, moves focus to the first tab and shows its panel.
End
When focus is on a tab, moves focus to the last tab and shows its panel.

Demos

States

Three Python runs, one per state, all closed through defaultOpen set to false. train.py is still running with a pulsing dot in its badge, sum.py is completed with a check, and load.py ended in an error with a cross and prints its traceback in a destructive Code Block. Close finished runs when the reply under them matters more.

Preview width

Files

One pandas run, analyze.py, with four tabs: the code, a table of revenue by region, the console and the two files it wrote, summary.csv and chart.png. It opens on Output, the tab that answers the question, and a copy button for the code sits at the end of SandboxRunTabsBar. Leave out a tab the run has nothing for.

Preview width

Streaming

Press Run again and check-endpoints.js prints a console line every 700 ms, with state at running until the last line and completed after it. Stream into the console tab as the sandbox reports, and switch the state only when the run ends, so the badge never says Completed over a console that is still filling.

Preview width

AI SDK

A code run is a tool call, runCode in this example, and SandboxRunHeader takes the tool part's state as it is, so the badge follows the part without a mapping of yours. The tool's input holds the code, and its output what the run printed.

{message.parts.map((part) =>
  part.type === "tool-runCode" ? (
    <SandboxRun key={part.toolCallId}>
      <SandboxRunHeader
        state={
          part.state === "output-available" && part.preliminary
            ? "running"
            : part.state
        }
        title={part.input?.filename}
      />
      <SandboxRunContent>{/* the tabs, from part.input and part.output */}</SandboxRunContent>
    </SandboxRun>
  ) : null
)}
stateBadge
input-streamingPending
running, input-availableRunning
approval-requestedAwaiting approval
approval-respondedResponded
completed, output-availableCompleted
error, output-errorError
output-deniedDenied

A tool that streams its console by yielding from execute sends each yield as output-available with preliminary: true, which the badge would read as Completed, so the header needs running until a part arrives without that flag.

Migrating from AI SDK Elements

If you added Sandbox from AI SDK Elements (opens in a new tab), your project has a components/ai-elements/sandbox.tsx. Here its parts are Sandbox Run's, because Sandbox is the editor that runs code in the page. Run the command in the Installation section, then import from @/components/ui/sandbox-run and rename each part: Sandbox is SandboxRun, SandboxHeader is SandboxRunHeader, SandboxContent is SandboxRunContent, SandboxTabs is SandboxRunTabs, SandboxTabsBar is SandboxRunTabsBar, SandboxTabsList is SandboxRunTabsList, SandboxTabsTrigger is SandboxRunTabsTrigger and SandboxTabContent is SandboxRunTabContent. To leave your JSX as it is, import them under the old names: import { SandboxRun as Sandbox, … }.

The props stay: title and state on the header, which takes a tool part's state as before, and value and defaultValue on the tabs. onValueChange on SandboxRunTabs gets the tab's value.

FAQ

What's the difference between Sandbox Run and Sandbox?

Sandbox Run shows code that already ran somewhere else, in an agent's sandbox, with what came out of it, and nothing runs in the page. Sandbox is an HTML, CSS and JavaScript playground, where the reader edits the files and sees them run in a sandboxed frame.

Their parts are named apart, SandboxRun and its parts against Sandbox and its own, so one page can hold both: the run the agent made, and an editor to try a change to it.

Which states does the Sandbox Run header show?

Three of its own, running, completed and error, as a badge with a pulsing dot, a check or a cross. It also takes an AI SDK tool part's state as it is, reading input-available as Running and output-available as Completed, and shows Pending, Awaiting approval, Responded or Denied for the states a run can't name. The dot stops pulsing for people who ask for reduced motion.

A run whose chat stopped mid-call stays input-available, so its badge keeps saying Running. Pass error once the chat has stopped and the part never got an output.

How do I start a Sandbox Run closed, or close it when the run ends?

Set defaultOpen to false on SandboxRun, since it starts open, as the States demo does. To close it from your code, hold the open state yourself and pass it as open, updating it from onOpenChange.

Closing a run when the reply under it starts keeps the answer in view, and the header still shows how the run ended. Leave a failed run open, so the traceback is the first thing people see.

Which tabs should a Sandbox Run have?

The ones the run has something for: the code, then what came out of it, as an output, a console or files. Name each in a SandboxRunTabsTrigger and give its SandboxRunTabContent the same value, and open on the tab that answers the question with defaultValue.

SandboxRunTabsBar holds the list and anything after it, such as a copy or download button, as the Files demo shows. An empty Files tab is a click for nothing, so leave it out.

How do I highlight the code in a Sandbox Run?

Put a Code Block in the code tab and fill it from useHighlightedCode, one CodeBlockLine per line, as every demo does. Give the block a rounded-none border-0 class so it sits flush inside the run's own border.

Sandbox Run doesn't highlight anything itself. Pass the hook the language the sandbox ran, python or js in the demos, and leave the console as plain text in a Code Block of its own, as they do.

Why does my Sandbox Run say Completed while the console is still streaming?

Because the AI SDK sends each value a tool yields as output-available with preliminary: true, and the header reads output-available as Completed. The run is done only when a part arrives without that flag.

Pass running to SandboxRunHeader while preliminary is set, as the AI SDK section's code does, and the part's own state after. The Streaming demo shows what to aim for: Running while lines arrive, and Completed once the last one is in.

Can people use a Sandbox Run with the keyboard?

Yes. The header is a button with aria-expanded, so Tab reaches it and Enter or Space opens or closes the run. Tab then moves to the selected tab, where the arrow keys move between tabs and show each one's panel.

A Code Block in a panel is a tab stop of its own, so the arrow keys scroll long code once it has focus. A button in SandboxRunTabsBar, such as the copy button in the Files demo, comes between the tabs and the panel in the tab order.

API Reference

SandboxRun

data-slot="sandbox-run"

PropType
open?boolean

Whether the panel is open. Pass it to control the collapsible.

defaultOpen?boolean

Whether the panel is open at first, when uncontrolled. Collapsible's default: false.

Default
true
onOpenChange?(open: boolean, details: ChangeDetails<CollapsibleOpenChangeReason>) → void

Runs when the panel wants to open or close. details.reason says why, and details.cancel() stops it.

Type
(open: boolean, details: ChangeDetails<CollapsibleOpenChangeReason>) → void
Parameters

open – boolean

details – ChangeDetails<CollapsibleOpenChangeReason>

Returns
void
controller?CollapsibleController

Opens and closes the panel from anywhere. Create one with createCollapsibleController().

debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops the trigger from opening or closing the panel. The trigger stays focusable.

Default
false
onOpenChangeComplete?(open: boolean) → void

Runs once the panel has finished opening or closing, after any animation.

Parameters

open – boolean

Returns
void
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

SandboxRunContent

data-slot="sandbox-run-content"

PropType
disableAnimation?boolean

Opens and closes it at once, without the height animation.

hiddenUntilFound?boolean

Keeps the closed panel in the page as hidden="until-found", so the browser's find in page can match its text and open it.

Default
false
keepMounted?boolean

Whether the panel stays in the page, hidden, while closed.

Default
false
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

SandboxRunHeader

data-slot="sandbox-run-header"

PropType
stateSandboxRunState

Where the run is, shown as a badge after the title.

title?string

What ran, in a few words: "Code execution", "analyze.py".

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-state?string

Mirrors state.

SandboxRunTabContent

data-slot="sandbox-run-tab-content"

PropType
valuestring

The value of the tab this panel belongs to.

hiddenUntilFound?boolean

Whether the panel stays findable with hidden="until-found" while its tab isn't selected. Overrides the tabs'.

keepMounted?boolean

Whether the panel stays in the page, hidden, while its tab isn't selected.

Default
false
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

SandboxRunTabs

data-slot="sandbox-run-tabs"

PropType
value?T | null

The selected tab's value. Pass it to control the tabs. null selects nothing.

Type
T | null
defaultValue?T | null

The tab selected at first, when uncontrolled. Defaults to the first enabled tab, which never calls onValueChange.

Type
T | null
onValueChange?(value: string) → void

Called with the tab chosen.

Parameters

value – string

Returns
void
activation?"automatic" | "manual"

"automatic" selects a tab as soon as arrow keys focus it. "manual" waits for Enter or Space.

Type
"automatic" | "manual"
Default
"automatic"
controller?{ getValue: () → T | null; setValue: (value: T | null, options?: TabsChangeOptions) → boolean; select: (value: T, options?: TabsChangeOptions) → boolean; focus: () → void; }

Selects tabs from anywhere. Create one with createTabsController().

Type
{ getValue: () → T | null; setValue: (value: T | null, options?: TabsChangeOptions) → boolean; select: (value: T, options?: TabsChangeOptions) → boolean; focus: () → void; }
debug?boolean

Logs every state change with its reason to the console, in development builds.

hiddenUntilFound?boolean

Keeps hidden panels in the page as hidden="until-found", so the browser's find in page can match their text and select their tab. Implies keepMounted.

Default
false
loop?boolean

Whether arrow keys wrap from the last tab to the first.

Default
true
orientation?TabsOrientation

Which arrow keys move between tabs: left and right, or up and down.

Default
"horizontal"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

SandboxRunTabsBar

<div> data-slot="sandbox-run-tabs-bar"

PropType
className?string

Merged with the part's classes.

SandboxRunTabsList

data-slot="sandbox-run-tabs-list"

PropType
activateOnFocus?boolean

Select a tab as soon as the arrow keys focus it; otherwise Enter or Space selects it. Sets the tabs' activation.

Default
true
loopFocus?boolean

Wrap the arrow keys from the last tab to the first. Sets the tabs' loop.

variant?"default" | "line"

TabsList's default: default.

Type
"default" | "line"
Default
"line"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

SandboxRunTabsTrigger

data-slot="sandbox-run-tabs-trigger"

PropType
valuestring

Identifies the tab and pairs it with the panel of the same value.

disabled?boolean

Keeps the tab focusable but stops it from being selected, as the tabs pattern asks.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.