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

Merged
merged 8 commits into from
Feb 24, 2025
Merged
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
25 changes: 22 additions & 3 deletions example/src/release_tests/nestedPressables/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,28 @@ const outerStyle = ({ pressed }: PressableStateCallbackType) => [

function GesturizedBoxes() {
return (
<GesturizedPressable style={outerStyle} testID="outer">
<GesturizedPressable style={middleStyle} testID="middle">
<GesturizedPressable style={innerStyle} testID="inner" />
<GesturizedPressable
style={outerStyle}
testID="outer"
onPressIn={() => console.log('[outer] onPressIn')}
onPressOut={() => console.log('[outer] onPressOut')}
onPress={() => console.log('[outer] onPress')}
onLongPress={() => console.log('[outer] onLongPress')}>
<GesturizedPressable
style={middleStyle}
testID="middle"
onPressIn={() => console.log('[middle] onPressIn')}
onPressOut={() => console.log('[middle] onPressOut')}
onPress={() => console.log('[middle] onPress')}
onLongPress={() => console.log('[middle] onLongPress')}>
<GesturizedPressable
style={innerStyle}
testID="inner"
onPressIn={() => console.log('[inner] onPressIn')}
onPressOut={() => console.log('[inner] onPressOut')}
onPress={() => console.log('[inner] onPress')}
onLongPress={() => console.log('[inner] onLongPress')}
/>
</GesturizedPressable>
</GesturizedPressable>
);
Expand Down
22 changes: 21 additions & 1 deletion src/components/Pressable/Pressable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@ 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);
pressDelayTimeoutRef.current = null;
}

return;
}

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

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

isTouchPropagationAllowed.current = true;
Expand Down
Loading