Command palette

@yasinsensoy/web

Web · Components

Stepper

Step indicator showing which step of a task you are on (progress chart).

17 exports

Live example

Preview

Create your account

If you type “[email protected]”, server validation fails.

Code

import { Wizard, TextField, RadioField, SwitchField, Summary, toast } from "@yasinsensoy/web";

<Wizard
  onComplete={async (data) => { await save(data); toast.success("Account created"); }}
  steps={[
    {
      label: "Account",
      title: "Create your account",
      content: <>
        <TextField label="Email" name="email" type="email" required />
        <TextField label="Password" name="password" type="password" minLength={8} required />
      </>,
      // Runs once the browser rules pass; returns errors by field name.
      validate: async (data) =>
        (await isRegistered(data.get("email"))) ? { email: "This email is already registered" } : undefined,
    },
    { label: "Profile", content: <><TextField label="Full name" name="name" required /> …</> },
    { label: "Plan", content: <RadioField label="Plan" name="plan" variant="card" … /> },
    { label: "Newsletter", optional: true, content: <SwitchField label="Monthly newsletter" name="newsletter" /> },
    { label: "Review", content: ({ data }) => <Summary data={data} /> },
  ]} />

Events

  1. Events appear here when you interact with the preview.

Server action: jumps to the step of the field that returned an error by itself

Username

If you type “admin”, the server returns an error on the last step.

Import

import { Stepper, Wizard, resolveStepStatus, useWizard } from "@yasinsensoy/web";

Stepper

componentClient component

Step indicator showing which step of a task you are on (progress chart). Steps are given with the items array. For a full flow with forms, validation and buttons use Wizard; Stepper is only the indicator.

  • States are computed from the order (previous ones ✓); to show an error, warning, loading or skipped, pass status to the step.
  • If onValueChange is given, completed steps can be clicked to go back (all of them with navigation="all"). Tab and Enter/Space work on the keyboard.
  • Accessibility: an ordered list inside <nav>; the selected step has aria-current="step", states are read to screen readers.
  • Mobile: Horizontal steps switch to a summary view on narrow screens (below 640 px): "Step 2 / 5", the name itself, the next step and a segmented bar. Done with CSS only; renders correctly on the server too, without shift. With mobile="scroll" the full list scrolls sideways.
  • In the vertical view the selected step's content opens below its name.
  • In right-to-left (RTL) writing the lines and order flip automatically.
Stepper({ items, value, defaultValue, onValueChange, navigation, orientation, variant: variantProp, effect, labelPlacement, mobile, size, label, classNames, className, style, ...props }: StepperProps): JSX.Element

Examples

<Stepper value={1} items={[
  { label: "Cart" },
  { label: "Address", description: "Delivery details" },
  { label: "Payment", status: "error" },
  { label: "Confirm", optional: true },
]} />

<Stepper variant="bar" value={step} onValueChange={setStep} items={steps} />

<Stepper orientation="vertical" value={step} items={[
  { label: "Account", content: <AccountForm /> },
  { label: "Profile", content: <ProfileForm /> },
]} />

Wizard

componentClient component

Step-by-step form (wizard): step indicator, step fields, Back / Next / Complete buttons and per-step validation in one component. Steps are given with the steps array; use ready-made fields like TextField, SelectField as content.

How it works:

  • All steps are inside a single Form; hidden steps stay in the DOM. So input isn't lost when going back, and on the last step all fields are submitted as a single FormData (server action included).
  • Pressing Next validates only that step's fields with browser rules; errors are shown below the fields and the first invalid field is focused. Then the step's validate function (can be async) runs, if any.
  • The Enter key moves to the next step, and submits on the last step.
  • If the server action returns state.errors, it automatically goes to the step containing the invalid field and focuses it.
  • Invalid steps are red (!) in the indicator, passed ones ✓, skipped ones dashed. Visited steps can be revisited by clicking the indicator.

Mobile and accessibility:

  • On narrow screens the indicator switches to the "Step 2 / 5" summary.
  • The back button (phone/browser) returns to the previous step (backButton).
  • When the step changes the new step's title is focused (read by the screen reader), and if the top of the wizard is above the screen it is scrolled into view.
  • With stickyFooter the buttons stick to the bottom of the screen.
  • The step transition slides in by direction; off with the reduced motion preference.
Wizard({ steps, step: stepProp, defaultStep, onStepChange, linear, onComplete, orientation, variant, effect, labelPlacement, mobile, size, labels, footer, stickyFooter, backButton, warnOnLeave, classNames, className, onSubmit, action, state, ref, schema, draft, ...props }: WizardProps<Schema>): JSX.Element

Examples

