Merged in fix/SW-2831-image-gallery-history (pull request #2155)

fix(SW-2831): checking isOpen before rendering Lightbox component to avoid spamming the window history

* fix(SW-2831): checking isOpen before rendering Lightbox component to avoid spamming the window history


Approved-by: Linus Flood
This commit is contained in:
Erik Tiekstra
2025-05-20 07:38:13 +00:00
committed by Linus Flood
parent f4ef5a342f
commit f60d07fd9e
5 changed files with 71 additions and 76 deletions

View File

@@ -69,6 +69,7 @@ export default function TopImages({ images, destinationName }: TopImageProps) {
defaultMessage: "See all photos", defaultMessage: "See all photos",
})} })}
</Button> </Button>
{lightboxState.open ? (
<Lightbox <Lightbox
images={lightboxImages} images={lightboxImages}
dialogTitle={intl.formatMessage( dialogTitle={intl.formatMessage(
@@ -77,10 +78,10 @@ export default function TopImages({ images, destinationName }: TopImageProps) {
}, },
{ title: destinationName } { title: destinationName }
)} )}
isOpen={lightboxState.open}
activeIndex={lightboxState.activeIndex} activeIndex={lightboxState.activeIndex}
onClose={() => setLightboxState({ open: false, activeIndex: 0 })} onClose={() => setLightboxState({ open: false, activeIndex: 0 })}
/> />
) : null}
</> </>
)} )}
</div> </div>

View File

@@ -71,6 +71,7 @@ export default function PreviewImages({
defaultMessage: "See all photos", defaultMessage: "See all photos",
})} })}
</Button> </Button>
{lightboxState.isOpen ? (
<Lightbox <Lightbox
images={lightboxImages} images={lightboxImages}
dialogTitle={intl.formatMessage( dialogTitle={intl.formatMessage(
@@ -79,10 +80,12 @@ export default function PreviewImages({
}, },
{ title: hotelName } { title: hotelName }
)} )}
isOpen={lightboxState.isOpen}
activeIndex={lightboxState.activeIndex} activeIndex={lightboxState.activeIndex}
onClose={() => setLightboxState({ activeIndex: 0, isOpen: false })} onClose={() =>
setLightboxState({ activeIndex: 0, isOpen: false })
}
/> />
) : null}
</> </>
)} )}
</div> </div>

View File

@@ -58,13 +58,14 @@ function ImageGallery({
})} })}
/> />
</div> </div>
{isOpen ? (
<Lightbox <Lightbox
images={images} images={images}
dialogTitle={title} dialogTitle={title}
isOpen={isOpen}
onClose={() => setIsOpen(false)} onClose={() => setIsOpen(false)}
hideLabel={hideLabel} hideLabel={hideLabel}
/> />
) : null}
</> </>
) )
} }

View File

@@ -14,19 +14,12 @@ export default function Lightbox({
images, images,
dialogTitle, dialogTitle,
onClose, onClose,
isOpen,
activeIndex = 0, activeIndex = 0,
hideLabel, hideLabel,
}: LightboxProps) { }: LightboxProps) {
const [selectedImageIndex, setSelectedImageIndex] = useState(activeIndex) const [selectedImageIndex, setSelectedImageIndex] = useState(activeIndex)
const [isFullView, setIsFullView] = useState(false) const [isFullView, setIsFullView] = useState(false)
useEffect(() => {
if (isOpen) {
setIsFullView(false)
}
}, [isOpen])
useEffect(() => { useEffect(() => {
setSelectedImageIndex(activeIndex) setSelectedImageIndex(activeIndex)
}, [activeIndex]) }, [activeIndex])
@@ -62,7 +55,7 @@ export default function Lightbox({
return ( return (
<ModalOverlay <ModalOverlay
isOpen={isOpen} isOpen={true}
onOpenChange={handleClose} onOpenChange={handleClose}
className={styles.overlay} className={styles.overlay}
isDismissable isDismissable
@@ -70,7 +63,6 @@ export default function Lightbox({
<Modal> <Modal>
<AnimatePresence> <AnimatePresence>
<Dialog aria-label={dialogTitle}> <Dialog aria-label={dialogTitle}>
{isOpen && (
<motion.div <motion.div
className={`${styles.content} ${ className={`${styles.content} ${
isFullView ? styles.fullViewContent : styles.galleryContent isFullView ? styles.fullViewContent : styles.galleryContent
@@ -105,7 +97,6 @@ export default function Lightbox({
/> />
)} )}
</motion.div> </motion.div>
)}
</Dialog> </Dialog>
</AnimatePresence> </AnimatePresence>
</Modal> </Modal>

View File

@@ -4,7 +4,6 @@ export interface LightboxProps {
images: GalleryImage[] images: GalleryImage[]
dialogTitle: string /* Accessible title for dialog screen readers */ dialogTitle: string /* Accessible title for dialog screen readers */
onClose: () => void onClose: () => void
isOpen: boolean
activeIndex?: number activeIndex?: number
hideLabel?: boolean hideLabel?: boolean
} }