Files
web/packages/design-system/lib/components/Lightbox/index.tsx
Matilda Landström fc857ad58f Merged in SW-2064-lightbox-updates (pull request #3347)
feat(SW-2064): Lightbox updates

* chore(SW-2064): add opacity to not selected images

* chore(SW-2064): set main image as the first image in thumbnail

* chore(SW-2064): disable navigation buttons on first and last image

* fix(SW-2064): use cx

* Revert "chore(SW-2064): disable navigation buttons on first and last image"

This reverts commit 9c5acd8a02b83740f35d1cfa88bfba6b006ba947.

* refactor(SW-2064): create ImageCounter component

* refactor(SW-2064)

* chore(SW-2064): add enter keu down on main image


Approved-by: Erik Tiekstra
2026-01-08 12:10:03 +00:00

121 lines
3.2 KiB
TypeScript

"use client"
import { AnimatePresence, motion } from "motion/react"
import { useEffect, useState } from "react"
import { Dialog, Modal, ModalOverlay } from "react-aria-components"
import usePopStateHandler from "@scandic-hotels/common/hooks/usePopStateHandler"
import { FullView } from "./FullView"
import { Gallery } from "./Gallery"
import styles from "./lightbox.module.css"
import { cx } from "class-variance-authority"
export type LightboxImage = {
src: string
alt: string
caption?: string | null
}
type LightboxProps = {
images: LightboxImage[]
dialogTitle: string /* Accessible title for dialog screen readers */
onClose: () => void
isOpen: boolean
activeIndex?: number
hideLabel?: boolean
}
export default function Lightbox({
images,
dialogTitle,
onClose,
isOpen,
activeIndex = 0,
hideLabel,
}: LightboxProps) {
const [selectedImageIndex, setSelectedImageIndex] = useState(activeIndex)
const [isFullView, setIsFullView] = useState(false)
function handleClose(moveBack = false) {
setSelectedImageIndex(0)
if (moveBack) {
window.history.back()
} else {
onClose()
}
}
usePopStateHandler(() => handleClose(), isOpen)
useEffect(() => {
if (isOpen) {
window.history.pushState(null, "", window.location.href)
}
}, [isOpen])
useEffect(() => {
setSelectedImageIndex(activeIndex)
}, [activeIndex])
function handleNext() {
setSelectedImageIndex((prevIndex) => (prevIndex + 1) % images.length)
}
function handlePrev() {
setSelectedImageIndex(
(prevIndex) => (prevIndex - 1 + images.length) % images.length
)
}
return (
<ModalOverlay
isOpen={isOpen}
onOpenChange={() => handleClose(true)}
className={styles.overlay}
isDismissable
>
<Modal>
<AnimatePresence>
<Dialog aria-label={dialogTitle}>
<motion.div
className={`${styles.content} ${
isFullView ? styles.fullViewContent : styles.galleryContent
}`}
initial={{ opacity: 0, scale: 0.95, x: "-50%", y: "-50%" }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95 }}
transition={{ duration: 0.2 }}
>
{isFullView ? (
<FullView
image={images[selectedImageIndex]}
onClose={() => setIsFullView(false)}
onNext={handleNext}
onPrev={handlePrev}
currentIndex={selectedImageIndex}
totalImages={images.length}
hideLabel={hideLabel}
/>
) : (
<Gallery
images={images}
onClose={handleClose}
onSelectImage={(image) => {
setSelectedImageIndex(
images.findIndex((img) => img === image)
)
}}
onImageClick={() => setIsFullView(true)}
selectedImage={images[selectedImageIndex]}
hideLabel={hideLabel}
/>
)}
</motion.div>
</Dialog>
</AnimatePresence>
</Modal>
</ModalOverlay>
)
}