Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: onPress callbacks are invoked for all nested Pressables #3295

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions example/src/new_api/nested_pressable/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { Pressable } from 'react-native-gesture-handler';

export default function NestedPressableExample() {
const pressIn = (name: string) => {
console.log(`${name} Pressable pressed in`);
};

const pressOut = (name: string) => {
console.log(`${name} Pressable pressed out`);
};

const press = (name: string) => {
console.log(`${name} Pressable pressed`);
};

const hoverIn = (name: string) => {
console.log(`${name} Hovered in`);
};

const hoverOut = (name: string) => {
console.log(`${name} Hovered out`);
};

const longPress = (name: string) => {
console.log(`${name} Long pressed`);
};

return (
<View style={{ flex: 1, backgroundColor: 'white' }}>
<Pressable
style={({ pressed }) => [
styles.pressableFirstLayer,
pressed && styles.highlight,
]}
onPressIn={() => pressIn('First')}
onPressOut={() => pressOut('First')}
onPress={() => press('First')}
onHoverIn={() => hoverIn('First')}
onHoverOut={() => hoverOut('First')}
onLongPress={() => longPress('First')}>
<Pressable
style={({ pressed }) => [
styles.pressableSecondLayer,
pressed && styles.highlight,
]}
onPressIn={() => pressIn('Second')}
onPressOut={() => pressOut('Second')}
onPress={() => press('Second')}
onHoverIn={() => hoverIn('Second')}
onHoverOut={() => hoverOut('Second')}
onLongPress={() => longPress('Second')}>
<Pressable
style={({ pressed }) => [
styles.pressableThirdLayer,
pressed && styles.highlight,
]}
onPressIn={() => pressIn('Third')}
onPressOut={() => pressOut('Third')}
onPress={() => press('Third')}
onHoverIn={() => hoverIn('Third')}
onHoverOut={() => hoverOut('Third')}
onLongPress={() => longPress('Third')}>
<Text style={styles.rectText}>Third Pressable</Text>
</Pressable>
<Text style={styles.rectText}>Second Pressable</Text>
</Pressable>
<Text style={styles.rectText}>First Pressable</Text>
</Pressable>
</View>
);
}

const BACKGROUND_COLOR = '#F5FCFF';

const styles = StyleSheet.create({
pressableFirstLayer: {
backgroundColor: '#FFD6E0',
padding: 40,
margin: 'auto',
},
pressableSecondLayer: {
backgroundColor: '#F29DC3',
padding: 40,
margin: 'auto',
},
pressableThirdLayer: {
width: 160,
height: 160,
margin: 'auto',
backgroundColor: 'mediumpurple',
},
textWrapper: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
color: 'white',
},
rectText: {
color: BACKGROUND_COLOR,
fontWeight: '700',
position: 'absolute',
right: 5,
bottom: 2,
},
highlight: {
backgroundColor: 'red',
},
});
21 changes: 20 additions & 1 deletion src/components/Pressable/Pressable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ export default function Pressable(props: PressableProps) {

const pressOutHandler = useCallback(
(event: PressableEvent) => {
if (!isTouchPropagationAllowed.current) {
hasPassedBoundsChecks.current = false;
isPressCallbackEnabled.current = true;
deferredEventPayload.current = null;

if (longPressTimeoutRef.current) {
clearTimeout(longPressTimeoutRef.current);
longPressTimeoutRef.current = null;
}

if (pressDelayTimeoutRef.current) {
clearTimeout(pressDelayTimeoutRef.current);
}

return;
}

if (
!hasPassedBoundsChecks.current ||
event.nativeEvent.touches.length >
Expand Down Expand Up @@ -337,7 +354,9 @@ export default function Pressable(props: PressableProps) {

if (shouldPreventNativeEffects.current) {
shouldPreventNativeEffects.current = false;
return;
if (!handlingOnTouchesDown.current) {
return;
}
}

isTouchPropagationAllowed.current = true;
Expand Down
Loading