Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(ngAria[ngClick]): preventDefault on pressing Space so the page doesn't scroll #14665

Closed
wants to merge 1 commit into from

Conversation

stowball
Copy link

This is a bug fix for the ngAria ngClick directive.

When a user presses space on a "normal" button, the page should not scroll. However, with a "fake", [role="button"], pressing space doesn't preventDefault(), so it scrolls the page.

You can see the bad behavior here: https://plnkr.co/edit/SPxQ0X2RQvvfvAmDVTNA?p=preview

This PR adds event.preventDefault(); so that pressing Space doesn't activate the native browser function.

@marcysutton
Copy link
Contributor

marcysutton commented May 25, 2016

Seems straight forward enough for me. I verified it still works when the div has an interactive child element: https://plnkr.co/edit/SPxQ0X2RQvvfvAmDVTNA?p=preview

But it highlights another problem, which I will file as an issue: ngAria needs to check for at least one interactive child element before binding role="button" and tabindex="0" because otherwise you get interactive elements wrapped around other interactive elements. This is a problem for keyboard AND screen reader users, and also why a <button> element wrapping other <button> elements is invalid HTML.

@@ -377,6 +377,7 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
elem.on('keypress', function(event) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32

@gkalpak
Copy link
Member

gkalpak commented Sep 10, 2016

I've somehow missed that 😃 LGTM, except for the lack of tests.
@stowball, would you be interested in adding some?

@tybiwety23
Copy link

Leave a commement

@stowball
Copy link
Author

I'm new to testing. How would we go about testing the whether the window scroll position changed?

@gkalpak
Copy link
Member

gkalpak commented Oct 16, 2016

It would be a little involved, because you would have to make sure there is room to scroll and then check the windows scrollTop probably. But I don't think you have to. You can just check that preventDefault() is called on the dispatched event (for ENTER, SPACEBAR only).

@Narretz Narretz modified the milestones: 1.5.x, 1.7.x Apr 12, 2018
@gkalpak gkalpak self-assigned this May 9, 2018
@petebacondarwin petebacondarwin self-assigned this May 14, 2018
gkalpak added a commit to gkalpak/angular.js that referenced this pull request Jun 16, 2018
By default, pressing spacebar causes the browser to scroll down.
However, when a native button is focused, the button is clicked instead.

`ngAria`'s `ngClick` directive, sets elements up to behave like buttons.
For example, it adds `role="button"` and forwards `ENTER` and `SPACEBAR`
keypresses to the `click` handler (to emulate the behavior of native
buttons).

Yet, pressing spacebar on such an element, still invokes the default
browser behavior of scrolling down.

This commit fixes this, by calling `preventDefault()` on the keyboard
event, thus preventing the default scrolling behavior and making custom
buttons behave closer to native ones.

Closes angular#14665
@gkalpak gkalpak closed this in 6c224a2 Jun 18, 2018
gkalpak added a commit that referenced this pull request Jun 18, 2018
By default, pressing spacebar causes the browser to scroll down.
However, when a native button is focused, the button is clicked instead.

`ngAria`'s `ngClick` directive, sets elements up to behave like buttons.
For example, it adds `role="button"` and forwards `ENTER` and `SPACEBAR`
keypresses to the `click` handler (to emulate the behavior of native
buttons).

Yet, pressing spacebar on such an element, still invokes the default
browser behavior of scrolling down.

This commit fixes this, by calling `preventDefault()` on the keyboard
event, thus preventing the default scrolling behavior and making custom
buttons behave closer to native ones.

Closes #14665

Closes #16604
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants