Giter Site home page Giter Site logo

malerba118 / react-sandbox-editor Goto Github PK

View Code? Open in Web Editor NEW
36.0 4.0 7.0 14.8 MB

React component library for sandboxed execution of html/js/css

Home Page: https://malerba118.github.io/react-sandbox-editor/#/demo

HTML 0.82% JavaScript 98.04% CSS 1.15%

react-sandbox-editor's Introduction

react-sandbox-editor

React components including a sandbox interpreter and editor

⚠️ I do NOT recommend using this library in production apps as the bundle size is very large due to in-browser transpilation via babel ⚠️

🔥 COMING SOON: A few years have passed and i'm remaking this library in a MUCH better way https://github.com/malerba118/react-esm-sandbox 🔥

NPM JavaScript Style Guide

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 = withDependencies([
  "https://unpkg.com/[email protected]/umd/react.development.js",
  "https://unpkg.com/[email protected]/umd/react-dom.development.js"
])(Sandbox);

let App = () => (
  <ReactSandbox
    theme="solarized_dark"
    scriptEditor={{
      defaultValue: 'ReactDOM.render(\n  <h1>Hello, world!</h1>,\n  document.getElementById(\'root\')\n);',
      mode: 'jsx',
      readOnly: false,
      wrapLines: false
    }}
    templateEditor={{
      defaultValue: '<div id="root"></div>',
      mode: 'html',
      readOnly: false,
      wrapLines: false
    }}
  />
);

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

Complex Usage

import React from "react";
import ReactDOM from "react-dom";
import { Sandbox, withDependencies } from "react-sandbox-editor";

const ReactSandbox = withDependencies([
  "https://unpkg.com/[email protected]/umd/react.development.js",
  "https://unpkg.com/[email protected]/umd/react-dom.development.js"
])(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')
);`;

class App extends React.Component {
  onTabClick = (event, tabName) => {
    console.log(tabName);
  };

  onPlayButtonClick = (event) => {
    console.log("Play button clicked!");
  };

  onCodeChange = (editorName, value) => {
    console.log(`Code in ${editorName} editor has changed: ${value}`);
  };

  render() {
    return (
      <ReactSandbox
        theme="solarized_dark"
        scriptEditor={{
          defaultValue: jsxCode,
          mode: "jsx",
          readOnly: false,
          wrapLines: true
        }}
        templateEditor={{
          defaultValue: '<div id="root"></div>',
          mode: "html",
          readOnly: false,
          wrapLines: true
        }}
        stylesheetEditor={{
          defaultValue: "body { background: pink; }",
          mode: "css",
          readOnly: false,
          wrapLines: true
        }}
        executeOnCodeChange={true}
        executeOnCodeChangeDebounce={1000}
        onTabClick={this.onTabClick}
        displayMode="horizontal-split"
        hideDisplayModeButton
        horizontalSplitOffset={60}
        onPlayButtonClick={this.onPlayButtonClick}
        onCodeChange={this.onCodeChange}
        permissions={[
          "allow-forms",
          "allow-pointer-lock",
          "allow-popups",
          "allow-modals",
          "allow-same-origin",
          "allow-scripts",
          "allow-top-navigation"
        ]}
        dependencies={[
          "https://unpkg.com/@material-ui/[email protected]/umd/material-ui.development.js"
        ]}
      />
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));

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 src
npm link
npm install
npm start

In tab 2:

cd example/src
npm link react-sandbox-editor
npm install
npm 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

react-sandbox-editor's People

Contributors

malerba118 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-sandbox-editor's Issues

Add typescript support

Something along the lines of

import * as ts from "typescript";

const source = "let x: string = 'string'";

let result = ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS }
});

window.alert(JSON.stringify(result));

Might want to make this a peer dependency/plugin option to reduce bundle size

Add more preprocessors

Is your feature request related to a problem? Please describe.
People often use tools like typescript, scss, less, markdown

Describe the solution you'd like
Add some of this functionality to src/utils/processors.js. Right now, only jsx is supported via babel.
It would be nice to extend to use other common preprocessors. Ideally the preprocessor util interface would take in a raw string, output a transpiled string or throw an error if invalid syntax. processors.js already contains some skeleton code for additional preprocessors.

Additional context
Must be conscious of the dependencies this would bring in and their footprints.
In my mind, Typescript would be the next most important preprocessor to support.
Then maybe scss, but I don't really find it imperative.

Is this repo still alive?

Hi!
I ran into this repo by accident and instantly loved it!
I'm using the component on a personal project and I was wondering if it is still having support!

Can it be used as a headless sandbox for html, js, css?

Hi. Thanks for the project!

Here's my scenario: I have some 3rd-party static resources in group. Each group has an index.html, some JS and CSS files that index.html imports. I want my React site to load one group of resources (index.html) into a section of my site. But I don't want any editor functionality. The end user has no idea that it's a sanbox. Is this repo a right choice? Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.