Introduction
Typed multi-step flows for Lit and web components, from one definition.
Stepperize is for flows where someone moves through named steps: checkout, onboarding, surveys, import wizards, approval flows and multi-step forms.
You describe the steps once. The definition gives you a reactive controller for your elements and a set of accessible primitives, as directives for your own templates, that all share the same typed step ids.
import { defineStepper } from "@stepperize/lit";
import { html, LitElement } from "lit";
import { z } from "zod";
const checkout = defineStepper([
{ id: "shipping", title: "Shipping", schema: z.object({ address: z.string().min(1) }) },
{ id: "payment", title: "Payment", schema: z.object({ card: z.string().length(16) }) },
{ id: "review", title: "Review" },
]);
class CheckoutSteps extends LitElement {
stepper = checkout.controller(this, { onFinish: (output) => placeOrder(output) });
render() {
const stepper = this.stepper.value;
return html`
<h2>${stepper.current.title}</h2>
${stepper.match({
shipping: () => html`<shipping-form></shipping-form>`,
payment: () => html`<payment-form></payment-form>`,
review: () => html`<order-review .data=${stepper.data}></order-review>`,
})}
<button ?disabled=${stepper.isFirst} @click=${() => stepper.prev()}>Back</button>
<button @click=${() => stepper.next()}>
${stepper.isLast ? "Place order" : "Continue"}
</button>
`;
}
}
customElements.define("checkout-steps", CheckoutSteps);A flow built with the primitives, running:
Loading lit…
Is it a form library?
No. Stepperize owns the flow: which step is current, moving between steps, what each step saved and which steps are complete. Your elements own the fields. See Forms.
What you get
- One call moves forward.
next(data)saves the step's data, validates it against the step's schema and moves on. It resolves with what happened instead of throwing. - Typed everywhere. Step ids, each step's data and your own fields
(
title,icon, anything) are inferred from the definition, including in the directives. - A controller, not a base class. It is a
ReactiveController: add it to anyLitElement, or to any otherReactiveControllerHost. - Your markup. The primitives are element directives for your own template. No custom elements are registered and nothing is hidden in a shadow root you do not own.
- Branching as data. A
nextmap decides where each step leads from its validated data, and a flow can have more than one ending. - Resumable. One
snapshotgoes in, oneonChangecomes out. The current step can follow the URL with a single option. - Accessible primitives. Roles, keyboard navigation, focus and announcements are handled for you, in shadow DOM and in light DOM, and they keep the attributes you write.
- Small. About 2.6 KiB for the controller and 6.7 KiB with the primitives, min + gzip, with no third-party dependencies.
The same flow as every other binding
The state machine is @stepperize/core, shared by every
binding, so the concepts are the same everywhere. Each has its own page,
with Lit code: steps,
the lifecycle of a move,
step state,
validation,
branching and
persistence.
Next
Last updated on