From c42395aab3bf6b3dc6c94109fb133fe74edbf52b Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 12 Jul 2026 03:19:49 -0400 Subject: [PATCH] x --- .../src/components/ProductImageGallery.tsx | 31 ++++++++++--------- .../website/src/components/ProductGallery.tsx | 31 ++++++++++--------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/apps/spa/src/components/ProductImageGallery.tsx b/apps/spa/src/components/ProductImageGallery.tsx index ab9c3ef..567994a 100644 --- a/apps/spa/src/components/ProductImageGallery.tsx +++ b/apps/spa/src/components/ProductImageGallery.tsx @@ -13,6 +13,23 @@ export default function ProductImageGallery({ images, fallback, alt = 'Product I const scrollRef = useRef(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) => { - 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
{sortedImages.map((img, idx) => ( diff --git a/apps/website/src/components/ProductGallery.tsx b/apps/website/src/components/ProductGallery.tsx index 11d53d7..43cd9a5 100644 --- a/apps/website/src/components/ProductGallery.tsx +++ b/apps/website/src/components/ProductGallery.tsx @@ -14,6 +14,23 @@ export default function ProductGallery({ productName, categoryName, images }: Pr const scrollRef = useRef(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) { + setActiveIndex(newIndex) + } + } + } + el.addEventListener('scrollend', onScrollEnd) + return () => el.removeEventListener('scrollend', onScrollEnd) + }, [images.length]) + const handleThumbnailClick = (idx: number) => { setActiveIndex(idx) if (scrollRef.current) { @@ -26,19 +43,6 @@ export default function ProductGallery({ productName, categoryName, images }: Pr } } - const handleScroll = (e: React.UIEvent) => { - 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 !== activeIndex && newIndex >= 0 && newIndex < images.length) { - setActiveIndex(newIndex) - } - } - } - if (images.length === 0) return null // If there's only one image, don't show thumbnails @@ -89,7 +93,6 @@ export default function ProductGallery({ productName, categoryName, images }: Pr
{images.map((img, idx) => (