-
Notifications
You must be signed in to change notification settings - Fork 78
Prevent behavior of a Button element when the space key is pressed #108
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
base: master
Are you sure you want to change the base?
Prevent behavior of a Button element when the space key is pressed #108
Conversation
src/Button.js
Outdated
}; | ||
|
||
handleKeyUp = (event) => { | ||
event.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.
Can we add a brief comment here noting why we're calling event.preventDefault
? This makes it easily apparent when viewing the code in the future.
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.
This seems like a dangerously broad move, preventing default on all key-up events regardless of the key — doesn't it? Should we limit it to only target Enter and Space?
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.
To add to David's comment, I think we only need to limit it to Space since Enter works.
src/__tests__/Button.test.js
Outdated
const wrapper = shallow(el(Button, null, 'foo'), shallowOptions); | ||
wrapper.simulate('keyUp', spaceEvent); | ||
|
||
expect(spaceEvent.preventDefault).toHaveBeenCalledTimes(1); |
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 we can improve upon this assertion here. Rather than asserting event.preventDefault
is called (that doesn't really provide any useful information whether the menu is being opened/toggled), we should first open the menu, and simulate the keyDown
, and keyUp
with the spaceEvent
you have and check if toggleMenu
is called multiple times like so.
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.
Thanks.
Could you tell me if you have a good idea about this?
In the test case, I think it is difficult to reproduce the behavior of Firefox simulating a click
event when you release the space bar on a button
element.
Calling toggleMenu
is the role of keyDown
or click
, and keyUp
itself is not involved in calling it, hence it asserts whether keyUp calls event.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.
Yeah, I'm not sure how to run these tests against a FF environment (I think this can be tackled as a separate issue). However, the tests is just to ensure it doesn't cause any regression in general.
I think something like the steps below would work:
- simulate click to open menu first
- expect
ambManager.isOpen
istrue
(ensures the menu is open before we run assertions to validate the Space events - simulate
keyDown
and then akeyUp
Space event - expect
ambManager.isOpen
isfalse
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.
Thank you very much.
I will work on it.
Apologies for just barging in here, particularly if I'm misreading the situation, but, from your example and description, it sounds like Firefox is behaving correctly - native HTML buttons fire the click event on spacebar and enter. If you're adding |
@malcalevak In Firefox, when the menu is closed and the focus moves to the Button component and then space key is released, if it is a native HTML button element, the Button component's As you said, if the Button component is a As @davidtheclark mentions, I'm thinking of limit the scope of |
@grgr-dkrk I always bristle at negating native functionality, but, after reviewing the code, the only other option I could think of would be changing how focus was triggered, which would still rely on a key event, and it would require more rework, while possibly still being imperfect. |
related: #107
The space key in the Button element has a behavior similar to the keyUp event.
This will fire an unexpected click event in some browsers.
This PR prevents keyUp events on the Button component and avoids malfunctions in browsers such as Firefox.
example: https://codesandbox.io/s/spacekey-sample-9odi3