mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 14:06:59 +00:00
68 lines
2.3 KiB
TypeScript
68 lines
2.3 KiB
TypeScript
'use client';
|
|
|
|
import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/24/outline';
|
|
import { createUrl } from 'lib/utils';
|
|
import Image from 'next/image';
|
|
import Link from 'next/link';
|
|
import { usePathname, useSearchParams } from 'next/navigation';
|
|
|
|
export function Gallery({ images }: { images: { src: string; alt: string }[] }) {
|
|
const pathname = usePathname();
|
|
const searchParams = useSearchParams();
|
|
const imageSearchParam = searchParams.get('image');
|
|
const imageIndex = imageSearchParam ? parseInt(imageSearchParam) : 0;
|
|
|
|
const nextSearchParams = new URLSearchParams(searchParams.toString());
|
|
const nextImageIndex = imageIndex + 1 < images.length ? imageIndex + 1 : 0;
|
|
nextSearchParams.set('image', nextImageIndex.toString());
|
|
const nextUrl = createUrl(pathname, nextSearchParams);
|
|
|
|
const previousSearchParams = new URLSearchParams(searchParams.toString());
|
|
const previousImageIndex = imageIndex === 0 ? images.length - 1 : imageIndex - 1;
|
|
previousSearchParams.set('image', previousImageIndex.toString());
|
|
const previousUrl = createUrl(pathname, previousSearchParams);
|
|
|
|
const buttonClassName =
|
|
'h-12 w-12 transition-all ease-in-out hover:scale-110 flex items-center justify-center';
|
|
|
|
return (
|
|
<>
|
|
<div className="relative aspect-square h-full w-full">
|
|
{images[imageIndex] && (
|
|
<Image
|
|
className="h-full w-full object-cover"
|
|
fill
|
|
sizes="(min-width: 1024px) 66vw, 100vw"
|
|
alt={images[imageIndex]?.alt as string}
|
|
src={images[imageIndex]?.src as string}
|
|
priority={true}
|
|
/>
|
|
)}
|
|
|
|
{images.length > 1 ? (
|
|
<div className="absolute top-1/2 flex w-full px-1">
|
|
<div className="flex w-full justify-between">
|
|
<Link
|
|
aria-label="Previous product image"
|
|
href={previousUrl}
|
|
className={buttonClassName}
|
|
scroll={false}
|
|
>
|
|
<ArrowLeftIcon className="h-6" />
|
|
</Link>
|
|
<Link
|
|
aria-label="Next product image"
|
|
href={nextUrl}
|
|
className={buttonClassName}
|
|
scroll={false}
|
|
>
|
|
<ArrowRightIcon className="h-6" />
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
</>
|
|
);
|
|
}
|