import React, { type ReactElement, type MouseEventHandler, Fragment, useState, } from 'react'; import { ModalBody, ModalOverlay } from '../Modal'; import type { ModalSlideshowSlide } from './types'; import styles from './ModalSlideshow.module.css'; import { IconButton } from '../IconButton'; import { useTranslate } from '../../hooks/useTranslate'; import { useId } from '../../hooks/useId'; import { Button } from '../Button'; import { Dots } from '../Dots'; export type ModalSlideshowProps = { /** * The slides to display in the ModalSlideshow. */ slides: ModalSlideshowSlide[]; /** * Whether the ModalSlideshow should have opening and closing animation. */ noAnimation?: boolean; /** * Whether the ModalSlideshow should be open. */ isOpen: boolean; /** * Handler that is called to cancel the slideshow. */ onCancel: MouseEventHandler; /** * Handler that is called when the ModalSlideshow is closed. */ onClose: ({ event, index, }: { event: React.MouseEvent; index: number; }) => void; /** * Handler that is called after the last slide has been displayed. */ onDone: MouseEventHandler; /** * The element where to mount the Modal. It needs to be outside the GrapesProvider tree for the focus trap to work properly. * @default document.body */ portalContainer?: Element; /** * The translations for the buttons. */ translations: { cancel: string; previous: string; next: string; done: string; }; }; export const ModalSlideshow = ({ slides, noAnimation, isOpen, onCancel, onClose, onDone, portalContainer, translations, }: ModalSlideshowProps): ReactElement | null => { const t = useTranslate(); const preloadImages = () => { const render = (slide: ModalSlideshowSlide | undefined) => slide && {slide.illustration}; return isOpen ? slides.map(render) : render(slides[0]); }; const prefixId = useId(); const [currentIndex, setCurrentIndex] = useState(0); const isFirstItem = currentIndex === 0; const isLastItem = slides.length - 1 === currentIndex; return (
{ onClose({ event, index: currentIndex }); setCurrentIndex(0); }} aria-label={t('close')} />
{slides.map((slide, index) => (

{slide.title}

{slide.content}
{currentIndex === index && (
{slide.illustration}
)}
))}
); };