|
| 1 | +--- |
| 2 | +title: Sandbox |
| 3 | +description: A collapsible container for displaying AI-generated code and output in chat interfaces. |
| 4 | +path: elements/components/sandbox |
| 5 | +--- |
| 6 | + |
| 7 | +The `Sandbox` component provides a structured way to display AI-generated code alongside its execution output in chat conversations. It features a collapsible container with status indicators and tabbed navigation between code and output views. |
| 8 | + |
| 9 | +<Preview path="sandbox" /> |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +<ElementsInstaller path="sandbox" /> |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- Collapsible container with smooth animations |
| 18 | +- Status badges showing execution state (Pending, Running, Completed, Error) |
| 19 | +- Tabs for Code and Output views |
| 20 | +- Syntax-highlighted code display |
| 21 | +- Copy button for easy code sharing |
| 22 | +- Works with AI SDK tool state patterns |
| 23 | + |
| 24 | +## Usage with AI SDK |
| 25 | + |
| 26 | +The Sandbox component integrates with the AI SDK's tool state to show code generation progress: |
| 27 | + |
| 28 | +```tsx title="components/code-sandbox.tsx" |
| 29 | +"use client"; |
| 30 | + |
| 31 | +import type { ToolUIPart } from "ai"; |
| 32 | +import { |
| 33 | + Sandbox, |
| 34 | + SandboxCode, |
| 35 | + SandboxContent, |
| 36 | + SandboxHeader, |
| 37 | + SandboxOutput, |
| 38 | + SandboxTabContent, |
| 39 | + SandboxTabs, |
| 40 | + SandboxTabsBar, |
| 41 | + SandboxTabsList, |
| 42 | + SandboxTabsTrigger, |
| 43 | +} from "@/components/ai-elements/sandbox"; |
| 44 | + |
| 45 | +type CodeSandboxProps = { |
| 46 | + toolPart: ToolUIPart; |
| 47 | +}; |
| 48 | + |
| 49 | +export const CodeSandbox = ({ toolPart }: CodeSandboxProps) => { |
| 50 | + const code = toolPart.input?.code ?? ""; |
| 51 | + const output = toolPart.output?.logs ?? ""; |
| 52 | + |
| 53 | + return ( |
| 54 | + <Sandbox> |
| 55 | + <SandboxHeader |
| 56 | + state={toolPart.state} |
| 57 | + title={toolPart.input?.filename ?? "code.tsx"} |
| 58 | + /> |
| 59 | + <SandboxContent> |
| 60 | + <SandboxTabs defaultValue="code"> |
| 61 | + <SandboxTabsBar> |
| 62 | + <SandboxTabsList> |
| 63 | + <SandboxTabsTrigger value="code">Code</SandboxTabsTrigger> |
| 64 | + <SandboxTabsTrigger value="output">Output</SandboxTabsTrigger> |
| 65 | + </SandboxTabsList> |
| 66 | + </SandboxTabsBar> |
| 67 | + <SandboxTabContent value="code"> |
| 68 | + <SandboxCode code={code} language="tsx" /> |
| 69 | + </SandboxTabContent> |
| 70 | + <SandboxTabContent value="output"> |
| 71 | + <SandboxOutput code={output} /> |
| 72 | + </SandboxTabContent> |
| 73 | + </SandboxTabs> |
| 74 | + </SandboxContent> |
| 75 | + </Sandbox> |
| 76 | + ); |
| 77 | +}; |
| 78 | +``` |
| 79 | + |
| 80 | +## Props |
| 81 | + |
| 82 | +### `<Sandbox />` |
| 83 | + |
| 84 | +<TypeTable |
| 85 | + type={{ |
| 86 | + "...props": { |
| 87 | + description: |
| 88 | + "Any other props are spread to the underlying Collapsible component.", |
| 89 | + type: "React.ComponentProps<typeof Collapsible>", |
| 90 | + }, |
| 91 | + }} |
| 92 | +/> |
| 93 | + |
| 94 | +### `<SandboxHeader />` |
| 95 | + |
| 96 | +<TypeTable |
| 97 | + type={{ |
| 98 | + title: { |
| 99 | + description: "The title displayed in the header (e.g., filename).", |
| 100 | + type: "string", |
| 101 | + default: "undefined", |
| 102 | + }, |
| 103 | + state: { |
| 104 | + description: |
| 105 | + "The current execution state, used to display the appropriate status badge.", |
| 106 | + type: 'ToolUIPart["state"]', |
| 107 | + required: true, |
| 108 | + }, |
| 109 | + className: { |
| 110 | + description: "Additional CSS classes for the header.", |
| 111 | + type: "string", |
| 112 | + }, |
| 113 | + }} |
| 114 | +/> |
| 115 | + |
| 116 | +### `<SandboxContent />` |
| 117 | + |
| 118 | +<TypeTable |
| 119 | + type={{ |
| 120 | + "...props": { |
| 121 | + description: "Any other props are spread to the CollapsibleContent.", |
| 122 | + type: "React.ComponentProps<typeof CollapsibleContent>", |
| 123 | + }, |
| 124 | + }} |
| 125 | +/> |
| 126 | + |
| 127 | +### `<SandboxTabs />` |
| 128 | + |
| 129 | +<TypeTable |
| 130 | + type={{ |
| 131 | + "...props": { |
| 132 | + description: |
| 133 | + "Any other props are spread to the underlying Tabs component.", |
| 134 | + type: "React.ComponentProps<typeof Tabs>", |
| 135 | + }, |
| 136 | + }} |
| 137 | +/> |
| 138 | + |
| 139 | +### `<SandboxTabsBar />` |
| 140 | + |
| 141 | +<TypeTable |
| 142 | + type={{ |
| 143 | + "...props": { |
| 144 | + description: "Any other props are spread to the container div.", |
| 145 | + type: "React.HTMLAttributes<HTMLDivElement>", |
| 146 | + }, |
| 147 | + }} |
| 148 | +/> |
| 149 | + |
| 150 | +### `<SandboxTabsList />` |
| 151 | + |
| 152 | +<TypeTable |
| 153 | + type={{ |
| 154 | + "...props": { |
| 155 | + description: |
| 156 | + "Any other props are spread to the underlying TabsList component.", |
| 157 | + type: "React.ComponentProps<typeof TabsList>", |
| 158 | + }, |
| 159 | + }} |
| 160 | +/> |
| 161 | + |
| 162 | +### `<SandboxTabsTrigger />` |
| 163 | + |
| 164 | +<TypeTable |
| 165 | + type={{ |
| 166 | + "...props": { |
| 167 | + description: |
| 168 | + "Any other props are spread to the underlying TabsTrigger component.", |
| 169 | + type: "React.ComponentProps<typeof TabsTrigger>", |
| 170 | + }, |
| 171 | + }} |
| 172 | +/> |
| 173 | + |
| 174 | +### `<SandboxTabContent />` |
| 175 | + |
| 176 | +<TypeTable |
| 177 | + type={{ |
| 178 | + "...props": { |
| 179 | + description: |
| 180 | + "Any other props are spread to the underlying TabsContent component.", |
| 181 | + type: "React.ComponentProps<typeof TabsContent>", |
| 182 | + }, |
| 183 | + }} |
| 184 | +/> |
| 185 | + |
| 186 | +### `<SandboxCode />` |
| 187 | + |
| 188 | +Renders a syntax-highlighted code block with a built-in copy button. |
| 189 | + |
| 190 | +<TypeTable |
| 191 | + type={{ |
| 192 | + "...props": { |
| 193 | + description: |
| 194 | + "Any other props are spread to the underlying CodeBlock component.", |
| 195 | + type: "React.ComponentProps<typeof CodeBlock>", |
| 196 | + }, |
| 197 | + }} |
| 198 | +/> |
| 199 | + |
| 200 | +### `<SandboxOutput />` |
| 201 | + |
| 202 | +Renders output with log syntax highlighting and a built-in copy button. |
| 203 | + |
| 204 | +<TypeTable |
| 205 | + type={{ |
| 206 | + "...props": { |
| 207 | + description: |
| 208 | + "Any other props (except language) are spread to the underlying CodeBlock component. Language is set to 'log'.", |
| 209 | + type: "Omit<React.ComponentProps<typeof CodeBlock>, 'language'>", |
| 210 | + }, |
| 211 | + }} |
| 212 | +/> |
0 commit comments