wealth/packages/island/src/molecules/media-carousel/components/MediaSlide.tsx

79 lines
2.0 KiB
TypeScript

import NextLink from 'next/link';
import { GridBox } from '../../../atoms/grid-box';
import { Element } from '../../../primitives/element';
import { cn } from '../../../tools/cn';
import type { MediaSlideProps } from '../types';
import { mediaImageVariants, mediaSlideVariants } from '../variants';
/**
* One image, contained in its box. With `onOpen` the box is the button that opens the Lightbox;
* with `href` a hidden link covers it, so a tap opens the post while the image keeps its alt.
*/
export function MediaSlide({
item,
index,
single,
label,
href,
onOpen,
className,
}: MediaSlideProps) {
const box = cn(mediaSlideVariants({ single }), className);
const image = (
<Element
tag="img"
src={item.src}
alt={item.alt}
width={item.width}
height={item.height}
loading={index === 0 ? undefined : 'lazy'}
decoding="async"
draggable={false}
className={mediaImageVariants({ single })}
/>
);
if (onOpen) {
return (
<GridBox
tag="button"
type="button"
justifyItems="justify-items-center"
alignItems="items-center"
onClick={() => onOpen(index)}
aria-label={label ?? (item.alt || undefined)}
className={cn(
box,
'actionable:cursor-zoom-in',
// A slide's ring would be clipped by the scrolling track, so the track draws it.
single ? 'focus-visible:focus-ring' : 'outline-none',
)}
>
{image}
</GridBox>
);
}
return (
<GridBox
justifyItems="justify-items-center"
alignItems="items-center"
className={box}
>
{image}
{href && (
// The post's own link carries the keyboard path; this one is only a bigger tap area.
<Element
tag={NextLink}
href={href}
tabIndex={-1}
aria-hidden
draggable={false}
className="absolute inset-0"
/>
)}
</GridBox>
);
}