Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-codesandboxer

codesandbox3.5kMIT3.1.5TypeScript support: definitely-typed

A simple react component to help easily deploy an example to codesandbox

readme

React-CodeSandboxer

A simple react component that allows you to deploy example code to Codesandbox. It can take a file content, or fetch an example file from github or bitbucket.

For fetching files, it will add both internal and external imports to the example, allowing you to build complex examples when you need to.

This is a client-side implementation of a workflow using codesandboxer

import React, { Component } from 'react';
import CodeSandboxer from 'react-codesandboxer';

export default () => (
  <CodeSandboxer
    examplePath="examples/file.js"
    gitInfo={{
      account: 'noviny',
      repository: 'react-codesandboxer',
      host: 'github',
    }}
  >
    {() => <button type="submit">Upload to CodeSandbox</button>}
  </CodeSandboxer>
);

What does this actually do?

With the minimal options provided, the sandboxer can fetch the file contents from github, as well as the relevant imports of that file (both internal and external)

Quick gotchas

  1. If the example file does not exist at the source, the deploy will fail. You can get around this by passing in the file's contents directly as the prop example.
  2. We follow relative imports in the example, so the example still works when uploaded. The fewer files your example depends upon, the faster it will be. (we will only ever fetch a file once, even if multiple things depend upon it)
  3. While it's not enforced, making sure you have a button with the type 'sumbit' at the top level is important for accessibility.
  4. You may find console errors from failed fetch requests. Codesandboxer captures and handles these errors, but we cannot stop them appearing in the console. See minutiae for details on why.

Props we super definitely need

With gitInfo and examplePath, we can take care of everything else for you, fetching all the example, and then fetching all other imports. Without this, things break.

examplePath: string

The absolute path to the example within the git file structure. This is used for fetching the example and other files that exist relative to the example.

gitInfo: GitInfo

This is all the information we need to fetch information from github or bitbucket. The format is:

{
  account: string,
  repository: string,
  branch?: string,
  host: 'bitbucket' | 'github',
}

If no branch is provided, you will have your code deployed from master. Host is not defaulted.

Props You Definitely Probably Want To Provide

While these props aren't necessary to have codesandboxer work, you will almost always want to configure these, to make sure the example you get on codesandbox is the same as the example when run in its local context.

children: ({ error, isLoading, isDeploying, sandboxId, sandboxUrl }) => Node

Render prop that return isLoading, files and error. This is the recommended way to respond to the contents of react-codesandboxer if you want to change the appearance of the button.

Children also receives the sandboxId and the sandboxUrl once those exist.

pkgJSON?: Package | string | Promise<Package | string>

The contents of the package.json. This is used to find the correct versions for imported npm packages used in your example and other files pulled in. If no package.json is provided, each package will use latest from npm. It has effectively 4 ways to pass in the package.JSON

  • Pass in the package itself as an object.
  • Pass in a string which is the git path to the package
  • Pass in a promise that resolves to:
    • An object that represents the package.json
    • A stringified version of the package.json object.

importReplacements?: Array<[string, string]>

Paths in the example that we do not want to be pulled from their relative location. These should be given as absolute git paths. This is most commonly used if your examples are pulling in something such as src, and you want to rely on the npm version of the component in codesandbox.

dependencies?: { [string]: string }

Dependencies to always include, even if they are not found in any file that was passed in. If you are using importReplacements, anything that is being added by it should go here as well. We always include react and react-dom for you.

Cool Props To Add Niceness

These props are less needed, and more to allow different use-cases, or some amount of debugging. You do not need to worry too much about them, but you can get some cool things done using them.

example?: string | Promise<string>

Pass in the example as code to prevent it being fetched. This can be used when you want to perform any transformation on the example. If you pass in a promise, the returned value of the promise will be used. This can be useful if you are performing your own fetch or similar to get your example's raw contents.

name: string

Name for the codesandbox instance. This sets the package name in the uploaded package.json, which in turn sets the sandbox name.

afterDeployError?: ({ name: string, description?: string, content?: string, }) => mixed

Function that is called when an error occurs in the deploy process, with details of the error.

autoDeploy?: boolean

Deploy the sandbox when component mounts, instead of waiting for the button to be clicked. You should only need to autodeploy if you plan on opening the sandbox immediately, such as in an iframe.

preload?: boolean

Load the files when component mounts, instead of waiting for the button to be clicked. This doesn't deploy the sandbox on mount yet, it only preloads all the data it needs to deploy.

onLoadComplete?: ({ parameters: string, files: Files } | { error: any }) => mixed,

Function called once loading has finished, whether this is from preload or from a button press. It returns an object with the parameters string to submit to CodeSandbox as well as the unprocessed files object. If there is an error, the error will be returned instead.

afterDeploy?: (sandboxUrl: string, sandboxId: string) => mixed

Function called once the deploy has occurred. This function is given both the base sandboxUrl that we open by default, as well as the sandboxId so you can generate your own urls, such as an embed url.

providedFiles?: Files

Pass in files separately to fetching them. Useful to go alongside specific replacements in importReplacements.

The shape of the files object is

{
  fileName: {
    content: string
  }
}

The filename is the absolute path where it will be created on CodeSandbox, and the content is the file's contents as a string.

If a fileName exists in your provided files, it will not be fetched when it is referenced.

skipRedirect?: boolean

Do not open the sandbox once the data has been sent. Using this along with the afterDeploy prop can allow you to handle what is done with the sandbox, including loading an embed using the ID.

