From 5572bef8b22cfc7b4d8b5989e72ccf578ba942ff Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Wed, 21 Oct 2026 00:09:28 +0300 Subject: [PATCH] [APP-3564] Start the lightbox open animation sooner, without a first-frame jump (#12093) --- src/components/Lightbox/pager/ImagePager.tsx | 56 ++++++++------------ src/components/Lightbox/state.tsx | 20 ++++++- src/view/com/util/List.tsx | 28 +++++----- 3 files changed, 56 insertions(+), 48 deletions(-) diff --git a/src/components/Lightbox/pager/ImagePager.tsx b/src/components/Lightbox/pager/ImagePager.tsx index b950a0180..48901c5ce 100644 --- a/src/components/Lightbox/pager/ImagePager.tsx +++ b/src/components/Lightbox/pager/ImagePager.tsx @@ -162,16 +162,9 @@ export default function ImageViewRoot({ thumbRects.set(initial) const isAnimated = canAnimate(nextLightbox) - - // https://github.com/software-mansion/react-native-reanimated/issues/6677 - rAF_FIXED(() => { - openProgress.set(isAnimated ? withClampedSpring(1, SLOW_SPRING) : 1) - }) + scheduleOnUI(animateOpenProgress, openProgress, 1, isAnimated) return () => { - // https://github.com/software-mansion/react-native-reanimated/issues/6677 - rAF_FIXED(() => { - openProgress.set(isAnimated ? withClampedSpring(0, SLOW_SPRING) : 0) - }) + scheduleOnUI(animateOpenProgress, openProgress, 0, isAnimated) } }, [nextLightbox, openProgress, thumbRects]) @@ -959,31 +952,26 @@ function withClampedSpring( return withSpring(value, {...config, overshootClamping: true}) } -// We have to do this because we can't trust RN's rAF to fire in order. -// https://github.com/facebook/react-native/issues/48005 -let isFrameScheduled = false -let pendingFrameCallbacks: Array<() => void> = [] -function rAF_FIXED(callback: () => void) { - pendingFrameCallbacks.push(callback) - if (!isFrameScheduled) { - isFrameScheduled = true +/** + * Springs the lightbox open or closed, starting on the second UI frame from + * now. If the main thread is busy (e.g. mounting the lightbox) when the display + * link wakes up, its first frame carries the timestamp of the vsync it missed, + * so a spring started on it would skip ahead on its first step. Worklet rAF + * callbacks run in order, so a close followed by a quick reopen still ends up + * open. + * + * Keep this below `withClampedSpring`: a module-level worklet captures the + * functions it calls when it's defined, so it would capture `undefined`. + */ +function animateOpenProgress( + openProgress: SharedValue, + to: number, + isAnimated: boolean, +) { + 'worklet' + requestAnimationFrame(() => { requestAnimationFrame(() => { - const callbacks = pendingFrameCallbacks.slice() - isFrameScheduled = false - pendingFrameCallbacks = [] - let hasError = false - let error - for (let i = 0; i < callbacks.length; i++) { - try { - callbacks[i]() - } catch (e) { - hasError = true - error = e - } - } - if (hasError) { - throw error - } + openProgress.set(isAnimated ? withClampedSpring(to, SLOW_SPRING) : to) }) - } + }) } diff --git a/src/components/Lightbox/state.tsx b/src/components/Lightbox/state.tsx index cb17670e9..6a4affa7b 100644 --- a/src/components/Lightbox/state.tsx +++ b/src/components/Lightbox/state.tsx @@ -1,4 +1,5 @@ import {createContext, useContext, useEffect, useMemo, useState} from 'react' +import {type SharedValue, useSharedValue} from 'react-native-reanimated' import {nanoid} from 'nanoid/non-secure' import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback' @@ -44,8 +45,12 @@ const LightboxControlContext = createContext<{ }) LightboxControlContext.displayName = 'LightboxControlContext' +const LightboxIsOpenContext = createContext | null>(null) +LightboxIsOpenContext.displayName = 'LightboxIsOpenContext' + export function Provider({children}: React.PropsWithChildren<{}>) { const [activeLightbox, setActiveLightbox] = useState(null) + const isOpen = useSharedValue(false) const {disableScope, enableScope} = useHotkeysContext() useEffect(() => { @@ -78,12 +83,14 @@ export function Provider({children}: React.PropsWithChildren<{}>) { return {...lightbox, id: nanoid()} } }) + isOpen.set(true) }, ) const closeLightbox = useNonReactiveCallback(() => { let wasActive = !!activeLightbox setActiveLightbox(null) + isOpen.set(false) return wasActive }) @@ -105,7 +112,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) { return ( - {children} + + {children} + ) @@ -118,3 +127,12 @@ export function useLightbox() { export function useLightboxControls() { return useContext(LightboxControlContext) } + +/** + * Whether a lightbox is open, as a shared value for UI-thread readers. Unlike + * `useLightbox()`, reading it doesn't re-render anything when the lightbox + * opens or closes. + */ +export function useLightboxIsOpenSharedValue() { + return useContext(LightboxIsOpenContext) +} diff --git a/src/view/com/util/List.tsx b/src/view/com/util/List.tsx index 25d069f69..0c5c6377b 100644 --- a/src/view/com/util/List.tsx +++ b/src/view/com/util/List.tsx @@ -1,4 +1,4 @@ -import {forwardRef, memo, useContext, useDeferredValue, useMemo} from 'react' +import {forwardRef, memo, useContext, useMemo} from 'react' import { type ListViewToken as ViewToken, RefreshControl, @@ -6,6 +6,7 @@ import { } from 'react-native' import { type FlatListPropsWithLayout, + useAnimatedProps, useAnimatedScrollHandler, useSharedValue, } from 'react-native-reanimated' @@ -25,7 +26,7 @@ import { CENTER_COLUMN_WIDTH, } from '#/components/Layout/const' import {ScrollbarOffsetContext} from '#/components/Layout/context' -import {useLightbox} from '#/components/Lightbox/state' +import {useLightboxIsOpenSharedValue} from '#/components/Lightbox/state' import {IS_IOS, IS_IPAD} from '#/env' import {FlatList_INTERNAL} from './Views' @@ -98,7 +99,7 @@ let List = forwardRef( gtMobile && !isWithinContainedTabletSurface const offsetContext = useMemo(() => ({isWithinOffsetView: true}), []) const dedupe = useDedupe(400) - const scrollsToTop = useAllowScrollToTop() + const scrollsToTopProps = useScrollsToTopProps() const handleScrolledDownChange = useNonReactiveCallback( (didScrollDown: boolean) => { @@ -216,7 +217,7 @@ let List = forwardRef( indicatorStyle={t.scheme === 'dark' ? 'white' : 'black'} refreshControl={refreshControl} onScroll={scrollHandler} - scrollsToTop={scrollsToTop} + animatedProps={scrollsToTopProps} scrollEventThrottle={1} style={[style, centerOnTablet && {width: '100%'}]} contentContainerStyle={[ @@ -250,21 +251,22 @@ List.displayName = 'List' List = memo(List) export {List} -/* - * We only want to use this context value on iOS because the `scrollsToTop` - * prop is iOS-only, so removing it saves us a re-render on Android. +/** + * Stops a status bar tap from scrolling the list while the lightbox is open. + * It's an animated prop so opening the lightbox doesn't re-render every list + * in the app just as the open animation starts. `scrollsToTop` is iOS-only. * * iPadOS 26 also scrolls to the top when a header button is tapped, and only * after the button's onPress has run, so it lands on the screen underneath a * closed lightbox or after a back navigation. Turn it off there entirely. See * https://github.com/facebook/react-native/issues/56061 */ -const useAllowScrollToTop = IS_IPAD - ? () => false +const useScrollsToTopProps = IS_IPAD + ? () => ({scrollsToTop: false}) : IS_IOS - ? useAllowScrollToTopIOS + ? useScrollsToTopPropsIOS : () => undefined -function useAllowScrollToTopIOS() { - const {activeLightbox} = useLightbox() - return useDeferredValue(!activeLightbox) +function useScrollsToTopPropsIOS() { + const isLightboxOpen = useLightboxIsOpenSharedValue() + return useAnimatedProps(() => ({scrollsToTop: !isLightboxOpen?.get()})) } -- knotmirror 0.0.1