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

Server-rendered pages

Rails, Laravel, Django and Astro, with one small script.

The pattern is the same in every server framework: the template renders the steps, a script binds them.

The script

Keep the steps next to the markup, as data, so the template is the single source of truth:

<div
  data-component="stepper"
  id="checkout"
  data-linear
  data-steps='[{"id":"shipping","title":"Shipping"},{"id":"payment","title":"Payment"}]'
>
  …
</div>
stepper.ts
import { createStepper } from "@stepperize/core";
import { bindStepper } from "@stepperize/core/dom";

for (const root of document.querySelectorAll<HTMLElement>('[data-component="stepper"][data-steps]')) {
  const flow = createStepper(JSON.parse(root.dataset.steps ?? "[]"), {
    // `data-linear` is this page's own convention: bindStepper reads `isLinear` from the flow.
    linear: root.hasAttribute("data-linear"),
  });
  bindStepper(root, flow);
}

Without a bundler, import both modules in a <script type="module"> from a CDN. esm.sh prepares packages for the browser; it builds them for production, and ?dev keeps the development warnings:

<script type="module">
  import { createStepper } from "https://esm.sh/@stepperize/core";
  import { bindStepper } from "https://esm.sh/@stepperize/core/dom";
</script>

A CDN that serves the published files untouched, such as jsDelivr or unpkg, works too. Point at the files in dist, and pin the version you tested:

<script type="module">
  import { createStepper } from "https://cdn.jsdelivr.net/npm/@stepperize/core/dist/index.js";
  import { bindStepper } from "https://cdn.jsdelivr.net/npm/@stepperize/core/dist/dom.js";
</script>

Those files leave out development warnings by reading process.env.NODE_ENV, which bundlers replace. A browser has no process, so there they run as in production: no warnings, and nothing breaks.

Templates

Loop over your steps and write the parts. hidden on the panels that are not first avoids a flash before the script runs.

<div data-component="stepper" id="checkout" data-linear data-steps="<%= @steps.to_json %>">
  <ol data-component="stepper-list">
    <% @steps.each do |step| %>
      <li data-component="stepper-item" data-step="<%= step[:id] %>">
        <button data-component="stepper-trigger"><%= step[:title] %></button>
      </li>
    <% end %>
  </ol>

  <% @steps.each_with_index do |step, index| %>
    <div data-component="stepper-content" data-step="<%= step[:id] %>" <%= "hidden" unless index.zero? %>>
      <%= render "checkout/#{step[:id]}" %>
    </div>
  <% end %>

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

Resuming where the user was

A snapshot is plain JSON. Have the server write the saved one next to the steps and save the new one on every change:

const flow = createStepper(steps, {
  snapshot: JSON.parse(root.dataset.snapshot ?? "{}"),
  onChange: (snapshot) => {
    navigator.sendBeacon("/checkout/draft", JSON.stringify(snapshot));
  },
});

The server can then render the right panel without hidden, so the first HTML is already on the saved step.

Edit on GitHub

Last updated on

On this page