diff --git a/apps/spa/src/components/ProductImageGallery.tsx b/apps/spa/src/components/ProductImageGallery.tsx index c2aa074..403d9f1 100644 --- a/apps/spa/src/components/ProductImageGallery.tsx +++ b/apps/spa/src/components/ProductImageGallery.tsx @@ -1,4 +1,4 @@ -import { useState } from 'react' +import { useState, useRef, useEffect } from 'react' import type { ProductImage } from '../types' interface ProductImageGalleryProps { @@ -10,6 +10,29 @@ interface ProductImageGalleryProps { export default function ProductImageGallery({ images, fallback, alt = 'Product Image', badgeText }: ProductImageGalleryProps) { const [currentIndex, setCurrentIndex] = useState(0) + const scrollRef = useRef(null) + + useEffect(() => { + if (scrollRef.current) { + const container = scrollRef.current + const child = container.children[currentIndex] as HTMLElement + if (child) { + container.scrollTo({ left: child.offsetLeft, behavior: 'smooth' }) + } + } + }, [currentIndex]) + + const handleScroll = (e: React.UIEvent) => { + 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)) : [] @@ -60,18 +83,38 @@ export default function ProductImageGallery({ images, fallback, alt = 'Product I {/* Main Image */}
- {alt} { (e.target as HTMLImageElement).src = fallback }} - /> +
+ {sortedImages.map((img, idx) => ( +
+ {`${alt} { (e.target as HTMLImageElement).src = fallback }} + /> +
+ ))} +
+ {badgeText && (
{badgeText}
)} + + {/* Mobile Dots */} +
+ {sortedImages.map((_, idx) => ( +
+ ))} +
) diff --git a/apps/website/src/components/ProductGallery.tsx b/apps/website/src/components/ProductGallery.tsx index 54af41b..08dd3bf 100644 --- a/apps/website/src/components/ProductGallery.tsx +++ b/apps/website/src/components/ProductGallery.tsx @@ -1,6 +1,6 @@ 'use client' -import { useState } from 'react' +import { useState, useRef, useEffect } from 'react' import Image from 'next/image' interface Props { @@ -11,6 +11,29 @@ interface Props { export default function ProductGallery({ productName, categoryName, images }: Props) { const [activeIndex, setActiveIndex] = useState(0) + const scrollRef = useRef(null) + + useEffect(() => { + if (scrollRef.current) { + const container = scrollRef.current + const child = container.children[activeIndex] as HTMLElement + if (child) { + container.scrollTo({ left: child.offsetLeft, behavior: 'smooth' }) + } + } + }, [activeIndex]) + + const handleScroll = (e: React.UIEvent) => { + 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 @@ -60,19 +83,39 @@ export default function ProductGallery({ productName, categoryName, images }: Pr {/* Main Image */}
- {productName} +
+ {images.map((img, idx) => ( +
+ {`${productName} +
+ ))} +
+ {categoryName && (
{categoryName}
)} + + {/* Mobile Dots */} +
+ {images.map((_, idx) => ( +
+ ))} +
)