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

Branching

Decide where each step leads from its data, and end a flow in more than one place.

By default next() goes to the following step in the list. The next option changes that per step.

On this page, stepper is the controller's value: this.stepper.value.

signup.ts
export const signup = defineStepper([
  { id: "account", schema: z.object({ type: z.enum(["personal", "team"]) }) },
  { id: "team" },
  { id: "payment" },
]);
signup-steps.ts
class SignupSteps extends LitElement {
  stepper = signup.controller(this, {
    next: {
      account: (data) => (data.type === "team" ? "team" : "payment"),
    },
  });
}

data is the validated output of that step's schema, and the function must return a known step id: a typo is a type error. Steps without an entry keep going to the following one.

Entry for a stepnext() from that step goes to
NoneThe following step in the list, or the end after the last one.
A functionThe id it returns, or the end if it returns null.
nullThe end of the flow.

A branch runs after the step was validated and before the guard, which receives the step the branch chose as to. Like every callback, it is read when it runs, so it sees your latest state and can also depend on something outside the flow.

Read a property of the element inside the function and it has its current value. Pass the options as a function, controller(this, () => ({ next: ... })), and they are read again before every render.

Depending on an earlier step

The second argument is the data of every step, so a branch can look at an answer given earlier in the flow.

signup.ts
export const signup = defineStepper([
  { id: "account", schema: z.object({ type: z.enum(["personal", "team"]) }) },
  { id: "profile" },
  { id: "team" },
  { id: "payment" },
]);
stepper = signup.controller(this, {
  next: {
    // The account type was answered two steps ago.
    profile: (_data, flow) => (flow.account?.type === "team" ? "team" : "payment"),
  },
});

flow already includes the data being saved by this next() call.

Going back

prev() returns to the step the person came from, not to the previous one in the list. In the example above, going back from payment lands on profile for a personal account and on team for a team.

A skipped step is never entered, so it stays pending and stores no data.

More than one ending

Without an entry, a step leads to the one after it in the list, so only the last step ends the flow. Set an entry to null to end the flow at that step. A branch may also return null.

application.ts
export const application = defineStepper([
  { id: "form", schema: applicationSchema },
  { id: "approved" },
  { id: "rejected" },
]);
stepper = application.controller(this, {
  next: {
    form: (data) => (data.score >= 50 ? "approved" : "rejected"),
    approved: null,
    rejected: null,
  },
  onFinish: (output) => submit(output),
});

Here approved would otherwise lead to rejected, the next step in the list.

The same branches can be given once, to the definition, when every flow made from it shares them: defineStepper(steps, { next: { ... } }). An option given to a flow replaces the definition's option of the same name, as a whole: a next map on the flow replaces the definition's map, it is not merged into it.

isLast

stepper.isLast is true when next() is certain to finish the flow: the step's entry is null, or it is the last step of the list and has no branch. Use it for the label of your button.

html`
  <button type="button" @click=${() => stepper.next()}>
    ${stepper.isLast ? "Submit" : "Continue"}
  </button>
`;

Progress

stepper.progress is the position of the current step in the list, from 0 to 1. With branches that skip steps it jumps accordingly. If you need progress along the path actually taken, count the completed steps instead:

const done = stepper.steps.filter((step) => stepper.state(step.id) === "complete").length;

Compute it in render(), which runs whenever the flow changes.

Edit on GitHub

Last updated on

On this page