-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathProductGallery.tsx
More file actions
124 lines (110 loc) · 4.26 KB
/
Copy pathProductGallery.tsx
File metadata and controls
124 lines (110 loc) · 4.26 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
"use client";
import Image from "next/image";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { ChevronLeft, ChevronRight, ImageOff } from "lucide-react";
import { useVariantStore } from "@/store/variant";
type Variant = {
color: string;
images: string[];
};
export interface ProductGalleryProps {
productId: string;
variants: Variant[];
initialVariantIndex?: number;
className?: string;
}
function isValidSrc(src: string | undefined | null) {
return typeof src === "string" && src.trim().length > 0;
}
export default function ProductGallery({
productId,
variants,
initialVariantIndex = 0,
className = "",
}: ProductGalleryProps) {
const validVariants = useMemo(
() => variants.filter((v) => Array.isArray(v.images) && v.images.some(isValidSrc)),
[variants]
);
const variantIndex =
useVariantStore(
(s) => s.selectedByProduct[productId] ?? Math.min(initialVariantIndex, Math.max(validVariants.length - 1, 0))
);
const images = validVariants[variantIndex]?.images?.filter(isValidSrc) ?? [];
const [activeIndex, setActiveIndex] = useState(0);
const mainRef = useRef<HTMLDivElement>(null);
useEffect(() => {
setActiveIndex(0);
}, [variantIndex]);
const go = useCallback(
(dir: -1 | 1) => {
if (images.length === 0) return;
setActiveIndex((i) => (i + dir + images.length) % images.length);
},
[images.length]
);
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (!mainRef.current) return;
if (!document.activeElement) return;
if (!mainRef.current.contains(document.activeElement)) return;
if (e.key === "ArrowLeft") go(-1);
if (e.key === "ArrowRight") go(1);
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [go]);
return (
<section className={`flex w-full flex-col gap-4 lg:flex-row ${className}`}>
<div className="order-2 flex gap-3 overflow-x-auto lg:order-1 lg:flex-col">
{images.map((src, i) => (
<button
key={`${src}-${i}`}
aria-label={`Thumbnail ${i + 1}`}
onClick={() => setActiveIndex(i)}
className={`relative h-16 w-16 flex-shrink-0 overflow-hidden rounded-lg ring-1 ring-light-300 focus:outline-none focus-visible:ring-2 focus-visible:ring-[--color-dark-500] ${i === activeIndex ? "ring-[--color-dark-500]" : ""}`}
>
<Image src={src} alt={`Thumbnail ${i + 1}`} fill sizes="64px" className="object-cover" />
</button>
))}
</div>
<div ref={mainRef} className="order-1 relative w-full h-[500px] overflow-hidden rounded-xl bg-light-200 lg:order-2">
{images.length > 0 ? (
<>
<Image
src={images[activeIndex]}
alt="Product image"
fill
sizes="(min-width:1024px) 720px, 100vw"
className="object-cover"
priority
/>
<div className="absolute inset-0 flex items-center justify-between px-2">
<button
aria-label="Previous image"
onClick={() => go(-1)}
className="rounded-full bg-light-100/80 p-2 ring-1 ring-light-300 transition hover:bg-light-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-[--color-dark-500]"
>
<ChevronLeft className="h-5 w-5 text-dark-900" />
</button>
<button
aria-label="Next image"
onClick={() => go(1)}
className="rounded-full bg-light-100/80 p-2 ring-1 ring-light-300 transition hover:bg-light-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-[--color-dark-500]"
>
<ChevronRight className="h-5 w-5 text-dark-900" />
</button>
</div>
</>
) : (
<div className="flex h-full w-full items-center justify-center text-dark-700">
<div className="flex items-center gap-2 rounded-lg border border-light-300 bg-light-100 px-4 py-3">
<ImageOff className="h-5 w-5" />
<span className="text-body">No images available</span>
</div>
</div>
)}
</div>
</section>
);
}