API reference
Every public method, option and field. For explanations and examples, see the guide.
new ComponentBuilder(options?)
Creates a builder with its own registry of components.
| Option | Type | Default | Description |
|---|---|---|---|
apiEndpoint | string | null | null |
Base URL of your component server. Needed for export(…, { type: 'cloud' }) and importing by name. |
Other options are kept on builder.config but not used.
define(name, definition)
Stores the component in the registry and, in a browser, registers it as a custom element. Returns the serializable component data.
Throws if:
nameisn't a valid custom element name;definition.templateisn't a function;- the name is already in this registry or already registered on the page;
initialStateisn't JSON-serializable;- a method is a bound or native function, which can't be turned into source.
When it throws, nothing is registered.
export(name, options?)
type | Resolves to |
|---|---|
'json' (default) | { type: 'json', data: ComponentData, usage: string } |
'script' | A standalone script as a string. See Standalone scripts. |
'cloud' | The JSON your server returns from POST {apiEndpoint}/components. |
data also includes exportedAt (a timestamp) and
exportOptions. Rejects if the component isn't in the registry, if
type: 'cloud' is used without apiEndpoint, or if the server responds
with a non-2xx status.
import(source, options?)
Registers a component and resolves to its tag name.
source | Treated as |
|---|---|
| An object | Component data. An export() result ({ type: 'json', data }) is unwrapped. |
String starting with { | JSON to parse. |
String starting with http://, https://, //, /, ./ or ../ | A URL to fetch. |
| Any other string | A name to load from GET {apiEndpoint}/components/{name}. |
options.name registers the component under a different tag name. Rejects on invalid
data, an invalid or taken name, a non-2xx response, or a missing apiEndpoint when
loading by name. A failed import registers nothing.
Importing evaluates the component's function sources. Only import from sources you trust.
ComponentBuilder.escapeHtml(value)
Converts value to a string and escapes &, <,
>, " and '. null and
undefined become ''. Templates receive the same function as
helpers.escape.
Properties
| Property | Type | Description |
|---|---|---|
registry | Map<string, ComponentData> | Components defined or imported by this builder, by name. |
config | object | The options passed to the constructor, with defaults applied. |
Lower-level methods
define, export and import use these. They're public for
custom workflows.
generateScript(data)
Returns the standalone script for component data. Throws if data.name is invalid.
registerFromData(data, options?)
The synchronous part of import: validates data, rebuilds functions, registers the element and stores it in the registry.
registerWebComponent(name, definition)
Registers a custom element from a definition with real functions, without touching the registry.
Returns false where customElements doesn't exist, such as Node. Throws if
the name is already registered on the page.
serializeMethods(methods)
Turns each function into source text that evaluates to a function. Method shorthand such as
save(x) { … } becomes function (x) { … }.
generateMethodsCode(methods)
Returns object-literal entries ("name": source) for serialized methods.
fetchJson(url, init?)
fetch plus response.json(), rejecting on non-2xx responses.
saveToCloud(data) and loadFromCloud(name)
The two requests to your component server. Both reject without apiEndpoint.
toPascalCase(str) and generateUsageInstructions(name)
toPascalCase('tally-counter') returns 'TallyCounter'.
generateUsageInstructions returns the text placed in a JSON export's
usage field.
ComponentDefinition
The object passed to define.
| Field | Type | Description |
|---|---|---|
template | (props, state, helpers) => string | Required. Returns the element's HTML. helpers is { escape }. |
props | string[] | Attributes to observe and pass to the template. Default []. |
styles | string | CSS added to the Shadow DOM. Default ''. |
methods | Record<string, Function> | Added to the element. Called from on-* attributes as (event, element). |
initialState | object | JSON-serializable starting state, copied for each element. Default {}. |
mounted | () => void | Runs after the first render each time the element is added to the page. |
version | string | Stored with the component. Default '1.0.0'. |
events | string[] | Names of events the component emits, for documentation. Default []. |
| Anything else | JSON value | Kept as metadata, for example description. Function values are dropped. |
Element instance
Available as this in templates, methods and mounted, and on the element in the page.
| Member | Description |
|---|---|
props | Current values of the observed attributes, as strings. |
state | This element's state. |
setState(partial) | Merges partial into state and re-renders if the element is on the page. |
render() | Re-renders the Shadow DOM from the template. |
emit(name, detail?) | Dispatches a bubbling, composed CustomEvent. Returns false if a listener called preventDefault(). |
| Your methods | Every function in methods. |
Delegated events for on-* attributes: click, dblclick,
input, change, submit, keydown,
keyup, focusin, focusout, pointerdown,
pointerup.
ComponentData
The serializable form returned by define, stored in the registry and sent by export.
| Field | Type | Description |
|---|---|---|
name | string | Tag name. |
version | string | From the definition. |
props | string[] | Observed attributes. |
template | string | Source of the template function. |
styles | string | CSS. |
methods | Record<string, string> | Source of each method. |
events | string[] | From the definition. |
initialState | object | Starting state. |
mounted | string | null | Source of mounted. |
created | number | When it was defined (ms since epoch). |
exportedAt, exportOptions | Added by export; not kept when imported. |
Errors
| Message starts with | Cause |
|---|---|
Invalid component name | The name breaks the custom element naming rules. |
Component "…" is already defined | The name is taken in this registry or on the page. |
Component "…" needs a template function | define was called without a template. |
Component … not found | export was called with a name that isn't in the registry. |
API endpoint not configured | A server operation was used without apiEndpoint. |
Request to … failed with status | The server or URL returned a non-2xx response. |
Invalid template source, Invalid method "…" source | Imported source isn't valid JavaScript. |
… must be a function source string, … does not evaluate to a function | Imported data has a missing or non-function template, method or mounted. |
Component source must be | import was called with something other than a string or object. |