# Introduction

> An accessible stepper on HTML that is already on the page, with no framework.

Framework: HTML (`@stepperize/core/dom`), Stepperize v9.
Source: https://stepperize.com/docs/v9/html

`@stepperize/core/dom` is for pages whose HTML comes from somewhere else: a
Rails, Laravel or Django template, an Astro page, a static file. You write the
markup; one function makes it a stepper.

```bash
npm install @stepperize/core
```

```html
<div data-component="stepper" id="signup">
  <ol data-component="stepper-list">
    <li data-component="stepper-item" data-step="account">
      <button data-component="stepper-trigger">Account</button>
    </li>
    <li data-component="stepper-item" data-step="plan">
      <button data-component="stepper-trigger">Plan</button>
    </li>
  </ol>

  <div data-component="stepper-content" data-step="account">…</div>
  <div data-component="stepper-content" data-step="plan">…</div>

  <button data-component="stepper-prev">Back</button>
  <button data-component="stepper-next">Continue</button>
</div>
```

```ts
import { createStepper } from "@stepperize/core";
import { bindStepper } from "@stepperize/core/dom";

const flow = createStepper([
  { id: "account", title: "Account" },
  { id: "plan", title: "Plan" },
]);

const unbind = bindStepper(document.getElementById("signup")!, flow);
```

Here it is running. The markup below was plain HTML before `bindStepper` ran:

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

```ts
import { createStepper } from "@stepperize/core";
import { bindStepper } from "@stepperize/core/dom";

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

const trigger =
	"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";
const indicator =
	"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";
const panel = "rounded-md bg-fd-muted p-3";

// What a server template would have rendered: `hidden` on the panels and `disabled` on Back
// avoid a flash before the script runs. Nothing below this string creates markup.
const html = `
<div data-component="stepper" data-demo="html" class="not-prose my-4 flex flex-col gap-4 rounded-lg border p-4 text-sm">
	<ol data-component="stepper-list" class="flex gap-2">
		${steps
			.map(
				(step, index) => `
		<li data-component="stepper-item" data-step="${step.id}" class="flex-1">
			<button data-component="stepper-trigger" class="${trigger}">
				<span data-component="stepper-indicator" class="${indicator}">${index + 1}</span>
				<span class="flex flex-col">
					<span data-component="stepper-title" class="font-medium"></span>
					<span data-component="stepper-description" class="text-xs text-fd-muted-foreground"></span>
				</span>
			</button>
		</li>`,
			)
			.join("")}
	</ol>
	<div data-component="stepper-content" data-step="account" class="${panel}">Create your account.</div>
	<div data-component="stepper-content" data-step="plan" class="${panel}" hidden>Pick the plan that fits.</div>
	<div data-component="stepper-content" data-step="done" class="${panel}" hidden data-done>Press Finish to complete the flow.</div>
	<div class="flex items-center gap-2">
		<button data-component="stepper-prev" class="rounded-md border px-3 py-1 disabled:opacity-40" disabled>Back</button>
		<button data-component="stepper-next" class="rounded-md border px-3 py-1 aria-disabled:opacity-40">Continue</button>
		<span data-step-count class="ml-auto text-xs text-fd-muted-foreground"></span>
	</div>
</div>`;

export function mount(el: HTMLElement) {
	el.innerHTML = html;
	const root = el.firstElementChild as HTMLElement;
	const flow = createStepper(steps);
	// Tabs, because the flow is not linear. `unbind` puts the markup back as it was.
	const unbind = bindStepper(root, flow);
	// The parts are bound; anything else on the page reads the flow.
	const paint = () => {
		const count = root.querySelector("[data-step-count]");
		const done = root.querySelector("[data-done]");
		const next = root.querySelector('[data-component="stepper-next"]');
		if (count)
			count.textContent = `Step ${flow.index + 1} of ${flow.steps.length}`;
		if (next) next.textContent = flow.isLast ? "Finish" : "Continue";
		if (done)
			done.textContent = flow.isFinished
				? "Finished. Thanks!"
				: "Press Finish to complete the flow.";
	};
	const unsubscribe = flow.subscribe(paint);
	paint();
	return () => {
		unsubscribe();
		unbind();
		flow.destroy();
		el.innerHTML = "";
	};
}
```

## What it does

`bindStepper` adds nothing to your content. It takes the elements you marked
and keeps them in sync with the flow:

- **Roles and ids.** A tablist with tabs and tab panels, or an ordered list of
  steps with `aria-current`, or named groups: the same three patterns as the
  [framework primitives](https://stepperize.com/docs/v9/react/guides/accessibility). A linear flow is
  the list, any other the tabs. Ids, names and roles you wrote are kept.
- **State for your CSS.** `data-state` and `data-invalid` on items, triggers
  and indicators; `data-disabled` on items, triggers, Prev and Next; `data-pending`
  on the root, Prev and Next.
- **Panels.** The panel of the current step is shown; the others get `hidden`.
- **Keyboard.** Arrow keys, Home and End between tabs, mirrored in
  right-to-left pages.
- **Focus and announcements.** The new step is announced to screen readers,
  and focus moves to the new panel when the focused field was hidden.
- **Buttons.** Triggers, Prev and Next move the flow, and are disabled when
  they cannot.

The flow itself is [`createStepper`](https://stepperize.com/docs/v9/core/create-stepper): validation,
branching, guards and snapshots all work as documented there.

## Good to know

- **No templating.** It never creates your content. The only element it adds
  is a visually hidden live region for announcements.
- **Undoable.** The function it returns puts the markup back as the server
  sent it, so binding again (Turbo, htmx, hot reload) starts clean. So does
  binding a copy of bound markup, such as the snapshot Turbo or htmx shows on
  Back: the binding recognises what an earlier one wrote.
- **Several steppers.** Call it once per stepper. Each one only touches its own
  parts, including when one stepper is nested in another's panel.
- **Markup that arrives later.** Parts swapped in by htmx, Alpine or Turbo
  become part of the stepper on their own; other changes on the page cost
  nothing.
- **Shadow DOM.** A stepper inside a web component's shadow root works the
  same: ids, focus and keys stay in that shadow root.
- **Server safe.** Importing the module touches nothing; only `bindStepper`
  needs a DOM. The main `@stepperize/core` entry has no DOM code at all.
- **Small.** About 4.5 KiB min + gzip on top of the 2.3 KiB state machine.

## Next

- [Markup and options](https://stepperize.com/docs/v9/html/markup): Every part, every attribute it reads and writes.

- [Server-rendered pages](https://stepperize.com/docs/v9/html/guides/server-rendered): Rails, Laravel, Django and Astro.

- [Alpine](https://stepperize.com/docs/v9/html/guides/alpine): Read the flow in x-text and x-show.

- [htmx](https://stepperize.com/docs/v9/html/guides/htmx): Let the server validate each step.
