A simple playground for HTML, CSS and JavaScript supporting module imports.
See the code![]()
A simple playground for HTML, CSS and JavaScript supporting module imports.
Playground is a HTML, CSS and JS demonstration component that runs entirely in the browser and does not require loading any third party sites.
Install from npm with:
npm install @agney/playground
import Playground from "@agney/playground";
/* Why is there a tabs import? https://github.com/agneym/playground#why-is-there-a-reacttabs-import*/
import "@reach/tabs/styles.css";
const App = () => {
const snippet = {
markup: `<div id=app />`,
css: ``,
javascript: `import { h, Component, render } from 'preact';
import htm from 'htm';
const html = htm.bind(h);
const app = html\`<div>Hello World from Playground!</div>\`
render(app, document.getElementById('app'));`,
};
return (
<Playground id="example" initialSnippet={snippet} defaultEditorTab="javascript" transformJs />
);
};
https://www.agney.dev/blog/introducing-playground
| Props | description | default | required |
|---|---|---|---|
| id | a unique identifier for the iFrame | true | |
| initialSnippet | Initial code to be displayed | true | |
| defaultEditorTab | Initial editor tab to be displyed. Possible values: "markup", "css", "javascript" | "markup" | false |
| defaultResultTab | Initial tab on result panel to be displayed. Possible values: "console", "result" | "result" | false |
| transformJs | Transform the JavaScript using Babel. This is required if you want bare module imports | false | false |
| presets | Array of presets you want Babel to transform. This works only if transformJs is true. Eg. ["react", "es2015"] | false | |
| theme | Pass in the theme variables to customise the appearance | Our Theme | false |
{
markup: `<h1>Title</h1>`,
css: `h1 { color: red }`,
javascript: `console.log("this")`
}
Playground uses @reach/tabs as a dependency. We could bundle the stylesheet or inject it inline on runtime. But both those options add unnecessary code if you are already using it.
This might cause breaking changes if you have a different version of @reach/tabs but then I'm just expecting it to be stable along the road.
If an NPM package exposes an endpoint for "module", then you can direcly import this package by it's name.
import { format } from "date-fns";
format(new Date(2014, 1, 11), "yyyy-MM-dd");
Unfortunately, not all packages currently support this feature. You can search through an entire list of packages through Skypack.
You can use community created packages to replicate the functionality. For eg. React would be:
import React, { createElement } from "https://cdn.skypack.dev/react";
import ReactDOM from "https://cdn.skypack.dev/react-dom";
ReactDOM.render(createElement("div", {}, "Hello World"), document.getElementById("app"));
import Playground from "@agney/playground";
const App = () => {
const snippet = {
markup: `<div id=app />`,
css: ``,
javascript: `import React, { createElement } from "https://cdn.skypack.dev/react";
import ReactDOM from "https://cdn.skypack.dev/react-dom";
ReactDOM.render(
<h1>Hello World</h1>,
document.getElementById("app")
);`,
};
return (
<Playground
initialSnippet={snippet}
defaultEditorTab="javascript"
transformJs
presets={["react"]}
/>
);
};
The component is fairly small at about . You can find the total size and time on Bundle Phobia.
When transforming JavaScript it uses Babel Standalone which adds a considerable size. Playground loads Babel from a CDN so that it can be loaded from browser cache on change.
It uses Chrome's Native Lazy Loading so that the iframes for results are loaded lazily and your pages remain fast.
This project makes use of pnpm Workspaces for development.
Run pnpm for installing dependencies.
Run pnpm start to start development on package.
To start example, run pnpm start-example.
Pull Requests are Welcome. Please create an issue to discuss before making a feature or large change. Thank You 😄
561 followers · starred Oct 2024
1,117 followers · starred Jul 2025
254 followers · starred Jun 2022
180 followers · starred Oct 2021
A simple playground for HTML, CSS and JavaScript supporting module imports.
See the code![]()
A simple playground for HTML, CSS and JavaScript supporting module imports.
Playground is a HTML, CSS and JS demonstration component that runs entirely in the browser and does not require loading any third party sites.
Install from npm with:
npm install @agney/playground
import Playground from "@agney/playground";
/* Why is there a tabs import? https://github.com/agneym/playground#why-is-there-a-reacttabs-import*/
import "@reach/tabs/styles.css";
const App = () => {
const snippet = {
markup: `<div id=app />`,
css: ``,
javascript: `import { h, Component, render } from 'preact';
import htm from 'htm';
const html = htm.bind(h);
const app = html\`<div>Hello World from Playground!</div>\`
render(app, document.getElementById('app'));`,
};
return (
<Playground id="example" initialSnippet={snippet} defaultEditorTab="javascript" transformJs />
);
};
https://www.agney.dev/blog/introducing-playground
| Props | description | default | required |
|---|---|---|---|
| id | a unique identifier for the iFrame | true | |
| initialSnippet | Initial code to be displayed | true | |
| defaultEditorTab | Initial editor tab to be displyed. Possible values: "markup", "css", "javascript" | "markup" | false |
| defaultResultTab | Initial tab on result panel to be displayed. Possible values: "console", "result" | "result" | false |
| transformJs | Transform the JavaScript using Babel. This is required if you want bare module imports | false | false |
| presets | Array of presets you want Babel to transform. This works only if transformJs is true. Eg. ["react", "es2015"] | false | |
| theme | Pass in the theme variables to customise the appearance | Our Theme | false |
{
markup: `<h1>Title</h1>`,
css: `h1 { color: red }`,
javascript: `console.log("this")`
}
Playground uses @reach/tabs as a dependency. We could bundle the stylesheet or inject it inline on runtime. But both those options add unnecessary code if you are already using it.
This might cause breaking changes if you have a different version of @reach/tabs but then I'm just expecting it to be stable along the road.
If an NPM package exposes an endpoint for "module", then you can direcly import this package by it's name.
import { format } from "date-fns";
format(new Date(2014, 1, 11), "yyyy-MM-dd");
Unfortunately, not all packages currently support this feature. You can search through an entire list of packages through Skypack.
You can use community created packages to replicate the functionality. For eg. React would be:
import React, { createElement } from "https://cdn.skypack.dev/react";
import ReactDOM from "https://cdn.skypack.dev/react-dom";
ReactDOM.render(createElement("div", {}, "Hello World"), document.getElementById("app"));
import Playground from "@agney/playground";
const App = () => {
const snippet = {
markup: `<div id=app />`,
css: ``,
javascript: `import React, { createElement } from "https://cdn.skypack.dev/react";
import ReactDOM from "https://cdn.skypack.dev/react-dom";
ReactDOM.render(
<h1>Hello World</h1>,
document.getElementById("app")
);`,
};
return (
<Playground
initialSnippet={snippet}
defaultEditorTab="javascript"
transformJs
presets={["react"]}
/>
);
};
The component is fairly small at about . You can find the total size and time on Bundle Phobia.
When transforming JavaScript it uses Babel Standalone which adds a considerable size. Playground loads Babel from a CDN so that it can be loaded from browser cache on change.
It uses Chrome's Native Lazy Loading so that the iframes for results are loaded lazily and your pages remain fast.
This project makes use of pnpm Workspaces for development.
Run pnpm for installing dependencies.
Run pnpm start to start development on package.
To start example, run pnpm start-example.
Pull Requests are Welcome. Please create an issue to discuss before making a feature or large change. Thank You 😄
561 followers · starred Oct 2024
1,117 followers · starred Jul 2025
254 followers · starred Jun 2022
180 followers · starred Oct 2021