0.8.1 • Published 2 years ago

@agney/playground v0.8.1

Weekly downloads
2
License
MIT
Repository
github
Last release
2 years ago

Playground is a React component that runs entirely in the browser and does not require loading any third party sites.

Usage

Install from npm with npm install @agney/playground or yarn add @agney/playground

import Playground from "@agney/playground";

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

Demo

Soon

Features

  1. Autoreloading preview for change
  2. Can load preview for predefined code.
  3. Allows for ES module imports. This means you can add imports for any library that supports ES Module resolution and playground will automatically load it into your code.

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. This could be big in educational articles and so.

API

Propsdescriptiondefaultrequired
ida unique identifier for the iFrametrue
initialSnippetInitial code to be displayedtrue
defaultEditorTabInitial editor tab to be displyed. Possible values: "markup", "css", "javascript""markup"false
defaultResultTabInitial tab on result panel to be displayed. Possible values: "console", "result""result"false

Format for initial snippet

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

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

Unfortuantely, not all packages currently support this feature. You can search through an entire list of packages through pika.dev.

You can use community created packages to replicate the functionality. For eg. React would be:

import React, { createElement } from "@pika/react";
import ReactDOM from "@pika/react-dom";

ReactDOM.render(
  createElement("div", {}, "Hello World"),
  document.getElementById("app")
);

What about the bundle size?

This component uses Babel Standalone and it adds a considerable amount to the total bundle size. You can find the total size and time on Bundle Phobia

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

0.8.1

2 years ago

0.8.0

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.0

4 years ago

0.3.5

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago