2023-04-17 23:00:47 -04:00
|
|
|
'use client';
|
|
|
|
|
2023-07-24 21:40:29 -05:00
|
|
|
import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/24/outline';
|
2023-04-17 23:00:47 -04:00
|
|
|
import { GridTileImage } from 'components/grid/tile';
|
2024-07-28 22:58:59 -05:00
|
|
|
import { useProduct, useUpdateURL } from 'components/product/product-context';
|
2023-07-24 21:40:29 -05:00
|
|
|
import Image from 'next/image';
|
2023-04-17 23:00:47 -04:00
|
|
|
|
2023-07-24 21:40:29 -05:00
|
|
|
export function Gallery({ images }: { images: { src: string; altText: string }[] }) {
|
2024-07-28 22:58:59 -05:00
|
|
|
const { state, updateImage } = useProduct();
|
|
|
|
const updateURL = useUpdateURL();
|
|
|
|
const imageIndex = state.image ? parseInt(state.image) : 0;
|
2023-04-17 23:00:47 -04:00
|
|
|
|
2023-08-01 20:18:56 -05:00
|
|
|
const nextImageIndex = imageIndex + 1 < images.length ? imageIndex + 1 : 0;
|
|
|
|
const previousImageIndex = imageIndex === 0 ? images.length - 1 : imageIndex - 1;
|
2023-04-17 23:00:47 -04:00
|
|
|
|
|
|
|
const buttonClassName =
|
2023-08-01 20:18:56 -05:00
|
|
|
'h-full px-6 transition-all ease-in-out hover:scale-110 hover:text-black dark:hover:text-white flex items-center justify-center';
|
2023-04-17 23:00:47 -04:00
|
|
|
|
|
|
|
return (
|
2024-07-28 22:58:59 -05:00
|
|
|
<form>
|
2023-08-02 21:07:35 -05:00
|
|
|
<div className="relative aspect-square h-full max-h-[550px] w-full overflow-hidden">
|
2023-08-01 20:18:56 -05:00
|
|
|
{images[imageIndex] && (
|
2023-07-24 21:40:29 -05:00
|
|
|
<Image
|
2023-08-02 21:07:35 -05:00
|
|
|
className="h-full w-full object-contain"
|
|
|
|
fill
|
|
|
|
sizes="(min-width: 1024px) 66vw, 100vw"
|
2023-08-01 20:18:56 -05:00
|
|
|
alt={images[imageIndex]?.altText as string}
|
|
|
|
src={images[imageIndex]?.src as string}
|
2023-04-17 23:00:47 -04:00
|
|
|
priority={true}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{images.length > 1 ? (
|
2023-07-24 21:40:29 -05:00
|
|
|
<div className="absolute bottom-[15%] flex w-full justify-center">
|
|
|
|
<div className="mx-auto flex h-11 items-center rounded-full border border-white bg-neutral-50/80 text-neutral-500 backdrop-blur dark:border-black dark:bg-neutral-900/80">
|
2024-07-28 22:58:59 -05:00
|
|
|
<button
|
|
|
|
formAction={() => {
|
|
|
|
const newState = updateImage(previousImageIndex.toString());
|
|
|
|
updateURL(newState);
|
|
|
|
}}
|
2023-07-24 21:40:29 -05:00
|
|
|
aria-label="Previous product image"
|
|
|
|
className={buttonClassName}
|
|
|
|
>
|
|
|
|
<ArrowLeftIcon className="h-5" />
|
2024-07-28 22:58:59 -05:00
|
|
|
</button>
|
2023-07-24 21:40:29 -05:00
|
|
|
<div className="mx-1 h-6 w-px bg-neutral-500"></div>
|
2024-07-28 22:58:59 -05:00
|
|
|
<button
|
|
|
|
formAction={() => {
|
|
|
|
const newState = updateImage(nextImageIndex.toString());
|
|
|
|
updateURL(newState);
|
|
|
|
}}
|
2023-08-04 00:04:44 +03:00
|
|
|
aria-label="Next product image"
|
|
|
|
className={buttonClassName}
|
|
|
|
>
|
2023-07-24 21:40:29 -05:00
|
|
|
<ArrowRightIcon className="h-5" />
|
2024-07-28 22:58:59 -05:00
|
|
|
</button>
|
2023-07-24 21:40:29 -05:00
|
|
|
</div>
|
2023-04-17 23:00:47 -04:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{images.length > 1 ? (
|
2023-08-02 21:07:35 -05:00
|
|
|
<ul className="my-12 flex items-center justify-center gap-2 overflow-auto py-1 lg:mb-0">
|
2023-04-17 23:00:47 -04:00
|
|
|
{images.map((image, index) => {
|
2023-08-01 20:18:56 -05:00
|
|
|
const isActive = index === imageIndex;
|
|
|
|
|
2023-04-17 23:00:47 -04:00
|
|
|
return (
|
2023-08-08 10:19:23 -05:00
|
|
|
<li key={image.src} className="h-20 w-20">
|
2024-07-28 22:58:59 -05:00
|
|
|
<button
|
|
|
|
formAction={() => {
|
|
|
|
const newState = updateImage(index.toString());
|
|
|
|
updateURL(newState);
|
|
|
|
}}
|
|
|
|
aria-label="Select product image"
|
2023-08-02 21:07:35 -05:00
|
|
|
className="h-full w-full"
|
|
|
|
>
|
|
|
|
<GridTileImage
|
|
|
|
alt={image.altText}
|
|
|
|
src={image.src}
|
|
|
|
width={80}
|
|
|
|
height={80}
|
|
|
|
active={isActive}
|
|
|
|
/>
|
2024-07-28 22:58:59 -05:00
|
|
|
</button>
|
2023-08-02 21:07:35 -05:00
|
|
|
</li>
|
2023-04-17 23:00:47 -04:00
|
|
|
);
|
|
|
|
})}
|
2023-08-02 21:07:35 -05:00
|
|
|
</ul>
|
2023-04-17 23:00:47 -04:00
|
|
|
) : null}
|
2024-07-28 22:58:59 -05:00
|
|
|
</form>
|
2023-04-17 23:00:47 -04:00
|
|
|
);
|
|
|
|
}
|