|
| 1 | +import { cn } from '@/shared/lib/core'; |
| 2 | + |
| 3 | +import { ImageGalleryProvider, useImageGallery } from './ImageGalleryContext'; |
| 4 | + |
| 5 | +import { Flex } from '../Flex'; |
| 6 | +import { Icon } from '../Icon'; |
| 7 | + |
| 8 | +export type ImageGalleryItem = { url: string; name?: string }; |
| 9 | +type Props = { |
| 10 | + /** |
| 11 | + * List of image name and url |
| 12 | + */ |
| 13 | + images: ImageGalleryItem[]; |
| 14 | + |
| 15 | + /** |
| 16 | + * Additional class for container |
| 17 | + */ |
| 18 | + className?: string; |
| 19 | +}; |
| 20 | + |
| 21 | +export function ImageGallery({ images, className }: Props) { |
| 22 | + return ( |
| 23 | + <ImageGalleryProvider images={images}> |
| 24 | + <ImageGalleryContent className={className} /> |
| 25 | + </ImageGalleryProvider> |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +function ImageGalleryContent({ className }: { className?: string }) { |
| 30 | + const { images, current, total, firstImage } = useImageGallery(); |
| 31 | + const loading = firstImage ? 'eager' : 'lazy'; |
| 32 | + |
| 33 | + return ( |
| 34 | + <Flex dir="col" alignItems="center" className={cn('w-full max-w-[500px]', className)}> |
| 35 | + <Flex |
| 36 | + alignItems="center" |
| 37 | + justifyContent="center" |
| 38 | + className="relative aspect-square w-full overflow-hidden bg-gray-50" |
| 39 | + > |
| 40 | + <img |
| 41 | + src={images[current].url} |
| 42 | + loading={loading} |
| 43 | + alt={images[current].name} |
| 44 | + width={500} |
| 45 | + height={500} |
| 46 | + className="h-full w-full object-contain" |
| 47 | + /> |
| 48 | + {total > 1 && ( |
| 49 | + <> |
| 50 | + <ImageGalleryArrow direction="prev" /> |
| 51 | + <ImageGalleryArrow direction="next" /> |
| 52 | + </> |
| 53 | + )} |
| 54 | + </Flex> |
| 55 | + <Flex justifyContent="center" className="mt-2"> |
| 56 | + <ImageGalleryDots /> |
| 57 | + </Flex> |
| 58 | + </Flex> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +function ImageGalleryDots() { |
| 63 | + const { images, current, goToIndex } = useImageGallery(); |
| 64 | + return ( |
| 65 | + <Flex alignItems="center" className="gap-2"> |
| 66 | + {images.map((_, index) => { |
| 67 | + const isActive = index === current; |
| 68 | + return ( |
| 69 | + <button |
| 70 | + key={index} |
| 71 | + type="button" |
| 72 | + aria-label={`Image ${index + 1}`} |
| 73 | + onClick={() => goToIndex(index)} |
| 74 | + className={cn('h-2 w-2 rounded-full', isActive ? 'bg-primary-300' : 'bg-gray-200')} |
| 75 | + /> |
| 76 | + ); |
| 77 | + })} |
| 78 | + </Flex> |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +type ImageGalleryArrowProps = { |
| 83 | + direction: 'prev' | 'next'; |
| 84 | +}; |
| 85 | +function ImageGalleryArrow({ direction }: ImageGalleryArrowProps) { |
| 86 | + const { firstImage, lastImage, goPrev, goNext } = useImageGallery(); |
| 87 | + const isPrev = direction === 'prev'; |
| 88 | + const isHidden = isPrev ? firstImage : lastImage; |
| 89 | + const onClick = isPrev ? goPrev : goNext; |
| 90 | + |
| 91 | + return ( |
| 92 | + <button |
| 93 | + type="button" |
| 94 | + onClick={onClick} |
| 95 | + className={cn( |
| 96 | + 'absolute top-1/2 z-10 -translate-y-1/2 rounded-full bg-white/75 p-1 shadow-sm transition-opacity hover:bg-white md:p-1.5', |
| 97 | + isPrev ? 'left-4' : 'right-4', |
| 98 | + isHidden && 'hidden' |
| 99 | + )} |
| 100 | + aria-label={`${direction} image button`} |
| 101 | + > |
| 102 | + <Icon name={isPrev ? 'arrowLeft' : 'arrowRight'} className="h-5 w-5 md:h-6 md:w-6" /> |
| 103 | + </button> |
| 104 | + ); |
| 105 | +} |
0 commit comments