Skip to content

[fix] Pressable: ignore pointer clicks that arrive without a press on the element - #2942

Open
charan-rathore wants to merge 2 commits into
necolas:masterfrom
charan-rathore:fix/2765-spurious-press-on-ancestor
Open

charan-rathore wants to merge 2 commits into
necolas:masterfrom
charan-rathore:fix/2765-spurious-press-on-ancestor

Conversation

@charan-rathore

Copy link
Copy Markdown
Contributor

Fix #2765.

Problem

After a drag out of a nested Pressable, the browser dispatches the native click at the nearest common ancestor of the press target and the release target. Because onPress was fired by any click that reached a Pressable, the ancestor Pressable fired onPress even though no press ever started on it. In React Native, a press that moves out of an element's responder region is cancelled and no onPress fires anywhere.

Reproduction on current master (real Chrome, CDP mouse input): press the center of a child Pressable, drag onto parent-only area, release. The child correctly cancels (onPressOut), but the click lands on the parent and the parent's onPress fires, without any preceding onPressIn on the parent.

Note: the literal direction in the original report (release over the child fires the child's onPress) did not reproduce on current master; the confirmed spurious target is the common ancestor. Both are the same defect class: a pointer click firing onPress on an element where the current gesture never started a press.

Fix

PressResponder now tracks whether a press started on its element during the current gesture. onClick fires onPress only when:

  • the click is keyboard-, screen-reader-, or script-originated (MouseEvent.detail === 0, i.e. no pointer gesture), which must always fire onPress; or
  • a pointer press was actually granted to this element during the current gesture.

The flag is armed only by the responder grant (keyboard presses, which start via keydown, never arm it), consumed in onClick, cleared on responder termination and keyboard keyup, and cleared by a 0ms timer after release when no click consumed it, so a stale gesture can never leak into a later one.

Intentional behavior change: synthetic pointer-originated clicks dispatched programmatically without a real gesture (new MouseEvent('click', { detail: 1 })) no longer fire onPress. element.click(), jsdom, and testing-library fireEvent.click dispatch detail: 0 clicks and are unaffected.

Tests

Five new Pressable tests cover the ancestor-drag suppression, the normal same-element press, programmatic/AT clicks without a gesture, repeated pointer clicks, and a stray pointer click after a keyboard-initiated press.

  • Before the fix, exactly the 2 regression tests fail (25/27 pass).
  • After the fix, all 27 pass; full unit suite: 54 suites, 757 passed, 6 skipped, 0 failed. flow check, prettier --check, and eslint are clean.

Verified manually in real Chrome (CDP input) for child-to-parent drags, parent-to-child drags, clean clicks, double-clicks, keyboard Enter, and programmatic clicks. Not exercised: multi-leg out-and-back-in pointer re-engagement (the latch stays armed unless the gesture terminates, matching RN's re-engagement semantics), and non-Chrome browsers.

… the element

Fix necolas#2765. A pointer click released over a common ancestor after a drag
out of a nested Pressable fired onPress on an element where no press
started. onPress now fires for pointer-originated clicks (MouseEvent
detail > 0) only when a press started on the element during the current
gesture; keyboard, screen-reader, and programmatic clicks (detail 0)
are unaffected.
@charan-rathore

Copy link
Copy Markdown
Contributor Author

Could you review the Pressable-specific fix here? I rechecked against current master a38a286: the two regression tests still fail, and applying this PR's source change makes all 33 Pressable tests pass. This covers a click reaching an ancestor Pressable where no press started; #2961 addressed PanResponder's click-capture forwarding instead. I haven't repeated the Chrome checks on this newer master.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PanResponder release triggers onPress of child component

1 participant