Share components across every project

Define a custom element with a template, styles and methods. Export it as JSON or a standalone script, then import it anywhere. No framework, no build step, no monorepo.

  • Free and open source
  • Zero dependencies
  • 3.6 kB gzipped

Project A

where the component is written

Project B

a separate page that only receives the JSON

This frame has its own document and its own custom element registry. It loads the library, calls builder.import(json), and renders the same HTML.

Change the label, the styles or a method, then press Export and import (or Ctrl/⌘ + Enter in the editor). Both projects start fresh on every run.

Custom elements work wherever HTML does
  • React
  • Vue
  • Svelte
  • Angular
  • Astro
  • WordPress
  • Plain HTML

Stop copying the same component into every repo

Copying components between projects

  • Every project keeps its own copy of the same component
  • A bug fixed in one copy stays broken in the others
  • Copies drift apart as each team tweaks its own
  • Sharing properly usually means a package, a build step or one framework for everyone
Every fixrepeated by hand in each repo
Many versionsof what should be one component
Core features

Everything a shared component needs

Props, events, state and scoped styles, in a definition small enough to send anywhere.

Read the guide

Attributes become props

Change an attribute and the element re-renders. This gauge is a real component built with the library: drag the slider.

Styles stay put

Each element has its own Shadow DOM, so its CSS doesn't leak out and the page's doesn't leak in.

Three calls, start to finish

Define a component, export it, import it in the next project. Click the board to flip it.

builder.define('tally-counter', { template, styles, methods })

Talk to the page

this.emit() sends events out of the element. Press the button: it's a component too.

Types included

TypeScript checks this.setState, this.emit and your methods.

const counter: ComponentDefinition = {
  initialState: { count: 0 },
  template: (p, s) => `${s.count}`,
  methods: {
    increment() {
      this.setState({ count: 1 });
    }
  }
};

Works in Node

Define components in a build script and write script exports to disk.

const js = await builder.export(
  'tally-counter',
  { type: 'script' }
);
fs.writeFileSync('tally-counter.js', js);
How it works

From one project to the next in three steps

The component travels with its template, styles, state and methods. The project that receives it doesn't need a build step.

3 calls
define, export and import
0 builds
needed in the project that imports

Define the component

Write a template, styles and methods with builder.define(). It's registered on the page straight away.

Three ways to share

Pick how the component travels

Use whichever fits how your projects are set up. You can mix them.

Before you share

Three rules keep shared components working

Components travel as source text. That keeps them small and framework-free.

  • Keep functions self-contained

    Templates and methods can't use variables or imports from the file they were written in. Those don't exist in the project that imports the component.

  • Only import from sources you trust

    Importing runs the component's code, the same as adding a script tag.

  • Escape what users type

    Props aren't escaped for you. Wrap anything that could contain user input in escape(), which every template receives.

Read more about writing shareable components

Ready when you are

Share your first component this afternoon

Install the package, define one element, and import it into the next project you open.

npm install shared-component-builder
Get started