Keep lightbox image index on device rotation

This commit is contained in:
vineyardbovines
2026-06-11 10:01:42 -04:00
parent 5c04a41a11
commit e24ce9af58
+14 -2
View File
@@ -91,6 +91,9 @@ export default function ImageViewRoot({
'use no memo' 'use no memo'
const ref = useAnimatedRef<View>() const ref = useAnimatedRef<View>()
const [activeLightbox, setActiveLightbox] = useState(nextLightbox) 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'>( const [orientation, setOrientation] = useState<'portrait' | 'landscape'>(
'portrait', 'portrait',
) )
@@ -101,6 +104,7 @@ export default function ImageViewRoot({
if (!activeLightbox && nextLightbox) { if (!activeLightbox && nextLightbox) {
setActiveLightbox(nextLightbox) setActiveLightbox(nextLightbox)
setImageIndex(nextLightbox.index)
} }
useEffect(() => { useEffect(() => {
@@ -193,6 +197,8 @@ export default function ImageViewRoot({
<ImageView <ImageView
key={activeLightbox.id + '-' + orientation} key={activeLightbox.id + '-' + orientation}
lightbox={activeLightbox} lightbox={activeLightbox}
imageIndex={imageIndex}
setImageIndex={setImageIndex}
orientation={orientation} orientation={orientation}
onRequestClose={onRequestClose} onRequestClose={onRequestClose}
onPressSave={onPressSave} onPressSave={onPressSave}
@@ -210,6 +216,8 @@ export default function ImageViewRoot({
function ImageView({ function ImageView({
lightbox, lightbox,
imageIndex,
setImageIndex,
orientation, orientation,
onRequestClose, onRequestClose,
onPressSave, onPressSave,
@@ -220,6 +228,8 @@ function ImageView({
thumbRects, thumbRects,
}: { }: {
lightbox: Lightbox lightbox: Lightbox
imageIndex: number
setImageIndex: React.Dispatch<React.SetStateAction<number>>
orientation: 'portrait' | 'landscape' orientation: 'portrait' | 'landscape'
onRequestClose: () => void onRequestClose: () => void
onPressSave: (uri: string) => void onPressSave: (uri: string) => void
@@ -229,12 +239,14 @@ function ImageView({
openProgress: SharedValue<number> openProgress: SharedValue<number>
thumbRects: SharedValue<Record<number, MeasuredDimensions | null>> thumbRects: SharedValue<Record<number, MeasuredDimensions | null>>
}) { }) {
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 ax = useAnalytics()
const isAnimated = useMemo(() => canAnimate(lightbox), [lightbox]) const isAnimated = useMemo(() => canAnimate(lightbox), [lightbox])
const [isScaled, setIsScaled] = useState(false) const [isScaled, setIsScaled] = useState(false)
const [isDragging, setIsDragging] = useState(false) const [isDragging, setIsDragging] = useState(false)
const [imageIndex, setImageIndex] = useState(initialImageIndex)
const [showControls, setShowControls] = useState(true) const [showControls, setShowControls] = useState(true)
const [isAltExpanded, setIsAltExpanded] = useState(false) const [isAltExpanded, setIsAltExpanded] = useState(false)
const dismissSwipeTranslateY = useSharedValue(0) const dismissSwipeTranslateY = useSharedValue(0)