# Introduction

> Typed multi-step flows for Lit and web components, from one definition.

Framework: Lit (`@stepperize/lit`), Stepperize v9.
Source: https://stepperize.com/docs/v9/lit

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.

```ts
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:

The live demo runs in the browser. Its source ([mount/lit.ts](https://github.com/damianricobelli/stepperize/blob/main/apps/docs/src/components/framework-demos/mount/lit.ts)):

```ts
import { defineStepper } from "@stepperize/lit";
import { html, LitElement } from "lit";

const demo = defineStepper([
	{ id: "account", title: "Account", description: "Who you are" },
	{ id: "plan", title: "Plan", description: "What you need" },
	{ id: "done", title: "Done", description: "All set" },
]);

const TAG = "stepperize-lit-demo";

class LitDemo extends LitElement {
	stepper = demo.controller(this);

	// Light DOM, so the page's utility classes style it like the other demos.
	protected createRenderRoot() {
		return this;
	}

	render() {
		const s = this.stepper;
		const stepper = s.value;
		const panel = "rounded-md bg-fd-muted p-3";
		return html`
			<div ${s.root()} data-demo="lit" class="not-prose my-4 flex flex-col gap-4 rounded-lg border p-4 text-sm">
				<ol ${s.list()} class="flex gap-2">
					${s.steps.map(
						(step, index) => html`
							<li ${s.item(step.id)} class="flex-1">
								<button
									${s.trigger(step.id)}
									class="flex w-full items-center gap-2 rounded-md border px-3 py-2 text-left data-[state=current]:border-fd-primary data-[state=current]:bg-fd-accent"
								>
									<span
										${s.indicator(step.id)}
										class="flex size-6 shrink-0 items-center justify-center rounded-full border text-xs data-[state=complete]:bg-fd-primary data-[state=complete]:text-fd-primary-foreground"
										>${index + 1}</span
									>
									<span class="flex flex-col">
										<span class="font-medium">${step.title}</span>
										<span class="text-xs text-fd-muted-foreground">${step.description}</span>
									</span>
								</button>
							</li>
						`,
					)}
				</ol>
				<div ${s.content("account")} class=${panel}>Create your account.</div>
				<div ${s.content("plan")} class=${panel}>Pick the plan that fits.</div>
				<div ${s.content("done")} class=${panel}>
					${stepper.isFinished ? "Finished. Thanks!" : "Press Finish to complete the flow."}
				</div>
				<div class="flex items-center gap-2">
					<button ${s.prev()} class="rounded-md border px-3 py-1 disabled:opacity-40">Back</button>
					<button ${s.next()} class="rounded-md border px-3 py-1 aria-disabled:opacity-40">
						${stepper.isLast ? "Finish" : "Continue"}
					</button>
					<span data-step class="ml-auto text-xs text-fd-muted-foreground">
						Step ${stepper.index + 1} of ${stepper.steps.length}
					</span>
				</div>
			</div>
		`;
	}
}

export function mount(el: HTMLElement) {
	if (!customElements.get(TAG)) customElements.define(TAG, LitDemo);
	const element = document.createElement(TAG);
	el.appendChild(element);
	return () => element.remove();
}
```

## 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](https://stepperize.com/docs/v9/lit/guides/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
  any `LitElement`, or to any other `ReactiveControllerHost`.
- **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 `next` map decides where each step leads from its
  validated data, and a flow can have more than one ending.
- **Resumable.** One `snapshot` goes in, one `onChange` comes 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`](https://stepperize.com/docs/v9/core), shared by every
binding, so the concepts are the same everywhere. Each has its own page,
with Lit code: [steps](https://stepperize.com/docs/v9/lit/concepts/steps),
[the lifecycle of a move](https://stepperize.com/docs/v9/lit/concepts/moves),
[step state](https://stepperize.com/docs/v9/lit/concepts/step-state),
[validation](https://stepperize.com/docs/v9/lit/guides/validation),
[branching](https://stepperize.com/docs/v9/lit/guides/branching) and
[persistence](https://stepperize.com/docs/v9/lit/guides/persistence).

## Next

- [Installation](https://stepperize.com/docs/v9/lit/installation): Add the package to a Lit 3 project.

- [Your first stepper](https://stepperize.com/docs/v9/lit/first-stepper): A working flow, built in four small steps.

- [Primitives](https://stepperize.com/docs/v9/lit/guides/primitives): Accessible directives for your own template.

- [Without Lit](https://stepperize.com/docs/v9/core): The same state machine in tests, servers and agents.
