View as Markdown

Shadcn Stepper

A multi step navigation for checkouts, onboarding and long forms. It shows where the user is, which steps are done and what comes next. Steps can be horizontal or vertical, locked in order with linear, and marked as loading, invalid or disabled. Each step is a real button with arrow key navigation, and the step content and Back and Next buttons connect to it without extra state.

Profile step content

Installation

npx shadcn@latest add @shadcnuikit/stepper1

This adds components/ui/stepper.tsx, the button component it depends on, and a demo file. Set up the registry once as described in Shadcn CLI. Stepper is free and installs without an API key.

Manual installation

Install the dependencies and the button component:

npm install @ui-sdk/core@beta @ui-sdk/react@beta lucide-react
npx shadcn@latest add button

Copy the source into components/ui/stepper.tsx:

components/ui/stepper.tsx
"use client";
import * as React from "react";import { createChange, createRovingFocus, definePrimitive, idPart, keepFocus, type ChangeDetails,} from "@ui-sdk/core";import { createParts } from "@ui-sdk/react/primitive";import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
import { Button } from "@/components/ui/button";import { cn } from "@/lib/utils";
type StepperOrientation = "horizontal" | "vertical";type StepState = "active" | "completed" | "inactive";type StepIndicators = { active?: React.ReactNode; completed?: React.ReactNode; inactive?: React.ReactNode; loading?: React.ReactNode; invalid?: React.ReactNode;};
// "next-press" and "previous-press" aren't in the shared vocabulary.type StepperReason = "item-press" | "next-press" | "previous-press" | "imperative";
type StepperOptions = { value?: number; defaultValue?: number; onValueChange?: (value: number, details: ChangeDetails<StepperReason>) => void; orientation?: StepperOrientation; linear?: boolean;};
type StepperState = { value: number; orientation: StepperOrientation; linear: boolean; focused: string | null; steps: number[]; reached: number;};
type StepFlags = { completed?: boolean; disabled?: boolean; loading?: boolean; invalid?: boolean;};
type StepProps = StepFlags & { value: string };
type StepPartState = { state: StepState; loading: boolean; invalid: boolean; disabled: boolean;};
function stepState(state: StepperState, step: number, flags: StepFlags): StepState { if (flags.completed || step < state.value) return "completed"; return step === state.value ? "active" : "inactive";}
function nextStep(state: StepperState) { return state.steps.find((step) => step > state.value) ?? state.value + 1;}
function previousStep(state: StepperState) { return state.steps.findLast((step) => step < state.value) ?? state.value - 1;}
function canNext(state: StepperState) { return state.steps.length === 0 || state.value < state.steps[state.steps.length - 1];}
function canPrevious(state: StepperState) { return state.steps.length === 0 ? state.value > 1 : state.value > state.steps[0];}
function reachable(state: StepperState, step: number, flags: StepFlags) { if (flags.disabled) return false; if (!state.linear) return true; return step <= Math.max(state.value, state.reached) || !!flags.completed || step === nextStep(state);}
function partState(state: StepperState, props: StepProps): StepPartState { const step = Number(props.value); return { state: stepState(state, step, props), loading: !!props.loading && step === state.value, invalid: !!props.invalid, disabled: !reachable(state, step, props), };}
function stepAttrs(state: StepperState, props: StepProps) { const current = partState(state, props); return { "data-state": current.state, "data-loading": current.loading ? "" : undefined, "data-invalid": current.invalid ? "" : undefined, "data-disabled": current.disabled ? "" : undefined, };}
const inherited = { value: { kind: "string", from: "item", required: true }, completed: { kind: "boolean", from: "item" }, disabled: { kind: "boolean", from: "item" }, loading: { kind: "boolean", from: "item" }, invalid: { kind: "boolean", from: "item" },} as const;
const stepper = definePrimitive({ name: "stepper", options: { value: "number", defaultValue: "number", onValueChange: "callback", orientation: "string", linear: "boolean", }, controlled: ["value"], state: (options: StepperOptions): StepperState => ({ value: options.value ?? options.defaultValue ?? 1, orientation: options.orientation ?? "horizontal", linear: options.linear ?? false, focused: null, steps: [], reached: options.value ?? options.defaultValue ?? 1, }), sync: (options) => ({ ...(options.value === undefined ? {} : { value: options.value }), orientation: options.orientation ?? "horizontal", linear: options.linear ?? false, }), api: (context) => { const change = createChange(context); context.onDestroy( context.store.subscribe(() => { const { value, reached } = context.store.get(); if (value > reached) context.store.set({ reached: value }); }), ); const roving = createRovingFocus<StepperState, StepProps>(context, { slot: "trigger", key: "focused", remember: false, orientation: () => "both", loop: () => true, disabled: (item) => !reachable(context.store.get(), Number(item.props.value), item.props), preferred: (item) => Number(item.key) === context.store.get().value, claims: (state, item) => Number(item.key) === state.value, });
const go = (step: number, reason: StepperReason, event?: Event) => { const changed = change("value", step, { reason, event }); const root = context.element("root"); if (changed && root?.contains(root.ownerDocument.activeElement)) { // Next and Back often live inside the step's content, which unmounts with it. keepFocus(() => context.element("trigger", String(context.store.get().value)) ?? undefined); } return changed; };
return { roving, register(step: number) { const { steps } = context.store.get(); if (steps.includes(step)) return; context.store.set({ steps: [...steps, step].sort((a, b) => a - b) }); }, unregister(step: number) { const { steps } = context.store.get(); context.store.set({ steps: steps.filter((item) => item !== step) }); }, select(step: number, event?: Event) { const flags = context.props<StepProps>("trigger", String(step)) ?? { value: String(step), }; if (!reachable(context.store.get(), step, flags)) return false; return go(step, event ? "item-press" : "imperative", event); }, next(event?: Event) { const state = context.store.get(); if (!canNext(state)) return false; return go(nextStep(state), event ? "next-press" : "imperative", event); }, previous(event?: Event) { const state = context.store.get(); if (!canPrevious(state)) return false; return go(previousStep(state), event ? "previous-press" : "imperative", event); }, }; }, slots: (slot) => ({ root: slot({ attrs: (state) => ({ "data-orientation": state.orientation, "data-linear": state.linear ? "" : undefined, }), }), nav: slot({ role: "list", attrs: (state) => ({ "data-orientation": state.orientation, "data-state": String(state.value), }), }), item: slot<StepProps, StepPartState>({ role: "listitem", props: { value: "string", completed: "boolean", disabled: "boolean", loading: "boolean", invalid: "boolean", }, state: (state, part) => partState(state, part.props), attrs: (state, part) => stepAttrs(state, part.props), bind: (_el, part) => { const step = Number(part.key); part.api.register(step); return () => part.api.unregister(step); }, }), trigger: slot<StepProps, StepPartState>({ role: "button", focus: "roving", props: inherited, state: (state, part) => partState(state, part.props), attrs: (state, part) => { const step = Number(part.key); const current = partState(state, part.props); return { type: "button", id: `${part.id}-trigger-${idPart(part.key)}`, "aria-current": step === state.value ? "step" : undefined, "aria-disabled": current.disabled ? "true" : undefined, tabindex: part.api.roving.tabIndex(state, part), ...stepAttrs(state, part.props), }; }, press: ({ api, key }, event) => { api.select(Number(key), event); }, on: { keydown: (event, { el, api }) => { if (el) api.roving.keydown(event as KeyboardEvent, el); }, }, bind: (el, part) => part.api.roving.bind(el, part), }), indicator: slot<StepProps, StepPartState>({ props: inherited, state: (state, part) => partState(state, part.props), attrs: (state, part) => ({ "aria-hidden": "true", ...stepAttrs(state, part.props), }), }), separator: slot<StepProps>({ props: inherited, attrs: (state, part) => ({ "aria-hidden": "true", ...stepAttrs(state, part.props), }), }), title: slot<StepProps>({ props: inherited, attrs: (state, part) => stepAttrs(state, part.props), }), description: slot<StepProps>({ props: inherited, attrs: (state, part) => stepAttrs(state, part.props), }), panel: slot({ attrs: (state) => ({ "data-state": String(state.value) }), }), content: slot<{ value: string; forceMount?: boolean }>({ props: { value: "string", forceMount: "boolean" }, present: (state, part) => !!part.props.forceMount || Number(part.key) === state.value, attrs: (state, part) => { const active = Number(part.key) === state.value; return { "data-state": active ? "active" : "inactive", hidden: active ? undefined : "", }; }, }), previous: slot({ role: "button", focus: "native", attrs: (state) => ({ type: "button", "aria-disabled": canPrevious(state) ? undefined : "true", }), press: ({ api }, event) => { api.previous(event); }, }), next: slot({ role: "button", focus: "native", attrs: (state) => ({ type: "button", "aria-disabled": canNext(state) ? undefined : "true", }), press: ({ api }, event) => { api.next(event); }, }), }), agent: { actions: ["select", "next", "previous"] },});
const Parts = createParts(stepper, { root: "div", nav: "ol", item: "li", trigger: "button", indicator: "span", separator: "span", title: "span", description: "span", panel: "div", content: "div", previous: "button", next: "button",});
const StepperIndicatorsContext = React.createContext<StepIndicators>({});
type PartClassName<T> = Omit< React.ComponentProps<T extends React.ElementType ? T : never>, "className"> & { className?: string;};
// A step's value and flags reach these parts from the StepperItem around them.type StepPartProps<T> = Omit<PartClassName<T>, keyof StepProps>;
type StepperProps = StepperOptions & Omit<PartClassName<typeof Parts.Root>, keyof StepperOptions> & { indicators?: StepIndicators; };
function Stepper({ indicators, className, ...props }: StepperProps) { const stable = React.useMemo(() => indicators ?? {}, [indicators]); return ( <StepperIndicatorsContext.Provider value={stable}> <Parts.Root data-slot="stepper" className={cn("group/stepper w-full", className)} {...props} /> </StepperIndicatorsContext.Provider> );}
function StepperNav({ className, ...props }: PartClassName<typeof Parts.Nav>) { return ( <Parts.Nav className={cn( "group/stepper-nav inline-flex data-[orientation=horizontal]:w-full data-[orientation=horizontal]:flex-row data-[orientation=vertical]:flex-col", className, )} {...props} /> );}
type StepperItemProps = Omit<PartClassName<typeof Parts.Item>, keyof StepProps> & StepFlags & { step: number };
function StepperItem({ step, className, ...props }: StepperItemProps) { return ( <Parts.Item value={String(step)} className={cn( "group/step flex items-center justify-center not-last:flex-1 group-data-[orientation=horizontal]/stepper-nav:flex-row group-data-[orientation=vertical]/stepper-nav:flex-col", className, )} {...props} /> );}
const statusText: Record<StepState, string> = { active: "current step", completed: "completed", inactive: "not started",};
type StepperTriggerProps = StepPartProps<typeof Parts.Trigger>;
function StepperTrigger({ className, children, ...props }: StepperTriggerProps) { return ( <Parts.Trigger className={cn( "inline-flex cursor-pointer items-center gap-2.5 rounded-full outline-none focus-visible:z-10 focus-visible:ring-3 focus-visible:ring-ring/50 aria-disabled:cursor-not-allowed aria-disabled:opacity-60", className, )} {...(props as PartClassName<typeof Parts.Trigger>)} > {(step: StepPartState) => ( <> {typeof children === "function" ? children(step) : children} <span className="sr-only"> {", "} {step.invalid ? "has an error" : step.loading ? "in progress" : statusText[step.state]} </span> </> )} </Parts.Trigger> );}
function StepperIndicator({ className, children, ...props}: StepPartProps<typeof Parts.Indicator>) { const indicators = React.useContext(StepperIndicatorsContext); return ( <Parts.Indicator className={cn( "relative flex size-6 shrink-0 items-center justify-center overflow-hidden rounded-full bg-muted text-xs font-medium text-muted-foreground data-[state=active]:bg-primary data-[state=active]:text-primary-foreground data-[state=completed]:bg-primary data-[state=completed]:text-primary-foreground data-invalid:data-state:bg-destructive/10 data-invalid:data-state:text-destructive", className, )} {...(props as PartClassName<typeof Parts.Indicator>)} > {(step: StepPartState) => ( <span className="absolute flex items-center justify-center"> {(step.loading && indicators.loading) || (step.invalid && indicators.invalid) || indicators[step.state] || (typeof children === "function" ? children(step) : children)} </span> )} </Parts.Indicator> );}
function StepperSeparator({ className, ...props }: StepPartProps<typeof Parts.Separator>) { return ( <Parts.Separator className={cn( "m-0.5 rounded-full bg-muted group-data-[orientation=horizontal]/stepper-nav:h-0.5 group-data-[orientation=horizontal]/stepper-nav:flex-1 group-data-[orientation=vertical]/stepper-nav:h-12 group-data-[orientation=vertical]/stepper-nav:w-0.5", className, )} {...(props as PartClassName<typeof Parts.Separator>)} /> );}
function StepperTitle({ className, ...props }: StepPartProps<typeof Parts.Title>) { return ( <Parts.Title className={cn("block text-sm leading-none font-medium", className)} {...(props as PartClassName<typeof Parts.Title>)} /> );}
function StepperDescription({ className, ...props}: StepPartProps<typeof Parts.Description>) { return ( <Parts.Description className={cn("block text-sm text-muted-foreground", className)} {...(props as PartClassName<typeof Parts.Description>)} /> );}
function StepperPanel({ className, ...props }: PartClassName<typeof Parts.Panel>) { return <Parts.Panel className={cn("w-full", className)} {...props} />;}
type StepperContentProps = Omit<PartClassName<typeof Parts.Content>, "value"> & { value: number;};
function StepperContent({ value, className, ...props }: StepperContentProps) { return <Parts.Content value={String(value)} className={cn("w-full", className)} {...props} />;}
type StepperButtonProps<T> = PartClassName<T> & Pick<React.ComponentProps<typeof Button>, "variant" | "size">;
function StepperPrevious({ className, children, variant = "outline", size, ...props}: StepperButtonProps<typeof Parts.Previous>) { return ( <Parts.Previous render={<Button variant={variant} size={size} />} className={cn("aria-disabled:pointer-events-none aria-disabled:opacity-50", className)} {...props} > {children ?? ( <> <ChevronLeftIcon aria-hidden="true" className="rtl:rotate-180" /> Back </> )} </Parts.Previous> );}
function StepperNext({ className, children, variant, size, ...props}: StepperButtonProps<typeof Parts.Next>) { return ( <Parts.Next render={<Button variant={variant} size={size} />} className={cn("aria-disabled:pointer-events-none aria-disabled:opacity-50", className)} {...props} > {children ?? ( <> Next <ChevronRightIcon aria-hidden="true" className="rtl:rotate-180" /> </> )} </Parts.Next> );}
export { Stepper, StepperContent, StepperDescription, StepperIndicator, StepperItem, StepperNav, StepperNext, StepperPanel, StepperPrevious, StepperSeparator, StepperTitle, StepperTrigger, stepper, type StepIndicators, type StepperContentProps, type StepperItemProps, type StepperOptions, type StepperProps, type StepperReason, type StepperTriggerProps,};

Usage

import {
  Stepper,
  StepperContent,
  StepperIndicator,
  StepperItem,
  StepperNav,
  StepperNext,
  StepperPanel,
  StepperPrevious,
  StepperSeparator,
  StepperTitle,
  StepperTrigger,
} from "@/components/ui/stepper";
<Stepper defaultValue={1}>
  <StepperNav>
    <StepperItem step={1}>
      <StepperTrigger>
        <StepperIndicator>1</StepperIndicator>
        <StepperTitle>Account</StepperTitle>
      </StepperTrigger>
      <StepperSeparator />
    </StepperItem>
    <StepperItem step={2}>
      <StepperTrigger>
        <StepperIndicator>2</StepperIndicator>
        <StepperTitle>Review</StepperTitle>
      </StepperTrigger>
    </StepperItem>
  </StepperNav>
  <StepperPanel>
    <StepperContent value={1}>Account form</StepperContent>
    <StepperContent value={2}>Review screen</StepperContent>
  </StepperPanel>
  <StepperPrevious />
  <StepperNext />
</Stepper>

Steps are numbered from 1. StepperItem takes the step number in step, and StepperContent takes the same number in value. Only the active step's content is rendered.

Controlled

const [step, setStep] = React.useState(1);

<Stepper value={step} onValueChange={setStep}>
  ...
</Stepper>;

onValueChange receives the new step and details. details.reason is "item-press", "next-press", "previous-press" or "imperative", and details.cancel() keeps the current step, for example while a form step is invalid.

Separator color

StepperSeparator is bg-muted by default. Color the lines of finished steps from the step's state:

<StepperSeparator className="group-data-[state=completed]/step:bg-primary" />

Examples

Orientation

orientation="vertical" stacks the steps. Pair it with StepperDescription for a timeline style list.

Indicators

indicators replaces the number inside StepperIndicator by state: completed, active, inactive, loading and invalid. Here step 3 is loading.

Invalid

invalid on a StepperItem marks the step with an error. The indicator turns destructive and the invalid indicator shows.

Linear

With linear, users can go back to any step they already reached and forward only to the next one. Steps further ahead are skipped by the keyboard and can't be pressed.

Disabled

disabled on a StepperItem locks that step.

Controlled

Pass value and onValueChange to read and set the step from your own state.

Step 2 of 4

More layouts built with Stepper, such as progress bars, icon cards and multi step forms, are on the Stepper components page.

API Reference

Stepper

The root. Holds the current step and connects every part inside it.

PropTypeDefaultDescription
valuenumberThe current step. Pass it to control the stepper.
defaultValuenumber1The step at first, when uncontrolled.
onValueChange(value, details) => voidRuns when the step changes. details.reason says why, details.cancel() stops it.
orientation"horizontal", "vertical""horizontal"Lays the steps out in a row or a column.
linearbooleanfalseAllows only steps already reached, the next step and steps marked completed.
indicatorsStepIndicatorsContent for StepperIndicator by state: active, completed, inactive, loading, invalid.
classNamestringClasses for the root.

The root has data-orientation and data-linear attributes for styling.

StepperNav

The list of steps, an ol. Renders as a row or a column from orientation.

PropTypeDefaultDescription
classNamestringClasses for the list.

StepperItem

One step, an li. Its number and flags reach the trigger, indicator, title, description and separator inside it.

PropTypeDefaultDescription
stepnumberThe step number, from 1. Required.
completedbooleanfalseMarks the step completed even when it comes after the current step. A completed step is always reachable.
disabledbooleanfalseLocks the step. It can't be pressed and arrow keys skip it.
loadingbooleanfalseMarks the step as loading while it is the current step. Shows the loading indicator.
invalidbooleanfalseMarks the step with an error. Shows the invalid indicator.
classNamestringClasses for the step.

Every part inside a step has data-state set to active, completed or inactive, plus data-loading, data-invalid and data-disabled when they apply. Style children from the step with group-data-[state=completed]/step:.

StepperTrigger

The button that moves to its step. Put the indicator and title inside it.

PropTypeDefaultDescription
childrenReactNode, (state) => ReactNodeThe content. A function receives state, loading, invalid and disabled.
classNamestringClasses for the button.

The trigger adds a visually hidden status, such as "completed" or "current step", after its content.

StepperIndicator

The circle with the step number or icon.

PropTypeDefaultDescription
childrenReactNode, (state) => ReactNodeShown when indicators has nothing for the current state.
classNamestringClasses for the circle.

StepperSeparator

The line after a step. Leave it out of the last step.

PropTypeDefaultDescription
classNamestringClasses for the line.

StepperTitle and StepperDescription

The step's name and a short line under it.

PropTypeDefaultDescription
classNamestringClasses for the text.

StepperPanel

Wraps the step contents.

PropTypeDefaultDescription
classNamestringClasses for the panel.

StepperContent

The content for one step. Only the current step's content is rendered.

PropTypeDefaultDescription
valuenumberThe step this content belongs to. Required.
forceMountbooleanfalseKeeps the content in the page, hidden, while its step is not active. Useful to keep form state.
classNamestringClasses for the content.

StepperPrevious and StepperNext

Buttons that move one step back or forward. They render the shadcn/ui Button and get aria-disabled at the first and last step.

PropTypeDefaultDescription
variantButton variant"outline" for Previous, "default" for NextThe button style.
sizeButton size"default"The button size.
childrenReactNodeBack, Next with a chevronThe button content.
classNamestringClasses for the button.

Keyboard

KeyAction
TabMoves focus to the current step's trigger, then out of the list.
ArrowRight, ArrowDownMoves focus to the next reachable step. Wraps to the first.
ArrowLeft, ArrowUpMoves focus to the previous reachable step. Wraps to the last.
Home, EndMoves focus to the first or last reachable step.
Enter, SpaceGoes to the focused step.

Arrow keys move focus only. The step changes when a trigger is pressed. Unreachable steps, disabled or locked by linear, are skipped.

Accessibility

  • The nav is an ordered list and every step is a list item, so screen readers announce the step count.
  • The current step's trigger has aria-current="step". Each trigger reads its status after the title, such as "completed", "not started", "in progress" or "has an error".
  • Unreachable steps get aria-disabled="true" and stay out of the arrow key order.
  • When Next or Back unmounts the content that held focus, focus moves to the new step's trigger.
  • The indicator and separator are aria-hidden. Put a StepperTitle in each trigger, or an aria-label when the trigger only has a number.

For AI Agents

When to use

  • A task split into ordered steps: checkout, onboarding, account setup, multi step forms.
  • Showing progress through a fixed process, such as order tracking, with orientation="vertical".
  • Any flow where users need to see which steps are done and which are left.

When not to use

  • Switching between independent views with no order. Use Tabs instead.
  • A single progress value with no named steps. Use a Progress bar.
  • Showing a page's place in a hierarchy. Use a Breadcrumb.

Agent notes

  • Import from @/components/ui/stepper. The exports are Stepper, StepperNav, StepperItem, StepperTrigger, StepperIndicator, StepperSeparator, StepperTitle, StepperDescription, StepperPanel, StepperContent, StepperPrevious, StepperNext and the stepper primitive.
  • Steps are numbers starting at 1, not strings. StepperItem uses step, StepperContent uses value.
  • To block Next until a form step is valid, call details.cancel() in onValueChange when the reason is "next-press", or set invalid on the step.
  • StepperSeparator does not color itself for completed steps. Add group-data-[state=completed]/step:bg-primary.
  • Don't add click handlers to triggers or wire Back and Next yourself. StepperPrevious and StepperNext already move the step.

Resources