Introduction
An accessible stepper on HTML that is already on the page, with no framework.
@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.
npm install @stepperize/core<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>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:
Loading html…
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. A linear flow is the list, any other the tabs. Ids, names and roles you wrote are kept. - State for your CSS.
data-stateanddata-invalidon items, triggers and indicators;data-disabledon items, triggers, Prev and Next;data-pendingon 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: 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
bindStepperneeds a DOM. The main@stepperize/coreentry has no DOM code at all. - Small. About 4.5 KiB min + gzip on top of the 2.3 KiB state machine.
Next
Edit on GitHub
Last updated on