From dca02b90f02f4852888c0bfd9d91f80fdad8c97e Mon Sep 17 00:00:00 2001 From: Niclas Edenvin Date: Tue, 7 Jan 2025 07:56:17 +0000 Subject: [PATCH] Merged in fix/sw-1127-image-fixes (pull request #1123) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix/sw-1127 image fixes for lightbox * fix(SW-1127): move back to top button behind lightbox * fix(SW-1127): don't loop lightbox thumbnails * fix(SW-1127): nicer animation in the gallery This both fixes a bug in the gallery where the animation in the carousel didn't work so good and also animates the images different directions depending on if the user go left or right. Approved-by: Matilda Landström --- app/globals.css | 1 + components/Lightbox/FullView.tsx | 41 +++++++++++++++---- components/Lightbox/Gallery.tsx | 28 ++++++++++--- components/Lightbox/Lightbox.module.css | 1 + .../backToTopButton.module.css | 2 +- 5 files changed, 60 insertions(+), 13 deletions(-) diff --git a/app/globals.css b/app/globals.css index d7b5369aa..0978d5dd9 100644 --- a/app/globals.css +++ b/app/globals.css @@ -123,6 +123,7 @@ --booking-widget-z-index: 10; --booking-widget-open-z-index: 100; --dialog-z-index: 9; + --back-to-top-button: 80; --sidepeek-z-index: 100; --lightbox-z-index: 150; --default-modal-overlay-z-index: 100; diff --git a/components/Lightbox/FullView.tsx b/components/Lightbox/FullView.tsx index 73308f2c2..fa880ad4e 100644 --- a/components/Lightbox/FullView.tsx +++ b/components/Lightbox/FullView.tsx @@ -1,5 +1,6 @@ "use client" import { AnimatePresence, motion } from "framer-motion" +import { useState } from "react" import ArrowRightIcon from "@/components/Icons/ArrowRight" import CloseIcon from "@/components/Icons/Close" @@ -20,10 +21,35 @@ export default function FullView({ currentIndex, totalImages, }: FullViewProps) { + const [animateLeft, setAnimateLeft] = useState(true) + function handleSwipe(offset: number) { if (offset > 30) onPrev() if (offset < -30) onNext() } + + function handleNext() { + setAnimateLeft(true) + onNext() + } + + function handlePrev() { + setAnimateLeft(false) + onPrev() + } + + 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, + }), + } + return (
- + diff --git a/components/Lightbox/Gallery.tsx b/components/Lightbox/Gallery.tsx index eca08bd22..686191edf 100644 --- a/components/Lightbox/Gallery.tsx +++ b/components/Lightbox/Gallery.tsx @@ -1,5 +1,6 @@ "use client" import { AnimatePresence, motion } from "framer-motion" +import { useState } from "react" import { useIntl } from "react-intl" import { ChevronLeftIcon } from "@/components/Icons" @@ -21,12 +22,13 @@ export default function Gallery({ selectedImage, }: GalleryProps) { const intl = useIntl() + const [animateLeft, setAnimateLeft] = useState(true) const mainImage = selectedImage || images[0] const mainImageIndex = images.findIndex((img) => img === mainImage) function getThumbImages() { const thumbs = [] - for (let i = 1; i <= 5; i++) { + for (let i = 1; i <= Math.min(5, images.length); i++) { const index = (mainImageIndex + i) % images.length thumbs.push(images[index]) } @@ -34,15 +36,29 @@ export default function Gallery({ } function handleNext() { + setAnimateLeft(true) const nextIndex = (mainImageIndex + 1) % images.length onSelectImage(images[nextIndex]) } function handlePrev() { + setAnimateLeft(false) const prevIndex = (mainImageIndex - 1 + images.length) % images.length onSelectImage(images[prevIndex]) } + 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, + }), + } + return (