Card Slider
Premium card slider with layered depth, spring animations, and pointer-driven drag interaction using Framer Motion.
Category
SliderReact
CSS
Tailwind
Manual
Create a file and paste the following code into it.
src/components/card-slider.tsx
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"use client";
import { useRef, useState, useEffect } from "react";
import {
motion,
useMotionValue,
useTransform,
animate,
} from "motion/react";
import {
MapPin,
PaintBrush,
Sparkle,
ThumbsUp,
VideoCamera,
} from "@phosphor-icons/react";
/* ═══════════════════════════════════════════════
Constants
═══════════════════════════════════════════════ */
const CARD_COUNT = 5;
const DRAG_SENSITIVITY = 130;
const springTransition = {
type: "spring" as const,
stiffness: 380,
damping: 36,
mass: 0.8,
};
interface PositionConfig {
rotate: number;
x: number;
y: number;
scale: number;
zIndex: number;
}
const desktopPositions: Record<string, PositionConfig> = {
"-2": { rotate: -20, x: -290, y: 38, scale: 0.76, zIndex: 1 },
"-1": { rotate: -10, x: -155, y: 14, scale: 0.87, zIndex: 2 },
"0": { rotate: 0, x: 0, y: -18, scale: 1, zIndex: 5 },
"1": { rotate: 10, x: 155, y: 14, scale: 0.87, zIndex: 2 },
"2": { rotate: 20, x: 290, y: 38, scale: 0.76, zIndex: 1 },
};
const mobilePositions: Record<string, PositionConfig> = {
"-2": { rotate: -18, x: -118, y: 24, scale: 0.58, zIndex: 1 },
"-1": { rotate: -9, x: -64, y: 10, scale: 0.72, zIndex: 2 },
"0": { rotate: 0, x: 0, y: -10, scale: 0.86, zIndex: 5 },
"1": { rotate: 9, x: 64, y: 10, scale: 0.72, zIndex: 2 },
"2": { rotate: 18, x: 118, y: 24, scale: 0.58, zIndex: 1 },
};
/* ═══════════════════════════════════════════════
Helpers
═══════════════════════════════════════════════ */
function interpolatePosition(
offset: number,
key: keyof PositionConfig,
positions: Record<string, PositionConfig> = desktopPositions,
): number {
const clamped = Math.max(-2, Math.min(2, offset));
const lower = Math.max(-2, Math.floor(clamped));
const upper = Math.min(2, Math.ceil(clamped));
if (lower === upper)
return positions[String(lower)][key] as number;
const t = (clamped - lower) / (upper - lower);
const a = positions[String(lower)][key] as number;
const b = positions[String(upper)][key] as number;
return a + (b - a) * t;
}
function circularOffset(index: number, base: number): number {
const raw = ((index - base) % CARD_COUNT + CARD_COUNT) % CARD_COUNT;
return raw > CARD_COUNT / 2 ? raw - CARD_COUNT : raw;
}
function snapToNearest(target: number, current: number): number {
let v = target;
while (v < current - CARD_COUNT / 2) v += CARD_COUNT;
while (v > current + CARD_COUNT / 2) v -= CARD_COUNT;
return v;
}
/* ═══════════════════════════════════════════════
Card Data
═══════════════════════════════════════════════ */
const iconMap: Record<string, React.ReactNode> = {
"thumbs-up": (
<ThumbsUp className="w-3.5 h-3.5 text-gray-900" weight="duotone" />
),
video: (
<VideoCamera className="w-3.5 h-3.5 text-gray-900" weight="duotone" />
),
paintbrush: (
<PaintBrush className="w-3.5 h-3.5 text-gray-900" weight="duotone" />
),
sparkles: (
<Sparkle className="w-3.5 h-3.5 text-gray-900" weight="duotone" />
),
"map-pin": (
<MapPin className="w-3.5 h-3.5 text-gray-900" weight="duotone" />
),
};
interface CardItem {
id: number;
label: string;
category: string;
categoryIcon: string;
bgClass: string;
hasPill?: boolean;
sticker?: { src: string; position: string; size: string };
}
const cards: CardItem[] = [
{
id: 0,
label: "douwe egberts",
category: "social",
categoryIcon: "thumbs-up",
bgClass: "bg-gradient-to-br from-stone-100 via-amber-100 to-stone-200",
hasPill: true,
sticker: {
src: "/images/components/card-slider/rabbit.png",
position: "absolute -bottom-6 -left-10 -rotate-12",
size: "w-24",
},
},
{
id: 1,
label: "smoothiebox",
category: "video",
categoryIcon: "video",
bgClass: "bg-gradient-to-br from-amber-600 via-amber-800 to-amber-950",
hasPill: true,
sticker: {
src: "/images/components/card-slider/pop-corn.png",
position: "absolute -top-7 -left-8 rotate-12",
size: "w-24",
},
},
{
id: 2,
label: "studio",
category: "design",
categoryIcon: "paintbrush",
bgClass: "bg-gradient-to-br from-slate-500 via-slate-700 to-slate-900",
sticker: {
src: "/images/components/card-slider/witch-hat.png",
position: "absolute -top-10 -left-10 -rotate-15",
size: "w-24",
},
},
{
id: 3,
label: "Select Codes",
category: "360",
categoryIcon: "sparkles",
bgClass: "bg-gradient-to-br from-indigo-800 via-purple-900 to-black",
sticker: {
src: "/images/components/card-slider/shooting-star.png",
position: "absolute -bottom-5 -right-8 rotate-6",
size: "w-24",
},
},
{
id: 4,
label: "campaign lab",
category: "ooh",
categoryIcon: "map-pin",
bgClass: "bg-gradient-to-br from-rose-300 via-pink-400 to-fuchsia-500",
hasPill: true,
sticker: {
src: "/images/components/card-slider/rabbit.png",
position: "absolute -bottom-7 -right-9 rotate-15",
size: "w-24",
},
},
];
/* ═══════════════════════════════════════════════
SliderCard
═══════════════════════════════════════════════ */
interface SliderCardProps {
card: CardItem;
index: number;
viewProgress: ReturnType<typeof useMotionValue<number>>;
didDrag: React.RefObject<boolean>;
isMobile: boolean;
}
function SliderCard({
card,
index,
viewProgress,
didDrag,
isMobile,
}: SliderCardProps) {
const positions = isMobile ? mobilePositions : desktopPositions;
const x = useTransform(viewProgress, (v) =>
interpolatePosition(circularOffset(index, v), "x", positions),
);
const y = useTransform(viewProgress, (v) =>
interpolatePosition(circularOffset(index, v), "y", positions),
);
const rotate = useTransform(viewProgress, (v) =>
interpolatePosition(circularOffset(index, v), "rotate", positions),
);
const scale = useTransform(viewProgress, (v) =>
interpolatePosition(circularOffset(index, v), "scale", positions),
);
const zIndex = useTransform(viewProgress, (v) =>
positions[
String(
Math.max(-2, Math.min(2, Math.round(circularOffset(index, v)))),
)
].zIndex,
);
function handleClick() {
if (didDrag.current) return;
animate(viewProgress, snapToNearest(index, viewProgress.get()), springTransition);
}
return (
<motion.div
className="absolute touch-none"
style={{ x, y, rotate, scale, zIndex }}
onClick={handleClick}
>
<div
className={`relative rounded-[1.75rem] p-[5px] bg-white shadow-2xl ${
isMobile ? "w-36 h-52" : "w-56 h-80"
}`}
>
<div
className={`relative w-full h-full rounded-[1.35rem] overflow-hidden ${card.bgClass}`}
>
<div className="absolute inset-0 bg-gradient-to-b from-transparent via-transparent to-black/25 rounded-[1.35rem]" />
<div className="absolute top-3 right-3 flex items-center gap-1.5 bg-white rounded-full px-3 py-1.5 shadow-sm">
{iconMap[card.categoryIcon]}
<span className="text-[11px] font-semibold text-gray-900 leading-none">
{card.category}
</span>
</div>
</div>
</div>
{card.sticker && (
<div
className={`${card.sticker.position} ${card.sticker.size} pointer-events-none`}
>
<img
src={card.sticker.src}
alt=""
className="w-full h-auto drop-shadow-lg"
draggable={false}
/>
</div>
)}
</motion.div>
);
}
/* ═══════════════════════════════════════════════
CardSlider (Main)
═══════════════════════════════════════════════ */
function CardSlider() {
const viewProgress = useMotionValue(2);
const startX = useRef<number | null>(null);
const startValue = useRef<number | null>(null);
const didDrag = useRef(false);
const [isDragging, setIsDragging] = useState(false);
const [isMobile, setIsMobile] = useState(
() => typeof window !== "undefined" && window.innerWidth < 640,
);
useEffect(() => {
const mql = window.matchMedia("(max-width: 639px)");
const handler = (e: MediaQueryListEvent) => setIsMobile(e.matches);
mql.addEventListener("change", handler);
return () => mql.removeEventListener("change", handler);
}, []);
function onPointerDown(e: React.PointerEvent) {
startX.current = e.clientX;
startValue.current = viewProgress.get();
didDrag.current = false;
viewProgress.stop();
}
function onPointerMove(e: React.PointerEvent) {
if (startX.current === null || startValue.current === null) return;
const dx = e.clientX - startX.current;
if (Math.abs(dx) > 8) {
if (!didDrag.current) setIsDragging(true);
didDrag.current = true;
}
const raw = -dx / DRAG_SENSITIVITY;
const clamped = Math.max(-1, Math.min(1, raw));
const rubber = Math.abs(raw) > 1
? (Math.abs(raw) - 1) * 0.2 * Math.sign(raw)
: 0;
viewProgress.set(startValue.current + clamped + rubber);
}
function onPointerUp() {
if (startX.current === null) return;
startX.current = null;
startValue.current = null;
setIsDragging(false);
animate(viewProgress, Math.round(viewProgress.get()), springTransition);
}
return (
<section
className={`relative w-full h-dvh bg-black overflow-hidden flex items-center justify-center select-none touch-none ${
isDragging ? "cursor-grabbing" : "cursor-grab"
}`}
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={onPointerUp}
onPointerLeave={onPointerUp}
>
<div
className={`relative flex items-center justify-center ${
isMobile ? "w-[320px] h-[260px]" : "w-[720px] h-[440px]"
}`}
>
{cards.map((card, i) => (
<SliderCard
key={card.id}
card={card}
index={i}
viewProgress={viewProgress}
didDrag={didDrag}
isMobile={isMobile}
/>
))}
</div>
</section>
);
}
/* ═══════════════════════════════════════════════
Demo Export
═══════════════════════════════════════════════ */
/** @description Card slider demo — premium card carousel with depth and smooth spring motion. */
export default function CardSliderDemo() {
return <CardSlider />;
}
Update the import paths to match your project setup.