Interactive JavaScript Sandbox
JavaScript Playgrounds is a library that provides an embeddable, interactive JavaScript sandbox environment, primarily designed for educational purposes and quick code experimentation. It allows users to execute JavaScript code directly within a web page, with support for various presets like React and React Native. The package, currently at version 1.2.8, does not specify a strict release cadence but appears to be actively maintained, being used in several 'Express' educational guides. Its key differentiators include easy integration as either a React component or a direct iframe, and the ability to pre-configure the sandbox with specific code and presets, making it highly versatile for showcasing or teaching JavaScript concepts without requiring complex setup for the end-user. Configuration parameters are passed via the URL hash, supporting both JSON and URI encoding for complex setups.
Common errors
-
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
cause Attempting to use `Playground` component without a correct import in a React application.fixEnsure `import Playground from 'javascript-playgrounds'` is used, as `Playground` is a default export. -
Sandbox content not loading or displaying 'Page Not Found' in iframe.
cause Incorrect `src` URL for the iframe, especially if modifying `unpkg.com` or using a custom `baseURL`.fixVerify the `src` URL points to the correct `index.html` file within the `public` directory of the `javascript-playgrounds` package, e.g., `//unpkg.com/javascript-playgrounds@^1.0.0/public/index.html`. -
Parameters passed to iframe are not reflected in the sandbox (e.g., initial code or preset not applied).
cause Configuration parameters in the `iframe` `src` are not correctly JSON-encoded and then URI-encoded, or the `#data=` prefix is missing/malformed.fixDouble-check the encoding: `const encoded = encodeURIComponent(JSON.stringify({ code: 'console.log("Hello");' })); const src = `//unpkg.com/.../index.html#data=${encoded}`;`
Warnings
- breaking Older versions (v1.x) had specific documentation and behavior tailored for React Native. The current main branch (v2+) has refactored aspects, and users specifically targeting legacy React Native integrations should refer to the `v1` branch.
- gotcha When using the sandbox directly via an `iframe`, all configuration parameters must be JSON-encoded and then URI-encoded before being appended to the URL hash. Incorrect encoding will lead to parameters not being parsed correctly by the sandbox.
- gotcha The `baseURL` prop/parameter defaults to `unpkg.com` for loading the sandbox assets. If your application has strict Content Security Policy (CSP) rules or you require self-hosting for reliability/offline support, you must configure a custom `baseURL`.
- gotcha The `style` and `className` props on the React `<Playground>` component apply to the wrapping `div`, not directly to the `iframe` itself. The `iframe` inside the wrapper will take `100%` width and height of this wrapping `div`.
Install
-
npm install javascript-playgrounds -
yarn add javascript-playgrounds -
pnpm add javascript-playgrounds
Imports
- Playground
const Playground = require('javascript-playgrounds')import Playground from 'javascript-playgrounds'
- JSX usage
<Playground style={{ width: 800, height: 500 }} /> - Iframe direct usage
<iframe src="//unpkg.com/javascript-playgrounds@^1.0.0/public/index.html#data=" width="880" height="425"></iframe>
Quickstart
import Playground from 'javascript-playgrounds';
import React from 'react';
// This component provides an interactive JavaScript sandbox.
// It defaults to a basic JavaScript environment.
// To configure with specific code or presets, pass props.
export default function App() {
// Example: Setting initial code
const initialCode = `
const message = 'Hello from the sandbox!';
console.log(message);
function greet(name) {
return 'Hello, ' + name + '!';
}
console.log(greet('World'));
`;
return (
<div style={{ padding: '20px' }}>
<h1>My Interactive Sandbox</h1>
<Playground
style={{ width: '100%', height: '600px', border: '1px solid #ddd' }}
code={initialCode}
preset="react" // Example: use the React preset
// You can also specify a custom base URL if needed, e.g.,
// baseURL="https://my-cdn.com/javascript-playgrounds"
/>
<p>Experiment with JavaScript, React, or other presets directly in this embedded editor.</p>
</div>
);
}