Skip to content

Commit 04242be

Browse files
FranciscoMorettihaydenbleaselvercel[bot]
authored
Sandbox component (#231)
* Implement Sandbox component with collapsible sections and tabs; export getStatusBadge function for external use * sandbox * feat: add new Sandbox component * Update packages/elements/src/sandbox.tsx Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> * Refactor Sandbox component: remove SandboxCopyButton and integrate copy functionality into SandboxCode and SandboxOutput components. Update related tests and documentation accordingly. --------- Co-authored-by: Hayden Bleasel <hello@haydenbleasel.com> Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
1 parent 8791981 commit 04242be

7 files changed

Lines changed: 1103 additions & 1 deletion

File tree

‎.changeset/three-bees-follow.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ai-elements": patch
3+
---
4+
5+
Added Sandbox componet

‎.github/CONTRIBUTING.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ When adding code examples:
8181
- Use semantic HTML in components
8282
- Follow Tailwind CSS conventions for styling
8383

84+
### Adding a New Component
85+
86+
When adding a new component to the library, follow these steps:
87+
88+
1. **Create the component file** in `packages/elements/src/`
89+
2. **Create example file(s)** in `packages/examples/src/`
90+
3. **Create documentation** in `apps/docs/content/docs/components/`
91+
4. **Wire up examples with Preview**
92+
8493
## Pull Request Guidelines
8594

8695
1. **Ensure your PR has a clear purpose**
@@ -174,6 +183,8 @@ When reporting issues:
174183
- Review the documentation at [ai-sdk.dev/elements](https://ai-sdk.dev/elements)
175184
- Reach out to maintainers if you need guidance
176185

186+
187+
177188
## Code of Conduct
178189

179190
This project follows a Code of Conduct. By participating, you agree to uphold professional, respectful, and inclusive behavior.
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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

Comments
 (0)