Components

CodesparkPreview

A sandboxed preview component that renders compiled code in an iframe.

CodesparkPreview is a sandboxed preview component that executes compiled React code in an isolated iframe environment. It supports Tailwind CSS, custom scripts/styles injection, and console output capture.

import { CodesparkPreview } from '@codespark/react';

Basic Usage

<CodesparkPreview code="anything you want to preview...">

Props

code

Source code to preview. Used when not providing a workspace instance.

TypeDefaultSince
string-v1.0.0
< ="export default () => <div>Hello</div>" />

workspace

Workspace instance to use for compilation and file management.

TypeDefaultSince
WorkspaceInherited from contextv1.0.0
import { ,  } from '@codespark/react';

const  = new ({
  : './App.tsx',
  : { './App.tsx': 'export default () => <div>Hello</div>' }
});

< ={} />

theme

Theme mode for the preview.

TypeDefaultSince
'light' | 'dark'Inherited from contextv1.0.0
< ="dark" />

framework

Framework to use for code analysis and compilation.

TypeDefaultSince
Framework | (new () => Framework) | stringInherited from contextv1.0.0
import {  } from '@codespark/framework/html';
import {  } from '@codespark/react';

< ={} />

tailwindcss

Whether to enable Tailwind CSS support in the preview.

TypeDefaultSince
booleantruev1.0.0

When enabled, you can use any Tailwind CSS class in your preview code.

< ={false} />

preflight

TypeDefaultSince
preflighttruev1.0.1

Whether to apply preflight styles (base reset, font smoothing, layout defaults) in the preview iframe.

:root {
  font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

html,
body {
  margin: 0;
  overflow: hidden;
}

html {
  width: 100vw;
  height: 100vh;
  padding: 0;
}

body {
  height: 100%;
  padding: 12px;
  overflow: auto;
  box-sizing: border-box;
  display: flex;
}

#root {
  margin: auto;
}

imports

Custom import map for external dependencies.

TypeDefaultSince
Record<string, string>Inherited from contextv1.0.0
< ={{ 'lodash-es': 'https://esm.sh/lodash-es@4.17.21' }} />

height

Height of the preview area in pixels.

TypeDefaultSince
number200v1.0.0
< ={300} />

className

CSS class name for the preview container.

TypeDefaultSince
string-v1.0.0
< ="h-full" />

children

Child elements to inject into the preview iframe, such as Script, Style, or Link components.

TypeDefaultSince
ReactNode-v1.0.0
import { , , ,  } from '@codespark/react';

<>
  <>{`.custom { color: red; }`}</>
  < ="https://example.com/script.js" />
  < ="stylesheet" ="https://example.com/styles.css" />
</>

See the Style, Script, and Link component documentation for more details.

onError

Callback fired when a runtime error occurs in the preview.

TypeDefaultSince
(error: unknown) => void-v1.0.0
<
  ={ => {
    .('Runtime error:', );
  }}
/>

onLoad

Callback fired when the preview iframe is loaded and ready.

TypeDefaultSince
(iframe: HTMLIFrameElement) => void-v1.0.0
<
  ={ => {
    .('Preview loaded:', );
  }}
/>

onRendered

Callback fired when the preview has finished rendering.

TypeDefaultSince
() => void-v1.0.0
<
  ={() => {
    .('Render complete');
  }}
/>

onConsole

Callback fired when console methods are called in the preview. Useful for capturing and displaying console output.

TypeDefaultSince
(data: OnConsoleData) => void-v1.0.0
interface OnConsoleData {
  level: string;    // 'log', 'warn', 'error', 'info', 'debug'
  args: unknown[];  // Arguments passed to console method
  duplicate?: boolean;
}
<
  ={ => {
    .(`[${.}]`, ....);
  }}
/>

Last updated on