aiellie

Search

Find a component, template or page

GitHub
Components

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.

Counter
/

Starting the preview

Open full screen
fig. 01 · open it full screen from the corner

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

  1. Drop in the whole thing

    SimpleSandbox takes 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} />
    }
  2. 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. onSave gets 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.

Heading
export default function App() {
  return <h1>Edit me, then save</h1>
}

Not saved yet

API reference

SimpleSandbox

PropTypeDefaultDescription
filesSandboxFilesrequired
templateSandpackPredefinedTemplate"react"
titlestringShown in the toolbar and names the zip.
defaultTab"preview" | "code" | "console""preview"
showSaveResetbooleanfalse
onSave(files: Record<string, string>) => void
heightnumber | string500
childrenReactNodeRendered inside the provider, for code that drives the sandbox with useSandpack.

SandboxTabs

PropTypeDefaultDescription
defaultValuestring"preview"
valuestringMakes the view controlled.
onValueChange(value: string) => void
titlestring
showToolbarbooleantrue
showSaveResetbooleanfalseShows Reset and Save once a file is edited.
onSave(files: Record<string, string>) => voidGiven every file's code when Save is pressed.

SandboxCodeEditor

PropTypeDefaultDescription
showHeaderbooleantrueThe tabs and actions.
showFileTreebooleantrue
showTabsbooleantrueA tab for each open file. Off, the header names the active one.
showLineNumbersbooleantrue

All other props go to Sandpack's SandpackCodeEditor.

SandboxFileExplorer

PropTypeDefaultDescription
autoHiddenFilesbooleantrueLeaves out files marked hidden.

SandboxProvider, SandboxPreview, SandboxConsole, SandboxLayout

PartDescription
SandboxProviderSandpack's provider, themed from the page unless you pass theme.
SandboxPreviewSandpack'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.
SandboxConsoleThe preview's logs as rows, with error and warning counts and Clear. resetOnPreviewRestart (default true) clears it on a restart.
SandboxLayoutSandpack's layout, without its frame.

useSandboxChanges

FieldTypeDescription
hasChangesbooleanA 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() => voidPuts every file back as it began.