-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcart-demo.tsx
More file actions
255 lines (234 loc) · 8.58 KB
/
cart-demo.tsx
File metadata and controls
255 lines (234 loc) · 8.58 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
"use client";
import Image from "next/image";
import { useCallback, useEffect, useRef, useState } from "react";
import { IconCart } from "@/components/assets/icons/icon-cart";
type Phase =
| "idle"
| "cursor-enter"
| "cursor-on-button"
| "press"
| "updated"
| "hold";
interface Product {
name: string;
price: string;
image: string;
imageDark: string;
}
const products: Product[] = [
{
name: "Running",
price: "$95",
image: "/sneakers/sneaker-1.png",
imageDark: "/sneakers/sneaker-1-dark.png",
},
{
name: "Sport",
price: "$88",
image: "/sneakers/sneaker-4.png",
imageDark: "/sneakers/sneaker-4-dark.png",
},
{
name: "Classic",
price: "$62",
image: "/sneakers/sneaker-2.png",
imageDark: "/sneakers/sneaker-2-dark.png",
},
];
const CursorIcon = ({ className }: { className?: string }) => (
<svg className={className} fill="currentColor" viewBox="0 0 24 24">
<path d="M5.5 3.21V20.8c0 .45.54.67.85.35l4.86-4.86a.5.5 0 0 1 .35-.15h6.87a.5.5 0 0 0 .35-.85L6.35 2.85a.5.5 0 0 0-.85.36Z" />
</svg>
);
export const CartDemo = () => {
const [phase, setPhase] = useState<Phase>("idle");
const ref = useRef<HTMLDivElement>(null);
const hasStarted = useRef(false);
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
const clearTimeouts = useCallback(() => {
if (timeoutRef.current) clearTimeout(timeoutRef.current);
}, []);
const runAnimation = useCallback(() => {
setPhase("cursor-enter");
timeoutRef.current = setTimeout(() => {
setPhase("cursor-on-button");
timeoutRef.current = setTimeout(() => {
setPhase("press");
timeoutRef.current = setTimeout(() => {
setPhase("updated");
timeoutRef.current = setTimeout(() => {
setPhase("hold");
timeoutRef.current = setTimeout(() => {
setPhase("idle");
timeoutRef.current = setTimeout(() => {
runAnimation();
}, 1500);
}, 2500);
}, 100);
}, 200);
}, 400);
}, 650);
}, []);
useEffect(() => {
if (hasStarted.current) return;
const observer = new IntersectionObserver(
([entry]) => {
if (entry?.isIntersecting && !hasStarted.current) {
hasStarted.current = true;
observer.disconnect();
setTimeout(() => runAnimation(), 800);
}
},
{ threshold: 0.5 },
);
if (ref.current) observer.observe(ref.current);
return () => observer.disconnect();
}, [runAnimation]);
useEffect(() => clearTimeouts, [clearTimeouts]);
const isUpdated = phase === "updated" || phase === "hold";
const showCursor =
phase === "cursor-enter" ||
phase === "cursor-on-button" ||
phase === "press";
const isPressing = phase === "press";
return (
<div
className="overflow-hidden rounded-xl border border-gray-alpha-400 bg-background-100"
ref={ref}
>
{/* Header */}
<div className="flex items-center justify-between border-b border-gray-alpha-400 px-5 py-3.5">
<span className="text-xs font-medium text-gray-700">Shop</span>
<div className="relative">
<IconCart className="text-foreground" size={16} />
{isUpdated && (
<span className="absolute -right-2 -top-2 flex size-3.5 items-center justify-center rounded-full bg-red-800 text-[9px] font-medium text-white animate-[scale-in_0.15s_ease-out]">
1
</span>
)}
</div>
</div>
{/* Body */}
<div className="flex flex-col bg-background-200 xl:flex-row">
{/* Products */}
<div className="relative flex flex-col gap-1 border-b border-gray-alpha-400 p-5 xl:flex-[2] xl:border-b-0 xl:border-r">
<span className="mb-2 text-xs font-medium text-gray-700">
Products
</span>
{products.map((product, i) => {
const isFirst = i === 0;
const isLast = i === products.length - 1;
return (
<div
className={`relative flex items-center gap-4 rounded px-2 ${
isFirst
? "border border-gray-alpha-400 bg-background-100"
: ""
} ${!isFirst && !isLast ? "border-b border-gray-200" : ""}`}
key={product.name}
>
<div className="relative size-14 shrink-0">
<Image
alt={product.name}
className="object-contain dark:hidden"
fill
sizes="56px"
src={product.image}
/>
<Image
alt={product.name}
className="hidden object-contain dark:block"
fill
sizes="56px"
src={product.imageDark}
/>
</div>
<div className="flex min-w-0 flex-1 flex-col">
<span className="text-xs text-foreground">
{product.name}
</span>
<span className="text-xs text-gray-600">{product.price}</span>
</div>
<button
className={`flex shrink-0 items-center gap-1.5 rounded-full px-3 py-2 text-xs font-medium transition-transform bg-background-200 text-gray-800 border border-gray-alpha-400 ${isFirst && isPressing ? "scale-95" : "scale-100"}`}
disabled={phase !== "idle"}
onClick={() => {
if (isFirst && phase === "idle") runAnimation();
}}
type="button"
>
<IconCart size={12} />
Add
</button>
{isLast && (
<div
aria-hidden
className="pointer-events-none absolute inset-0 bg-gradient-to-t from-background-200 to-transparent"
/>
)}
</div>
);
})}
{/* Animated cursor */}
{showCursor && (
<div
className="pointer-events-none absolute z-10 transition-all duration-[600ms] ease-[cubic-bezier(0.22,1,0.36,1)] drop-shadow-md"
style={{
top: phase === "cursor-enter" ? "85%" : "70px",
right: phase === "cursor-enter" ? "5%" : "32px",
}}
>
<CursorIcon
className={`size-6 text-foreground transition-transform duration-100 ${
isPressing ? "scale-75" : "scale-100"
}`}
/>
</div>
)}
</div>
{/* Cart */}
<div className="flex w-full flex-col p-5 xl:w-[34%]">
<span className="mb-4 text-xs font-medium text-gray-700">Cart</span>
{isUpdated ? (
<div className="flex flex-col gap-4 animate-[fade-in_0.15s_ease]">
<div className="flex items-center gap-3">
<div className="relative flex size-12 shrink-0 items-center justify-center rounded border border-gray-alpha-400 bg-background-100">
<Image
alt="Running"
className="object-contain p-1 dark:hidden"
fill
sizes="48px"
src="/sneakers/sneaker-1.png"
/>
<Image
alt="Running"
className="hidden object-contain p-1 dark:block"
fill
sizes="48px"
src="/sneakers/sneaker-1-dark.png"
/>
</div>
<div className="flex min-w-0 flex-1 flex-col">
<div className="flex items-center justify-between gap-2">
<span className="text-xs text-foreground">Running</span>
<span className="text-xs text-gray-700">$95</span>
</div>
<span className="text-[9px] text-gray-600">Shoe</span>
</div>
</div>
<div className="border-t border-gray-alpha-400" />
<div className="flex items-center justify-between text-xs">
<span className="text-foreground">Total</span>
<span className="text-foreground">$95.00</span>
</div>
</div>
) : (
<div className="flex flex-1 items-center justify-center">
<span className="text-xs text-gray-500">Empty</span>
</div>
)}
</div>
</div>
</div>
);
};