@@ -33,8 +33,10 @@ import {
3333} from "react" ;
3434import {
3535 AccessibilityInfo ,
36+ Animated as NativeAnimated ,
3637 AppState ,
3738 type ColorValue ,
39+ Easing ,
3840 Pressable ,
3941 ScrollView ,
4042 StyleSheet ,
@@ -91,17 +93,12 @@ import type { FilePreviewSource } from "../../components/FilePreviewModal";
9193import { FilePreviewGallery } from "../../components/FilePreviewGallery" ;
9294import { ThreadMarkdownImage } from "./ThreadMarkdownImage" ;
9395import Animated , {
94- cancelAnimation ,
95- Easing ,
9696 FadeIn ,
9797 FadeOut ,
9898 LinearTransition ,
9999 ReduceMotion ,
100100 useAnimatedStyle ,
101101 useSharedValue ,
102- withDelay ,
103- withRepeat ,
104- withSequence ,
105102 withTiming ,
106103} from "react-native-reanimated" ;
107104import { useAssetUrl } from "../../state/assets" ;
@@ -267,7 +264,7 @@ export function ShimmeringWorkContent(props: {
267264 const [ appIsActive , setAppIsActive ] = useState ( AppState . currentState === "active" ) ;
268265 const [ reducedMotion , setReducedMotion ] = useState ( true ) ;
269266 const screenIsFocused = useIsFocused ( ) ;
270- const progress = useSharedValue ( 0 ) ;
267+ const [ progress ] = useState ( ( ) => new NativeAnimated . Value ( 0 ) ) ;
271268 const gradientId = `work-shimmer-${ useId ( ) . replaceAll ( ":" , "" ) } ` ;
272269 const contentWidth = Math . min (
273270 availableWidth ,
@@ -290,37 +287,35 @@ export function ShimmeringWorkContent(props: {
290287 return ( ) => subscription . remove ( ) ;
291288 } , [ ] ) ;
292289
290+ // Runs on React Native's native driver instead of Reanimated, whose
291+ // per-frame commits starve React's while commit pausing is disabled
292+ // (package.json). See docs/internals/performance-regressions.md.
293293 useEffect ( ( ) => {
294- cancelAnimation ( progress ) ;
295- progress . value = 0 ;
294+ progress . setValue ( 0 ) ;
296295 if ( contentWidth <= 0 || reducedMotion || ! appIsActive || ! screenIsFocused ) return ;
297296
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 ,
297+ const loop = NativeAnimated . loop (
298+ NativeAnimated . timing ( progress , {
299+ toValue : 1 ,
300+ duration : SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS ,
301+ easing : Easing . linear ,
302+ useNativeDriver : true ,
303+ } ) ,
314304 ) ;
315- return ( ) => cancelAnimation ( progress ) ;
305+ loop . start ( ) ;
306+ return ( ) => loop . stop ( ) ;
316307 } , [ appIsActive , contentWidth , progress , reducedMotion , screenIsFocused ] ) ;
317308
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- } ) ) ;
309+ // One timing covers the sweep and the pause, so the native driver can loop it.
310+ const sweepInputRange = [ 0 , SHIMMER_SWEEP_MS / ( SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS ) , 1 ] ;
311+ const sweepTranslateX = progress . interpolate ( {
312+ inputRange : sweepInputRange ,
313+ outputRange : [ - SHIMMER_WIDTH , contentWidth , contentWidth ] ,
314+ } ) ;
315+ const counterSweepTranslateX = progress . interpolate ( {
316+ inputRange : sweepInputRange ,
317+ outputRange : [ SHIMMER_WIDTH , - contentWidth , - contentWidth ] ,
318+ } ) ;
324319
325320 return (
326321 < View
@@ -341,13 +336,20 @@ export function ShimmeringWorkContent(props: {
341336 onTextLayout = { ( event ) => setTextWidth ( event . nativeEvent . lines [ 0 ] ?. width ?? 0 ) }
342337 />
343338 { ! reducedMotion && appIsActive && screenIsFocused && contentWidth > 0 ? (
344- < Animated . View
345- className = "absolute inset-y-0 left-0 overflow-hidden"
339+ < NativeAnimated . View
346340 pointerEvents = "none"
347341 accessible = { false }
348342 accessibilityElementsHidden
349343 importantForAccessibility = "no-hide-descendants"
350- style = { [ { width : SHIMMER_WIDTH } , sweepStyle ] }
344+ style = { {
345+ position : "absolute" ,
346+ top : 0 ,
347+ bottom : 0 ,
348+ left : 0 ,
349+ overflow : "hidden" ,
350+ width : SHIMMER_WIDTH ,
351+ transform : [ { translateX : sweepTranslateX } ] ,
352+ } }
351353 >
352354 < MaskedView
353355 style = { StyleSheet . absoluteFill }
@@ -368,7 +370,9 @@ export function ShimmeringWorkContent(props: {
368370 </ Svg >
369371 }
370372 >
371- < Animated . View style = { [ { width : availableWidth } , counterSweepStyle ] } >
373+ < NativeAnimated . View
374+ style = { { width : availableWidth , transform : [ { translateX : counterSweepTranslateX } ] } }
375+ >
372376 < ShimmerWorkContent
373377 textClassName = { props . textClassName }
374378 compact = { props . compact }
@@ -381,9 +385,9 @@ export function ShimmeringWorkContent(props: {
381385 themeAppearance = { props . themeAppearance }
382386 toolIcon = { props . toolIcon }
383387 />
384- </ Animated . View >
388+ </ NativeAnimated . View >
385389 </ MaskedView >
386- </ Animated . View >
390+ </ NativeAnimated . View >
387391 ) : null }
388392 </ View >
389393 ) ;
0 commit comments