Skip to content

Commit 524cac7

Browse files
committed
fix placeholder translate
1 parent 7f2128e commit 524cac7

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

src/components/PlaceholderItem.tsx

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import React, { useCallback } from "react";
1+
import React, { useCallback, useState } from "react";
22
import { StyleSheet } from "react-native";
33
import Animated, {
44
call,
5-
set,
65
useCode,
7-
useValue,
86
onChange,
97
greaterThan,
108
cond,
9+
sub,
10+
block,
1111
} from "react-native-reanimated";
1212
import { useAnimatedValues } from "../context/animatedValueContext";
1313
import { useDraggableFlatListContext } from "../context/draggableFlatListContext";
@@ -24,17 +24,17 @@ type Props<T> = {
2424
function 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
}

src/context/animatedValueContext.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ function useSetupAnimatedValues<T>() {
9292
const hoverOffset = useNode(add(hoverAnim, scrollOffset));
9393

9494
const placeholderOffset = useValue<number>(0);
95-
const placeholderScreenOffset = useNode(sub(placeholderOffset, scrollOffset));
9695

9796
// Note: this could use a refactor as it combines touch state + cell animation
9897
const resetTouchedCell = useNode(
@@ -121,7 +120,6 @@ function useSetupAnimatedValues<T>() {
121120
isTouchActiveNative,
122121
panGestureState,
123122
placeholderOffset,
124-
placeholderScreenOffset,
125123
resetTouchedCell,
126124
scrollOffset,
127125
scrollViewSize,
@@ -146,7 +144,6 @@ function useSetupAnimatedValues<T>() {
146144
isTouchActiveNative,
147145
panGestureState,
148146
placeholderOffset,
149-
placeholderScreenOffset,
150147
resetTouchedCell,
151148
scrollOffset,
152149
scrollViewSize,

0 commit comments

Comments
 (0)