react-sandpack-for-storybook
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

React Sandpack

React components that help you create a full fledged online web application editor. Powered by Sandpack, the online bundler used by CodeSandbox.

Getting Started

You can install this package by running npm i --save @codesandbox/react-sandpack or yarn add @codesandbox/react-sandpack.

An example implementation of this is:

import React from 'react';
import { render } from 'react-dom';
import {
  FileExplorer,
  CodeMirror,
  BrowserPreview,
  SandpackProvider,
} from 'react-sandpack/es/components';
import 'react-smooshpack/dist/styles.css';
 
const files = {
  '/index.js': {
    code: "document.body.innerHTML = `<div>${require('uuid')}</div>`",
  },
};
 
const dependencies = {
  uuid: 'latest',
};
 
const App = () => (
  <SandpackProvider files={files} dependencies={dependencies} entry="/index.js">
    <div style={{ display: 'flex', width: '100%', height: '100%' }}>
      <FileExplorer style={{ width: 300 }} />
      <CodeMirror style={{ flex: 1 }} />
      <BrowserPreview style={{ flex: 1 }} />
    </div>
  </SandpackProvider>
);
 
render(<App />, document.getElementById('root'));

This renders a simple FileExplorer, with an editor and a preview with navigation.

Writing a custom component

We expose the full API of Sandpack, you can access it by using a SandpackConsumer or the withLive function, both exported:

<SandpackConsumer>
  {sandpack => {
    // Your logic!
 
    return <div>Hello</div>;
  }}
</SandpackConsumer>

The Sandpack context consists of these properties:

{
  browserFrameHTMLIFrameElement | null;
  managerStatusManagerStatus;
  managerStateIManagerState | undefined;
  bundlerURLstring | undefined;
  openedPathstring;
  errorsArray<IModuleError>;
  filesIFiles;
  openFile: (path: string) => void;
  updateFiles: (files: IFiles) => void;
  getManagerTranspilerContext: () => Promise<{ [transpilerstring]Object }>;
}

Readme

Keywords

none

Package Sidebar

Install

npm i react-sandpack-for-storybook

Weekly Downloads

2

Version

0.0.1

License

GPL-3.0

Unpacked Size

1.32 MB

Total Files

146

Last publish

Collaborators

  • libetl