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

Your first stepper

Define the steps, render the current one, move between them, and finish.

This page builds a three-step checkout from nothing. It takes about five minutes, and every later page builds on it.

1. Define the steps

A step needs an id. Every other field is yours and stays typed.

checkout.ts
import { defineStepper } from "@stepperize/svelte";

export const checkout = defineStepper([
  { id: "shipping", title: "Shipping" },
  { id: "payment", title: "Payment" },
  { id: "review", title: "Review" },
]);

Create the definition once, in a module. It is not state: it is the typed description of the flow, with functions and components bound to it.

2. Render the current step

useStepper() creates a flow owned by the component. What it returns is reactive: read it in markup and the markup follows the flow.

Checkout.svelte
<script lang="ts">
  import { checkout } from "./checkout";

  const stepper = checkout.useStepper();
</script>

<section>
  <h2>{stepper.current.title}</h2>

  {#if stepper.current.id === "shipping"}
    <p>Where should we send it?</p>
  {:else if stepper.current.id === "payment"}
    <p>How would you like to pay?</p>
  {:else}
    <p>Check everything before placing the order.</p>
  {/if}
</section>

stepper.current is the step object you defined, so title is typed, and stepper.current.id is the union of your ids: a typo in the {#if} does not compile.

Do not destructure the state

stepper.current is always the latest, but const { current } = stepper is a copy that never changes. Use const current = $derived(stepper.current) when you want a local name. The actions (next, prev, goTo...) are safe to destructure.

Call useStepper() at the top of the <script>, while the component initialises, like setContext or $effect: the component owns the flow and destroys it when it unmounts. For a flow that lives in a .svelte.ts module instead, see Outside a component.

3. Move

<button type="button" disabled={stepper.isFirst} onclick={() => stepper.prev()}>Back</button>
<button type="button" onclick={() => stepper.next()}>
  {stepper.isLast ? "Place order" : "Continue"}
</button>

next() moves forward and prev() goes back. isFirst and isLast are derived for you, so there is no index arithmetic.

4. Finish

next() on the last step finishes the flow and calls onFinish, once. From then on stepper.isFinished is true, until the flow changes again.

const stepper = checkout.useStepper({
  onFinish: () => alert("Order placed"),
});
{#if stepper.isFinished}<p>Thanks for your order.</p>{/if}

The whole thing

Checkout.svelte
<script module lang="ts">
  import { defineStepper } from "@stepperize/svelte";

  const checkout = defineStepper([
    { id: "shipping", title: "Shipping" },
    { id: "payment", title: "Payment" },
    { id: "review", title: "Review" },
  ]);
</script>

<script lang="ts">
  const stepper = checkout.useStepper({
    onFinish: () => alert("Order placed"),
  });
</script>

{#if stepper.isFinished}
  <p>Thanks for your order.</p>
{:else}
  <section>
    <p>Step {stepper.index + 1} of {stepper.steps.length}</p>
    <h2>{stepper.current.title}</h2>

    {#if stepper.current.id === "shipping"}
      <p>Where should we send it?</p>
    {:else if stepper.current.id === "payment"}
      <p>How would you like to pay?</p>
    {:else}
      <p>Check everything before placing the order.</p>
    {/if}

    <button type="button" disabled={stepper.isFirst} onclick={() => stepper.prev()}>Back</button>
    <button type="button" onclick={() => stepper.next()}>
      {stepper.isLast ? "Place order" : "Continue"}
    </button>
  </section>
{/if}

What you did not write

  • No index $state, and no clamping at the edges.
  • No comparing against untyped strings: a typo in a step id does not compile.
  • No "is this the last step?" logic.

Where to go next

  • Collect data and validate it: Forms.
  • Read the flow from several components: Shared state.
  • Use ready-made accessible components: Primitives.
  • Skip steps depending on the answers: Branching (the options are the same in every binding).
Edit on GitHub

Last updated on

On this page