<Wizard
  onComplete={async (data) => { await save(data); toast.success("Account created"); }}
  steps={[
    {
      label: "Account",
      content: (
        <>
          <TextField label="Email" name="email" type="email" required />
          <TextField label="Password" name="password" type="password" minLength={8} required />
        </>
      ),
      validate: async (data) =>
        (await isRegistered(data.get("email"))) ? { email: "This email is registered" } : undefined,
    },
    { label: "Profile", content: <TextField label="Name" name="name" required /> },
    { label: "Newsletter", optional: true, content: <CheckboxField label="Subscribe" name="newsletter" /> },
    { label: "Confirm", content: ({ data }) => <Summary data={data} /> },
  ]}
/>

// With a server action: errors are distributed to the matching step by field name.
const [state, formAction] = useActionState(signUp, null);
<Wizard action={formAction} state={state} steps={steps} />

Computes the step's state from the order and the manually given state.

resolveStepStatus(item: StepItem, index: number, current: number): StepStatus

useWizard

function

Returns the state and navigation functions of the nearest Wizard. For writing your own buttons or showing a summary in step content.

useWizard(): WizardContextValue

Examples

function ChoosePlan() {
  const { next } = useWizard();
  return <Button onClick={next}>Continue with this plan</Button>;
}

State of a step.

  • complete: completed (✓).
  • current: the current step.
  • upcoming: not reached yet.
  • error: invalid; needs fixing (!).
  • warning: has a warning but you can proceed (!).
  • loading: an operation is running in the step (spinner).
  • skipped: an optional step was skipped (dashed).
type StepStatus = "complete" | "current" | "upcoming" | "error" | "warning" | "loading" | "skipped"

StepItem

interface

A step inside Stepper.

