Repository navigation
Expand file tree
/
Copy pathprogress-stepper.tsx
More file actions
300 lines (283 loc) · 11.1 KB
/
Copy pathprogress-stepper.tsx
File metadata and controls
300 lines (283 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"use client";
import { useEffect, useRef, useState } from "react";
import {
animate,
motion,
useMotionValue,
useTransform,
type AnimationPlaybackControls,
} from "motion/react";
import { useReducedMotion } from "@/lib/use-reduced-motion";
import { cn } from "@/lib/cn";
const ICON_SWAP = { type: "spring", duration: 0.3, bounce: 0 } as const;
// One connector fills in 240ms: long enough to read as travel from one step
// to the next, short enough to stay under the 300ms UI budget.
const LINE_MS = 240;
// A jump across several steps runs the connectors as a relay, each starting
// halfway through the previous one, so a full reset still ends in ~480ms.
const STAGGER_MS = 120;
// The step a line is heading for lights up just before the fill reaches it,
// so the circle reads as the line's destination rather than a separate event.
const ARRIVE_MS = 160;
// The "you are here" halo is one capsule that inches along the track: the
// head leaves with the line fill and lands as it does, the tail lets go this
// much later and catches up, so the halo stretches over the connector and
// then pulls itself in around the new step. Longer than the 300ms budget in
// total (up to ~600ms on a full reset) because it is the travel the fill
// already takes, with the tail's short lag on the end.
const TAIL_LAG = 0.12;
const INCH_EASE = [0.77, 0, 0.175, 1] as const;
// 36px circle plus a 4px halo on each side.
const HALO = 22;
type Status = "complete" | "current" | "upcoming";
export function ProgressStepper({
steps,
current,
label = "Progress",
className,
}: {
steps: string[];
current: number;
label?: string;
className?: string;
}) {
const reduceMotion = useReducedMotion();
// Remembers where the last move started, so delays run in the direction of
// travel: forward fills left to right, back unfills right to left.
const [move, setMove] = useState({ from: current, to: current });
if (move.to !== current) setMove({ from: move.to, to: current });
const { from } = move;
const last = steps.length - 1;
const n = steps.length;
const olRef = useRef<HTMLOListElement>(null);
const rtl = useRef(false);
const head = useMotionValue(current);
const tail = useMotionValue(current);
const runs = useRef<AnimationPlaybackControls[]>([]);
const prev = useRef(current);
useEffect(() => {
const start = prev.current;
prev.current = current;
if (start === current) return;
if (olRef.current) rtl.current = getComputedStyle(olRef.current).direction === "rtl";
runs.current.forEach((r) => r.stop());
if (reduceMotion) {
head.jump(current);
tail.jump(current);
return;
}
// Matches the relay: the last connector finishes filling at this point.
const travel = ((Math.abs(current - start) - 1) * STAGGER_MS + LINE_MS) / 1000;
runs.current = [
animate(head, current, { duration: travel, ease: INCH_EASE }),
animate(tail, current, { duration: travel, ease: INCH_EASE, delay: TAIL_LAG }),
];
}, [current, reduceMotion, head, tail]);
useEffect(() => () => runs.current.forEach((r) => r.stop()), []);
// One strip the width of the list, clipped down to a capsule between the
// centres of the two ends. clip-path keeps it off layout, and percentages
// follow the grid's equal columns at any width.
const clip = useTransform([head, tail], ([h, t]: number[]) => {
const lo = ((Math.min(h, t) + 0.5) / n) * 100;
const hi = ((Math.max(h, t) + 0.5) / n) * 100;
const [l, r] = rtl.current ? [100 - hi, lo] : [lo, 100 - hi];
return `inset(0 calc(${r}% - ${HALO}px) 0 calc(${l}% - ${HALO}px) round ${HALO}px)`;
});
const lineDelay = (k: number) => {
if (reduceMotion) return 0;
if (current > from && k >= from && k < current) return (k - from) * STAGGER_MS;
if (current < from && k >= current && k < from)
return (from - 1 - k) * STAGGER_MS;
return 0;
};
const stepDelay = (j: number) => {
if (reduceMotion) return 0;
if (current > from && j > from && j <= current)
return (j - 1 - from) * STAGGER_MS + ARRIVE_MS;
if (current < from && j >= current && j < from)
return (from - 1 - j) * STAGGER_MS + ARRIVE_MS;
return 0;
};
const statusOf = (j: number): Status =>
j < current ? "complete" : j === current ? "current" : "upcoming";
return (
<div className={cn("w-[min(520px,100%)]", className)}>
<ol
ref={olRef}
aria-label={label}
className="relative grid"
style={{ gridTemplateColumns: `repeat(${steps.length}, minmax(0, 1fr))` }}
>
<motion.span
aria-hidden
style={{ clipPath: clip }}
// Sits under the circles and the connector, so at rest only a 4px
// ring shows and in flight the line runs through it like a tube.
className="pointer-events-none absolute inset-x-0 -top-1 h-11 bg-foreground/10"
/>
{steps.map((step, j) => {
const status = statusOf(j);
// The last step is a destination, not a task: reaching it completes it.
const checked =
status === "complete" || (status === "current" && j === last);
const delay = stepDelay(j);
return (
<li
key={step}
aria-current={status === "current" ? "step" : undefined}
className="relative flex flex-col items-center gap-2.5"
>
{j < last && (
// Starts 8px clear of each 36px circle: 18px radius + 8px gap.
<span
aria-hidden
className="absolute top-[17px] right-[calc(-50%+26px)] left-[calc(50%+26px)] h-0.5 overflow-hidden rounded-full bg-border"
>
<span
className={cn(
"absolute inset-0 origin-left rounded-full bg-foreground transition-[scale] ease-[cubic-bezier(0.77,0,0.175,1)] motion-reduce:transition-none rtl:origin-right",
j < current ? "scale-x-100" : "scale-x-0",
)}
style={{
transitionDuration: `${LINE_MS}ms`,
transitionDelay: `${lineDelay(j)}ms`,
}}
/>
</span>
)}
<span
aria-hidden
className={cn(
"relative grid size-9 place-items-center rounded-full text-sm font-medium tabular-nums inset-ring transition-[background-color,color,box-shadow] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)]",
checked
? "bg-foreground text-background inset-ring-foreground"
: status === "current"
? "bg-background text-foreground inset-ring-foreground"
: "bg-background text-muted inset-ring-border",
)}
style={{ transitionDelay: `${delay}ms` }}
>
<Swap visible={!checked} delay={delay} reduceMotion={reduceMotion}>
{j + 1}
</Swap>
<Swap visible={checked} delay={delay} reduceMotion={reduceMotion}>
<svg
viewBox="0 0 16 16"
className="size-4"
fill="none"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="m3.5 8.5 3 3 6-7" />
</svg>
</Swap>
</span>
<span
className={cn(
"text-center text-sm transition-[color] duration-200 ease-out",
status === "upcoming" ? "text-muted" : "text-foreground",
)}
style={{ transitionDelay: `${delay}ms` }}
>
{step}
<span className="sr-only">
{checked ? ", completed" : status === "upcoming" ? ", not started" : ""}
</span>
</span>
</li>
);
})}
</ol>
<span className="sr-only" aria-live="polite">
{current !== from
? `Step ${current + 1} of ${steps.length}: ${steps[current]}`
: ""}
</span>
</div>
);
}
function Swap({
visible,
delay,
reduceMotion,
children,
}: {
visible: boolean;
delay: number;
reduceMotion: boolean | null;
children: React.ReactNode;
}) {
// Reduced motion keeps the cross-fade but drops the scale and blur.
const hidden = reduceMotion
? { opacity: 0 }
: { scale: 0.25, opacity: 0, filter: "blur(4px)" };
return (
<motion.span
className="col-start-1 row-start-1 grid place-items-center"
initial={false}
animate={visible ? { scale: 1, opacity: 1, filter: "blur(0px)" } : hidden}
// Both glyphs wait with the circle's fill, so a circle is never left
// blank while its color is still catching up.
transition={{ ...ICON_SWAP, delay: delay / 1000 }}
>
{children}
</motion.span>
);
}
const button =
"h-10 touch-manipulation rounded-full px-4 text-sm font-medium outline-hidden transition-[scale,opacity] duration-150 ease-out select-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-[opacity]";
export default function ProgressStepperDemo() {
const steps = ["Cart", "Shipping", "Payment", "Done"];
const [current, setCurrent] = useState(0);
const last = steps.length - 1;
const atStart = current === 0;
const atEnd = current === last;
return (
<div className="flex w-[min(520px,100%)] flex-col gap-10">
<ProgressStepper steps={steps} current={current} label="Checkout progress" />
<div className="flex items-center justify-between">
{/* aria-disabled rather than disabled, so a keyboard user who backs up
to the start keeps focus on the button instead of losing it. */}
<button
type="button"
aria-disabled={atStart}
onClick={() => !atStart && setCurrent((c) => c - 1)}
className={cn(
button,
"bg-surface text-foreground shadow-raised",
atStart && "cursor-not-allowed opacity-50 active:scale-100",
)}
>
Back
</button>
<button
type="button"
onClick={() => setCurrent((c) => (c === last ? 0 : c + 1))}
className={cn(button, "bg-foreground text-background")}
>
{/* Both labels share one cell, so the button keeps one width and
never shifts under the cursor when it turns into Start over. */}
<span className="grid">
<Label visible={!atEnd}>Continue</Label>
<Label visible={atEnd}>Start over</Label>
</span>
</button>
</div>
</div>
);
}
function Label({ visible, children }: { visible: boolean; children: React.ReactNode }) {
return (
<span
aria-hidden={!visible}
className={cn(
"col-start-1 row-start-1 transition-[opacity,filter] duration-200 ease-out",
!visible && "opacity-0 blur-[4px]",
)}
>
{children}
</span>
);
}