Repository navigation
[fix] Pressable: ignore pointer clicks that arrive without a press on the element - #2942
Open
charan-rathore wants to merge 2 commits into
Open
charan-rathore wants to merge 2 commits into
charan-rathore wants to merge 2 commits into
Conversation
… 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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #2765.
Problem
After a drag out of a nested
Pressable, the browser dispatches the nativeclickat the nearest common ancestor of the press target and the release target. BecauseonPresswas fired by anyclickthat reached aPressable, the ancestorPressablefiredonPresseven though no press ever started on it. In React Native, a press that moves out of an element's responder region is cancelled and noonPressfires 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'sonPressfires, without any precedingonPressInon 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 firingonPresson an element where the current gesture never started a press.Fix
PressRespondernow tracks whether a press started on its element during the current gesture.onClickfiresonPressonly when:MouseEvent.detail === 0, i.e. no pointer gesture), which must always fireonPress; orThe flag is armed only by the responder grant (keyboard presses, which start via
keydown, never arm it), consumed inonClick, cleared on responder termination and keyboardkeyup, 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 fireonPress.element.click(), jsdom, and testing-libraryfireEvent.clickdispatchdetail: 0clicks and are unaffected.Tests
Five new
Pressabletests 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.flow check,prettier --check, andeslintare 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.