-
Notifications
You must be signed in to change notification settings - Fork 49.2k
[react-interactions] Prevent duplicate onPress firing for keyboard Enter #17266
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
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 9034656:
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 529a5dd:
|
221bdba
to
53bc9d4
Compare
53bc9d4
to
8ac6c1e
Compare
@@ -914,8 +914,7 @@ describe.each(environmentTable)('Press responder', hasPointerEvents => { | |||
ReactDOM.render(<Component />, container); | |||
|
|||
const target = createEventTarget(ref.current); | |||
target.keydown({key: 'Enter'}); | |||
target.click({preventDefault}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was the click
event removed from this test? When Enter is pressed is certain host elements it will cause a click
to be dispatched
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not if you preventDefault
, meaning click
will never fire.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason we preventDefault on Spacebar it because otherwise it scrolls the window. Should we preventDefault on keydown for Enter though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, unless you know of any reason why we should not?
@necolas You seem to know quite a bit about this logic. If you are happy and have time, please could you do some digging into it to help me? I'd really appreciate it :) |
Internally, we encountered an issue where the
LegacyPress
responder would double fireonPress
when theEnter
keyboard key was pressed. Looking into this deeply, the reason for this was because the browser triggers aclick
event which passes the logic inisScreenReaderVirtualClick
.I tried to find different discrepancies between screen reader clicks and the click that is fired from the browser when you press
key – we trigger
Enter
but I couldn't see anything that stood out – so I took another approach and realized, by accident, the reason this doesn't happen when you pressSpace
ornativeEvent.preventDefault
. This means that the corresponding nativeclick
never gets triggered by the browser, but the responder still correctly fires theonPress
callback – this time only once though!I also noticed that is behavior didn't occur in the newer Press responder. I added support for the
Spacebar
key type there, but I also noticed a difference ininput
andtextarea
logic between the two responders that might be worth while tracking in a separate task – but maybe this is something @necolas has already considered.