react-code-canvas
Version:
A browser-based canvas for rendering React components at runtime
33 lines (32 loc) • 1.89 kB
TypeScript
import React from "react";
export interface ReactCanvasProps {
code: string;
scope?: Record<string, React.ComponentType | unknown>;
showPreview?: boolean;
showEditor?: boolean;
showError?: boolean;
/** localStorage key to persist edited code across page reloads; omit to disable */
persistKey?: string;
/** called whenever the code changes in the editor */
onCodeChange?: (code: string) => void;
/** full-page overlay shown until the first successful render. Defaults to
* `!showEditor` -- the overlay is `position: fixed; inset: 0`, so it covers
* the editor too; if it defaulted to on with an editor visible, the very
* first (near-certain to be incomplete/invalid) keystroke into an empty
* canvas would block the whole screen, editor included, until valid code
* exists. Pass explicitly to override either way. */
showLoader?: boolean;
/** replace the default spinner overlay with a custom node */
loader?: React.ReactNode;
/** called whenever non-empty code fails to produce output: it threw, or it
* evaluated cleanly and rendered nothing. Fires independently of
* `showError`, which only controls whether the message is displayed.
* See useRunner for the per-case reporting frequency. */
onError?: (error: string) => void;
/** replaces the built-in error toast. Pass a node, or a function receiving
* the error message. Requires `showError`. */
errorComponent?: React.ReactNode | ((error: string, dismiss: () => void) => React.ReactNode);
/** show a dismiss button on the built-in error toast; true by default */
dismissibleError?: boolean;
}
export default function ReactCanvas({ code, scope, showPreview, showEditor, showError, persistKey, onCodeChange, showLoader, loader, onError, errorComponent, dismissibleError, }: ReactCanvasProps): React.JSX.Element;