@@ -33,11 +33,14 @@ import {
3333} from "react" ;
3434import {
3535 AccessibilityInfo ,
36+ Animated as NativeAnimated ,
3637 AppState ,
3738 type ColorValue ,
39+ Easing ,
3840 Pressable ,
3941 ScrollView ,
4042 StyleSheet ,
43+ useAnimatedValue ,
4144 View ,
4245} from "react-native" ;
4346import Svg , { Defs , LinearGradient , Rect , Stop } from "react-native-svg" ;
@@ -91,17 +94,12 @@ import type { FilePreviewSource } from "../../components/FilePreviewModal";
9194import { FilePreviewGallery } from "../../components/FilePreviewGallery" ;
9295import { ThreadMarkdownImage } from "./ThreadMarkdownImage" ;
9396import Animated , {
94- cancelAnimation ,
95- Easing ,
9697 FadeIn ,
9798 FadeOut ,
9899 LinearTransition ,
99100 ReduceMotion ,
100101 useAnimatedStyle ,
101102 useSharedValue ,
102- withDelay ,
103- withRepeat ,
104- withSequence ,
105103 withTiming ,
106104} from "react-native-reanimated" ;
107105import { useAssetUrl } from "../../state/assets" ;
@@ -267,7 +265,7 @@ export function ShimmeringWorkContent(props: {
267265 const [ appIsActive , setAppIsActive ] = useState ( AppState . currentState === "active" ) ;
268266 const [ reducedMotion , setReducedMotion ] = useState ( true ) ;
269267 const screenIsFocused = useIsFocused ( ) ;
270- const progress = useSharedValue ( 0 ) ;
268+ const progress = useAnimatedValue ( 0 ) ;
271269 const gradientId = `work-shimmer-${ useId ( ) . replaceAll ( ":" , "" ) } ` ;
272270 const contentWidth = Math . min (
273271 availableWidth ,
@@ -290,37 +288,37 @@ export function ShimmeringWorkContent(props: {
290288 return ( ) => subscription . remove ( ) ;
291289 } , [ ] ) ;
292290
291+ // Runs on React Native's native driver instead of Reanimated, whose
292+ // per-frame commits starve React's while commit pausing is disabled
293+ // (package.json). See docs/internals/performance-regressions.md.
293294 useEffect ( ( ) => {
294- cancelAnimation ( progress ) ;
295- progress . value = 0 ;
295+ progress . setValue ( 0 ) ;
296296 if ( contentWidth <= 0 || reducedMotion || ! appIsActive || ! screenIsFocused ) return ;
297297
298- progress . value = withRepeat (
299- withSequence (
300- withTiming ( 1 , {
301- duration : SHIMMER_SWEEP_MS ,
302- easing : Easing . linear ,
303- reduceMotion : ReduceMotion . Never ,
304- } ) ,
305- withDelay (
306- SHIMMER_PAUSE_MS ,
307- withTiming ( 0 , { duration : 0 , reduceMotion : ReduceMotion . Never } ) ,
308- ) ,
309- ) ,
310- - 1 ,
311- false ,
312- undefined ,
313- ReduceMotion . Never ,
298+ const loop = NativeAnimated . loop (
299+ NativeAnimated . timing ( progress , {
300+ toValue : 1 ,
301+ duration : SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS ,
302+ easing : Easing . linear ,
303+ useNativeDriver : true ,
304+ } ) ,
314305 ) ;
315- return ( ) => cancelAnimation ( progress ) ;
306+ loop . start ( ) ;
307+ return ( ) => loop . stop ( ) ;
316308 } , [ appIsActive , contentWidth , progress , reducedMotion , screenIsFocused ] ) ;
317309
318- const sweepStyle = useAnimatedStyle ( ( ) => ( {
319- transform : [ { translateX : - SHIMMER_WIDTH + progress . value * ( contentWidth + SHIMMER_WIDTH ) } ] ,
320- } ) ) ;
321- const counterSweepStyle = useAnimatedStyle ( ( ) => ( {
322- transform : [ { translateX : SHIMMER_WIDTH - progress . value * ( contentWidth + SHIMMER_WIDTH ) } ] ,
323- } ) ) ;
310+ // One timing covers the sweep and the pause, so the native driver can loop it.
311+ // The React Compiler memoizes these, so re-renders don't rebuild the native
312+ // animation nodes.
313+ const inputRange = [ 0 , SHIMMER_SWEEP_MS / ( SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS ) , 1 ] ;
314+ const sweepTranslateX = progress . interpolate ( {
315+ inputRange,
316+ outputRange : [ - SHIMMER_WIDTH , contentWidth , contentWidth ] ,
317+ } ) ;
318+ const counterSweepTranslateX = progress . interpolate ( {
319+ inputRange,
320+ outputRange : [ SHIMMER_WIDTH , - contentWidth , - contentWidth ] ,
321+ } ) ;
324322
325323 return (
326324 < View
@@ -341,13 +339,20 @@ export function ShimmeringWorkContent(props: {
341339 onTextLayout = { ( event ) => setTextWidth ( event . nativeEvent . lines [ 0 ] ?. width ?? 0 ) }
342340 />
343341 { ! reducedMotion && appIsActive && screenIsFocused && contentWidth > 0 ? (
344- < Animated . View
345- className = "absolute inset-y-0 left-0 overflow-hidden"
342+ < NativeAnimated . View
346343 pointerEvents = "none"
347344 accessible = { false }
348345 accessibilityElementsHidden
349346 importantForAccessibility = "no-hide-descendants"
350- style = { [ { width : SHIMMER_WIDTH } , sweepStyle ] }
347+ style = { {
348+ position : "absolute" ,
349+ top : 0 ,
350+ bottom : 0 ,
351+ left : 0 ,
352+ overflow : "hidden" ,
353+ width : SHIMMER_WIDTH ,
354+ transform : [ { translateX : sweepTranslateX } ] ,
355+ } }
351356 >
352357 < MaskedView
353358 style = { StyleSheet . absoluteFill }
@@ -368,7 +373,9 @@ export function ShimmeringWorkContent(props: {
368373 </ Svg >
369374 }
370375 >
371- < Animated . View style = { [ { width : availableWidth } , counterSweepStyle ] } >
376+ < NativeAnimated . View
377+ style = { { width : availableWidth , transform : [ { translateX : counterSweepTranslateX } ] } }
378+ >
372379 < ShimmerWorkContent
373380 textClassName = { props . textClassName }
374381 compact = { props . compact }
@@ -381,9 +388,9 @@ export function ShimmeringWorkContent(props: {
381388 themeAppearance = { props . themeAppearance }
382389 toolIcon = { props . toolIcon }
383390 />
384- </ Animated . View >
391+ </ NativeAnimated . View >
385392 </ MaskedView >
386- </ Animated . View >
393+ </ NativeAnimated . View >
387394 ) : null }
388395 </ View >
389396 ) ;
0 commit comments