Attributes become props
Change an attribute and the element re-renders. This gauge is a real component built with the library: drag the slider.
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.
where the component is written
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.
Props, events, state and scoped styles, in a definition small enough to send anywhere.
Read the guideChange an attribute and the element re-renders. This gauge is a real component built with the library: drag the slider.
Each element has its own Shadow DOM, so its CSS doesn't leak out and the page's doesn't leak in.
Define a component, export it, import it in the next project. Click the board to flip it.
builder.define('tally-counter', { template, styles, methods })
this.emit() sends events out of the element. Press the button: it's a component too.
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 });
}
}
};
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);
The component travels with its template, styles, state and methods. The project that receives it doesn't need a build step.
Write a template, styles and methods with builder.define(). It's registered on the page straight away.
builder.export() turns it into JSON, or a standalone script that doesn't need the library.
builder.import() loads it from a file, a URL or your server, and the element is ready to use.
Use whichever fits how your projects are set up. You can mix them.
Components travel as source text. That keeps them small and framework-free.
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.
Importing runs the component's code, the same as adding a script tag.
Props aren't escaped for you. Wrap anything that could contain user input in escape(), which every template receives.
Install the package, define one element, and import it into the next project you open.
npm install shared-component-builder