Skip to content

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

Components

Free

Sandbox Run for React Aria

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 React Aria.

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 defaultExpanded 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.

SandboxRun is a React Aria Disclosure, so it takes defaultExpanded, isExpanded and onExpandedChange where AI Elements' takes defaultOpen, open and onOpenChange.

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 defaultExpanded to false on SandboxRun, since it starts expanded, as the States demo does. To close it from your code, hold the state yourself and pass it as isExpanded, updating it from onExpandedChange.

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

PropType
defaultExpanded?boolean

Whether the disclosure is expanded by default (uncontrolled).

Default
true
onExpandedChange?(isExpanded: boolean) → void

Handler that is called when the disclosure expanded state changes.

Parameters

isExpanded – boolean

Returns
void
isExpanded?boolean

Whether the disclosure is expanded (controlled).

className?string

Merged with the part's classes.

SandboxRunContent

PropType
childrenReactNode

The children of the component.

disableAnimation?boolean

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

labelElementType?ElementType

The HTML element used to render the label, e.g. label, or span.

Default
"label"
role?"group" | "region"

The accessibility role for the disclosure's panel.

Type
"group" | "region"
Default
"group"
style?object

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element.

Type
{ prop?: string]: string | number }
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
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.

isDisabled?boolean

Whether the button is disabled.

title?string

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

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 that shows it.

children?ChildrenOrFunction<T>

The children of the component. A function may be provided to alter the children based on component state.

Type
ChildrenOrFunction<T>
shouldForceMount?boolean

Whether to mount the tab panel in the DOM even when it is not currently selected. Inactive tab panels are inert and cannot be interacted with. They must be styled appropriately so this is clear to the user visually.

Default
false
style?StyleOrFunction<T>

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.

Type
StyleOrFunction<T>
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
className?string

Merged with the part's classes.

SandboxRunTabs

data-slot="sandbox-run-tabs"

PropType
value?string

The tab shown, controlled.

defaultValue?string

The tab shown first, uncontrolled.

onValueChange?(value: string) → void

Called with the tab chosen.

Parameters

value – string

Returns
void
children?ChildrenOrFunction<T>

The children of the component. A function may be provided to alter the children based on component state.

Type
ChildrenOrFunction<T>
isDisabled?boolean

Whether the TabList is disabled. Shows that a selection exists, but is not available in that circumstance.

keyboardActivation?"automatic" | "manual"

Whether tabs are activated automatically on focus or manually.

Type
"automatic" | "manual"
Default
"automatic"
orientation?"horizontal" | "vertical"

The orientation of the tabs.

Type
"horizontal" | "vertical"
Default
"horizontal"
slot?string | null

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Type
string | null
Default
sandbox-run-tabs
style?StyleOrFunction<T>

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.

Type
StyleOrFunction<T>
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
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
defaultSelectedKey?Key

The initial selected keys in the collection (uncontrolled).

isDisabled?boolean

Whether the TabList is disabled. Shows that a selection exists, but is not available in that circumstance.

onSelectionChange?(key: Key) → void

Handler that is called when the selection changes.

Parameters

key – Key

Returns
void
selectedKey?Key

The currently selected key in the collection (controlled).

variant?"default" | "line"

Ark default: default.

Type
"default" | "line"
Default
"line"
className?string

Merged with the part's classes.

SandboxRunTabsTrigger

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

PropType
valuestring

The tab's value: the value of the content it shows.

children?ChildrenOrFunction<T>

The children of the component. A function may be provided to alter the children based on component state.

Type
ChildrenOrFunction<T>
isDisabled?boolean

Whether the tab is disabled.

style?StyleOrFunction<T>

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.

Type
StyleOrFunction<T>
render?(props: DetailedHTMLProps<LinkWithRequiredHref, HTMLAnchorElement> | React.JSX.IntrinsicElements[Fallback], renderProps: T) → ReactElement

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Note: You can check if 'href' in props in order to tell whether to render an <a> element. Requirements: - You must render the expected element type (e.g. if <a> is expected, you cannot render a <button>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
(props: DetailedHTMLProps<LinkWithRequiredHref, HTMLAnchorElement> | React.JSX.IntrinsicElements[Fallback], renderProps: T) → ReactElement
Parameters

props – DetailedHTMLProps<LinkWithRequiredHref

HTMLAnchorElement> | React.JSX.IntrinsicElements[Fallback]

renderProps – T

Returns
ReactElement
className?string

Merged with the part's classes.