Sandbox
Code that runs in the page with Sandpack, in our own chrome: a live preview with a loading bar and its errors, an editor with a file tree and file tabs, and a console, under one toolbar that follows light and dark.
Starting the preview
import { useState } from "react" import "./theme.css" import "./styles.css" export default function App() { const [count, setCount] = useState(0) return ( <main> <section className="card"> <p className="label">Pressed</p> <p className="count">{count}</p> <p className="hint">{count === 1 ? "time" : "times"} so far</p> <div className="actions"> <button className="ghost" disabled={count === 0} onClick={() => setCount(0)} > Reset </button> <button onClick={() => setCount(count + 1)}>Press me</button> </div> </section> </main> ) }
No console output
Installation
npx shadcn@latest add "@aiellie/sandbox"shadcn needs the @aiellie registry in components.json first. npx aiellie add sandbox adds it on its first run, then installs the same way.
Sandbox runs code in the page with Sandpack. One toolbar switches between the live preview, the code and the console, reloads the preview and downloads every file as a zip. The editor has the file tree beside it, and the console's tab is marked when an error or a warning lands out of sight.
Everything around the code is ours rather than Sandpack's: the preview has an address bar like Web Preview's, a live bar sweeps along the top while the bundler starts, its progress shows in the middle, and a runtime error docks at the bottom of the preview. The editor has file tabs like Code Workspace, and the console lists messages the way Web Preview's does. It's drawn in the theme's own colours, so it matches the page in light and dark without being told which is on.
Getting started
Drop in the whole thing
SimpleSandboxtakes the files and a Sandpack template and lays out all three views. Paths start with a slash, as Sandpack has them.components/counter-sandbox.tsx"use client" import { SimpleSandbox } from "@/components/aiellie/sandbox" const FILES = { "/App.js": `export default function App() { return <h1>Hello</h1> }`, } export function CounterSandbox() { return <SimpleSandbox files={FILES} title="Hello" height={420} /> }Let people keep their edits
With
showSaveReset, an edited file shows Edited in the toolbar, with Reset to go back to the original and Save.onSavegets every file's code, to keep wherever you like.
Anatomy
<SandboxProvider>
<SandboxTabs>
{/* toolbar: views, title, save and reset, download, reload */}
<SandboxTabsContent value="preview">
<SandboxPreview />
</SandboxTabsContent>
<SandboxTabsContent value="code">
<SandboxCodeEditor />
</SandboxTabsContent>
<SandboxTabsContent value="console">
<SandboxConsole />
</SandboxTabsContent>
</SandboxTabs>
</SandboxProvider>Every view stays mounted while hidden, so the preview doesn't reload each time you look at the code.
SandboxCodeEditor puts SandboxFileExplorer, a FileTree over the sandbox's files, beside Sandpack's editor, with copy and download for the open file. The tree folds away from its header.
Sandpack bundles and runs the code on CodeSandbox's servers, so the preview needs a network connection.
Examples
Composed by hand
Build it from the parts to choose the views. This one opens on the code, leaves out the tree, and notes when it was saved.
Starting the preview
export default function App() { return <h1>Edit me, then save</h1> }
Not saved yet
API reference
SimpleSandbox
| Prop | Type | Default | Description |
|---|---|---|---|
files | SandboxFiles | required | |
template | SandpackPredefinedTemplate | "react" | |
title | string | Shown in the toolbar and names the zip. | |
defaultTab | "preview" | "code" | "console" | "preview" | |
showSaveReset | boolean | false | |
onSave | (files: Record<string, string>) => void | ||
height | number | string | 500 | |
children | ReactNode | Rendered inside the provider, for code that drives the sandbox with useSandpack. |
SandboxTabs
| Prop | Type | Default | Description |
|---|---|---|---|
defaultValue | string | "preview" | |
value | string | Makes the view controlled. | |
onValueChange | (value: string) => void | ||
title | string | ||
showToolbar | boolean | true | |
showSaveReset | boolean | false | Shows Reset and Save once a file is edited. |
onSave | (files: Record<string, string>) => void | Given every file's code when Save is pressed. |
SandboxCodeEditor
| Prop | Type | Default | Description |
|---|---|---|---|
showHeader | boolean | true | The tabs and actions. |
showFileTree | boolean | true | |
showTabs | boolean | true | A tab for each open file. Off, the header names the active one. |
showLineNumbers | boolean | true |
All other props go to Sandpack's SandpackCodeEditor.
SandboxFileExplorer
| Prop | Type | Default | Description |
|---|---|---|---|
autoHiddenFiles | boolean | true | Leaves out files marked hidden. |
SandboxProvider, SandboxPreview, SandboxConsole, SandboxLayout
| Part | Description |
|---|---|
SandboxProvider | Sandpack's provider, themed from the page unless you pass theme. |
SandboxPreview | Sandpack's preview in our chrome: an address bar with back, forward and open in a new tab (showNavigator, default true), a loading bar and progress instead of its cube, Try again if the bundler times out, and runtime errors docked at the bottom. Its own buttons are off, since the toolbar has them. |
SandboxConsole | The preview's logs as rows, with error and warning counts and Clear. resetOnPreviewRestart (default true) clears it on a restart. |
SandboxLayout | Sandpack's layout, without its frame. |
useSandboxChanges
| Field | Type | Description |
|---|---|---|
hasChanges | boolean | A file differs from the last save. Hidden files don't count, so the host can change them. |
save | () => Record<string, string> | Marks the files saved. |
reset | () => void | Puts every file back as it began. |