Horizontal Numbered Stepper With Back and Next

A four-step shop set-up flow: Account, Shop, Payouts and Review. Numbered circles sit in a row joined by thin lines; finished steps turn solid blue with a tick, the current one gets a blue ring, and the line ahead fills as you press Next. Earlier steps can be clicked to go back, later ones stay locked. On phones the labels shrink but the row still fits.

Code

import { useEffect, useRef, useState } from "react";

// Steps of a shop set-up form. Each has a short label and the text shown below.
const steps = [
  { label: "Account", title: "Create your account", body: "Use the email you check most. We send order alerts and payout notices there." },
  { label: "Shop", title: "Name your shop", body: "Pick a name and a short description. You can change both later from Settings." },
  { label: "Payouts", title: "Connect a bank account", body: "Sales are paid out every Friday. We only need your account and sort code." },
  { label: "Review", title: "Check and open your shop", body: "Look over your details. When you open the shop, your first product page goes live." },
];

export default function HorizontalNumberedStepperWithBackAndNext() {
  const [current, setCurrent] = useState(1);
  const heading = useRef<HTMLHeadingElement>(null);
  const moved = useRef(false);
  const last = steps.length - 1;

  // Move focus to the new step's heading after Back or Next (not on first render).
  useEffect(() => {
    if (moved.current) heading.current?.focus();
  }, [current]);

  const go = (i: number) => {
    moved.current = true;
    setCurrent(Math.max(0, Math.min(last, i)));
  };

  return (
    <div className="bg-blue-50 px-4 py-12 sm:px-8">
      <div className="mx-auto max-w-2xl rounded-2xl bg-white p-6 shadow-sm ring-1 ring-blue-100 sm:p-8">
        <ol className="flex items-start">
          {steps.map((s, i) => {
            const done = i < current;
            const now = i === current;
            return (
              <li key={s.label} aria-current={now ? "step" : undefined} className="relative flex flex-1 flex-col items-center">
                {i < last && (
                  <span aria-hidden="true" className="absolute left-[calc(50%+20px)] right-[calc(-50%+20px)] top-4 h-0.5 overflow-hidden rounded bg-slate-200">
                    <span className={`block h-full origin-left bg-blue-600 transition-transform duration-500 ease-out motion-reduce:transition-none ${done ? "scale-x-100" : "scale-x-0"}`} />
                  </span>
                )}
                <button
                  type="button"
                  disabled={i > current}
                  onClick={() => go(i)}
                  className={`relative z-10 grid h-9 w-9 place-items-center rounded-full text-sm font-semibold transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 focus-visible:ring-offset-2 ${done ? "bg-blue-600 text-white hover:bg-blue-700" : now ? "bg-white text-blue-700 ring-2 ring-blue-600" : "bg-slate-100 text-slate-600"}`}
                >
                  {done ? (
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M5 12l5 5 9-10" /></svg>
                  ) : (
                    i + 1
                  )}
                  <span className="sr-only">{`${s.label}, ${done ? "completed" : now ? "current step" : "not started"}`}</span>
                </button>
                <span className={`mt-2 text-xs font-medium sm:text-sm ${now ? "text-slate-900" : "text-slate-600"}`} aria-hidden="true">{s.label}</span>
              </li>
            );
          })}
        </ol>

        <div className="mt-8 min-h-[96px]">
          <p className="text-sm font-medium text-blue-700">Step {current + 1} of {steps.length}</p>
          <h2 ref={heading} tabIndex={-1} className="mt-1 rounded text-xl font-semibold text-slate-900 focus:outline-none">{steps[current].title}</h2>
          <p className="mt-2 text-slate-600">{steps[current].body}</p>
        </div>

        <div className="mt-6 flex justify-between border-t border-slate-100 pt-5">
          <button type="button" onClick={() => go(current - 1)} disabled={current === 0} className="h-11 rounded-lg px-4 text-sm font-medium text-slate-700 hover:bg-slate-100 disabled:cursor-not-allowed disabled:opacity-40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-600">Back</button>
          <button type="button" onClick={() => go(current + 1)} className="h-11 rounded-lg bg-blue-600 px-5 text-sm font-semibold text-white hover:bg-blue-700 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 focus-visible:ring-offset-2">
            {current === last ? "Open shop" : "Next"}
          </button>
        </div>
      </div>
    </div>
  );
}

Install with one command: npx shadcn@latest add https://we.inc/r/horizontal-numbered-stepper-with-back-and-next.json

When to use it

  • A sign-up or set-up form has three to five steps that each fit on one screen.
  • People should see the whole journey before they start, so they know how long it will take.
  • Completed steps need to stay reachable for corrections without losing what was typed.
  • You want a stepper that works the same in a React app and on a plain HTML page.

How to customize it

  • Edit `steps` to rename stages and change the text under the row; the circles and lines follow the array length.
  • Recolour through `bg-blue-600`, `ring-blue-600` and `text-blue-700`, or `--hns-accent` in the plain version.
  • Change how fast the connecting line fills with `duration-500` on the inner span.
  • Replace the panel text with your real form fields and call `go(current + 1)` only after the fields validate.

Accessibility

  • Steps are an ordered list, the current one has aria-current="step", and each circle carries hidden text saying completed, current step or not started.
  • After Back or Next, focus moves to the new step heading so screen reader users hear what changed.
  • Future steps are disabled buttons, so they cannot be reached until earlier steps are done.

Questions

How do the connecting lines fill without measuring anything?

Each line is a grey track with a blue bar inside it set to scaleX(0). When the step before it is finished the bar switches to scaleX(1) with transform-origin on the left, so it grows from left to right on the compositor.

How do I stop Next until the form is valid?

Keep your field values above the stepper and check them in the Next handler. Only call the step change when validation passes; otherwise show the field errors and leave the stepper where it is.

Can I use more than five steps?

The row will fit six short labels on a laptop, but on phones the labels get cramped. Past five steps, a vertical stepper or a compact dot row with a Step 3 of 7 label is easier to read.

Should the step number be inside the circle for screen readers?

The visible number alone does not say whether a step is done. That is why each circle also has a short hidden status, and the ordered list already announces position in the sequence.

Related: React Stepper and Multi-Step Form Components, Booking Steps Push Up With a Filling Progress Bar, Underline Tabs With Sliding Indicator.

More timelines and steppers or all free UI components. Build your site free to have a full site built around it.

More in Timelines

Product

Who It's For

Features

Resources

Company

View Sitemap