# Primitives

> Unstyled components that handle roles, keyboard, focus and announcements.

Framework: Vue (`@stepperize/vue`), Stepperize v9.
Source: https://stepperize.com/docs/v9/vue/guides/primitives

Every definition comes with a `Stepper` object: components bound to your steps.
They render plain elements with no styles, and take care of the parts that are
easy to get wrong: roles, ids, keyboard navigation, focus and announcements.

For a simple flow, your own buttons and a `v-if` are enough.

## Basic structure

```vue title="CheckoutSteps.vue"
<script setup lang="ts">
import { checkout } from "./checkout";

const { Stepper } = checkout;
</script>

<template>
  <Stepper.Root linear @finish="placeOrder">
    <Stepper.List>
      <template #item>
        <Stepper.Item>
          <Stepper.Trigger>
            <Stepper.Indicator />
            <Stepper.Title />
          </Stepper.Trigger>
        </Stepper.Item>
      </template>
    </Stepper.List>

    <Stepper.Content step="shipping"><ShippingForm /></Stepper.Content>
    <Stepper.Content step="payment"><PaymentForm /></Stepper.Content>
    <Stepper.Content step="review"><Review /></Stepper.Content>

    <Stepper.Prev>Back</Stepper.Prev>
    <Stepper.Next>Continue</Stepper.Next>
  </Stepper.Root>
</template>
```

