• Stars
    star
    1,047
  • Rank 44,020 (Top 0.9 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple playground for HTML, CSS and JavaScript supporting module imports.

Playground

A simple playground for HTML, CSS and JavaScript supporting module imports.

code style: prettier

Playground is a HTML, CSS and JS demonstration component that runs entirely in the browser and does not require loading any third party sites.

screenshot

Usage

Install from npm with:

npm install @agney/playground
# OR
yarn add @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
    />
  );
};

Demo

https://www.agney.dev/blog/introducing-playground

Features

  1. Load preview for predefined code.
  2. Autoreloading preview.
  3. Allows bare ES package imports. This means you can add imports for any library that supports ES Module resolution and playground will automatically load it into your code.
  4. See console output in the component itself.
  5. Control the tab loaded by default for your use-case.

Applications

  1. Add to static blogs to present your HTML, CSS or JavaScript code.
  2. Allow users to change the code and see the output in real time.

API

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

Format for initial snippet

{
  markup: `<h1>Title</h1>`,
  css: `h1 { color: red }`,
  javascript: `console.log("this")`
}

Why is there a @reach/tabs import?

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.

How does module imports work?

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")
);

How do I demo React code with JSX?

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"]}
    />
  );
};

What about the bundle size?

The component is fairly small at about npm bundle size. 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.

Contributing

This project makes use of Yarn Workspaces for development.

  1. Run yarn for installing dependencies.

  2. Run yarn start to start development on package.

  3. To start example, run yarn start-example.

Pull Requests are Welcome. Please create an issue to discuss before making a feature or large change. Thank You 😄

Credits

Icons made by Freepik from www.flaticon.com

More Repositories

1

svelte-tailwind-snowpack

TailwindCSS with Svelte and Snowpack v3
JavaScript
101
star
2

react-loading

Accessible and Simple Loading indicators in React
TypeScript
64
star
3

json-formatter

An extensible JSON Viewer, Editor, Formatter, Validator based on Monaco Editor
JavaScript
46
star
4

generate-og-image

Generate open graph images with Github Action from Markdown files
TypeScript
39
star
5

preact-cli-tailwind

Preact CLI Plugin that helps add Tailwind CSS to your project
JavaScript
32
star
6

gatsby-blog-starter

A Gatsby Blog Starter with Styled Components
JavaScript
19
star
7

react-use-web-share

React Hook to use the native web share prompt
TypeScript
16
star
8

gatsby-wedding-theme

A Simple Wedding Theme for Gatsby
JavaScript
15
star
9

ir-toast

Imperative Toast Component for Ionic React
TypeScript
14
star
10

reco

In Browser Recording Studio 🎥
JavaScript
12
star
11

react-avatar

👤Create Image Avatars with text based fallbacks.
TypeScript
10
star
12

coder-agent

Automatically create applications with a self task managing GPT
JavaScript
10
star
13

tailwind-snowpack

A simple Tailwind Snowpack starter
CSS
8
star
14

gatsby-careers-page

Job Listing Page
JavaScript
8
star
15

calendar-url-ui

Generate links for Gmail, Outlook, Yahoo or plain old ICS right from your browser
Svelte
7
star
16

the-reader

A JAM Stack Reading Mode PWA with React & Netlify Functions
TypeScript
6
star
17

create-profile-card

Create a Custom Profile Card CLI
JavaScript
6
star
18

logikk

Logical Expression Visualiser
JavaScript
6
star
19

quick-react-lib

A fast react component boilerplate powered on Tsup and Vite ⚡️
TypeScript
5
star
20

react-annotate-text

Created with CodeSandbox
JavaScript
5
star
21

next-goober-twin

A starter repository with NextJS, GooberJS and Twin.Macro
JavaScript
4
star
22

portfolio

Personal Portfolio Page
MDX
4
star
23

create-electrojet

Setup Modern Javascript Applications with a single command
JavaScript
4
star
24

deliverable

Deliveries in the time of Corona
JavaScript
3
star
25

usePip

A custom react hook for triggering Picture in Picture functionality
JavaScript
3
star
26

og-image-element

Custom Element used for creating Open Graph Image
JavaScript
3
star
27

quick-read-slack

Ad free light version of article inside Slack
EJS
3
star
28

wroom

CARS
ApacheConf
3
star
29

a3k

Abhiyanthriki is the binneal technical festival of Rajagiri School of Engineering and Technology.
CSS
2
star
30

antd-typescript

Antd with Typescript and React
JavaScript
2
star
31

node-express-starter

Node-Express Starter Kit
JavaScript
2
star
32

jc-viewer

JSON Crew Plugin
JavaScript
2
star
33

noter

Note Tasking Application
JavaScript
2
star
34

metro-workshop-app

Kochi Metro Guide Web Application
JavaScript
2
star
35

cra-template-netlify-fns

Get started on Netlify Functions with CRA and a single command ✨️
JavaScript
1
star
36

scikit-learn

Python
1
star
37

babel-plugin-goober-css-prop

Babel plugin that enables CSS prop for Goober
JavaScript
1
star
38

shoes

Shoes
HTML
1
star
39

microservice-spa

Microservice Frontend with Vue and Angular 1
JavaScript
1
star
40

blog

Personal Blog
HTML
1
star
41

card

Personal Information Card
JavaScript
1
star
42

typescript-frontend-starter

A Minimal Typescript Webpack Configuration
JavaScript
1
star
43

sequelize-express-sample

Sequelize ORM with ExpressJS
JavaScript
1
star
44

jc-transform-json

Transform JSON using JavaScript
JavaScript
1
star
45

jc-json-utils

A sample plugin for use JSON Crew
JavaScript
1
star
46

compare-repos

Compare any two repositories
JavaScript
1
star
47

instaton

Instagram Clone FrontEnd Project
JavaScript
1
star
48

react-js-live

Live Frontend Editor Component for React
JavaScript
1
star
49

electrojet-svelte

Default Svelte Template for Electrojet CLI
EJS
1
star
50

electrojet-scripts

A CLI for running applications generated with Electrojet CLI
JavaScript
1
star
51

mdx-wrapper-recreation

CSS
1
star
52

learning-rails

From JavaScript & NodeJS to learning Ruby & Rails
1
star
53

electrojet-electron-default

Default Application for Electrojet Electron Applications
JavaScript
1
star
54

ochha

WIP
JavaScript
1
star
55

cra-template-tailwind

A CRA Template for Tailwind CSS
JavaScript
1
star
56

reactjs-workshop

ReactJS Workshop for Facebook Developers Circle Kochi
JavaScript
1
star