NameTypeDescription
labelReactNodeName of the step.
description?ReactNodeShort description below the name.
icon?IconPropIcon shown instead of the number: name or element. Replaced by ✓ when completed.
status?StepStatusState of the step. If omitted, computed from the order: previous ones complete, the selected one current, later ones upcoming. Pass it to show an error or loading.
optional?boolean | ReactNodeOptional step: "İsteğe bağlı" is written below the name. If text is given, that is written.
disabled?booleanNot clickable and looks faded.
content?ReactNodeContent shown below the step name in the vertical view while the step is selected (e.g. that step's form and buttons). Not used in the horizontal view.
tooltip?TooltipPropTooltip shown when hovering the step. See TooltipProp.

Style of the step indicator.

  • circle: numbered circles with filling lines between them.
  • dot: small dots; for simple flows.
  • bar: a bar for each step with its name below (checkout page style).
type StepperVariant = "circle" | "dot" | "bar"

View of horizontal steps on narrow screens (below 640 px).

  • compact: "Step 2 / 5", the step's name, the next step and a segmented progress bar. Always fits the screen.
  • scroll: the full list scrolls sideways; the selected step is brought into view.
  • full: unchanged (if steps are few and names are short).
type StepperMobile = "compact" | "scroll" | "full"

Classes that can be passed to the inner parts of Stepper.

NameTypeDescription
list?stringStep list (<ol>).
step?stringEach step (<li>).
indicator?stringCircle, dot or bar.
connector?stringLine between steps.
label?stringStep name.
description?stringStep description.
content?stringStep content in the vertical view.
compact?stringSummary view on narrow screens.

Which steps can be clicked.

  • none: none (indicator only).
  • completed: completed, skipped, invalid steps and those before the selected one (to go back).
  • all: all steps that aren't disabled.
type StepperNavigation = "none" | "completed" | "all"

StepperProps

interface

Props of the Stepper component.

Extends: Omit<ComponentProps<"nav">, "defaultValue" | "onChange" | "children">

NameTypeDefaultDescription
itemsreadonly StepItem[]Steps.
value?numberIndex of the selected step in controlled use (starts at 0).
defaultValue?number0Initial step in uncontrolled use.
onValueChange?(index: number) => voidCalled when a step is clicked.
navigation?StepperNavigation"completed"` if `onValueChange` is given, otherwise `"none"Which steps can be clicked. See StepperNavigation.
orientation?"horizontal" | "vertical""horizontal"Layout.
variant?StepperVariant | CustomVariant<"stepper">"circle"Indicator style: built-in, or a custom one added with defineAppearance (its classes are added to the root; drawn like circle unless extends is given). If omitted, UIProvider appearance.defaults.stepper.variant. See StepperVariant.
effect?EffectPropEffect or effects of the step indicators (circle, dot, bar) (e.g. "pulse", ["lift", "glow"]). The accent color is the primary color. If omitted, UIProvider appearance.defaults.stepper.effect. See BuiltinEffect.
labelPlacement?"end" | "bottom""end"Position of the name in the horizontal view: next to the circle (end) or below it (bottom, steps are equal width).
mobile?StepperMobile"compact"View on narrow screens. See StepperMobile.
size?Size"md"Size.
label?string"Adımlar"Screen reader name.
classNames?StepperClassNamesClasses added to the inner parts.

Return value of a step's validate function.

  • undefined / true: valid, it advances.
  • false: doesn't advance (if you showed the message yourself).
  • Text: shown in an error box above the step.
  • { fieldName: "message" }: messages are shown below fields with a matching name (like a server error); they clear when the user changes the value. Messages for names not in the step fall into the error box.
type WizardValidateResult = undefined | null | boolean | string | Readonly<Record<string, string | null | undefined>>

Value received by useWizard and step content given as a function.

NameTypeDescription
stepnumberIndex of the current step (starts at 0).
countnumberNumber of steps.
isFirstbooleanIs it on the first step?
isLastbooleanIs it on the last step?
busybooleanIs a validation or submission running?
next() => Promise<boolean>Validates the step and moves to the next one. Returns true if it moved; otherwise focuses the invalid field and returns false.
back() => voidReturns to the previous step (no validation).
goTo(index: number) => Promise<boolean>Goes to a step. When going forward (in a linear flow) the steps in between are validated; it stops at a step that doesn't pass.
skip() => voidSkips an optional step without validating; its fields aren't submitted.
dataFormData | nullForm data at the moment the step was entered. Used to show what was entered on a summary/confirm step.
getData() => FormDataCurrent data of the form.

WizardStep

interface

A step inside Wizard.

NameTypeDescription
labelReactNodeName in the step indicator.
description?ReactNodeShort description below the name.
icon?IconPropIcon shown instead of the number: name or element.
title?ReactNodeTitle shown above the step's content. If omitted, the title is read only to screen readers (label).
contentReactNode | ((wizard: WizardContextValue) => ReactNode)Content of the step: fields. If a function is given, it receives the wizard's state (e.g. data on a summary step). A function can only be passed from a client component.
validate?(data: FormData, wizard: WizardContextValue) => WizardValidateResult | Promise<WizardValidateResult>Extra validation that runs after the fields' browser rules (required, type, pattern…) pass. Can return a Promise (e.g. "is this email registered?" on the server); meanwhile the Next button shows loading. See WizardValidateResult.
optional?booleanOptional step: "İsteğe bağlı" is written in the indicator and a "Skip" button appears. Fields of a skipped step aren't submitted or validated.
disabled?booleanThe step is skipped and can't be visited.
tooltip?TooltipPropTooltip shown when hovering the step in the indicator.

WizardLabels

interface

Button texts of Wizard.

NameTypeDefaultDescription
back?ReactNode"Geri"
next?ReactNode"İleri"
finish?ReactNode"Tamamla"Submit button on the last step.
skip?ReactNode"Atla"

Classes that can be passed to the inner parts of Wizard.

NameTypeDescription
stepper?stringStep indicator.
panel?stringContent box of each step.
title?stringStep title.
footer?stringBottom bar holding the buttons.

WizardProps

interface

Props of the Wizard component.

Extends: Omit<FormProps<Schema>, "children" | "noValidate" | "onValidSubmit">

NameTypeDefaultDescription
stepsreadonly WizardStep[]Steps.
step?numberIndex of the current step in controlled use.
defaultStep?number0Initial step in uncontrolled use.
onStepChange?(index: number) => voidCalled when the step changes.
linear?booleantrueLinear flow: you can't go to the next step without passing one; from the indicator you can only return to visited steps. With false you move freely between steps; everything is validated on submit.
onComplete?(data: FormData, values: FormValues<Schema>) => void | Promise<void>Called with the form data on the last step if all steps are valid. If schema is given, the second argument is the schema's typed output. If it returns a Promise, the Complete button shows loading until it ends. If omitted, the form is submitted normally (action or onSubmit).
orientation?"horizontal" | "vertical""horizontal"Position of the step indicator: on top (horizontal) or on the left (vertical; moves to the top and the summary view on narrow screens).
variant?StepperVariant | CustomVariant<"stepper">"circle"Indicator style; see StepperProps.variant.
effect?EffectPropEffect of the step indicators; see StepperProps.effect.
labelPlacement?"end" | "bottom""end"Position of the name in the horizontal indicator.
mobile?StepperMobile"compact"View of the indicator on narrow screens. See StepperMobile.
size?Size"md"Indicator size.
labels?WizardLabelsButton texts.
footer?ReactNodeExtra content shown in the bottom bar left of the buttons (e.g. "Save draft").
stickyFooter?booleanfalseOn narrow screens the button bar sticks to the bottom of the screen; the Next button is always at hand on long steps.
backButton?booleantrueThe phone's or browser's back button returns to the previous step instead of leaving the page.
warnOnLeave?booleanfalseIf the form was changed but not completed and the page is closed or reloaded, the browser shows a warning.
classNames?WizardClassNamesClasses added to the inner parts.