Multi-step form
A three-step project brief with a stepper that fills as you go, steps that slide in the direction you travel, and a review screen before sending.
"use client";
import { useRef, useState, type CSSProperties } from "react";
import "./multi-step-form.css";
const STEPS = ["Project", "Budget", "Contact", "Review"];
const TYPES = ["Website", "Brand identity", "Mobile app", "Something else"];
const BUDGETS = ["Under $5k", "$5k – $15k", "$15k – $40k", "$40k +"];
/**
* MultiStepForm
* - One state object for all steps; each step only validates its own fields.
* - `dir` remembers the travel direction so the next panel slides in from the right side.
* - Focus moves to the new step's heading, so screen reader and keyboard users land in the right place.
*/
export function MultiStepForm({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [step, setStep] = useState(0);
const [dir, setDir] = useState<1 | -1>(1);
const [data, setData] = useState({
type: "",
details: "",
budget: "",
name: "",
email: "",
});
const [error, setError] = useState("");
const [sent, setSent] = useState(false);
const heading = useRef<HTMLHeadingElement>(null);
const valid = [
() => (data.type ? "" : "Pick what you need."),
() => (data.budget ? "" : "Pick a budget range, even a rough one."),
() =>
!data.name.trim()
? "Add your name."
: !/^\S+@\S+\.\S+$/.test(data.email)
? "Add an email we can reply to."
: "",
() => "",
];
const go = (to: number) => {
if (to > step) {
const msg = valid[step]();
setError(msg);
if (msg) return;
} else setError("");
setDir(to > step ? 1 : -1);
setStep(to);
requestAnimationFrame(() => heading.current?.focus());
};
const set = (k: keyof typeof data, v: string) => {
setData((d) => ({ ...d, [k]: v }));
setError("");
};
if (sent) {
return (
<div className="gu-steps gu-steps--sent" data-theme={theme} role="status">
<span className="gu-steps__burst" aria-hidden="true">
✓
</span>
<h3>Brief sent</h3>
<p>
Thanks, {data.name.split(" ")[0]}. Expect a reply at {data.email}{" "}
within two working days.
</p>
</div>
);
}
const choice = (k: "type" | "budget", options: string[]) => (
<div
className="gu-steps__choices"
role="radiogroup"
aria-label={k === "type" ? "Project type" : "Budget"}
>
{options.map((o) => (
<label key={o} className="gu-steps__choice">
<input
type="radio"
name={k}
checked={data[k] === o}
onChange={() => set(k, o)}
/>
<span>{o}</span>
</label>
))}
</div>
);
return (
<form
className="gu-steps"
data-theme={theme}
onSubmit={(e) => {
e.preventDefault();
if (step < 3) go(step + 1);
else setSent(true);
}}
noValidate
>
<ol
className="gu-steps__rail"
style={{ "--progress": step / (STEPS.length - 1) } as CSSProperties}
>
{STEPS.map((s, i) => (
<li
key={s}
data-state={i < step ? "done" : i === step ? "now" : "next"}
aria-current={i === step ? "step" : undefined}
>
<button
type="button"
disabled={i > step}
onClick={() => go(i)}
aria-label={`${s}${i < step ? ", done" : ""}`}
>
{i < step ? "✓" : i + 1}
</button>
<span>{s}</span>
</li>
))}
</ol>
<div className="gu-steps__panel" key={step} data-dir={dir}>
<h3 ref={heading} tabIndex={-1}>
{
[
"What are we making?",
"What's the budget?",
"How do we reach you?",
"Look good?",
][step]
}
</h3>
{step === 0 && (
<>
{choice("type", TYPES)}
<label className="gu-steps__field">
<span>A sentence or two about it (optional)</span>
<textarea
rows={2}
value={data.details}
onChange={(e) => set("details", e.target.value)}
/>
</label>
</>
)}
{step === 1 && choice("budget", BUDGETS)}
{step === 2 && (
<>
<label className="gu-steps__field">
<span>Name</span>
<input
value={data.name}
autoComplete="name"
onChange={(e) => set("name", e.target.value)}
/>
</label>
<label className="gu-steps__field">
<span>Email</span>
<input
type="email"
value={data.email}
autoComplete="email"
onChange={(e) => set("email", e.target.value)}
/>
</label>
</>
)}
{step === 3 && (
<dl className="gu-steps__review">
{[
[
"Project",
`${data.type}${data.details ? ` · ${data.details}` : ""}`,
0,
],
["Budget", data.budget, 1],
["Contact", `${data.name} · ${data.email}`, 2],
].map(([k, v, i]) => (
<div key={k as string}>
<dt>{k}</dt>
<dd>{v}</dd>
<button type="button" onClick={() => go(i as number)}>
Edit<span className="gu-sr"> {k}</span>
</button>
</div>
))}
</dl>
)}
<p className="gu-steps__error" role="alert">
{error}
</p>
</div>
<div className="gu-steps__bar">
<button
type="button"
className="gu-steps__back"
onClick={() => go(step - 1)}
disabled={step === 0}
>
Back
</button>
<button type="submit" className="gu-steps__next">
{step === 3 ? "Send brief" : "Continue"}
</button>
</div>
</form>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Multi-step form
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A project brief form split into steps (Project, Budget, Contact, Review). A stepper rail fills as you progress, each step slides in from the direction you travel, the review screen lets you jump back to edit, and sending shows a celebratory check.
## Layout and look
- Card 480px, padding 24px, radius 26px.
- Rail: four 36px numbered circles joined by a 3px line; the orange part grows with progress. Done steps are orange with a check; the current step is Ink and 12% bigger. Labels under each.
- Panel: big question title (24px, weight 900), then either a 2-column grid of 56px choice cards (orange border and tint when chosen), text fields, or a review list (rows with label, value and an underlined "Edit").
- Bottom bar: outlined "Back" and an orange "Continue"/"Send brief" pill.
## Behavior and motion
1. Each step validates only its own fields; errors show in an alert line under the panel.
2. Panels re-mount with a 450ms slide (+40px forward, -40px back) and fade. The rail fill uses scaleX over 600ms.
3. Focus moves to the new heading after each step. Done steps on the rail are clickable to go back.
4. Sent: an orange circle with a check pops and rotates in.
## Touch
Large choice cards; choices collapse to one column in narrow containers.
## Accessibility
- Radio groups for choices (native radios hidden visually), aria-current="step" on the rail, role="alert" for errors.
- prefers-reduced-motion: a short fade instead of sliding.
## Props
theme. Edit STEPS, TYPES, BUDGETS and the validators.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### MultiStepForm.tsx
```tsx
"use client";
import { useRef, useState, type CSSProperties } from "react";
import "./multi-step-form.css";
const STEPS = ["Project", "Budget", "Contact", "Review"];
const TYPES = ["Website", "Brand identity", "Mobile app", "Something else"];
const BUDGETS = ["Under $5k", "$5k – $15k", "$15k – $40k", "$40k +"];
/**
* MultiStepForm
* - One state object for all steps; each step only validates its own fields.
* - `dir` remembers the travel direction so the next panel slides in from the right side.
* - Focus moves to the new step's heading, so screen reader and keyboard users land in the right place.
*/
export function MultiStepForm({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [step, setStep] = useState(0);
const [dir, setDir] = useState<1 | -1>(1);
const [data, setData] = useState({
type: "",
details: "",
budget: "",
name: "",
email: "",
});
const [error, setError] = useState("");
const [sent, setSent] = useState(false);
const heading = useRef<HTMLHeadingElement>(null);
const valid = [
() => (data.type ? "" : "Pick what you need."),
() => (data.budget ? "" : "Pick a budget range, even a rough one."),
() =>
!data.name.trim()
? "Add your name."
: !/^\S+@\S+\.\S+$/.test(data.email)
? "Add an email we can reply to."
: "",
() => "",
];
const go = (to: number) => {
if (to > step) {
const msg = valid[step]();
setError(msg);
if (msg) return;
} else setError("");
setDir(to > step ? 1 : -1);
setStep(to);
requestAnimationFrame(() => heading.current?.focus());
};
const set = (k: keyof typeof data, v: string) => {
setData((d) => ({ ...d, [k]: v }));
setError("");
};
if (sent) {
return (
<div className="gu-steps gu-steps--sent" data-theme={theme} role="status">
<span className="gu-steps__burst" aria-hidden="true">
✓
</span>
<h3>Brief sent</h3>
<p>
Thanks, {data.name.split(" ")[0]}. Expect a reply at {data.email}{" "}
within two working days.
</p>
</div>
);
}
const choice = (k: "type" | "budget", options: string[]) => (
<div
className="gu-steps__choices"
role="radiogroup"
aria-label={k === "type" ? "Project type" : "Budget"}
>
{options.map((o) => (
<label key={o} className="gu-steps__choice">
<input
type="radio"
name={k}
checked={data[k] === o}
onChange={() => set(k, o)}
/>
<span>{o}</span>
</label>
))}
</div>
);
return (
<form
className="gu-steps"
data-theme={theme}
onSubmit={(e) => {
e.preventDefault();
if (step < 3) go(step + 1);
else setSent(true);
}}
noValidate
>
<ol
className="gu-steps__rail"
style={{ "--progress": step / (STEPS.length - 1) } as CSSProperties}
>
{STEPS.map((s, i) => (
<li
key={s}
data-state={i < step ? "done" : i === step ? "now" : "next"}
aria-current={i === step ? "step" : undefined}
>
<button
type="button"
disabled={i > step}
onClick={() => go(i)}
aria-label={`${s}${i < step ? ", done" : ""}`}
>
{i < step ? "✓" : i + 1}
</button>
<span>{s}</span>
</li>
))}
</ol>
<div className="gu-steps__panel" key={step} data-dir={dir}>
<h3 ref={heading} tabIndex={-1}>
{
[
"What are we making?",
"What's the budget?",
"How do we reach you?",
"Look good?",
][step]
}
</h3>
{step === 0 && (
<>
{choice("type", TYPES)}
<label className="gu-steps__field">
<span>A sentence or two about it (optional)</span>
<textarea
rows={2}
value={data.details}
onChange={(e) => set("details", e.target.value)}
/>
</label>
</>
)}
{step === 1 && choice("budget", BUDGETS)}
{step === 2 && (
<>
<label className="gu-steps__field">
<span>Name</span>
<input
value={data.name}
autoComplete="name"
onChange={(e) => set("name", e.target.value)}
/>
</label>
<label className="gu-steps__field">
<span>Email</span>
<input
type="email"
value={data.email}
autoComplete="email"
onChange={(e) => set("email", e.target.value)}
/>
</label>
</>
)}
{step === 3 && (
<dl className="gu-steps__review">
{[
[
"Project",
`${data.type}${data.details ? ` · ${data.details}` : ""}`,
0,
],
["Budget", data.budget, 1],
["Contact", `${data.name} · ${data.email}`, 2],
].map(([k, v, i]) => (
<div key={k as string}>
<dt>{k}</dt>
<dd>{v}</dd>
<button type="button" onClick={() => go(i as number)}>
Edit<span className="gu-sr"> {k}</span>
</button>
</div>
))}
</dl>
)}
<p className="gu-steps__error" role="alert">
{error}
</p>
</div>
<div className="gu-steps__bar">
<button
type="button"
className="gu-steps__back"
onClick={() => go(step - 1)}
disabled={step === 0}
>
Back
</button>
<button type="submit" className="gu-steps__next">
{step === 3 ? "Send brief" : "Continue"}
</button>
</div>
</form>
);
}
```
### multi-step-form.css
```css
/* MultiStepForm: change these colors to match your brand. */
.gu-steps {
--card: #fbf8f2;
--field: #f3eee3;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.16);
--accent: #ff5a1f;
--focus: #2b35ff;
--error: #c2410c;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
display: grid;
gap: 18px;
width: min(100%, 480px);
padding: 24px;
overflow: hidden;
container-type: inline-size;
border: 1px solid var(--line);
border-radius: 26px;
background: var(--card);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-steps[data-theme="dark"] {
--card: #18181b;
--field: #0e0e10;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.16);
--focus: #d7ff3a;
--error: #fb923c;
}
/* Stepper rail: a line behind the dots fills with --progress */
.gu-steps__rail {
position: relative;
display: flex;
justify-content: space-between;
margin: 0;
padding: 0;
list-style: none;
}
.gu-steps__rail::before,
.gu-steps__rail::after {
content: "";
position: absolute;
top: 17px;
left: 18px;
right: 18px;
height: 3px;
border-radius: 3px;
background: var(--line);
}
.gu-steps__rail::after {
background: var(--accent);
transform: scaleX(var(--progress));
transform-origin: left;
transition: transform 0.6s var(--ease);
}
.gu-steps__rail li {
position: relative;
z-index: 1;
display: grid;
justify-items: center;
gap: 6px;
font-size: 0.75rem;
font-weight: 700;
color: var(--muted);
}
.gu-steps__rail button {
width: 36px;
height: 36px;
border: 2px solid var(--line);
border-radius: 50%;
background: var(--card);
color: inherit;
font: inherit;
font-size: 0.85rem;
cursor: pointer;
transition:
background-color 0.3s,
border-color 0.3s,
color 0.3s,
transform 0.4s var(--ease);
}
.gu-steps__rail button:disabled {
cursor: default;
}
.gu-steps__rail [data-state="done"] button {
border-color: var(--accent);
background: var(--accent);
color: #0e0e10;
}
.gu-steps__rail [data-state="now"] {
color: var(--text);
}
.gu-steps__rail [data-state="now"] button {
border-color: var(--text);
background: var(--text);
color: var(--card);
transform: scale(1.12);
}
.gu-steps__rail button:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 2px;
}
.gu-steps__panel {
display: grid;
gap: 12px;
min-height: 250px;
align-content: start;
animation: gu-steps-in 0.45s var(--ease);
}
.gu-steps__panel[data-dir="-1"] {
animation-name: gu-steps-back;
}
@keyframes gu-steps-in {
from {
opacity: 0;
transform: translateX(40px);
}
}
@keyframes gu-steps-back {
from {
opacity: 0;
transform: translateX(-40px);
}
}
.gu-steps h3 {
margin: 0 0 4px;
font-size: 1.5rem;
font-weight: 900;
letter-spacing: -0.03em;
outline: none;
}
.gu-steps__choices {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.gu-steps__choice {
position: relative;
}
.gu-steps__choice input {
position: absolute;
opacity: 0;
}
.gu-steps__choice span {
display: flex;
align-items: center;
min-height: 56px;
padding: 10px 14px;
border: 1.5px solid var(--line);
border-radius: 14px;
font-weight: 700;
cursor: pointer;
transition:
border-color 0.2s,
background-color 0.2s,
transform 0.2s;
}
.gu-steps__choice span:hover {
border-color: var(--text);
}
.gu-steps__choice input:checked + span {
border-color: var(--accent);
background: color-mix(in srgb, var(--accent) 14%, transparent);
}
.gu-steps__choice input:active + span {
transform: scale(0.97);
}
.gu-steps__choice input:focus-visible + span {
outline: 3px solid var(--focus);
outline-offset: 2px;
}
.gu-steps__field {
display: grid;
gap: 6px;
font-size: 0.85rem;
font-weight: 700;
}
.gu-steps__field input,
.gu-steps__field textarea {
width: 100%;
min-height: 50px;
padding: 12px 14px;
border: 1.5px solid var(--line);
border-radius: 12px;
outline: none;
background: var(--field);
color: inherit;
font: inherit;
font-weight: 500;
font-size: 1rem;
resize: vertical;
}
.gu-steps__field :is(input, textarea):focus {
border-color: var(--focus);
}
.gu-steps__review {
display: grid;
gap: 8px;
margin: 0;
}
.gu-steps__review div {
display: grid;
grid-template-columns: 1fr auto;
gap: 2px 12px;
padding: 12px 14px;
border-radius: 14px;
background: var(--field);
}
.gu-steps__review dt {
color: var(--muted);
font-size: 0.78rem;
font-weight: 700;
}
.gu-steps__review dd {
grid-column: 1;
margin: 0;
font-weight: 700;
overflow-wrap: anywhere;
}
.gu-steps__review button {
grid-row: 1 / span 2;
grid-column: 2;
align-self: center;
min-height: 40px;
padding: 0 12px;
border: 0;
border-radius: 99px;
background: none;
color: inherit;
font: inherit;
font-weight: 700;
text-decoration: underline;
cursor: pointer;
}
.gu-steps__error {
min-height: 1.2em;
margin: 0;
color: var(--error);
font-size: 0.85rem;
font-weight: 700;
}
.gu-steps__bar {
display: flex;
justify-content: space-between;
gap: 10px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.gu-steps__bar button {
min-height: 50px;
padding: 0 22px;
border-radius: 999px;
font: inherit;
font-weight: 800;
cursor: pointer;
}
.gu-steps__back {
border: 1.5px solid var(--line);
background: none;
color: inherit;
}
.gu-steps__back:disabled {
opacity: 0.35;
cursor: not-allowed;
}
.gu-steps__next {
flex: 1;
max-width: 220px;
border: 0;
background: var(--accent);
color: #0e0e10;
}
.gu-steps__bar button:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 2px;
}
.gu-steps--sent {
justify-items: center;
padding: 44px 28px;
text-align: center;
}
.gu-steps--sent p {
margin: 0;
color: var(--muted);
}
.gu-steps__burst {
display: grid;
place-items: center;
width: 72px;
height: 72px;
border-radius: 50%;
background: var(--accent);
color: #0e0e10;
font-size: 2rem;
font-weight: 900;
animation: gu-steps-pop 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes gu-steps-pop {
from {
transform: scale(0) rotate(-30deg);
}
}
.gu-sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
}
@container (max-width: 380px) {
.gu-steps__choices {
grid-template-columns: 1fr;
}
}
@media (prefers-reduced-motion: reduce) {
.gu-steps__panel,
.gu-steps__panel[data-dir="-1"] {
animation: gu-steps-fade 0.15s;
}
@keyframes gu-steps-fade {
from {
opacity: 0;
}
}
.gu-steps__rail::after,
.gu-steps__rail button {
transition: none;
}
.gu-steps__burst {
animation: none;
}
}
```
You might also want
Next piece: Newsletter signupDate range picker
Components, advanced
File dropzone
Components, intermediate
Floating label form
Components, beginner