Skip to content

Core API

Find all elements matching selector and attach the component factory to each. Idempotent — calling twice on the same element is a no-op.

import { enhance } from "@beardcoder/stitch-js";
import { accordion } from "@beardcoder/stitch-js/components/accordion";
const instances = enhance("[data-accordion]", accordion({ multiple: true }));
instances.forEach((i) => i.destroy());

Options:

OptionDescription
rootScope the query to a subtree (default: document)
optionsOverride the factory options

Destroy all enhanced instances on matching elements. Pass a factory to only remove that behavior.

Declarative auto-initialization:

import { register, autoInit, tabs, accordion } from "@beardcoder/stitch-js";
register("[data-tabs]", tabs());
register("[data-accordion]", accordion());
autoInit(); // runs on DOMContentLoaded

The core primitive for creating components. Provides a scoped ComponentContext with DOM queries, delegated events, attribute parsing, and auto-cleanup.

import { defineComponent, enhance } from "@beardcoder/stitch-js";
const toggle = defineComponent(
{ activeClass: "is-active" },
(ctx) => {
ctx.on("click", () => {
ctx.el.classList.toggle(ctx.options.activeClass);
});
},
);
enhance("[data-toggle]", toggle());

See the Custom Components guide for the full ComponentContext API reference.