npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ui-js/code-playground

v1.10.2

Published

A web component to display editable, executable code

Downloads

36

Readme

code-playground

Code Playground is a web component to display an editable and executable code section in a web page.

To use it, add a script tag pointing to the code for the component:

<script type="module" src="../dist/code-playground.min.js"></script>

Then add a <code-playground> tag in your page:

<code-playground>
  <div slot="javascript">console.log("Hello world")</div>
</code-playground>

Add one or more <div> with a slot attribute indicating the type of content: "javascript", "html" or "css".

If there are multiple "slots", each will be displayed in a separate tab or section.

If CodeMirror is included in the page, it will be used as the editor for the content. To include it, add these script tags to your page:

<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.11/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.11/mode/javascript/javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.11/mode/xml/xml.min.js"></script>

If the attribute autorun=never is present, pres Run to execute the code after changing it.

To revert your changes and restore the initial content, press Reset.

The JavaScript source code is modified before execution to be executable in a 'sandbox':

  • the console calls, such as console.log() are intercepted so they are displayed in the playground rather than in the browser's console
  • the document.querySelector(), 'document.querySelectorAll() and 'document.getElementById() calls are also intercepted to respect the scope of the playground, that is using these calls you can also access content in the playground. This means you can also have multiple elements with the same ID in different playgrounds, and the correct one will be selected.
  • if a style slot is provided, its content is injected as a CSS stylesheet before the script execution
<code-playground layout="stack" show-line-numbers>
  <style slot="style">
    .grey-zone {
      background: grey;
      height: 100px;
      width: 100%;
    }
  </style>
  <div slot="html">&lt;div class="grey-zone"&gt;&lt\div&gt;</div>
</code-playground>
  • if a preamble slot is provided, it is inserted before the script.
  • if a output-stylesheets attribute is provided, it is used to insert links to stylesheets in the output.
  • the path of import directives can be modified as well. Add a <script> tag inside the component that defines a moduleMap variable that maps a module name to a URL. For example:
<code-playground>
  <script>
    moduleMap = {
      mathlive: "//unpkg.com/mathlive/dist/mathlive.min.mjs",
    };
  </script></code-playground>
  <div slot="javascript">import MathLive from 'mathlive';</div>
</code-playground>

will result in the "mathlive" module to be loaded from the "//unpkg.com/mathlive/dist/mathlive.min.mjs" URL;

Attributes and Properties

The attributes can be specified directly on the component, e.g.

<code-playground layout="stack">
  <div slot="javascript">...</div>
  <div slot="html">...</div>
</code-playground>

There are corresponding properties for the attributes which can be accessed via JavaScript.

| Attribute | Property | | | ----------------------- | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | layout | | stack (each section is displayed in a column) or tabs (each section is displayed in a panel with a tab selector) | | active-tab | activeTab | html, javascript or css: indicate which tab is visible if the layout is tab | | show-line-numbers | showLineNumbers | true or false. If true, displays line numbers in the gutter of the source code. | | button-bar-visibility | buttonBarVisibility | visible: always show the button bar, hidden: never show the button bar, auto: show the button bar when the content is modified | | autorun | autorun | never: the Run button must be pressed to see changes, otherwise a number indicating a lag in millisecond between editing changes and re-running the playground |