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

@lume/live-code

v0.6.6

Published

A `<live-code>` element for editable code with live output.

Downloads

71

Readme

@lume/live-code

A <live-code> HTML element that gives you a code editor with live results as you type.

npm install @lume/live-code

Compatible with Solid.js, Svelte, Vue, Angular, Ember, jQuery, React, Astro, Qwik.js, and every other web framework or library for creating HTML-based applications.

Examples:

  • CodePen: https://codepen.io/trusktr/pen/PogvVBj
  • Live demos on Lume's docs site are made with <live-code>: https://docs.lume.io/examples/hello-world/

Run the examples

npm install && npm start

Usage

Specify content with the content attribute:

<live-code content="console.log('hello')" mode="script>iframe" />

<script type="importmap">
	... Set up an import map, or if you use a build tool like Webpack, Vite, Parcel, Rollup,
	 esbuild, swc, etc, skip this part. See examples/index.html for an importmap example, or
	 use a tool to generate an import map such as @jsenv/importmap-node-module. ...
</script>

<script type="module">
	import '@lume/live-code' // defines the element
</script>

Specify content with the content property:

<live-code id="editor" mode="script>iframe" />

<script type="importmap">
	...
</script>

<script type="module">
	import '@lume/live-code' // defines the element

	const editor = document.querySelector('#editor')
	editor.content = `
    if (true)
      console.log('hello')
  `
</script>

The content attribute or JS property is more useful for short pieces of text, or for programmatically setting from a string, and with template systems that set attributes from JS variables.

Here's a JSX example (useful in React, Preact, Solid.js, etc, requires using a compiler such as Babel, TypeScript, or ESBuild):

function SomeComponent(props) {
	// Set the content from a variable.
	return <live-code content={props.someCode} />
}

Here's a Lit html example (does not require any build step):

render() {
  return html`<live-code content=${this.someCode}></live-code>`
}

Here's a Solid.js html example in a Lume Element (does not require any build step):

template() {
  return html`<live-code content=${() => this.someCode}></live-code>`
}

Etc. <live-code> can be used in Vue, Svelte, Angular, and all the rest.

Specify a file with the src attribute or JS property, and text content will be fetched from that file:

<live-code src="./path/to/file.js" mode="script>iframe" />

Lastly, use a <template> to specify text content inline. This can be cleaner than placing multiline text inside the content attribute by hand:

<live-code>
	<template>
		<h1>Example</h1>
		<script>
			const h1 = document.querySelector('h1')
			h1.style.color = 'royalblue'
		</script>
	</template>
</live-code>

Note that if src or content are specified, those take priority over the <template> method.

Note that <template> currently only works when it is initially present, but not if it is added later. If you're doing things programmatically, then send the content in via the content property instead of appending a <template> (f.e. editor.content = template.innerHTML).

Attributes

Each attribute has a respective JS property of the same name.

  • content - Either a string of text, or a class or ID selector starting with . or #. The given text string, or the text content of the selected element, will appear in the editor. Any time the user resets the editor with the Reset button or reset() method, the text in the editor will reset to the initial value specified by this. Default: "" which is ignored.

  • src - Specify a file from which to get text content from. If content is also specified, content loaded from src will have priority and content will be overridden. Default: "" which is ignored.

  • autorun - A boolean. If true, editing the text will cause the preview area to automatically re-run based on the new content. The Rerun button will always force a rerun. Default: true.

  • stripIndent - A boolean. If true, the given code will be unindented, which is useful for template strings that are indented within the source where they are defined. Default: true.

  • trim - A boolean. If true, leading and trailing white space will be removed. Default: true.

  • debounce - A number. If autorun is true, then autorun is debounced by this amount in milliseconds after a user types into the code editor. Defaults: 1000.

  • mode - The mode specifies which type of content the editor will execute. Possible values are the following strings:

    • "html>iframe" - The content will be treated as HTML and placed in an iframe.
    • "script>iframe" - The content is executed as a <script> inside an iframe.

    Default: "html>iframe"

Methods

reset()

Resets the text content back to the original before it was modified. This is the same thing the Reset button does.

copy()

Copy the current text to the system clipboard. This is the same thing the Copy button does.

rerun()

Reruns the live output. This is the same thing the Rerun button does.

toggleFullscreen()

Toggles fullscreen mode. This is the same thing the Toggle Fullscreen button does.