extensions?: Array<string>

An array of extensions that will be treated as javascript files. For example, if you pass in [.jsx], when loading files, we will attempt to fetch .jsx files as well as .js and .json files. The extension type of your example is automatically added, so if you pass in the examplePath my/cool/example.jsx, you will not need to pass in the jsx extension.

If your example file is fo type .ts or .tsx both are added.

template?: string

This template prop sets what codesandbox template to use. Currently we support:

  • create-react-app
  • create-react-app-typescript

We auto-detect which one we think we should use, so you should only need to provide this if you want to override our selected template.

Unsupported templates will still cause the bundled files to be sent to codesandbox under that template, but the bundling may fail.

A slightly more complicated example:

import pkgJSON from '../package.json';

<CodeSandboxer
  examplePath="deeply/nested/thing/some-example.js"
  pkgJSON={pkgJSON}
  gitInfo={{
    account: 'noviny',
    repository: 'react-CodeSandbox',
    branch: 'master',
    host: 'github',
  }}
  importReplacements={[['src', pkgJSON.name]]}
  dependencies={{
    '@atlaskit/css-reset': 'latest',
    [pkgJSON.name]: pkgJSON.version,
  }}
  providedFiles={{ 'index.js': content: { 'abcde....' } }}
  afterDeploy={console.log}
>
  {({ isLoading, error }) =>
    isLoading
      ? <div>Uploading</div>
      : (
        <button type="submit"  disabled={!!error}>
          Upload to CodeSandbox
        </button>
      )
  }
</CodeSandboxer>

This shows off some more advanced usage:

  • We are providing the pkgJSON as an object, so it does not need to be fetched (this also accepts a promise that resolves to a pkgJSON)
  • We are specifying a branch to pull files from (also accepts git hashes)
  • We are replacing src files with a reference to the package, so the package's own internals are pulled from npm (this is a nice optimisation in component docs)
  • We have a collection of dependencies we always include
  • We are providing our own index, allowing us to add the css-reset to it. We can also pass in extra files
  • We are logging after a deploy
  • We have provided a custom button component

changelog

Changelog

3.1.5

Patch Changes

  • [patch]2c210fa: Last publish did not have correct dists - republishing

3.1.4

Patch Changes

  • [patch]a4400e5: Update dependencies

3.1.3

  • [patch]cedfb74:
    • Update repository references to point to new home.

3.1.2

3.1.1

  • [patch] af01387:

    • Handle passing through the fileName path

3.1.0

  • [minor] 4b2b662:

    • Add autoload option to react-codesandboxer - should only be used to automatically open iframes
  • Updated dependencies [9db3c25]:

3.0.1

3.0.0

  • [minor] 🎉 ADD TYPESCRIPT SUPPORT 🎉 (comes with auto-detection of typescript examples)
  • [BREAKING] remove the allowJSX prop, replacing it with the extensions prop.
  • [minor] Add template prop, so you can directly set the codesandbox template to use in uploading.

2.1.4

2.1.3

Update codesandboxer version

2.1.2

Update codesandboxer version so the changes in 2.1.1 do not break your app

2.1.1

Update version of codesandboxer This mostly incorporates API changes with codesandboxer that should not impact those using react-coesandboxer

2.1.0

Add new prop afterDeployError, to allow responding to errors. Add more robust erroring, so people are less lost when something goes wrong.

2.0.3

Update of codesandboxer after a bug in jsx component reading.

2.0.2

Add flag to allow the loading of jsx components.

2.0.1

Update codesandboxer dependency to resolve bug

2.0.0

Big change here is that, with the release of codesandboxer, react-codesandboxer is no longer personally carrying the file-fetching and deploying logic. This has been done to make codesandboxer more useful in more contexts, and so it can eventually support non-react sandboxes.

This also means we are using fetch instead of form submission, which means that we can return you a sandbox ID and url to your rendered child. This means you can choose how to open your sandbox, and makes it easy to open an example in an embed instead of on codesandbox itself.

There's also a small breaking change to handle a bug.

  • Breaking: dependencies that cannot be found in the passed in package.json will now no longer be added to the sandbox dependencies at 'latest'. This solves a bug where packages that were reaching into a file would have all those reach-ins added as dependencies.
  • Breaking: SkipDeploy has been renamed to SkipRedirect to more accurately support its role, and make the embed process naming make sense.
  • Breaking: Most logic has been pushed into codesandboxer, a standalone package to allow the complex logic in that to exist outside a single react component.

1.0.0

This is not hugely different from 0.4.2, with most changes being to documentation, mostly moving it in to v1. That said, there are some nice quality of life improvements:

  • Add name prop to set the name value of the sandbox.
  • Example file is now open by default instead of index.
  • Update package.json and index.js templates for clarity
  • Edit pass on the documentation

0.4.2

  • Update our import capture function to recognise export { default } from 'somewhere' as a valid import.
  • handle multiple spaces and space-types in import capturing and comparing.

0.4.1

  • Imports of place resolving to place/index.js were being added to codesandbox as place.js. They now get their correct path.

v0.4.0

  • Added changelog
  • Add preload prop, allowing fetching of content to happen before it is used
  • For imports without file extensions, attempt .js, .json, then /index.js, in node resolution order, instead of just failing
  • child render function now returns isDeploying and error, no longer returns files.
  • onLoadComplete() prop was added. It is called with files and parameters in an object.
  • afterDeploy() is no longer called with the parameters and files. Use onLoadComplete to hook into this information.