# defineStepper

> The definition and everything it returns.

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

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

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/svelte/api/options). |

It returns:

| | |
| --- | --- |
| `steps` | The array you passed. |
| `useStepper(options?)` | A flow owned by the calling component. |
| `useStepperContext()` | The flow of the nearest `Provider` or `Stepper.Root`. |
| `Provider` | Shares one flow with the components below. |
| `Stepper` | The [primitives](https://stepperize.com/docs/v9/svelte/api/primitives). Not in `/headless`. |
| `create(options?)` | A flow without Svelte. See [Core](https://stepperize.com/docs/v9/core). Wrap it in [`fromFlow`](#fromflow) to read it reactively. |

## useStepper

```ts
const stepper = checkout.useStepper();
const stepper = checkout.useStepper({ linear: true, onFinish });
const stepper = checkout.useStepper(() => ({ step: page.params.step }));
```

Call it while the component initialises. It returns the
[stepper](https://stepperize.com/docs/v9/svelte/api/stepper) as a reactive object: reads are tracked,
so markup, `$derived` and `$effect` follow the flow. Do not destructure its
state.

- Options can be an object or a function. A function is read again when the
  state it uses changes, and the flow gets the new options.
- `snapshot` is read once, when the flow is created.
- The flow is destroyed with the component: a running move is cancelled.
- On the server it renders the current step and runs no callback.
- Outside a component (module scope, an event handler) it throws
  [SZ104](https://stepperize.com/docs/v9/core/errors#sz104) in the browser: nothing would destroy
  the flow. On the server it does not throw, so a call at module scope can
  pass server rendering and fail once the page runs; never call it there. Use
  [`fromFlow`](#fromflow) instead.

## useStepperContext

```ts
const stepper = checkout.useStepperContext();
```

Returns the shared reactive stepper. Call it while the component initialises.
Outside the flow's `Provider` or `Stepper.Root` it throws
[SZ100](https://stepperize.com/docs/v9/core/errors#sz100).

There is no selector argument: wrap what you need in `$derived`.

## Provider

```svelte
<checkout.Provider linear snapshot={saved} onChange={save} onFinish={submit}>
  {@render children()}
</checkout.Provider>
```

Every [option](https://stepperize.com/docs/v9/svelte/api/options) is a prop. It renders its children
and no element.

## fromFlow

```ts
import { fromFlow } from "@stepperize/svelte";

const flow = checkout.create({ linear: true });
const stepper = fromFlow(flow);
```

Turns a flow you created into the same reactive stepper `useStepper()`
returns. It creates no effect, so it works in a `.svelte.ts` module, a class
or a component that receives a flow from elsewhere. It is also exported by
`/headless`.

- It subscribes to the flow only while an effect reads it, and stops with the
  last one. Outside an effect a read returns the latest state.
- The flow stays yours: change its options with `flow.setOptions()` and stop it
  with `flow.destroy()`.
- At module scope the flow is shared by everything that imports it, on the
  server too: see [Outside a component](https://stepperize.com/docs/v9/svelte/guides/shared-state#outside-a-component).

## Types

Everything `@stepperize/core` exports as a type is re-exported:
`Stepper<S>`, `Options<S>`, `Snapshot<S>`, `Result<S>`, `Id<S>` and the rest.
The primitives' props are exported too (`RootProps<S>`, `ButtonProps`,
`ChildProps` and the others): see [Primitives](https://stepperize.com/docs/v9/svelte/api/primitives).
