From 0a9f7423e2ead2083b92501c6ca69fcffa8c541b Mon Sep 17 00:00:00 2001 From: Spence Pope Date: Thu, 11 Jun 2026 16:58:11 -0400 Subject: [PATCH] Keep lightbox image index on device rotation (#10867) --- src/components/Lightbox/pager/ImagePager.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/components/Lightbox/pager/ImagePager.tsx b/src/components/Lightbox/pager/ImagePager.tsx index 43942b0f37..c960490e91 100644 --- a/src/components/Lightbox/pager/ImagePager.tsx +++ b/src/components/Lightbox/pager/ImagePager.tsx @@ -91,6 +91,9 @@ export default function ImageViewRoot({ 'use no memo' const ref = useAnimatedRef() const [activeLightbox, setActiveLightbox] = useState(nextLightbox) + // Lives here rather than in ImageView so it survives the remount + // when the orientation-based key below changes on rotation. + const [imageIndex, setImageIndex] = useState(nextLightbox?.index ?? 0) const [orientation, setOrientation] = useState<'portrait' | 'landscape'>( 'portrait', ) @@ -101,6 +104,7 @@ export default function ImageViewRoot({ if (!activeLightbox && nextLightbox) { setActiveLightbox(nextLightbox) + setImageIndex(nextLightbox.index) } useEffect(() => { @@ -193,6 +197,8 @@ export default function ImageViewRoot({ > orientation: 'portrait' | 'landscape' onRequestClose: () => void onPressSave: (uri: string) => void @@ -229,12 +239,14 @@ function ImageView({ openProgress: SharedValue thumbRects: SharedValue> }) { - const {images, index: initialImageIndex, metricsContext} = lightbox + const {images, metricsContext} = lightbox + // Capture at mount: after a rotation remount this is the preserved + // current index, so the pager re-opens on the same image. + const [initialImageIndex] = useState(imageIndex) const ax = useAnalytics() const isAnimated = useMemo(() => canAnimate(lightbox), [lightbox]) const [isScaled, setIsScaled] = useState(false) const [isDragging, setIsDragging] = useState(false) - const [imageIndex, setImageIndex] = useState(initialImageIndex) const [showControls, setShowControls] = useState(true) const [isAltExpanded, setIsAltExpanded] = useState(false) const dismissSwipeTranslateY = useSharedValue(0)