Skip to content

Commit dfc6c56

Browse files
committed
fix perf issue, use animatedProps for pointerEvents
1 parent 340e634 commit dfc6c56

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/index.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Animated, {
1717
interpolate,
1818
Extrapolate,
1919
WithSpringConfig,
20+
useAnimatedProps,
2021
} from "react-native-reanimated";
2122

2223
export enum OpenDirection {
@@ -260,9 +261,14 @@ function SwipeableItem<T>(
260261
return refObject;
261262
});
262263

263-
function onAnimationEnd(openDirection: OpenDirection, snapPoint: number) {
264-
setOpenDirection(openDirection);
265-
onChange({ open: openDirection, snapPoint });
264+
function onAnimationEnd(_openDirection: OpenDirection, snapPoint: number) {
265+
setOpenDirection(_openDirection);
266+
const didChange =
267+
openDirection !== OpenDirection.NONE ||
268+
_openDirection !== OpenDirection.NONE;
269+
if (didChange) {
270+
onChange({ open: _openDirection, snapPoint });
271+
}
266272
}
267273

268274
const startX = useSharedValue(0);
@@ -305,6 +311,7 @@ function SwipeableItem<T>(
305311
}, Infinity);
306312

307313
const onComplete = () => {
314+
"worklet";
308315
const openDirection =
309316
closestSnapPoint === 0
310317
? OpenDirection.NONE
@@ -379,18 +386,32 @@ function SwipeableItem<T>(
379386
hasRight,
380387
]);
381388

389+
const animPropsLeft = useAnimatedProps(() => {
390+
return {
391+
pointerEvents:
392+
percentOpenLeft.value > 0 ? ("auto" as const) : ("none" as const),
393+
};
394+
}, []);
395+
396+
const animPropsRight = useAnimatedProps(() => {
397+
return {
398+
pointerEvents:
399+
percentOpenRight.value > 0 ? ("auto" as const) : ("none" as const),
400+
};
401+
}, []);
402+
382403
return (
383404
<OverlayContext.Provider value={overlayParams}>
384405
<Animated.View
385-
pointerEvents={openDirection === OpenDirection.LEFT ? "auto" : "none"}
406+
animatedProps={animPropsLeft}
386407
style={[styles.underlay, leftStyle]}
387408
>
388409
<UnderlayContext.Provider value={underlayLeftParams}>
389410
{renderUnderlayLeft(underlayLeftParams)}
390411
</UnderlayContext.Provider>
391412
</Animated.View>
392413
<Animated.View
393-
pointerEvents={openDirection === OpenDirection.RIGHT ? "auto" : "none"}
414+
animatedProps={animPropsRight}
394415
style={[styles.underlay, rightStyle]}
395416
>
396417
<UnderlayContext.Provider value={underlayRightParams}>

0 commit comments

Comments
 (0)