Not only React anymoreStepperize now runs in Vue, Svelte, Solid, Angular, Lit, and HTML
Skip to Content
Stepperize

Primitives

Element directives that handle roles, keyboard, focus and announcements.

The controller carries a set of element directives, bound to your steps. Each one goes on an element of your own template and takes care of the parts that are easy to get wrong: roles, ids, keyboard navigation, focus and announcements. The markup, the tag names and the styles stay yours.

For a simple flow, your own buttons and stepper.match are enough.

Basic structure

import { html, LitElement } from "lit";
import { checkout } from "./checkout";

class CheckoutSteps extends LitElement {
  stepper = checkout.controller(this, { linear: true, onFinish: placeOrder });

  render() {
    const s = this.stepper;

    return html`
      <div ${s.root()}>
        <ol ${s.list()}>
          ${s.steps.map(
            (step) => html`
              <li ${s.item(step.id)}>
                <button ${s.trigger(step.id)}>
                  <span ${s.indicator(step.id)}></span>
                  ${step.title}
                </button>
              </li>
            `,
          )}
        </ol>

        <div ${s.content("shipping")}><shipping-form></shipping-form></div>
        <div ${s.content("payment")}><payment-form></payment-form></div>
        <div ${s.content("review")}><order-review></order-review></div>

        <button ${s.prev()}>Back</button>
        <button ${s.next()}>Continue</button>
      </div>
    `;
  }
}

Loading lit…

How the pieces connect

  1. root() marks the element that contains the stepper. Every other part must be inside it, in your template or in a child component's; clicks and keys are handled there.
  2. list() marks the list of steps and gives it arrow-key navigation.
  3. item(id) is one step.
  4. trigger(id) goes to that step when pressed.
  5. indicator(id) is a decorative mark, hidden from assistive technology.
  6. content(id) is the panel of a step: hidden unless its step is current.
  7. prev() and next() call prev() and next() on the flow.

Step ids are typed: s.content("shiping") does not compile.

A directive is written inside the opening tag, next to the attributes. It adds to what you wrote: class, style, your own attributes and your own @click stay. See Your attributes for the ones a directive also sets.

Why directives and not custom elements

A <stepper-trigger> element would put its button in a shadow root of its own. aria-controls and aria-labelledby cannot point across shadow roots, so a tab could not name its panel, and your styles could not reach the button. Directives leave every element in your template, in one root, and keep step ids typed by your definition.

They work the same in shadow DOM and in light DOM (createRenderRoot() returning this).

Three patterns, chosen for you

The flowWhat is rendered
Not linear, with triggersA tablist: triggers are tabs with roving focus and arrow keys, panels are tabpanels.
linearAn ordered list of buttons with aria-current="step". Steps that cannot be reached yet are disabled.
No triggersPanels are groups named by the step's title.

On every step change the new step is announced to screen readers by its title, and if the focused element was hidden or removed, focus moves to the new panel instead of being lost.

Styling

Nothing is styled. Every element has a data-component and the state it shows:

AttributeOnValues
data-stateitem, trigger, indicatorpending, current, complete
data-invaliditem, trigger, indicatorPresent after a failed validation.
data-disableditem, trigger, prev, nextPresent when it cannot be used.
data-pendingroot, prev, nextPresent while an async check or onFinish runs.
data-finishedrootPresent once the flow has finished.
data-orientationroot, listhorizontal, vertical
static styles = css`
  [data-component="stepper-trigger"][data-state="current"] { font-weight: 600; }
  [data-component="stepper-indicator"][data-state="complete"] { background: green; }
`;

Reading a step's state

s.at(id) returns what an item knows about its step, for your own markup:

${s.steps.map((step, index) => {
  const item = s.at(step.id);
  return html`<li ${s.item(step.id)}>
    <button ${s.trigger(step.id)}>${item.state === "complete" ? "✓" : index + 1} ${step.title}</button>
  </li>`;
})}

Handling the move yourself

Your own @click runs first. Call preventDefault() in it to take over, for example to pass form values:

html`<button
  ${s.next()}
  @click=${(event: Event) => {
    event.preventDefault();
    s.value.next(this.values);
  }}
>
  Continue
</button>`;

Your attributes

A directive never removes an attribute it did not set. Where you and the directive both set one:

AttributeOnWho wins
role, aria-label, aria-labelledby, tabindexroot, list, item, content, indicatorYours. A name you give replaces the stepper's, both ways of naming included.
typeany <button>Yours. Otherwise type="button".
disabled, hiddenany partBoth. Yours adds to the stepper's: disabled if either says so. aria-disabled and data-disabled follow.
ARIA and idtrigger, prev, next, and the id of contentThe stepper's. They are what make the tabs, the keyboard and focus work.
data-* the stepper setsany partThe stepper's, and yours comes back when it stops setting one.
html`
  <div ${s.root()} aria-label="Checkout">…</div>
  <section ${s.content("payment")} aria-labelledby="payment-heading">
    <h3 id="payment-heading">Pay</h3>
  </section>
`;

To disable a button on top of the flow, prefer the option: s.next({ disabled: this.saving }). A ?disabled=${…} binding works too, with one gap: when it turns on while the stepper has already disabled the button (Back on the first step), the stepper cannot see it, and enables the button when the flow moves on.

Trigger and panel ids are the stepper's: the tabs, aria-controls, the panel's name and the focus hand-off are tied by them. Select a panel by [data-step="payment"] instead of an id of your own.

Parts in a child component

The parts can be rendered by another element, in its own shadow root: pass it the controller as a property.

import type { StepperPrimitives } from "@stepperize/lit";

class CheckoutSteps extends LitElement {
  stepper = checkout.controller(this);

  render() {
    const s = this.stepper;
    return html`
      <div ${s.root()}>
        <ol ${s.list()}>…</ol>
        <div ${s.content("shipping")}>…</div>
        <checkout-nav .stepper=${s}></checkout-nav>
      </div>
    `;
  }
}

class CheckoutNav extends LitElement {
  static properties = { stepper: { attribute: false } };
  declare stepper: StepperPrimitives<typeof checkout.steps>;

  render() {
    return html`<button ${this.stepper.prev()}>Back</button><button ${this.stepper.next()}>Continue</button>`;
  }
}

Parts that arrive later, from a child component, until or anything else, are painted as soon as their template is in place. Clicks reach the root and keys reach the list from inside the child's shadow root.

Ids do not cross shadow roots, so two things need one root: a trigger and the panel it controls name each other by id, and focus moves to the new panel by id. Render the panels, and the triggers of a flow shown as tabs, in the same shadow root as root(). In development a trigger or panel in another root is reported as SZ004. Everything else can be in a child's shadow root: the list and its items, indicators, Back and Continue, and the triggers of a linear flow whose steps have a title.

Ending the stepper

this.stepper.destroy() ends the controller and stops its directives: see Ending a flow.

Panels in or out of the template

content(id) hides its element while the step is not current, so the fields inside keep their state. To drop a panel from the DOM instead, leave it out of the template:

${s.value.current.id === "review" ? html`<div ${s.content("review")}>…</div>` : nothing}

With server rendering, either way needs a binding or a condition so the server HTML only shows the current panel.

See the primitives reference for every option.

Edit on GitHub

Last updated on

On this page