# The flow and the stepper

> The three objects you work with, and how they relate.

Framework: Lit (`@stepperize/lit`), Stepperize v9.
Source: https://stepperize.com/docs/v9/lit/concepts/stepper

Three words are used throughout these docs. They are different things.

| Name | What it is | Where it comes from |
| --- | --- | --- |
| **Definition** | The typed description of the steps, with a controller bound to it. Holds no state. | `defineStepper(steps)` |
| **Flow** | One running instance: the current step, the data of every step, which steps are complete. | `controller(this, options)` or `create()` each create one |
| **Stepper** | What your element reads: a picture of the flow at one moment, plus the actions that change it. | The controller's `value` |

```ts
const checkout = defineStepper([{ id: "shipping" }, { id: "payment" }, { id: "review" }]); // definition

class CheckoutSteps extends LitElement {
  stepper = checkout.controller(this); // creates a flow; `value` is its stepper

  render() {
    return html`<p>${this.stepper.value.current.id}</p>`;
  }
}
```

## One flat object

Everything you read and everything you do is directly on the stepper. There
are no nested namespaces to learn.

```ts
const stepper = this.stepper.value; // the stepper as it is now

stepper.current; // the step being shown, with your fields
stepper.index; // its position
stepper.data; // the data of every step
stepper.next(values); // save, validate and move forward
stepper.prev(); // go back
```

Most flows ship with seven members:

| Member | What it gives you |
| --- | --- |
| `current` | The step being shown. |
| `match({...})` | Exhaustive rendering by step id. |
| `next(data?)`, `prev()` | Move through the flow. |
| `isFirst`, `isLast` | What your Back and Continue buttons need. |
| `data` | What each step has saved. |

The rest (branching, snapshots, jumping to a step) is there when you need it.
The [full reference](https://stepperize.com/docs/v9/lit/api/stepper) lists every member.

## Derived from one position

You only express intent: go next, go back, jump to review. The stepper derives
the consequences from the flow's state, so they can never disagree.

```ts
const flow = checkout.create(); // the same flow, without a component

flow.index; // 0
flow.isFirst; // true
flow.progress; // 0

await flow.next();

flow.index; // 1
flow.current.id; // "payment"
flow.state("shipping"); // "complete"
flow.progress; // 0.5
```

## Each read is a snapshot in time

Each change produces a new stepper, and your template renders with it. A
stepper you already read into a variable does not change. That is what makes
it safe to compare, and it has one consequence worth knowing: after an
`await`, that variable still describes the moment before the move.

```ts
async onContinue() {
  const stepper = this.stepper.value;
  const result = await stepper.next(this.values);

  stepper.current.id; // still the step you were on
  this.stepper.value.current.id; // the step the flow is on now
  if (result.accepted) result.to; // where the flow went, or null if it finished
}
```

Read what happened from the result, or read the stepper again. The actions
themselves (`next`, `prev`, `goTo`, `setData`...) always act on the live flow,
so calling them from an old stepper is fine.

## Who owns the flow

The element that creates the flow owns it and configures it.

| You write | The flow lives |
| --- | --- |
| `checkout.controller(this, options)` | In that element. Each controller is a separate flow. |
| `checkout.create(options)` | Wherever you keep the returned object. |

Options go on the owner. Another element follows a flow it does not own with
`checkout.controller(this, flow)`: it reads the flow and calls its actions, but
never changes its options or destroys it.

See [Shared state](https://stepperize.com/docs/v9/lit/guides/shared-state).

Next: [the lifecycle of a move](https://stepperize.com/docs/v9/lit/concepts/moves).