The live demo runs in the browser. Its source ([VueDemo.vue](https://github.com/damianricobelli/stepperize/blob/main/apps/docs/src/components/framework-demos/VueDemo.vue)):

```vue
<script lang="ts">
import { defineStepper } from "@stepperize/vue";

// Module scope: one definition, shared by every instance of this component.
const onboarding = defineStepper([
	{ id: "account", title: "Account", description: "Who you are" },
	{ id: "plan", title: "Plan", description: "What you need" },
	{ id: "done", title: "Done", description: "All set" },
]);
</script>

<script setup lang="ts">
import { ref } from "vue";

const { Stepper } = onboarding;
const finished = ref(false);
</script>

<template>
	<Stepper.Root
		v-slot="stepper"
		class="not-prose my-4 flex flex-col gap-4 rounded-lg border p-4 text-sm"
		data-demo="vue"
		@finish="finished = true"
		@change="finished = false"
	>
		<Stepper.List class="flex gap-2">
			<template #item="{ index }">
				<Stepper.Item class="flex-1">
					<Stepper.Trigger
						class="flex w-full items-center gap-2 rounded-md border px-3 py-2 text-left data-[state=current]:border-fd-primary data-[state=current]:bg-fd-accent"
					>
						<Stepper.Indicator
							class="flex size-6 shrink-0 items-center justify-center rounded-full border text-xs data-[state=complete]:bg-fd-primary data-[state=complete]:text-fd-primary-foreground"
						>
							{{ index + 1 }}
						</Stepper.Indicator>
						<span class="flex flex-col">
							<Stepper.Title class="font-medium" />
							<Stepper.Description class="text-xs text-fd-muted-foreground" />
						</span>
					</Stepper.Trigger>
				</Stepper.Item>
			</template>
		</Stepper.List>

		<Stepper.Content step="account" class="rounded-md bg-fd-muted p-3">Create your account.</Stepper.Content>
		<Stepper.Content step="plan" class="rounded-md bg-fd-muted p-3">Pick the plan that fits.</Stepper.Content>
		<Stepper.Content step="done" class="rounded-md bg-fd-muted p-3">
			{{ finished ? "Finished. Thanks!" : "Press Finish to complete the flow." }}
		</Stepper.Content>

		<div class="flex items-center gap-2">
			<Stepper.Prev class="rounded-md border px-3 py-1 disabled:opacity-40">Back</Stepper.Prev>
			<Stepper.Next class="rounded-md border px-3 py-1 aria-disabled:opacity-40">
				{{ stepper.isLast ? "Finish" : "Continue" }}
			</Stepper.Next>
			<span data-step class="ml-auto text-xs text-fd-muted-foreground">
				Step {{ stepper.index + 1 }} of {{ stepper.steps.length }}
			</span>
		</div>
	</Stepper.Root>
</template>
```

## How the pieces connect

1. `Stepper.Root` creates the flow and provides it. It takes the options as
   props and listeners.
2. `Stepper.List` renders the list and handles arrow keys.
3. Its `#item` slot is rendered once per step.
4. `Stepper.Item` is one step. Everything inside it knows which one.
5. `Stepper.Trigger` goes to that step when pressed.
6. `Stepper.Content` renders while its `step` is current.
7. `Stepper.Prev` and `Stepper.Next` call `prev()` and `next()`.

`step` is typed: `<Stepper.Content step="shiping">` is an error in the editor
and in `vue-tsc`.

## Reading the flow in the template

The default slot of `Root` receives the stepper, and the default slot of `Item`
receives that item:

```vue
<Stepper.Root v-slot="stepper">
  <p>Step {{ stepper.index + 1 }} of {{ stepper.steps.length }}</p>

  <Stepper.List>
    <template #item="{ step, index }">
      <Stepper.Item v-slot="item">
        <Stepper.Trigger>
          {{ item.state === "complete" ? "✓" : index + 1 }} {{ step.title }}
        </Stepper.Trigger>
      </Stepper.Item>
    </template>
  </Stepper.List>
</Stepper.Root>
```

In a child component, `checkout.useStepperContext()` returns the same stepper,
and `Stepper.useItem()` returns the same item as a `ComputedRef`: read
`.value` in `<script>`.

### In TSX

Pass slots through `v-slots` (from `@vitejs/plugin-vue-jsx`): their
parameters are inferred and a slot name that does not exist is a type error.
Vue's JSX types do not check children written as an object, so there the
parameters would be `any`.

```tsx
<Stepper.Root
  v-slots={{
    default: (stepper) => (
      <Stepper.List v-slots={{ item: ({ step }) => <Stepper.Item>{step.title}</Stepper.Item> }} />
    ),
  }}
/>
```

## Three patterns, chosen for you

| The flow | What is rendered |
| --- | --- |
| Not `linear`, with triggers | A `tablist`: triggers are tabs with roving focus and arrow keys, panels are `tabpanel`s. |
| `linear` | An ordered list of buttons with `aria-current="step"`. Steps that cannot be reached yet are disabled. |
| No triggers | Panels are `group`s named by the step's `title`. |

On every step change the new step is announced to screen readers by its
`title`, and if the focused element disappeared, focus moves to the new panel
instead of being lost.

## Styling

Nothing is styled. Every element has a `data-component` and the state it shows:

| Attribute | On | Values |
| --- | --- | --- |
| `data-state` | `Item`, `Trigger`, `Indicator` | `pending`, `current`, `complete` |
| `data-invalid` | `Item`, `Trigger`, `Indicator` | Present after a failed validation. |
| `data-disabled` | `Item`, `Trigger`, `Prev`, `Next` | Present when it cannot be used. |
| `data-pending` | `Root`, `Prev`, `Next` | Present while an async check or `onFinish` runs. |
| `data-finished` | `Root` | Present once the flow has finished. |
| `data-orientation` | `Root`, `List` | `horizontal`, `vertical` |

```css
[data-component="stepper-trigger"][data-state="current"] { font-weight: 600; }
[data-component="stepper-indicator"][data-state="complete"] { background: green; }
```

`class`, `style` and any other attribute you pass go to the element. On
`Root`, `List`, `Item`, `Content` and `Indicator` yours win over the
primitive's, but do not pass `id` to `Content`: the trigger's `aria-controls`
points at the id the primitive gives it. On `Trigger`, `Prev` and `Next` the
ids, roles, `tabindex`, `disabled`, ARIA and `data-*` attributes the primitive
manages take precedence; only the default `type="button"` gives way to yours.

## Rendering your own element

Add `as-child` and put one element or component in the slot. It receives the
ARIA attributes, ids and handlers instead of the default tag:

```vue
<Stepper.Next as-child>
  <MyButton variant="primary">Continue</MyButton>
</Stepper.Next>
```

- **One child.** Only the first node of the slot is rendered. Wrap several in
  one element.
- **A component must pass its attributes on.** Vue does that for a single-root
  component. One with `inheritAttrs: false` has to bind `v-bind="$attrs"` on
  the element it renders, or the ARIA attributes and handlers are lost.
- **An empty child is filled.** `Title` and `Description` put the step's text
  in it, and a `List` with an `#item` slot puts the items in it:

```vue
<Stepper.Title as-child>
  <h3 class="step-heading" />
</Stepper.Title>

<Stepper.List as-child>
  <nav class="steps" />
  <template #item>
    <Stepper.Item>...</Stepper.Item>
  </template>
</Stepper.List>
```

In development, each of these mistakes is reported as
[SZ001](https://stepperize.com/docs/v9/core/errors#sz001).

## Handling the move yourself

Call `preventDefault()` in your own listener to take over, for example to pass
form values:

```vue
<Stepper.Next @click="(event) => { event.preventDefault(); stepper.next(values); }">
  Continue
</Stepper.Next>
```

## Keeping panels mounted

`<Stepper.Content step="payment" force-mount>` stays in the DOM, `hidden`,
while its step is not current, so the state of the fields inside survives.

## Animating panels

Wrap the panels in Vue's
[`<Transition>`](https://vuejs.org/guide/built-ins/transition.html). Either
one keyed panel for the current step:

```vue
<Stepper.Root v-slot="stepper">
  <Transition name="fade" mode="out-in">
    <Stepper.Content :key="stepper.current.id" :step="stepper.current.id">
      <component :is="panels[stepper.current.id]" />
    </Stepper.Content>
  </Transition>
</Stepper.Root>
```

or one `<Transition>` per panel, which animates both at once:

```vue
<Transition name="fade">
  <Stepper.Content step="shipping"><ShippingForm /></Stepper.Content>
</Transition>
<Transition name="fade">
  <Stepper.Content step="payment"><PaymentForm /></Stepper.Content>
</Transition>
```

Focus is handled in every mode. When it was in the panel that is leaving, it
moves to the new panel. With `mode="out-in"` the new panel only mounts once
the old one has left: focus and the announcement wait for it, so the step is
not read twice. Focus that you moved elsewhere in the meantime stays there.

See the [primitives reference](https://stepperize.com/docs/v9/vue/api/primitives) for every prop.
