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

Commit 3a517c2

Browse files
committed
fix(ngAria): do not scroll when pressing spacebar on custom buttons
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
1 parent 29b8dcf commit 3a517c2

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/ngAria/aria.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,10 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
387387
if ($aria.config('bindKeydown') && !attr.ngKeydown && !attr.ngKeypress && !attr.ngKeyup) {
388388
elem.on('keydown', function(event) {
389389
var keyCode = event.which || event.keyCode;
390-
if (keyCode === 32 || keyCode === 13) {
390+
391+
if (keyCode === 13 || keyCode === 32) {
392+
// Prevent the default browser behavior (e.g. scrolling when pressing spacebar).
393+
event.preventDefault();
391394
scope.$apply(callback);
392395
}
393396

test/ngAria/ariaSpec.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -929,11 +929,12 @@ describe('$aria', function() {
929929
clickEvents = [];
930930
scope.onClick = jasmine.createSpy('onClick').and.callFake(function(evt) {
931931
var nodeName = evt ? evt.target.nodeName.toLowerCase() : '';
932-
clickEvents.push(nodeName);
932+
var prevented = !!(evt && evt.isDefaultPrevented());
933+
clickEvents.push(nodeName + '(' + prevented + ')');
933934
});
934935
});
935936

936-
it('should trigger a click from the keyboard', function() {
937+
it('should trigger a click from the keyboard (and prevent default action)', function() {
937938
compileElement(
938939
'<section>' +
939940
'<div ng-click="onClick($event)"></div>' +
@@ -948,7 +949,7 @@ describe('$aria', function() {
948949
divElement.triggerHandler({type: 'keydown', keyCode: 32});
949950
liElement.triggerHandler({type: 'keydown', keyCode: 32});
950951

951-
expect(clickEvents).toEqual(['div', 'li', 'div', 'li']);
952+
expect(clickEvents).toEqual(['div(true)', 'li(true)', 'div(true)', 'li(true)']);
952953
});
953954

954955
it('should trigger a click in browsers that provide `event.which` instead of `event.keyCode`',
@@ -967,7 +968,7 @@ describe('$aria', function() {
967968
divElement.triggerHandler({type: 'keydown', which: 32});
968969
liElement.triggerHandler({type: 'keydown', which: 32});
969970

970-
expect(clickEvents).toEqual(['div', 'li', 'div', 'li']);
971+
expect(clickEvents).toEqual(['div(true)', 'li(true)', 'div(true)', 'li(true)']);
971972
}
972973
);
973974

0 commit comments

Comments
 (0)