Repository navigation
Expand file tree
/
Copy pathcarousel-3d.tsx
More file actions
346 lines (327 loc) · 11.1 KB
/
Copy pathcarousel-3d.tsx
File metadata and controls
346 lines (327 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"use client";
import { useEffect, useRef, useState } from "react";
import {
animate,
motion,
useMotionValue,
useTransform,
type MotionValue,
} from "motion/react";
import { useReducedMotion } from "@/lib/use-reduced-motion";
import { cn } from "@/lib/cn";
type Item = { title: string; note: string };
const CARD_W = 168;
const CARD_H = 208;
// Space between neighbouring cards along the ring.
const GAP = 24;
const PERSPECTIVE = 1100;
// Dragging this far turns the ring by one card: close to the arc a front card
// travels, so the card under the finger stays under it.
const PX_PER_CARD = 180;
// Apple's scroll deceleration. Projecting the release velocity picks the card
// a flick is heading for, not the one nearest where the finger let go.
const DECELERATION = 0.998;
// Apple ships damping 0.8, response 0.4 for rotation. The small overshoot is
// earned here: the ring only moves after a spin, a throw or a step.
const SPIN = { type: "spring", visualDuration: 0.4, bounce: 0.2 } as const;
// Past this the pointer is dragging, not clicking a card.
const CLICK_SLOP = 6;
// How much a card shrinks and dims by the time it reaches the back.
const BACK_SCALE = 0.9;
const BACK_OPACITY = 0.35;
// Reduced motion lays the ring out flat; neighbours sit at half strength.
const ROW_OPACITY = 0.5;
function project(velocity: number) {
return ((velocity / 1000) * DECELERATION) / (1 - DECELERATION);
}
// The copy of `index` nearest the current rotation, so clicking a card always
// turns the short way round.
function nearestTurn(index: number, rotation: number, count: number) {
return index + Math.round((rotation - index) / count) * count;
}
function wrap(value: number, count: number) {
return ((Math.round(value) % count) + count) % count;
}
export function Carousel3D({
items,
label,
onChange,
className,
}: {
items: Item[];
label: string;
onChange?: (index: number) => void;
className?: string;
}) {
const reduce = useReducedMotion() ?? false;
const count = items.length;
// Counted in cards and never wrapped, so a spin can pass any number of
// turns and the spring still takes the direct path to its target.
const rotation = useMotionValue(0);
const target = useRef(0);
const [active, setActive] = useState(0);
const drag = useRef<{
id: number;
x: number;
from: number;
card: number | null;
moved: boolean;
} | null>(null);
const radius = (CARD_W + GAP) / 2 / Math.tan(Math.PI / count);
const goTo = (next: number, velocity = rotation.getVelocity()) => {
target.current = next;
const index = wrap(next, count);
setActive(index);
onChange?.(index);
if (reduce) {
rotation.jump(next);
return;
}
animate(rotation, next, { ...SPIN, velocity });
};
// Presses stack: two quick presses aim two cards ahead instead of
// restarting from wherever the ring happens to be mid-turn.
const step = (direction: 1 | -1) => goTo(target.current + direction);
const settle = () => goTo(Math.round(rotation.get()));
useEffect(() => () => rotation.stop(), [rotation]);
return (
<div
className={cn(
"flex w-[min(520px,100%)] flex-col items-center gap-5",
className,
)}
>
<div
role="group"
aria-roledescription="carousel"
aria-label={label}
tabIndex={0}
// pan-y: a vertical swipe still scrolls the page on touch screens.
className="relative h-[260px] w-full cursor-grab touch-pan-y overflow-hidden rounded-2xl outline-hidden select-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground active:cursor-grabbing"
style={{ perspective: PERSPECTIVE }}
onKeyDown={(e) => {
if (e.key !== "ArrowLeft" && e.key !== "ArrowRight") return;
e.preventDefault();
step(e.key === "ArrowLeft" ? -1 : 1);
}}
onPointerDown={(e) => {
// A second finger mid-drag would make the ring jump to it.
if (e.button !== 0 || drag.current) return;
// Grabbing a spinning ring stops it exactly where it is.
rotation.stop();
const card = (e.target as HTMLElement).closest<HTMLElement>(
"[data-card]",
);
drag.current = {
id: e.pointerId,
x: e.clientX,
from: rotation.get(),
card: card ? Number(card.dataset.card) : null,
moved: false,
};
e.currentTarget.setPointerCapture(e.pointerId);
}}
onPointerMove={(e) => {
const d = drag.current;
if (!d || d.id !== e.pointerId) return;
const dx = e.clientX - d.x;
if (!d.moved) {
if (Math.abs(dx) < CLICK_SLOP) return;
d.moved = true;
// Start from the finger, not from where the slop ran out.
d.x = e.clientX;
return;
}
// Dragging right brings the card on the left forward.
rotation.set(d.from - dx / PX_PER_CARD);
}}
onPointerUp={(e) => {
const d = drag.current;
if (!d || d.id !== e.pointerId) return;
drag.current = null;
if (d.moved) {
const velocity = rotation.getVelocity();
// Capped at one full turn, so a wild flick never loses your place.
const reach = Math.max(-count, Math.min(project(velocity), count));
goTo(Math.round(rotation.get() + reach), velocity);
} else if (d.card !== null) {
goTo(nearestTurn(d.card, rotation.get(), count));
} else {
settle();
}
}}
onPointerCancel={(e) => {
if (drag.current?.id !== e.pointerId) return;
drag.current = null;
settle();
}}
>
{/* Pushed back by the radius, so the front card sits on the
perspective plane and renders at exactly its own size. */}
<div
className="absolute top-1/2 left-1/2"
style={{
transformStyle: "preserve-3d",
transform: reduce ? "none" : `translateZ(${-radius}px)`,
}}
>
{items.map((item, i) => (
<Card
// Remounts when the preference flips, so every transform
// switches layout at once.
key={`${i}-${reduce}`}
item={item}
index={i}
count={count}
radius={radius}
rotation={rotation}
front={i === active}
reduce={reduce}
/>
))}
</div>
</div>
<div className="flex items-center gap-3">
<RingButton label="Previous card" onClick={() => step(-1)}>
<path d="M10 3.5 5.5 8l4.5 4.5" />
</RingButton>
<p
className="w-16 text-center font-mono text-sm text-muted tabular-nums"
aria-live="polite"
aria-atomic
>
<span className="sr-only">{items[active].title}, card </span>
{String(active + 1).padStart(2, "0")}
<span aria-hidden> / </span>
<span className="sr-only"> of </span>
{String(count).padStart(2, "0")}
</p>
<RingButton label="Next card" onClick={() => step(1)}>
<path d="m6 3.5 4.5 4.5L6 12.5" />
</RingButton>
</div>
</div>
);
}
function Card({
item,
index,
count,
radius,
rotation,
front,
reduce,
}: {
item: Item;
index: number;
count: number;
radius: number;
rotation: MotionValue<number>;
front: boolean;
reduce: boolean;
}) {
// Signed distance from the front in cards, wrapped the short way round.
const offset = useTransform(rotation, (r) => {
const d = (((index - r) % count) + count) % count;
return d > count / 2 ? d - count : d;
});
// 1 facing you, -1 facing away. Every depth cue reads from this one number.
const facing = useTransform(offset, (o) =>
Math.cos((o / count) * Math.PI * 2),
);
const transform = useTransform(offset, (o) => {
if (reduce) return `translateX(${o * (CARD_W + GAP)}px)`;
const f = Math.cos((o / count) * Math.PI * 2);
const scale = BACK_SCALE + ((1 - BACK_SCALE) * (f + 1)) / 2;
return `rotateY(${(o / count) * 360}deg) translateZ(${radius}px) scale(${scale})`;
});
const opacity = useTransform(offset, (o) => {
if (reduce) return Math.abs(o) < 0.5 ? 1 : ROW_OPACITY;
const f = Math.cos((o / count) * Math.PI * 2);
return BACK_OPACITY + ((1 - BACK_OPACITY) * (f + 1)) / 2;
});
// Text is gone before a card turns side-on, so nobody reads a card mirrored
// through the ring; the back half shows blank faces.
const text = useTransform(facing, (f) =>
reduce ? 1 : Math.min(Math.max((f - 0.1) / 0.5, 0), 1),
);
return (
<motion.div
data-card={index}
aria-hidden={!front}
className={cn(
"absolute flex flex-col justify-between rounded-[20px] bg-background p-5 shadow-raised",
// Reduced motion: the row cross-fades which card is lit instead.
reduce && "transition-[opacity] duration-200 ease-out",
)}
style={{
width: CARD_W,
height: CARD_H,
left: -CARD_W / 2,
top: -CARD_H / 2,
transform,
opacity,
}}
>
<motion.span
style={{ opacity: text }}
className="font-mono text-xs text-muted tabular-nums"
>
{String(index + 1).padStart(2, "0")}
</motion.span>
<motion.p
style={{ opacity: text }}
className="text-lg leading-snug font-medium tracking-tight text-balance text-foreground"
>
{item.title}
<span className="mt-1.5 block text-sm leading-normal font-normal tracking-normal text-pretty text-muted">
{item.note}
</span>
</motion.p>
</motion.div>
);
}
function RingButton({
label,
onClick,
children,
}: {
label: string;
onClick: () => void;
children: React.ReactNode;
}) {
return (
<button
type="button"
aria-label={label}
onClick={onClick}
className="flex size-11 items-center justify-center rounded-full bg-surface text-foreground shadow-raised outline-hidden transition-[scale] duration-150 ease-out focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-none"
>
<svg
viewBox="0 0 16 16"
className="size-4"
fill="none"
stroke="currentColor"
strokeWidth={1.5}
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden
>
{children}
</svg>
</button>
);
}
const ITEMS: Item[] = [
{ title: "Response", note: "Feedback lands on press." },
{ title: "Tracking", note: "Content stays under the finger." },
{ title: "Interruption", note: "Grab it mid-flight." },
{ title: "Momentum", note: "Land where the flick was heading." },
{ title: "Resistance", note: "Soft edges, never walls." },
{ title: "Springs", note: "Behaviour, not a timeline." },
{ title: "Restraint", note: "Bounce only after a throw." },
{ title: "Consistency", note: "Leave the way you came." },
];
export default function Carousel3dDemo() {
return <Carousel3D items={ITEMS} label="Motion principles" />;
}