x
This commit is contained in:
1 parent
c2fc97844f
commit
c42395aab3
2 files changed
+34
-28
No files matched your search
@@ -13,6 +13,23 @@ export default function ProductImageGallery({ images, fallback, alt = 'Product I
|
||||
const scrollRef = useRef<HTMLDivElement>(null)
|
||||
const isScrollingRef = useRef(false)
|
||||
|
||||
useEffect(() => {
|
||||
const el = scrollRef.current
|
||||
if (!el) return
|
||||
const onScrollEnd = () => {
|
||||
if (isScrollingRef.current) return
|
||||
const width = el.clientWidth
|
||||
if (width > 0) {
|
||||
const newIndex = Math.round(el.scrollLeft / width)
|
||||
if (newIndex >= 0 && newIndex < (images?.length || 0)) {
|
||||
setCurrentIndex(newIndex)
|
||||
}
|
||||
}
|
||||
}
|
||||
el.addEventListener('scrollend', onScrollEnd)
|
||||
return () => el.removeEventListener('scrollend', onScrollEnd)
|
||||
}, [images?.length])
|
||||
|
||||
const handleThumbnailClick = (idx: number) => {
|
||||
setCurrentIndex(idx)
|
||||
if (scrollRef.current) {
|
||||
@@ -25,19 +42,6 @@ export default function ProductImageGallery({ images, fallback, alt = 'Product I
|
||||
}
|
||||
}
|
||||
|
||||
const handleScroll = (e: React.UIEvent<HTMLDivElement>) => {
|
||||
if (isScrollingRef.current) return
|
||||
const container = e.currentTarget
|
||||
const scrollLeft = container.scrollLeft
|
||||
const width = container.clientWidth
|
||||
if (width > 0) {
|
||||
const newIndex = Math.round(scrollLeft / width)
|
||||
if (newIndex !== currentIndex && newIndex >= 0 && newIndex < (images?.length || 0)) {
|
||||
setCurrentIndex(newIndex)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort images by order if they exist, else just use the array
|
||||
const sortedImages = images?.length ? [...images].sort((a, b) => (a.order || 0) - (b.order || 0)) : []
|
||||
|
||||
@@ -89,7 +93,6 @@ export default function ProductImageGallery({ images, fallback, alt = 'Product I
|
||||
<div className="relative aspect-square w-full flex-1 overflow-hidden rounded-3xl bg-pop-bg shadow-sm border border-pop-black/10 flex items-center justify-center">
|
||||
<div
|
||||
ref={scrollRef}
|
||||
onScroll={handleScroll}
|
||||
className="flex w-full h-full overflow-x-auto snap-x snap-mandatory hide-scrollbar"
|
||||
>
|
||||
{sortedImages.map((img, idx) => (
|
||||
|
||||
Reference in new issue
Block a user