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

Commit 708eacb

Browse files
author
Marcy Sutton
committed
feat(ngAria): Adds button role to ng-click
Closes #9254
1 parent 93253df commit 708eacb

File tree

3 files changed

+96
-74
lines changed

3 files changed

+96
-74
lines changed

docs/content/guide/accessibility.ngdoc

+6-4
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,13 @@ The default CSS for `ngHide`, the inverse method to `ngShow`, makes ngAria redun
213213
If `ng-click` or `ng-dblclick` is encountered, ngAria will add `tabindex="0"` if it isn't there
214214
already.
215215

216-
For `ng-click`, keypress will also be bound to `div` and `li` elements. You can turn this
217-
functionality on or off with the `bindKeypress` configuration option.
216+
To fix widespread accessibility problems with `ng-click` on div elements, ngAria will dynamically
217+
bind keypress by default as long as the element isn't an anchor, button, input or textarea.
218+
You can turn this functionality on or off with the `bindKeypress` configuration option. ngAria
219+
will also add the `button` role to communicate to users of assistive technologies.
218220

219-
For `ng-dblclick`, you must manually add `ng-keypress` to non-interactive elements such as `div`
220-
or `taco-button` to enable keyboard access.
221+
For `ng-dblclick`, you must still manually add `ng-keypress` and role to non-interactive elements such
222+
as `div` or `taco-button` to enable keyboard access.
221223

222224
<h3>Example</h3>
223225
```html

src/ngAria/aria.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
*
2323
* | Directive | Supported Attributes |
2424
* |---------------------------------------------|----------------------------------------------------------------------------------------|
25-
* | {@link ng.directive:ngModel ngModel} | aria-checked, aria-valuemin, aria-valuemax, aria-valuenow, aria-invalid, aria-required |
2625
* | {@link ng.directive:ngDisabled ngDisabled} | aria-disabled |
2726
* | {@link ng.directive:ngShow ngShow} | aria-hidden |
2827
* | {@link ng.directive:ngHide ngHide} | aria-hidden |
29-
* | {@link ng.directive:ngClick ngClick} | tabindex, keypress event |
3028
* | {@link ng.directive:ngDblclick ngDblclick} | tabindex |
3129
* | {@link module:ngMessages ngMessages} | aria-live |
30+
* | {@link ng.directive:ngModel ngModel} | aria-checked, aria-valuemin, aria-valuemax, aria-valuenow, aria-invalid, aria-required, input roles |
31+
* | {@link ng.directive:ngClick ngClick} | tabindex, keypress event, button role |
3232
*
3333
* Find out more information about each directive by reading the
3434
* {@link guide/accessibility ngAria Developer Guide}.
@@ -315,17 +315,22 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
315315
var fn = $parse(attr.ngClick, /* interceptorFn */ null, /* expensiveChecks */ true);
316316
return function(scope, elem, attr) {
317317

318+
var nodeBlackList = ['BUTTON', 'A', 'INPUT', 'TEXTAREA'];
319+
318320
function isNodeOneOf(elem, nodeTypeArray) {
319321
if (nodeTypeArray.indexOf(elem[0].nodeName) !== -1) {
320322
return true;
321323
}
322324
}
325+
if (!elem.attr('role') && !isNodeOneOf(elem, nodeBlackList)) {
326+
elem.attr('role', 'button');
327+
}
323328

324329
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
325330
elem.attr('tabindex', 0);
326331
}
327332

328-
if ($aria.config('bindKeypress') && !attr.ngKeypress && isNodeOneOf(elem, ['DIV', 'LI'])) {
333+
if ($aria.config('bindKeypress') && !attr.ngKeypress && !isNodeOneOf(elem, nodeBlackList)) {
329334
elem.on('keypress', function(event) {
330335
if (event.keyCode === 32 || event.keyCode === 13) {
331336
scope.$apply(callback);

0 commit comments

Comments
 (0)