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
This commit is contained in:
@@ -0,0 +1,44 @@
|
|||||||
|
import type { Meta, StoryObj } from "@storybook/nextjs-vite"
|
||||||
|
|
||||||
|
import { ImageCounter } from "./index"
|
||||||
|
|
||||||
|
const meta: Meta<typeof ImageCounter> = {
|
||||||
|
title: "Core Components/ImageCounter",
|
||||||
|
component: ImageCounter,
|
||||||
|
argTypes: {},
|
||||||
|
}
|
||||||
|
|
||||||
|
export default meta
|
||||||
|
|
||||||
|
type Story = StoryObj<typeof ImageCounter>
|
||||||
|
|
||||||
|
export const Large: Story = {
|
||||||
|
args: {
|
||||||
|
size: "Large",
|
||||||
|
leadingIcon: false,
|
||||||
|
number: "12/36",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Small: Story = {
|
||||||
|
args: {
|
||||||
|
size: "Small",
|
||||||
|
leadingIcon: false,
|
||||||
|
number: "12/36",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const LargeWithLeadingIcon: Story = {
|
||||||
|
args: {
|
||||||
|
size: "Large",
|
||||||
|
leadingIcon: true,
|
||||||
|
number: "10",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
export const SmallWithLeadingIcon: Story = {
|
||||||
|
args: {
|
||||||
|
size: "Small",
|
||||||
|
leadingIcon: true,
|
||||||
|
number: 10,
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
.imageCounter {
|
||||||
|
background-color: var(--Overlay-90);
|
||||||
|
border-radius: var(--Corner-radius-sm);
|
||||||
|
color: var(--Text-Inverted);
|
||||||
|
justify-content: center;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--Space-x025);
|
||||||
|
}
|
||||||
|
|
||||||
|
.small {
|
||||||
|
padding: 0 var(--Space-x05);
|
||||||
|
height: 26px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.large {
|
||||||
|
padding: 0 var(--Space-x1);
|
||||||
|
height: 32px;
|
||||||
|
}
|
||||||
29
packages/design-system/lib/components/ImageCounter/index.tsx
Normal file
29
packages/design-system/lib/components/ImageCounter/index.tsx
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { VariantProps } from "class-variance-authority"
|
||||||
|
import { Typography } from "../Typography"
|
||||||
|
import { variants } from "./variants"
|
||||||
|
import { MaterialIcon } from "../Icons/MaterialIcon"
|
||||||
|
|
||||||
|
interface ImageCounterProps extends VariantProps<typeof variants> {
|
||||||
|
number: number | string
|
||||||
|
leadingIcon?: boolean
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ImageCounter({
|
||||||
|
number,
|
||||||
|
size,
|
||||||
|
leadingIcon = false,
|
||||||
|
className,
|
||||||
|
}: ImageCounterProps) {
|
||||||
|
const classNames = variants({ className, size })
|
||||||
|
return (
|
||||||
|
<Typography variant="Tag/sm">
|
||||||
|
<span className={classNames}>
|
||||||
|
{leadingIcon && (
|
||||||
|
<MaterialIcon icon="filter" color="Icon/Inverted" size={16} />
|
||||||
|
)}
|
||||||
|
{number}
|
||||||
|
</span>
|
||||||
|
</Typography>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { cva } from "class-variance-authority"
|
||||||
|
|
||||||
|
import styles from "./imageCounter.module.css"
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
variants: {
|
||||||
|
size: {
|
||||||
|
Large: styles.large,
|
||||||
|
Small: styles.small,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: {
|
||||||
|
size: "Large",
|
||||||
|
},
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const variants = cva(styles.imageCounter, config)
|
||||||
@@ -9,14 +9,6 @@
|
|||||||
position: absolute;
|
position: absolute;
|
||||||
bottom: var(--Space-x2);
|
bottom: var(--Space-x2);
|
||||||
right: var(--Space-x2);
|
right: var(--Space-x2);
|
||||||
background-color: var(--Overlay-90);
|
|
||||||
padding: var(--Space-x025) var(--Space-x05);
|
|
||||||
border-radius: var(--Corner-radius-sm);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
gap: var(--Space-x025);
|
|
||||||
color: var(--Text-Inverted);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.imageCountBottom {
|
.imageCountBottom {
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import Lightbox from "../Lightbox"
|
|||||||
import { Typography } from "../Typography"
|
import { Typography } from "../Typography"
|
||||||
|
|
||||||
import styles from "./imageGallery.module.css"
|
import styles from "./imageGallery.module.css"
|
||||||
|
import { ImageCounter } from "../ImageCounter"
|
||||||
|
import { cx } from "class-variance-authority"
|
||||||
|
|
||||||
export interface GalleryImage {
|
export interface GalleryImage {
|
||||||
src: string
|
src: string
|
||||||
@@ -65,18 +67,16 @@ function ImageGallery({
|
|||||||
onError={() => setImageError(true)}
|
onError={() => setImageError(true)}
|
||||||
{...imageProps}
|
{...imageProps}
|
||||||
/>
|
/>
|
||||||
<Typography variant="Body/Supporting text (caption)/smRegular">
|
<ImageCounter
|
||||||
<span
|
className={cx(styles.imageCount, {
|
||||||
className={`${styles.imageCount} ${
|
[styles.imageCountTop]: imageCountPosition === "top",
|
||||||
imageCountPosition === "top"
|
[styles.imageCountBottom]: imageCountPosition === "bottom",
|
||||||
? styles.imageCountTop
|
})}
|
||||||
: styles.imageCountBottom
|
number={images.length}
|
||||||
}`}
|
size="Large"
|
||||||
>
|
leadingIcon
|
||||||
<MaterialIcon icon="filter" color="Icon/Inverted" size={16} />
|
/>
|
||||||
<span>{images.length}</span>
|
|
||||||
</span>
|
|
||||||
</Typography>
|
|
||||||
<ButtonRAC
|
<ButtonRAC
|
||||||
className={styles.triggerArea}
|
className={styles.triggerArea}
|
||||||
onPress={() => setIsOpen(true)}
|
onPress={() => setIsOpen(true)}
|
||||||
|
|||||||
@@ -26,13 +26,6 @@
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.imageCount {
|
|
||||||
background-color: var(--Overlay-90);
|
|
||||||
padding: var(--Space-x025) var(--Space-x05);
|
|
||||||
border-radius: var(--Corner-radius-sm);
|
|
||||||
color: var(--Text-Inverted);
|
|
||||||
}
|
|
||||||
|
|
||||||
.content {
|
.content {
|
||||||
position: relative;
|
position: relative;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { AnimatePresence, motion } from "motion/react"
|
import { AnimatePresence, motion } from "motion/react"
|
||||||
import { useEffect, useState } from "react"
|
import { useState } from "react"
|
||||||
import { useIntl } from "react-intl"
|
import { useIntl } from "react-intl"
|
||||||
|
|
||||||
import Image from "../../Image"
|
import Image from "../../Image"
|
||||||
@@ -11,6 +11,9 @@ import { Typography } from "../../Typography"
|
|||||||
|
|
||||||
import { LightboxImage } from "../index"
|
import { LightboxImage } from "../index"
|
||||||
import styles from "./fullView.module.css"
|
import styles from "./fullView.module.css"
|
||||||
|
import { cx } from "class-variance-authority"
|
||||||
|
import { ImageCounter } from "../../ImageCounter"
|
||||||
|
import { animationVariants, useKeyboardNavigation } from "../util"
|
||||||
|
|
||||||
type FullViewProps = {
|
type FullViewProps = {
|
||||||
image: LightboxImage
|
image: LightboxImage
|
||||||
@@ -22,7 +25,7 @@ type FullViewProps = {
|
|||||||
hideLabel?: boolean
|
hideLabel?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function FullView({
|
export function FullView({
|
||||||
image,
|
image,
|
||||||
onClose,
|
onClose,
|
||||||
onNext,
|
onNext,
|
||||||
@@ -32,56 +35,28 @@ export default function FullView({
|
|||||||
hideLabel,
|
hideLabel,
|
||||||
}: FullViewProps) {
|
}: FullViewProps) {
|
||||||
const intl = useIntl()
|
const intl = useIntl()
|
||||||
const [animateLeft, setAnimateLeft] = useState(true)
|
const [slideDirection, setSlideDirection] = useState<"left" | "right">("left")
|
||||||
|
|
||||||
function handleSwipe(offset: number) {
|
function handleSwipe(offset: number) {
|
||||||
if (offset > 30) {
|
if (offset > 30) {
|
||||||
setAnimateLeft(false)
|
setSlideDirection("right")
|
||||||
onPrev()
|
onPrev()
|
||||||
}
|
}
|
||||||
if (offset < -30) {
|
if (offset < -30) {
|
||||||
setAnimateLeft(true)
|
setSlideDirection("left")
|
||||||
onNext()
|
onNext()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleNext() {
|
function handleNext() {
|
||||||
setAnimateLeft(true)
|
setSlideDirection("left")
|
||||||
onNext()
|
onNext()
|
||||||
}
|
}
|
||||||
|
|
||||||
function handlePrev() {
|
function handlePrev() {
|
||||||
setAnimateLeft(false)
|
setSlideDirection("right")
|
||||||
onPrev()
|
onPrev()
|
||||||
}
|
}
|
||||||
|
useKeyboardNavigation(handlePrev, handleNext)
|
||||||
const handleKeyDown = (e: KeyboardEvent) => {
|
|
||||||
if (e.key === "ArrowLeft") {
|
|
||||||
handlePrev()
|
|
||||||
} else if (e.key === "ArrowRight") {
|
|
||||||
handleNext()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
window.addEventListener("keydown", handleKeyDown)
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
window.removeEventListener("keydown", handleKeyDown)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const variants = {
|
|
||||||
initial: (animateLeft: boolean) => ({
|
|
||||||
opacity: 0,
|
|
||||||
x: animateLeft ? 300 : -300,
|
|
||||||
}),
|
|
||||||
animate: { opacity: 1, x: 0 },
|
|
||||||
exit: (animateLeft: boolean) => ({
|
|
||||||
opacity: 0,
|
|
||||||
x: animateLeft ? -300 : 300,
|
|
||||||
}),
|
|
||||||
} as const
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.fullView}>
|
<div className={styles.fullView}>
|
||||||
@@ -96,19 +71,14 @@ export default function FullView({
|
|||||||
iconName="close"
|
iconName="close"
|
||||||
/>
|
/>
|
||||||
<div className={styles.header}>
|
<div className={styles.header}>
|
||||||
<Typography variant="Tag/sm">
|
<ImageCounter number={`${currentIndex + 1} / ${totalImages}`} />
|
||||||
<span className={styles.imageCount}>
|
|
||||||
{/* eslint-disable-next-line formatjs/no-literal-string-in-jsx */}
|
|
||||||
{`${currentIndex + 1} / ${totalImages}`}
|
|
||||||
</span>
|
|
||||||
</Typography>
|
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.content}>
|
<div className={styles.content}>
|
||||||
<AnimatePresence initial={false} custom={animateLeft}>
|
<AnimatePresence initial={false} custom={slideDirection}>
|
||||||
<motion.div
|
<motion.div
|
||||||
key={image.src}
|
key={image.src}
|
||||||
custom={animateLeft}
|
custom={slideDirection}
|
||||||
variants={variants}
|
variants={animationVariants}
|
||||||
initial="initial"
|
initial="initial"
|
||||||
animate="animate"
|
animate="animate"
|
||||||
exit="exit"
|
exit="exit"
|
||||||
@@ -137,14 +107,14 @@ export default function FullView({
|
|||||||
|
|
||||||
<IconButton
|
<IconButton
|
||||||
variant="Muted"
|
variant="Muted"
|
||||||
className={`${styles.navigationButton} ${styles.prev}`}
|
|
||||||
onPress={handlePrev}
|
onPress={handlePrev}
|
||||||
|
className={cx(styles.navigationButton, styles.prev)}
|
||||||
iconName="arrow_back"
|
iconName="arrow_back"
|
||||||
/>
|
/>
|
||||||
<IconButton
|
<IconButton
|
||||||
variant="Muted"
|
variant="Muted"
|
||||||
className={`${styles.navigationButton} ${styles.next}`}
|
|
||||||
onPress={handleNext}
|
onPress={handleNext}
|
||||||
|
className={cx(styles.navigationButton, styles.next)}
|
||||||
iconName="arrow_forward"
|
iconName="arrow_forward"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
"use client"
|
||||||
|
import { motion } from "motion/react"
|
||||||
|
|
||||||
|
import Image from "../../Image"
|
||||||
|
import { LightboxImage } from ".."
|
||||||
|
import styles from "./gallery.module.css"
|
||||||
|
import { useIntl } from "react-intl"
|
||||||
|
import { cx } from "class-variance-authority"
|
||||||
|
import { Button as ButtonRAC } from "react-aria-components"
|
||||||
|
import { memo } from "react"
|
||||||
|
|
||||||
|
interface ThumbnailImage {
|
||||||
|
image: LightboxImage
|
||||||
|
index: number
|
||||||
|
onSelect: (image: LightboxImage) => void
|
||||||
|
isMainImage?: boolean
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
export const ThumbnailImage = memo(function ThumbnailImage({
|
||||||
|
image,
|
||||||
|
index,
|
||||||
|
onSelect,
|
||||||
|
isMainImage = false,
|
||||||
|
className,
|
||||||
|
}: ThumbnailImage) {
|
||||||
|
const intl = useIntl()
|
||||||
|
return (
|
||||||
|
<motion.div
|
||||||
|
key={image.src}
|
||||||
|
className={styles.thumbnailContainer}
|
||||||
|
initial={{ opacity: 0, x: 50 }}
|
||||||
|
animate={{ opacity: 1, x: 0 }}
|
||||||
|
exit={{ opacity: 0, x: -50 }}
|
||||||
|
transition={{ duration: 0.2, delay: index * 0.05 }}
|
||||||
|
>
|
||||||
|
<ButtonRAC
|
||||||
|
className={styles.imageButton}
|
||||||
|
onPress={() => onSelect(image)}
|
||||||
|
aria-label={intl.formatMessage({
|
||||||
|
id: "lightbox.openImage",
|
||||||
|
defaultMessage: "Open image",
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
<Image
|
||||||
|
src={image.src}
|
||||||
|
alt={image.alt}
|
||||||
|
fill
|
||||||
|
sizes="200px"
|
||||||
|
className={cx(styles.image, className, {
|
||||||
|
[styles.notActiveImage]: !isMainImage,
|
||||||
|
[styles.activeImage]: isMainImage,
|
||||||
|
})}
|
||||||
|
/>
|
||||||
|
</ButtonRAC>
|
||||||
|
</motion.div>
|
||||||
|
)
|
||||||
|
})
|
||||||
@@ -19,6 +19,13 @@
|
|||||||
height: 242px;
|
height: 242px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.thumbnail {
|
||||||
|
grid-column: 2 / span 5;
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(5, 1fr);
|
||||||
|
gap: var(--Space-x15);
|
||||||
|
}
|
||||||
|
|
||||||
.fullWidthImage {
|
.fullWidthImage {
|
||||||
grid-column: 1 / -1;
|
grid-column: 1 / -1;
|
||||||
height: 240px;
|
height: 240px;
|
||||||
@@ -34,9 +41,11 @@
|
|||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
z-index: 0;
|
z-index: 0;
|
||||||
|
border-radius: var(--Corner-radius-sm);
|
||||||
|
|
||||||
&:focus-visible {
|
&:focus-visible {
|
||||||
outline-offset: -2px; /* Adjust the outline offset as wrappers uses overflow-hidden */
|
outline: 2px solid var(--Border-Interactive-Focus);
|
||||||
|
outline-offset: 2px; /* Adjust the outline offset as wrappers uses overflow-hidden */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +53,21 @@
|
|||||||
transition: opacity 0.3s ease-in-out;
|
transition: opacity 0.3s ease-in-out;
|
||||||
object-fit: cover;
|
object-fit: cover;
|
||||||
z-index: -1;
|
z-index: -1;
|
||||||
border-radius: var(--Corner-radius-md);
|
}
|
||||||
|
|
||||||
|
.notActiveImage {
|
||||||
|
opacity: 0.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.activeImage {
|
||||||
|
border: 2px solid var(--Border-Interactive-Active);
|
||||||
|
}
|
||||||
|
|
||||||
|
.imageCounter {
|
||||||
|
position: absolute;
|
||||||
|
bottom: var(--Space-x1);
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
}
|
}
|
||||||
|
|
||||||
@media screen and (max-width: 767px) {
|
@media screen and (max-width: 767px) {
|
||||||
@@ -81,7 +104,7 @@
|
|||||||
|
|
||||||
.desktopGallery {
|
.desktopGallery {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-rows: 28px 1fr 125px;
|
grid-template-rows: 28px 1fr 130px;
|
||||||
row-gap: var(--Space-x15);
|
row-gap: var(--Space-x15);
|
||||||
background-color: var(--Background-Primary);
|
background-color: var(--Background-Primary);
|
||||||
height: 100%;
|
height: 100%;
|
||||||
@@ -117,7 +140,7 @@
|
|||||||
|
|
||||||
.mainImage {
|
.mainImage {
|
||||||
object-fit: contain;
|
object-fit: contain;
|
||||||
border-radius: var(--Corner-radius-Medium);
|
border-radius: var(--Corner-radius-sm);
|
||||||
/* Override Next.js Image styles, we can't set width/height on the image as we don't know the aspect ratio of the image */
|
/* Override Next.js Image styles, we can't set width/height on the image as we don't know the aspect ratio of the image */
|
||||||
width: auto !important;
|
width: auto !important;
|
||||||
height: auto !important;
|
height: auto !important;
|
||||||
@@ -129,10 +152,9 @@
|
|||||||
|
|
||||||
.desktopThumbnailGrid {
|
.desktopThumbnailGrid {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: repeat(5, 1fr);
|
grid-template-columns: 0.2fr repeat(5, 1fr) 0.2fr;
|
||||||
gap: var(--Space-x1);
|
gap: var(--Space-x15);
|
||||||
max-height: 7.8125rem;
|
max-height: 7.8125rem;
|
||||||
overflow: hidden;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.thumbnailContainer {
|
.thumbnailContainer {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client"
|
"use client"
|
||||||
import { AnimatePresence, motion } from "motion/react"
|
import { AnimatePresence, motion } from "motion/react"
|
||||||
import { useEffect, useState } from "react"
|
import { useMemo, useState } from "react"
|
||||||
import { Button as ButtonRAC } from "react-aria-components"
|
import { Button as ButtonRAC } from "react-aria-components"
|
||||||
import { useIntl } from "react-intl"
|
import { useIntl } from "react-intl"
|
||||||
|
|
||||||
@@ -13,8 +13,12 @@ import { cx } from "class-variance-authority"
|
|||||||
import { useMediaQuery } from "usehooks-ts"
|
import { useMediaQuery } from "usehooks-ts"
|
||||||
import { LightboxImage } from ".."
|
import { LightboxImage } from ".."
|
||||||
import styles from "./gallery.module.css"
|
import styles from "./gallery.module.css"
|
||||||
|
import { ImageCounter } from "../../ImageCounter"
|
||||||
|
import { animationVariants, useKeyboardNavigation } from "../util"
|
||||||
|
import { ThumbnailImage } from "./ThumnailImage"
|
||||||
|
import { useThumbnail } from "./useThumbNail"
|
||||||
|
|
||||||
type GalleryProps = {
|
interface GalleryProps {
|
||||||
images: LightboxImage[]
|
images: LightboxImage[]
|
||||||
onClose: () => void
|
onClose: () => void
|
||||||
onSelectImage: (image: LightboxImage) => void
|
onSelectImage: (image: LightboxImage) => void
|
||||||
@@ -23,7 +27,7 @@ type GalleryProps = {
|
|||||||
hideLabel?: boolean
|
hideLabel?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Gallery({
|
export function Gallery({
|
||||||
images,
|
images,
|
||||||
onClose,
|
onClose,
|
||||||
onSelectImage,
|
onSelectImage,
|
||||||
@@ -32,59 +36,34 @@ export default function Gallery({
|
|||||||
hideLabel,
|
hideLabel,
|
||||||
}: GalleryProps) {
|
}: GalleryProps) {
|
||||||
const intl = useIntl()
|
const intl = useIntl()
|
||||||
const [animateLeft, setAnimateLeft] = useState(true)
|
const [slideDirection, setSlideDirection] = useState<"left" | "right">("left")
|
||||||
const mainImage = selectedImage || images[0]
|
|
||||||
const mainImageIndex = images.findIndex((img) => img === mainImage)
|
|
||||||
const isMobile = useMediaQuery("(max-width: 767px)")
|
const isMobile = useMediaQuery("(max-width: 767px)")
|
||||||
|
|
||||||
function getThumbImages() {
|
const mainImage = selectedImage || images[0]
|
||||||
const thumbs = []
|
const mainImageIndex = useMemo(
|
||||||
for (let i = 1; i <= Math.min(5, images.length); i++) {
|
() => images.findIndex((img) => img === mainImage),
|
||||||
const index = (mainImageIndex + i) % images.length
|
[images, mainImage]
|
||||||
thumbs.push(images[index])
|
)
|
||||||
}
|
|
||||||
return thumbs
|
const thumbnail = useThumbnail({
|
||||||
}
|
images: images,
|
||||||
|
mainImageIdx: mainImageIndex,
|
||||||
|
})
|
||||||
|
|
||||||
function handleNext() {
|
function handleNext() {
|
||||||
setAnimateLeft(true)
|
setSlideDirection("left")
|
||||||
const nextIndex = (mainImageIndex + 1) % images.length
|
const nextIndex = (mainImageIndex + 1) % images.length
|
||||||
onSelectImage(images[nextIndex])
|
onSelectImage(images[nextIndex])
|
||||||
}
|
}
|
||||||
|
|
||||||
function handlePrev() {
|
function handlePrev() {
|
||||||
setAnimateLeft(false)
|
setSlideDirection("right")
|
||||||
const prevIndex = (mainImageIndex - 1 + images.length) % images.length
|
const prevIndex = (mainImageIndex - 1 + images.length) % images.length
|
||||||
onSelectImage(images[prevIndex])
|
onSelectImage(images[prevIndex])
|
||||||
}
|
}
|
||||||
|
useKeyboardNavigation(handlePrev, handleNext)
|
||||||
|
|
||||||
const handleKeyDown = (e: KeyboardEvent) => {
|
const hasPrevImage = thumbnail.previous !== undefined
|
||||||
if (e.key === "ArrowLeft") {
|
const hasNextImage = thumbnail.next !== undefined
|
||||||
handlePrev()
|
|
||||||
} else if (e.key === "ArrowRight") {
|
|
||||||
handleNext()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
window.addEventListener("keydown", handleKeyDown)
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
window.removeEventListener("keydown", handleKeyDown)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const variants = {
|
|
||||||
initial: (animateLeft: boolean) => ({
|
|
||||||
opacity: 0,
|
|
||||||
x: animateLeft ? 300 : -300,
|
|
||||||
}),
|
|
||||||
animate: { opacity: 1, x: 0 },
|
|
||||||
exit: (animateLeft: boolean) => ({
|
|
||||||
opacity: 0,
|
|
||||||
x: animateLeft ? -300 : 300,
|
|
||||||
}),
|
|
||||||
} as const
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.gallery}>
|
<div className={styles.gallery}>
|
||||||
@@ -110,12 +89,12 @@ export default function Gallery({
|
|||||||
</p>
|
</p>
|
||||||
</Typography>
|
</Typography>
|
||||||
<div className={styles.content}>
|
<div className={styles.content}>
|
||||||
<AnimatePresence initial={false} custom={animateLeft}>
|
<AnimatePresence initial={false} custom={slideDirection}>
|
||||||
<motion.div
|
<motion.div
|
||||||
key={mainImage.src}
|
key={mainImage.src}
|
||||||
className={styles.motionContainer}
|
className={styles.motionContainer}
|
||||||
custom={animateLeft}
|
custom={slideDirection}
|
||||||
variants={variants}
|
variants={animationVariants}
|
||||||
initial="initial"
|
initial="initial"
|
||||||
animate="animate"
|
animate="animate"
|
||||||
exit="exit"
|
exit="exit"
|
||||||
@@ -134,6 +113,11 @@ export default function Gallery({
|
|||||||
defaultMessage: "Open image",
|
defaultMessage: "Open image",
|
||||||
})}
|
})}
|
||||||
role="button"
|
role="button"
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === 'Enter' || e.key === ' ') {
|
||||||
|
onImageClick()
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</motion.div>
|
</motion.div>
|
||||||
</AnimatePresence>
|
</AnimatePresence>
|
||||||
@@ -157,36 +141,37 @@ export default function Gallery({
|
|||||||
})}
|
})}
|
||||||
iconName="arrow_forward"
|
iconName="arrow_forward"
|
||||||
/>
|
/>
|
||||||
|
<ImageCounter
|
||||||
|
number={`${mainImageIndex + 1} / ${images.length}`}
|
||||||
|
className={styles.imageCounter}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.desktopThumbnailGrid}>
|
<div className={styles.desktopThumbnailGrid}>
|
||||||
<AnimatePresence initial={false}>
|
<AnimatePresence initial={false}>
|
||||||
{getThumbImages().map((image, index) => (
|
{hasPrevImage && (
|
||||||
<motion.div
|
<ThumbnailImage
|
||||||
key={image.src}
|
image={thumbnail.previous!}
|
||||||
className={styles.thumbnailContainer}
|
index={0}
|
||||||
initial={{ opacity: 0, x: 50 }}
|
onSelect={onSelectImage}
|
||||||
animate={{ opacity: 1, x: 0 }}
|
/>
|
||||||
exit={{ opacity: 0, x: -50 }}
|
)}
|
||||||
transition={{ duration: 0.2, delay: index * 0.05 }}
|
<span className={styles.thumbnail}>
|
||||||
>
|
{thumbnail.images.map((image, index) => (
|
||||||
<ButtonRAC
|
<ThumbnailImage
|
||||||
className={styles.imageButton}
|
image={image}
|
||||||
onPress={() => onSelectImage(image)}
|
index={hasPrevImage ? index + 1 : index}
|
||||||
aria-label={intl.formatMessage({
|
onSelect={onSelectImage}
|
||||||
id: "lightbox.openImage",
|
isMainImage={mainImage === image}
|
||||||
defaultMessage: "Open image",
|
/>
|
||||||
})}
|
))}
|
||||||
>
|
</span>
|
||||||
<Image
|
{hasNextImage && (
|
||||||
src={image.src}
|
<ThumbnailImage
|
||||||
alt={image.alt}
|
image={thumbnail.next!}
|
||||||
fill
|
index={6}
|
||||||
sizes="200px"
|
onSelect={onSelectImage}
|
||||||
className={styles.image}
|
/>
|
||||||
/>
|
)}
|
||||||
</ButtonRAC>
|
|
||||||
</motion.div>
|
|
||||||
))}
|
|
||||||
</AnimatePresence>
|
</AnimatePresence>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -196,7 +181,9 @@ export default function Gallery({
|
|||||||
{images.map((image, index) => (
|
{images.map((image, index) => (
|
||||||
<motion.div
|
<motion.div
|
||||||
key={image.src}
|
key={image.src}
|
||||||
className={`${styles.thumbnailContainer} ${index % 3 === 0 ? styles.fullWidthImage : ""}`}
|
className={cx(styles.thumbnailContainer, {
|
||||||
|
[styles.fullWidthImage]: index % 3 === 0,
|
||||||
|
})}
|
||||||
initial={{ opacity: 0, y: 20 }}
|
initial={{ opacity: 0, y: 20 }}
|
||||||
animate={{ opacity: 1, y: 0 }}
|
animate={{ opacity: 1, y: 0 }}
|
||||||
transition={{ duration: 0.3, delay: index * 0.05 }}
|
transition={{ duration: 0.3, delay: index * 0.05 }}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import { useMemo } from "react"
|
||||||
|
import { LightboxImage } from ".."
|
||||||
|
|
||||||
|
export const THUMBNAIL_WINDOW = 5
|
||||||
|
export const THUMBNAIL_OFFSET = Math.ceil(THUMBNAIL_WINDOW / 2)
|
||||||
|
|
||||||
|
interface useThumbnailProps {
|
||||||
|
images: LightboxImage[]
|
||||||
|
mainImageIdx: number
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Keep the main image in the center of the thumbnail, unless when it's one of the first or the last pictures.
|
||||||
|
*/
|
||||||
|
export function useThumbnail({ images, mainImageIdx }: useThumbnailProps) {
|
||||||
|
return useMemo(() => {
|
||||||
|
if (mainImageIdx < THUMBNAIL_OFFSET) {
|
||||||
|
return {
|
||||||
|
images: images.slice(0, THUMBNAIL_WINDOW),
|
||||||
|
next: images[THUMBNAIL_WINDOW + 1],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (mainImageIdx >= images.length - THUMBNAIL_OFFSET) {
|
||||||
|
return {
|
||||||
|
previous: images[images.length - THUMBNAIL_WINDOW - 1],
|
||||||
|
images: images.slice(images.length - THUMBNAIL_WINDOW, images.length),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
previous: images[mainImageIdx - THUMBNAIL_OFFSET],
|
||||||
|
images: images.slice(
|
||||||
|
mainImageIdx - THUMBNAIL_OFFSET + 1,
|
||||||
|
mainImageIdx + THUMBNAIL_OFFSET
|
||||||
|
),
|
||||||
|
next: images[mainImageIdx + THUMBNAIL_OFFSET],
|
||||||
|
}
|
||||||
|
}, [images, mainImageIdx])
|
||||||
|
}
|
||||||
@@ -5,10 +5,11 @@ import { Dialog, Modal, ModalOverlay } from "react-aria-components"
|
|||||||
|
|
||||||
import usePopStateHandler from "@scandic-hotels/common/hooks/usePopStateHandler"
|
import usePopStateHandler from "@scandic-hotels/common/hooks/usePopStateHandler"
|
||||||
|
|
||||||
import FullView from "./FullView"
|
import { FullView } from "./FullView"
|
||||||
import Gallery from "./Gallery"
|
import { Gallery } from "./Gallery"
|
||||||
|
|
||||||
import styles from "./lightbox.module.css"
|
import styles from "./lightbox.module.css"
|
||||||
|
import { cx } from "class-variance-authority"
|
||||||
|
|
||||||
export type LightboxImage = {
|
export type LightboxImage = {
|
||||||
src: string
|
src: string
|
||||||
|
|||||||
30
packages/design-system/lib/components/Lightbox/util.tsx
Normal file
30
packages/design-system/lib/components/Lightbox/util.tsx
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { useCallback, useEffect } from 'react'
|
||||||
|
|
||||||
|
const ANIMATION_OFFSET = 300
|
||||||
|
|
||||||
|
export const animationVariants = {
|
||||||
|
initial: (animateLeft: boolean) => ({
|
||||||
|
opacity: 0,
|
||||||
|
x: animateLeft ? ANIMATION_OFFSET : -ANIMATION_OFFSET,
|
||||||
|
}),
|
||||||
|
animate: { opacity: 1, x: 0 },
|
||||||
|
exit: (animateLeft: boolean) => ({
|
||||||
|
opacity: 0,
|
||||||
|
x: animateLeft ? -ANIMATION_OFFSET : ANIMATION_OFFSET,
|
||||||
|
}),
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export function useKeyboardNavigation(onPrev: () => void, onNext: () => void) {
|
||||||
|
const handleKeyDown = useCallback(
|
||||||
|
(e: KeyboardEvent) => {
|
||||||
|
if (e.key === 'ArrowLeft') onPrev()
|
||||||
|
if (e.key === 'ArrowRight') onNext()
|
||||||
|
},
|
||||||
|
[onPrev, onNext]
|
||||||
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.addEventListener('keydown', handleKeyDown)
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown)
|
||||||
|
}, [handleKeyDown])
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user