Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 'use client'; /** * ImageGallery Component * * Product image gallery with: * - Main image display * - Thumbnail navigation * - Previous/Next controls * - Keyboard navigation * - Loading states */ import { useState, memo, useCallback, useEffect } from 'react'; import Image from 'next/image'; import { cn } from '@/lib/core'; import { Icon } from '@/components/ui/icons'; interface GalleryImage { src: string; alt: string; } interface ImageGalleryProps { /** Array of images to display */ images: GalleryImage[]; /** Additional CSS classes for container */ className?: string; /** Size of thumbnails */ thumbnailSize?: number; /** Show navigation arrows */ showNavigation?: boolean; /** Enable keyboard navigation */ enableKeyboard?: boolean; /** Aspect ratio for main image */ aspectRatio?: '1:1' | '4:3' | '16:9' | '3:4'; } const aspectRatioClasses = { '1:1': 'aspect-square', '4:3': 'aspect-[4/3]', '16:9': 'aspect-video', '3:4': 'aspect-[3/4]', }; export const ImageGallery = memo(function ImageGallery({ images, className, thumbnailSize = 64, showNavigation = true, enableKeyboard = true, aspectRatio = '1:1', }: ImageGalleryProps) { const [selectedIndex, setSelectedIndex] = useState(0); const [isLoading, setIsLoading] = useState(true); const selectedImage = images[selectedIndex]; const hasMultipleImages = images.length > 1; const handleThumbnailClick = useCallback((index: number) => { setSelectedIndex(index); setIsLoading(true); }, []); const handlePrevious = useCallback(() => { setSelectedIndex((prev) => (prev > 0 ? prev - 1 : images.length - 1)); setIsLoading(true); }, [images.length]); const handleNext = useCallback(() => { setSelectedIndex((prev) => (prev < images.length - 1 ? prev + 1 : 0)); setIsLoading(true); }, [images.length]); // Keyboard navigation useEffect(() => { if (!enableKeyboard || !hasMultipleImages) return; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'ArrowLeft') { handlePrevious(); } else if (e.key === 'ArrowRight') { handleNext(); } }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [enableKeyboard, hasMultipleImages, handlePrevious, handleNext]); if (images.length === 0) { return ( <div className={cn( aspectRatioClasses[aspectRatio], 'bg-gray-100 dark:bg-gray-800 flex items-center justify-center rounded-lg', className )} > <span className="text-gray-400 dark:text-gray-500">No images</span> </div> ); } return ( <div className={cn('space-y-4', className)}> {/* Main image container */} <div className={cn('relative rounded-lg overflow-hidden', aspectRatioClasses[aspectRatio])}> {/* Loading skeleton */} {isLoading && ( <div className="absolute inset-0 bg-gray-200 dark:bg-gray-700 animate-pulse" aria-hidden="true" /> )} <Image src={selectedImage.src} alt={selectedImage.alt} fill sizes="(max-width: 768px) 100vw, 600px" className={cn( 'object-contain transition-opacity duration-300', isLoading ? 'opacity-0' : 'opacity-100' )} onLoad={() => setIsLoading(false)} priority={selectedIndex === 0} /> {/* Navigation arrows */} {showNavigation && hasMultipleImages && ( <> <button onClick={handlePrevious} className={cn( 'absolute left-2 top-1/2 -translate-y-1/2', 'p-2 bg-white/90 dark:bg-gray-800/90 rounded-full', 'shadow-md hover:bg-white dark:hover:bg-gray-800', 'transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500' )} aria-label="Previous image" > <Icon name="chevron-left" className="w-5 h-5 text-gray-700 dark:text-gray-200" /> </button> <button onClick={handleNext} className={cn( 'absolute right-2 top-1/2 -translate-y-1/2', 'p-2 bg-white/90 dark:bg-gray-800/90 rounded-full', 'shadow-md hover:bg-white dark:hover:bg-gray-800', 'transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500' )} aria-label="Next image" > <Icon name="chevron-right" className="w-5 h-5 text-gray-700 dark:text-gray-200" /> </button> </> )} {/* Image counter */} {hasMultipleImages && ( <div className={cn( 'absolute bottom-2 right-2', 'px-2 py-1 bg-black/60 text-white text-xs rounded' )} > {selectedIndex + 1} / {images.length} </div> )} </div> {/* Thumbnails */} {hasMultipleImages && ( <div className="flex gap-2 overflow-x-auto pb-2 scrollbar-thin scrollbar-thumb-gray-300 dark:scrollbar-thumb-gray-600" role="tablist" aria-label="Image thumbnails" > {images.map((image, index) => ( <button key={index} onClick={() => handleThumbnailClick(index)} role="tab" aria-selected={index === selectedIndex} aria-label={`View image ${index + 1}`} className={cn( 'relative flex-shrink-0 rounded overflow-hidden', 'ring-2 transition-all focus:outline-none', index === selectedIndex ? 'ring-blue-500' : 'ring-transparent hover:ring-gray-300 dark:hover:ring-gray-600' )} style={{ width: thumbnailSize, height: thumbnailSize }} > <Image src={image.src} alt={`Thumbnail ${index + 1}`} fill sizes={`${thumbnailSize}px`} className="object-cover" /> </button> ))} </div> )} </div> ); }); export default ImageGallery; |