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

feat(ngAria[ngClick]): anchor should treat keypress as click #13996

Closed
wants to merge 8 commits into from
Closed

feat(ngAria[ngClick]): anchor should treat keypress as click #13996

wants to merge 8 commits into from

Conversation

LeeAdcock
Copy link
Contributor

Enhances ngAria to trigger ngClick for 'space' keypresses on anchor and button elements, and
for 'enter' keypresses on anchor elements when there is no value for the href attribute.

Closes #11053

…hor, button elements

Enhances ngAria to trigger ngClick for 'space' keypresses on anchor and button elements, and
for 'enter' keypresses on anchor tags when there is no value for the href attribute.

Closes #11053
@@ -377,6 +377,27 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
});
}
}

if ($aria.config('bindKeypress') && isNodeOneOf(elem, ['A', 'BUTTON'])) {
Copy link
Member

Choose a reason for hiding this comment

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

Actually, we don't want to do anything with buttons, just as. The browser handles pressing both ENTER and SPACE on buttons.

Copy link
Member

Choose a reason for hiding this comment

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

You should also check that attr.ngKeypress is falsy.

@gkalpak
Copy link
Member

gkalpak commented Feb 11, 2016

I left some comments (that should be easy to address). Additonally, according to #11053 (comment), we should also bind the role to link on <a> elements (if the relevant setting in $aria is enabled).

@LeeAdcock, feel free to ping when/if you've made any changes, so I can take another look. Thx !

@LeeAdcock
Copy link
Contributor Author

Thank you for your feedback. I'm made the suggested changes, but I believe there's still a little bit of work to go.

  • In the case that href (and xlink:href) is falsy the browser doesn't react to ENTER key presses. I believe we need to be watching for these in the keypress binding.
  • The SPACE doesn't have any impact on links where href is defined. I believe we do need to be watching for SPACE key presses regardless of the href.
  • I'm not sure executing the ng-click is the best way to provide full accessibility. Even when watching for SPACE key presses for anchors with hrefs, the ENTER is interpreted as a click and follows the link, while SPACE only executes ng-click. It would seem both should be a click and follow the link.
  • This isn't the documented use of bindRoleForClick and bindKeypress, I assume I should update the documentation to include details about anchor handling, versus creating new config flags.

Please let me know which direction you'd like me to go. @gkalpak

@LeeAdcock
Copy link
Contributor Author

I continued to iterate on these changes, and believe the PR now aligns with the intent of the issue and provides what I would have expected from ngAria for enhanced accessibility for links. This results in the same behavior for mouse clicks, the space key, and the enter key. The role='link' attribute is now also defined when needed.

I would suggest that the previous behavior might be more than an inconvenience. We have written our own custom directives to work around these shortcomings, since the current Angular behavior fails accessibility standards.

Feedback welcome, @gkalpak.

@LeeAdcock LeeAdcock changed the title feat(ngAria[ngClick]): button, anchor should treat keypress as click feat(ngAria[ngClick]): anchor should treat keypress as click Feb 13, 2016
elem[0].click();
}

function callback() {
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like callback is never used inside this code path.

@marcysutton
Copy link
Contributor

Thanks for working on this! Keep in mind the expected keyboard interaction for links is ENTER, not SPACE. That is more of a typical button interaction.

if ($aria.config('bindRoleForClick') && !attr.href && !attr.xlinkHref && !attr.role) {
elem.attr('role', 'link');
}
if ($aria.config('bindKeypress')) {
Copy link
Member

Choose a reason for hiding this comment

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

To avoid unnecessary work, we should check for !attr.ngKeypress here (instead of inside the listener below).

@gkalpak
Copy link
Member

gkalpak commented Feb 16, 2016

Keep in mind the expected keyboard interaction for links is ENTER, not SPACE. That is more of a typical button interaction.

But native buttons do respond to SPACE too. So, our "custom button band-aid" should handle that as well, right ?

}
if ($aria.config('bindKeypress')) {
elem.on('keypress', function(event) {
var keyCode = event.which || event.keyCode;
Copy link
Contributor

Choose a reason for hiding this comment

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

This section looks an awful lot like https://github.com/LeeAdcock/angular.js/blob/ngclick-accessible-11053/src/ngAria/aria.js#L367 above. I wonder if we could combine those two sections somehow, perhaps with a reusable function?

@LeeAdcock LeeAdcock closed this by deleting the head repository Dec 3, 2022
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.

ngAria should make ng-click as accessible for anchor tags as it does for DIV/LI
5 participants