react-sandbox-editor
React components including a sandbox interpreter and editor
Overview
There are many hosted web playground solutions these days (CodeSandbox, JSFiddle, CodePen). CodeSandbox in particular has blown me away, props to Ives van Hoorne for putting it together. However, I've yet to find a good unhosted playground solution. The intent of this library is to provide a highly-customizable unhosted web playground solution so that in your React apps you can include web snippets as flexible React components and not as rigid iframes with embedded content.
Demo/Reference
https://malerba118.github.io/react-sandbox-editor/#/demo
https://malerba118.github.io/react-sandbox-editor/#/docs/latest
Other Examples
Material UI Sandbox
Read Only Sandbox With No Header
Sandbox Inside Sandbox
Vue Sandbox
TypeScript Sandbox
PreScript/PostScript Sandbox
Install
npm install --save react-sandbox-editor
Simple Usage
import React from 'react';import ReactDOM from 'react-dom';import Sandbox withDependencies from "react-sandbox-editor"; const ReactSandbox = Sandbox; let <ReactSandbox ="solarized_dark" = = />; ReactDOM;
Complex Usage
import React from "react";import ReactDOM from "react-dom";import Sandbox withDependencies from "react-sandbox-editor"; const ReactSandbox = Sandbox; const jsxCode = `const { Chip, Avatar } = window["material-ui"] ReactDOM.render( <Chip avatar={<Avatar>MB</Avatar>} label="Clickable Chip" onClick={() => alert("Chip Clicked!")} />, document.getElementById('root'));`; Component { console; }; { console; }; { console; }; { return <ReactSandbox ="solarized_dark" = = = = = = ="horizontal-split" = = = = = /> ; } ReactDOM;
Sandbox Execution
Under the hood, code is executed in a sandboxed iframe as follows:
<iframe>
<html>
<head>
<style>{stylesheetEditorCode}</style>
</head>
<body>
{templateEditorCode}
<script src={dependency1}></script>
<script src={dependency2}></script>
...
<script src={dependencyN}></script>
<script>{preScript}</script>
<script>{scriptEditorCode}</script>
<script>{postScript}</script>
</body>
</html>
</iframe>
Compatibility
Version 1.X.X requires react 15.X.X || 16.X.X and react-dom 15.X.X || 16.X.X
Version 2.X.X requires react >= 16.3.0 and react-dom >= 16.3.0
For local development
Install nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
Install node 8.9.3
nvm install v8.9.3
Open two different terminal tabs and (assuming your pwd is the root of this repo)
In tab 1:
cd srcnpm linknpm installnpm start
In tab 2:
cd example/srcnpm link react-sandbox-editornpm installnpm start
src
contains the code for the react-sandbox-editor library.
example
contains a demo app that consumes the react-sandbox-editor library
License
MIT © malerba118