# defineStepper

> The definition and the signals API it returns.

Framework: Angular (`@stepperize/angular`), Stepperize v9.
Source: https://stepperize.com/docs/v9/angular/api/define-stepper

```ts
import { defineStepper } from "@stepperize/angular";

const checkout = defineStepper(steps, options?);
```

| Argument | Description |
| --- | --- |
| `steps` | An array of steps. Each needs a unique `id`; `schema` types and validates its data; every other field is yours. |
| `options` | Defaults for every flow created from this definition. See [Options](https://stepperize.com/docs/v9/angular/api/options). |

It returns:

| | |
| --- | --- |
| `steps` | The array you passed. |
| `injectStepper(options?)` | A flow owned by the caller. |
| `provideStepper(options?)` | A provider that shares one flow. |
| `injectStepperContext()` | The flow of the nearest `provideStepper()` or `stepperRoot`. |
| `create(options?)` | A flow without Angular. See [Core](https://stepperize.com/docs/v9/core). |

## injectStepper

```ts
readonly stepper = checkout.injectStepper();
readonly stepper = checkout.injectStepper({ linear: true, onFinish });
readonly stepper = checkout.injectStepper(() => ({ step: this.step() }));
```

Call it in an injection context: a field initialiser, a constructor or a
factory. Outside one it throws `NG0203`; in development the message names
`injectStepper()`.

It returns a `StepperRef`: a signal of the
[stepper](https://stepperize.com/docs/v9/angular/api/stepper), which is a new immutable view after
every change, with these properties:

| Property | Description |
| --- | --- |
| `steps` | The steps of the definition, for `@for`. |
| `flow` | The flow itself, to hand to code outside the component. |

Whether the flow is linear right now is `stepper().isLinear`.

### When options apply

- Options can be an object or a function. A function runs in the caller's
  injection context, so it can `inject()`, and is read again when the signals
  it uses change.
- Callbacks (`onChange`, `onFinish`, `onInvalidStep`, `guard`) and `next` are
  read when the flow calls them, so the latest ones always run, even when you
  change them and move in the same tick.
- `linear` applies before the next render, and as soon as you read the
  stepper: `stepper().canGoTo(…)` right after changing it already uses it.
- The flow is created before the owner's first render, or the first time it is
  read if that is earlier. `snapshot` is read at that moment, so it can come
  from an input. Read too early (in a constructor, before a required input is
  set), creation throws Angular's error; it is tried again once that input is
  set, and the stepper recovers.
- Following `step` happens before each render, in the browser only. On the
  server the flow renders the step it was created on and no callback runs.
- The flow is destroyed with the injector it was created in: the component,
  directive or service. A move still running is cancelled.

## provideStepper

```ts
@Component({ providers: [checkout.provideStepper({ linear: true })] })
```

A provider for a component, a route or the application. Each injector that has
it gets its own flow. The options function runs in that injector's context, so
it can call `inject()`.

## injectStepperContext

```ts
readonly stepper = checkout.injectStepperContext();
```

Returns the `StepperRef` of the nearest `provideStepper()` of this definition,
or of the nearest `[stepperRoot]` bound to one of its steppers. Providers and
roots of other definitions are skipped. Without either it throws
[SZ100](https://stepperize.com/docs/v9/core/errors#sz100).

It takes no selector, unlike React's `useStepperContext`: a signal already is
one. Wrap what you need in `computed()`, and the template only updates when
that value changes.

```ts
readonly title = computed(() => this.stepper().current.title);
```

## Types

Everything `@stepperize/core` exports as a type is re-exported:
`Stepper<S>`, `Flow<S>`, `Options<S>`, `Snapshot<S>`, `Result<S>`, `Id<S>` and
the rest, plus `StepperRef<S>` and `Definition<S>`. The full entry adds
`StepperRegistry`, `StepId` and `StepFor` for
[typed step ids in templates](https://stepperize.com/docs/v9/angular/guides/primitives#typed-step-ids).
