Skip to content

Commit

Permalink
fix: fixed the hitslop and pointerevent for android
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed May 28, 2020
1 parent af56e5d commit 7062ab9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
1 change: 0 additions & 1 deletion example/src/screens/Basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ const Basic = () => {
<View style={containerStyle}>
<StickyItemFlatList
ref={flatListRef}
initialScrollIndex={1}
itemWidth={STORY_WIDTH}
itemHeight={STORY_HEIGHT}
separatorSize={SEPARATOR_SIZE}
Expand Down
2 changes: 1 addition & 1 deletion src/StickyItemFlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ const StickyItemFlatList = forwardRef(
if (tapRef.current) {
// @ts-ignore
tapRef.current.setNativeProps({
hitSlop: getHitSlop(initialScrollIndex === 0),
hitSlop: getHitSlop(initialScrollIndex !== 0),
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
47 changes: 45 additions & 2 deletions src/components/sticky-item/StickyItem.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React from 'react';
import React, { useRef } from 'react';
import { Dimensions } from 'react-native';
import Animated, {
multiply,
sub,
greaterThan,
cond,
add,
useCode,
onChange,
call,
eq,
} from 'react-native-reanimated';
import { transformOrigin } from 'react-native-redash';
import StickyItemBackground from '../sticky-item-background';
Expand All @@ -26,6 +30,7 @@ const StickyItem = ({
stickyItemBackgroundColors,
isRTL,
}: StickyItemProps) => {
const containerRef = useRef<Animated.View>(null);
const threshold = itemWidth - stickyItemWidth - separatorSize;
//#region Container
const animatedTranslateX = multiply(
Expand Down Expand Up @@ -76,8 +81,46 @@ const StickyItem = ({
<StickyItemContent {...props} />
);
};

// effects
/**
* @DEV
* here we manipulate the container `pointerEvents` to avoid
* the double press event on Android.
*/
useCode(
() => [
onChange(animatedTranslateX, [
cond(
eq(animatedTranslateX, separatorSize),
call([], () => {
const container = containerRef.current;
if (container) {
// @ts-ignore
container.setNativeProps({
pointerEvents: 'none',
});
}
})
),
cond(
eq(animatedTranslateX, (threshold - separatorSize) * -1),
call([], () => {
const container = containerRef.current;
if (container) {
// @ts-ignore
container.setNativeProps({
pointerEvents: 'auto',
});
}
})
),
]),
],
[separatorSize, threshold]
);
return (
<Animated.View pointerEvents={'none'} style={containerStyle}>
<Animated.View ref={containerRef} style={containerStyle}>
<StickyItemBackground
threshold={threshold}
x={x}
Expand Down

0 comments on commit 7062ab9

Please sign in to comment.