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

Shared state

One flow read by several components, with a Provider and useStepperContext.

useStepper() gives one component its own flow. When the step list, the current panel and the buttons live in different components, share one flow instead.

Provider

Checkout.vue
<script setup lang="ts">
import { checkout } from "./checkout";
import CheckoutHeader from "./CheckoutHeader.vue";
import CheckoutBody from "./CheckoutBody.vue";

const { Provider } = checkout;
</script>

<template>
  <Provider linear @finish="placeOrder">
    <CheckoutHeader />
    <CheckoutBody />
  </Provider>
</template>

Provider takes every option as a prop and renders no element. The callbacks can be written as listeners: @change, @finish and @invalid-step are onChange, onFinish and onInvalidStep.

Stepper.Root is a Provider too: components inside it can call useStepperContext().

useStepperContext

CheckoutHeader.vue
<script setup lang="ts">
import { checkout } from "./checkout";

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

<template>
  <p>Step {{ stepper.index + 1 }} of {{ stepper.steps.length }}</p>
</template>

It returns the same reactive stepper as useStepper(). Outside the flow's own Provider it throws SZ100. Providers nest: each composable reads the nearest Provider of its own definition.

Selectors

Pass a function to get a computed with just what you need:

const isLast = checkout.useStepperContext((stepper) => stepper.isLast);
// ComputedRef<boolean>

Vue already tracks what a template reads, so selectors are not needed for performance the way they are in React. They are convenient when you want a ref to pass along or to watch:

const step = checkout.useStepperContext((stepper) => stepper.current.id);
watch(step, (id) => analytics.track("step", { id }));

Watch the stepper through a selector or a getter: watch(stepper, ...) never fires.

In a Pinia store

A setup store can own the flow. The store does not wrap the stepper, so store.stepper is the stepper itself, actions and all.

stores/checkout.ts
import { defineStore } from "pinia";
import { checkout } from "~/flows/checkout";

export const useCheckoutStore = defineStore("checkout", () => {
  const stepper = checkout.useStepper();
  return { stepper };
});

It is not part of the store's state: persist it with onChange and snapshot, not with a state plugin.

Outside a component

A flow does not need a component. checkout.create() returns the same state machine with get() and subscribe(), for tests and servers; and useStepper() works inside an effectScope(), which destroys the flow when it stops. Called with neither (after an await in a hand-written async setup()), the flow is never destroyed, and in development Stepperize warns (SZ104).

Edit on GitHub

Last updated on

On this page