Repository navigation
Expand file tree
/
Copy pathstory-progress.tsx
More file actions
263 lines (244 loc) · 9.2 KB
/
Copy pathstory-progress.tsx
File metadata and controls
263 lines (244 loc) · 9.2 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
"use client";
import { useEffect, useRef, useState } from "react";
import { AnimatePresence, motion } from "motion/react";
import { useReducedMotion } from "@/lib/use-reduced-motion";
import { cn } from "@/lib/cn";
// Long enough to read a short caption, the same as Instagram's default.
const DURATION = 4000;
// Shorter presses are taps that navigate; longer ones are holds that pause.
const HOLD_MS = 200;
const ICON_SWAP = { type: "spring", duration: 0.3, bounce: 0 } as const;
export type Story = { value: string; caption: string };
export function StoryProgress({
stories,
className,
}: {
stories: Story[];
className?: string;
}) {
const reduce = useReducedMotion();
const [index, setIndex] = useState(0);
// Auto-advancing stays silent; only stories the user moved to are announced.
const [manual, setManual] = useState(false);
const [pressing, setPressing] = useState(false);
const [held, setHeld] = useState(false);
const [toggled, setToggled] = useState(false);
const [hidden, setHidden] = useState(false);
const paused = pressing || toggled || hidden;
const pausedRef = useRef(paused);
const fills = useRef<(HTMLSpanElement | null)[]>([]);
const timer = useRef<Animation>(null);
const holdTimer = useRef<ReturnType<typeof setTimeout>>(undefined);
const count = stories.length;
const go = (step: number) => {
setManual(true);
setIndex((i) => (i + step + count) % count);
};
// A compositor-driven transform animation is the timer itself: when it
// finishes, the story advances, so the bar and the content can't drift.
useEffect(() => {
const fill = fills.current[index];
if (!fill) return;
const animation = fill.animate(
[{ transform: "scaleX(0)" }, { transform: "scaleX(1)" }],
{ duration: DURATION, easing: "linear", fill: "forwards" },
);
if (pausedRef.current) animation.pause();
animation.onfinish = () => {
setManual(false);
setIndex((i) => (i + 1) % count);
};
timer.current = animation;
return () => animation.cancel();
}, [index, count]);
useEffect(() => {
pausedRef.current = paused;
const animation = timer.current;
if (!animation) return;
if (paused) animation.pause();
else if (animation.playState === "paused") animation.play();
}, [paused]);
useEffect(() => {
const sync = () => setHidden(document.hidden);
document.addEventListener("visibilitychange", sync);
return () => document.removeEventListener("visibilitychange", sync);
}, []);
useEffect(() => () => clearTimeout(holdTimer.current), []);
const release = (navigate: boolean, clientX = 0, box?: DOMRect) => {
clearTimeout(holdTimer.current);
setPressing(false);
if (held) setHeld(false);
else if (navigate && box) go(clientX < box.left + box.width / 2 ? -1 : 1);
};
const story = stories[index];
const dimmed = held || toggled;
return (
<div
role="region"
aria-roledescription="stories"
aria-label="Stories"
tabIndex={0}
className={cn(
"relative flex h-[480px] w-[min(320px,100%)] touch-manipulation flex-col overflow-hidden rounded-3xl bg-surface text-foreground shadow-raised outline-hidden select-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground",
className,
)}
onPointerDown={(e) => {
if (e.button !== 0) return;
e.currentTarget.setPointerCapture(e.pointerId);
// The fill freezes the moment you press; the dim waits until it is
// clearly a hold, so quick taps don't flicker.
setPressing(true);
holdTimer.current = setTimeout(() => setHeld(true), HOLD_MS);
}}
onPointerUp={(e) =>
release(true, e.clientX, e.currentTarget.getBoundingClientRect())
}
onPointerCancel={() => release(false)}
onContextMenu={(e) => e.preventDefault()}
onKeyDown={(e) => {
if (e.key === "ArrowRight") go(1);
else if (e.key === "ArrowLeft") go(-1);
else if (e.key === " " && !e.repeat) setToggled((t) => !t);
else return;
e.preventDefault();
}}
>
<div className="flex gap-1 px-3 pt-3" aria-hidden>
{stories.map((_, i) => (
<span
key={i}
className="h-[3px] flex-1 overflow-hidden rounded-full bg-foreground/15"
>
<span
ref={(el) => {
fills.current[i] = el;
}}
className="block h-full origin-left rounded-full bg-foreground"
style={{ transform: `scaleX(${i < index ? 1 : 0})` }}
/>
</span>
))}
</div>
<div className="flex h-11 items-center justify-between px-4">
<span className="text-xs text-muted tabular-nums">
{index + 1} / {count}
</span>
{/* Static cue for the paused state, so the frozen bar isn't the only signal. */}
<button
type="button"
aria-label={toggled ? "Play" : "Pause"}
className="relative -mr-2 grid size-10 place-items-center rounded-full text-muted transition-[scale,color] duration-150 ease-out hover:text-foreground focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.96]"
onPointerDown={(e) => e.stopPropagation()}
onPointerUp={(e) => e.stopPropagation()}
onKeyDown={(e) => {
// Let Space reach the button's own click instead of toggling twice.
if (e.key === " " || e.key === "Enter") e.stopPropagation();
}}
onClick={() => setToggled((t) => !t)}
>
<PlayPause paused={dimmed} />
</button>
</div>
<div
className={cn(
"relative flex-1 transition-[opacity] duration-200 ease-out",
dimmed && "opacity-60",
)}
>
<AnimatePresence initial={false}>
<motion.div
key={index}
aria-hidden
className="absolute inset-0 flex flex-col justify-end gap-2 p-6"
initial={{ opacity: 0, filter: reduce ? "blur(0px)" : "blur(4px)" }}
animate={{
opacity: 1,
filter: "blur(0px)",
transition: { duration: 0.25, ease: [0.23, 1, 0.32, 1] },
}}
exit={{
opacity: 0,
filter: reduce ? "blur(0px)" : "blur(4px)",
transition: { duration: 0.15, ease: [0.23, 1, 0.32, 1] },
}}
>
<span className="text-7xl font-medium tracking-tight tabular-nums">
{story.value}
</span>
<span className="text-[15px] text-pretty text-muted">
{story.caption}
</span>
</motion.div>
</AnimatePresence>
{/* A persistent region: one that mounts with its content is rarely read. */}
<p className="sr-only" aria-live={manual ? "polite" : "off"}>
{story.value}. {story.caption}
</p>
</div>
{/* Reachable by screen readers; pointer taps are handled by the card,
so a real click (detail > 0) is ignored here to avoid a double step. */}
<div className="absolute inset-x-0 top-[60px] bottom-0 flex">
<button
type="button"
tabIndex={-1}
aria-label="Previous story"
className="flex-1 outline-hidden"
onClick={(e) => e.detail === 0 && go(-1)}
/>
<button
type="button"
tabIndex={-1}
aria-label="Next story"
className="flex-1 outline-hidden"
onClick={(e) => e.detail === 0 && go(1)}
/>
</div>
</div>
);
}
function PlayPause({ paused }: { paused: boolean }) {
return (
<span className="grid size-4">
<motion.svg
viewBox="0 0 16 16"
className="col-start-1 row-start-1 size-4"
fill="currentColor"
initial={false}
animate={
paused
? { scale: 0.25, opacity: 0, filter: "blur(4px)" }
: { scale: 1, opacity: 1, filter: "blur(0px)" }
}
transition={ICON_SWAP}
>
<rect x="4" y="3" width="2.5" height="10" rx="1" />
<rect x="9.5" y="3" width="2.5" height="10" rx="1" />
</motion.svg>
<motion.svg
viewBox="0 0 16 16"
className="col-start-1 row-start-1 size-4"
fill="currentColor"
initial={false}
animate={
paused
? { scale: 1, opacity: 1, filter: "blur(0px)" }
: { scale: 0.25, opacity: 0, filter: "blur(4px)" }
}
transition={ICON_SWAP}
>
{/* Nudged right of centre so the triangle looks optically centred. */}
<path d="M5.5 3.4v9.2a.6.6 0 0 0 .9.5l7-4.6a.6.6 0 0 0 0-1L6.4 2.9a.6.6 0 0 0-.9.5Z" />
</motion.svg>
</span>
);
}
const STORIES: Story[] = [
{ value: "12k", caption: "People tried the lab this week." },
{ value: "4.8", caption: "Average rating across every component." },
{ value: "38%", caption: "Fewer layout shifts since the last release." },
{ value: "212", caption: "Commits that were only about easing curves." },
{ value: "0", caption: "Components that animate from scale zero." },
];
export default function StoryProgressDemo() {
return <StoryProgress stories={STORIES} />;
}