Coder Social home page Coder Social logo

agneym / playground Goto Github PK

View Code? Open in Web Editor NEW
986.0 5.0 154.0 2.23 MB

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

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

License: MIT License

HTML 2.39% JavaScript 3.37% TypeScript 93.94% Shell 0.30%
react typescript playground styled-components microbundle

playground's Introduction

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

playground's People

Contributors

agneym avatar dependabot[bot] avatar joshwcomeau avatar surjithctly avatar teloah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

playground's Issues

Global CSS cannot be imported from within node_modules

Hello,

First of all, thanks for the awesome project. I just installed this playground as per the instructions and when I run on a Next.js Project, I got the following error. Since the error is coming from your plugin, not sure if I can fix this on my side.

./node_modules/@reach/tabs/styles.css
Global CSS cannot be imported from within node_modules.
Read more: https://err.sh/next.js/css-npm
Location: node_modules\@agney\playground\dist\editor.module.js

You can see the error on this sandbox as well: https://codesandbox.io/s/affectionate-sanne-5dj0l

Make borders draggable

For smaller screens, the output might just be too small.

If we can drag the seperation, it could result in more area

Not able to install it in my project

Hi! I'm having some problems to install it. I'm sending you the error log so you can know what could be. It seems that React 18 might be a problem??

npm install @agney/playground
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR!   react@"^18.2.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^17.0.1" from @agney/[email protected]
npm ERR! node_modules/@agney/playground
npm ERR!   @agney/playground@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Thanks a lot!

React demo can't resolve dependencies

Hi there!

Thanks so much for this awesome project :)

I'm running into some trouble trying to render React. I followed the instructions in the README, and it appears to successfully load the @pika/react module, but then try and fail to load react. Here's a screenshot of my network tab:

image

Walking through the "initiator" sequence, it looks like this:

• @pika/react-dom@latest redirects to @pika/[email protected]
• @pika/[email protected] redirects to @pika/[email protected]/source.production.js
source.production.js starts with this code:

import React from "https://unpkg.com/react@^16.0.0?module";

That line tries to pull the package from unpkg, and it fails, since there is no ESM module for it. When I visit in-browser:

Package [email protected] does not contain an ES module

I'm guessing this is more of a Pika issue than a Playground issue, though yeah I'm curious if you've found a workaround? I had settled on using Preact, but I'm realizing a lot of the dependencies I want to use depend on React, and I don't know how to alias it in this situation, given the lack of a bundler.

Appreciate any ideas you have!

Reproduction instructions

• Go to https://blog.agney.dev/introducing-playground/
• Scroll down and copy the example React code
• Paste it into the demo at the top

Feature Request: Option to hide unused Tabs

It would be great if this plugin could provide an option to hide unused tabs.

For example, if I have a sample code that does not require JS & Console, there is no option to disable it for now.

Currently, we need to pass empty string such as css: `` or javascript: ``

Expected solution:
When the snippet is not provided, consider it as empty and remove it from the render view.
Add an option to disable the console tab from the preview.

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.