Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | import React, { useCallback, useEffect, useRef, useState } from 'react'; import DialogBox from '../DialogBox/DialogBox'; import Button from '../Button/Button'; import Carousel from 'react-multi-carousel'; import 'react-multi-carousel/lib/styles.css'; import { Box, Typography } from '@mui/material'; import './styles.scss'; import BusySpinner from '../BusySpinner'; export type PhotoItem = { url: string; alt?: string; decoration?: React.ReactNode; }; export interface ViewPhotosDialogProps { open: boolean; onClose: () => void; photos: PhotoItem[]; initialSelectedSlide: number; nextButtonLabel: string; prevButtonLabel: string; title: string; numbered?: boolean; dots?: boolean; } export default function ViewPhotosDialog(props: ViewPhotosDialogProps): JSX.Element { const { onClose, open, photos, initialSelectedSlide, nextButtonLabel, prevButtonLabel, title, numbered, dots } = props; const [isPreviousDisabled, setIsPreviousDisabled] = useState(false); const [isNextDisabled, setIsNextDisabled] = useState(false); const [isLoading, setIsLoading] = useState<boolean[]>([]); const [selectedSlide, setSelectedSlide] = useState(initialSelectedSlide); const myCarousel = useRef<Carousel>(null); const responsive = { mobile: { breakpoint: { max: 4000, min: 0 }, items: 1, }, }; const handleChange = useCallback( (args?: any) => { if (myCarousel.current) { if (myCarousel.current.state.currentSlide + 1 >= photos.length) { setIsNextDisabled(true); } else { setIsNextDisabled(false); } if (myCarousel.current.state.currentSlide - 1 < 0) { setIsPreviousDisabled(true); } else { setIsPreviousDisabled(false); } if (args !== undefined) { // if we came from the carousel component's call of handleChange, do the right thing // don't set this slide if we came from useEffect on open setSelectedSlide(myCarousel.current.state.currentSlide); } else { // we need to reinitialize to last state if (selectedSlide === initialSelectedSlide) { // explicitly go to slide, the useEffect won't be triggered myCarousel.current.goToSlide(selectedSlide); } } } }, [photos.length, selectedSlide, initialSelectedSlide, selectedSlide] ); useEffect(() => { setIsLoading(new Array(photos.length).fill(true)); }, [photos.length]); useEffect(() => { setSelectedSlide(initialSelectedSlide); }, [initialSelectedSlide]); useEffect(() => { handleChange(); }, [open, handleChange]); useEffect(() => { if (myCarousel.current) { myCarousel.current.goToSlide(selectedSlide); } }, [selectedSlide]); const finishLoading = (index: number) => { const newIsLoading = [...isLoading]; newIsLoading[index] = false; setIsLoading(newIsLoading); }; return ( <DialogBox onClose={onClose} open={open} title={title} size='large' scrolled={true} middleButtons={[ <Button label={prevButtonLabel} priority='secondary' onClick={() => setSelectedSlide((slide) => Math.max(0, slide - 1))} key='button-1' disabled={isPreviousDisabled} icon='caretLeft' />, <Button label={nextButtonLabel} onClick={() => setSelectedSlide((slide) => Math.min(photos.length - 1, slide + 1))} key='button-2' disabled={isNextDisabled} rightIcon='caretRight' />, ]} > <Box sx={{ '& .react-multi-carousel-list': { paddingBottom: '20px', }, }} > <Carousel responsive={responsive} ref={myCarousel} showDots={dots ?? true} arrows={false} ssr={true} afterChange={handleChange} > {photos.map((p, i) => ( <div key={`photo-${i}-container`} className='view-photos-dialog-container'> {isLoading[i] ? <BusySpinner noBackground={true} /> : undefined} <a href={p.url} target='blank'> <img className='view-photos-dialog-image' src={p.url} alt={p.alt} onLoad={() => finishLoading(i)} /> </a> </div> ))} </Carousel> {numbered ? ( <Typography className='photo-numbering'>{`${selectedSlide + 1}/${photos.length}`}</Typography> ) : undefined} {photos[selectedSlide] && photos[selectedSlide].decoration} </Box> </DialogBox> ); } |