1- import React , { useCallback } from "react" ;
1+ import React , { useCallback , useState } from "react" ;
22import { StyleSheet } from "react-native" ;
33import Animated , {
44 call ,
5- set ,
65 useCode ,
7- useValue ,
86 onChange ,
97 greaterThan ,
108 cond ,
9+ sub ,
10+ block ,
1111} from "react-native-reanimated" ;
1212import { useAnimatedValues } from "../context/animatedValueContext" ;
1313import { useDraggableFlatListContext } from "../context/draggableFlatListContext" ;
@@ -24,17 +24,17 @@ type Props<T> = {
2424function PlaceholderItem < T > ( { renderPlaceholder } : Props < T > ) {
2525 const {
2626 activeCellSize,
27- placeholderScreenOffset ,
27+ placeholderOffset ,
2828 spacerIndexAnim,
29+ scrollOffset,
2930 } = useAnimatedValues ( ) ;
31+ const [ placeholderSize , setPlaceholderSize ] = useState ( 0 ) ;
32+
3033 const { keyToIndexRef, propsRef } = useRefs < T > ( ) ;
3134
3235 const { activeKey } = useDraggableFlatListContext ( ) ;
3336 const { horizontal } = useProps ( ) ;
3437
35- // for some reason using placeholderScreenOffset directly is buggy
36- const translate = useValue ( 0 ) ;
37-
3838 const onPlaceholderIndexChange = useCallback (
3939 ( index : number ) => {
4040 propsRef . current . onPlaceholderIndexChange ?.( index ) ;
@@ -44,12 +44,21 @@ function PlaceholderItem<T>({ renderPlaceholder }: Props<T>) {
4444
4545 useCode (
4646 ( ) =>
47- onChange (
48- spacerIndexAnim ,
49- call ( [ spacerIndexAnim ] , ( [ i ] ) => {
50- onPlaceholderIndexChange ( i ) ;
51- } )
52- ) ,
47+ block ( [
48+ onChange (
49+ activeCellSize ,
50+ call ( [ activeCellSize ] , ( [ size ] ) => {
51+ setPlaceholderSize ( size ) ;
52+ } )
53+ ) ,
54+ onChange (
55+ spacerIndexAnim ,
56+ call ( [ spacerIndexAnim ] , ( [ i ] ) => {
57+ onPlaceholderIndexChange ( i ) ;
58+ if ( i === - 1 ) setPlaceholderSize ( 0 ) ;
59+ } )
60+ ) ,
61+ ] ) ,
5362 [ ]
5463 ) ;
5564
@@ -65,9 +74,9 @@ function PlaceholderItem<T>({ renderPlaceholder }: Props<T>) {
6574
6675 const animStyle = {
6776 opacity,
68- [ sizeKey ] : activeCellSize ,
77+ [ sizeKey ] : placeholderSize , // Using animated values to set height caused a bug where item wouldn't correctly update
6978 transform : ( [
70- { [ translateKey ] : translate } ,
79+ { [ translateKey ] : sub ( placeholderOffset , scrollOffset ) } ,
7180 ] as unknown ) as Animated . AnimatedTransform ,
7281 } ;
7382
@@ -79,9 +88,6 @@ function PlaceholderItem<T>({ renderPlaceholder }: Props<T>) {
7988 { ! activeItem || activeIndex === undefined
8089 ? null
8190 : renderPlaceholder ?.( { item : activeItem , index : activeIndex } ) }
82- < Animated . Code >
83- { ( ) => set ( translate , placeholderScreenOffset ) }
84- </ Animated . Code >
8591 </ Animated . View >
8692 ) ;
8793}
0 commit comments