# Steps and ids

> Model each step with a stable id, and keep your own data on it.

Framework: Angular (`@stepperize/angular`), Stepperize v9.
Source: https://stepperize.com/docs/v9/angular/concepts/steps

A step is a plain object. Only `id` is required; everything else belongs to
your app.

```ts title="checkout.ts"
import { defineStepper } from "@stepperize/angular";

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

The definition is the contract for the whole flow. The stepper, `match`, the
data of each step, the `next` map and the primitives all use these same ids,
and TypeScript knows them as the union `"shipping" | "payment" | "review"`.

On this page, `stepper` is the value of the signal `injectStepper()` returns:
in a template, `stepper()`.

## Step ids

Ids are what you save, put in the URL and branch on. Pick ones that describe
the business step, not the current label or position.

| Good | Avoid | Why |
| --- | --- | --- |
| `"shipping"` | `"Step 1"` | Reordering steps would change its meaning. |
| `"payment"` | `"Pay now"` | Copy changes; ids should not. |
| `"review"` | `"confirm-2026"` | Generated ids cannot be typed or bookmarked. |

Ids must be unique. An empty list or a repeated id throws
[SZ103](https://stepperize.com/docs/v9/core/errors#sz103) when the first flow is created from the
definition: the first component that uses it, or the first `create()`.

## Your own fields

Any other field stays on the step, fully typed, wherever Stepperize hands you a
step: `stepper.current`, `stepper.steps`, the `match` handlers and the
primitives.

```ts title="step-header.component.ts"
import { Component } from "@angular/core";
import { checkout } from "./checkout";
import { Icon } from "./icon.component";

@Component({
  selector: "app-step-header",
  imports: [Icon],
  template: `
    <h2><app-icon [name]="stepper().current.icon" /> {{ stepper().current.title }}</h2>
  `,
})
export class StepHeader {
  readonly stepper = checkout.injectStepperContext();
}
```

Two fields have a meaning for the library:

| Field | Used for |
| --- | --- |
| `schema` | Types the step's data, and `next()` validates against it. See [Validation](https://stepperize.com/docs/v9/angular/guides/validation). |
| `title`, `description` | Shown by the title and description primitives. A string `title` is also what screen readers hear when the step changes. |

Everything else (`icon`, `optional`, `estimatedMinutes`, a component) is yours.

## Define once, at module scope

A definition holds no state. It is the description of the flow, plus what your
framework needs to use it. Create it once, in a module, outside your
components, and import it where you need it.

```ts
// Wrong: a new definition for every instance of the component. No other
// component can import it, so none can read this flow.
export class Checkout {
  readonly checkout = defineStepper([{ id: "shipping" }, { id: "payment" }]);
  readonly stepper = this.checkout.injectStepper();
}
```

Every flow created from a definition is independent, so one definition can
back several flows on a page.

## Keep ids literal

Ids stay literal when the array is written inline. If you build the array
first, add `as const`, or the ids widen to `string` and you lose the checks.

```ts
// Ids are string: nothing is checked.
const steps = [{ id: "account" }, { id: "profile" }];
const loose = defineStepper(steps);

// Ids are "account" | "profile".
const inline = defineStepper([{ id: "account" }, { id: "profile" }]);

const list = [{ id: "account" }, { id: "profile" }] as const;
const pinned = defineStepper(list);
```

## Fields that only some steps have

`stepper.current` is the union of all your steps. A field that exists on some
steps only is not available until you know which step you are on. `match`
narrows for you:

```ts title="signup.ts"
export const signup = defineStepper([
  { id: "account", title: "Account", help: "We never share your email." },
  { id: "done", title: "All set" },
]);
```

```ts title="step-body.component.ts"
import { Component, computed } from "@angular/core";
import { signup } from "./signup";

@Component({
  selector: "app-step-body",
  template: `<p>{{ body() }}</p>`,
})
export class StepBody {
  readonly stepper = signup.injectStepperContext();
  readonly body = computed(() =>
    this.stepper().match({
      account: (step) => step.help,
      done: (step) => step.title,
    }),
  );
}
```

Comparing `stepper.current.id` narrows it too.

```ts
// A template narrows a local, not a call: name the step with @let.
template: `
  @let current = stepper().current;
  @if (current.id === "account") {
    <p>{{ current.help }}</p>
  }
`,
```

Next: [the flow and the stepper](https://stepperize.com/docs/v9/angular/concepts/stepper), the
objects you work with at runtime.
