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

fix(sidenav): trigger the focus handler for autofocus target too. #6101

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/components/sidenav/sidenav.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $animate,
])
.then(function() {
// Perform focus when animations are ALL done...
if (scope.isOpen) {
focusEl && focusEl.focus();
if (scope.isOpen && focusEl) {
focusEl.focus();
focusEl.triggerHandler('focus');
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why this is necessary. Invoking focus on an element should always trigger any event listeners to be called. @devversion can you explain?

Copy link
Member Author

Choose a reason for hiding this comment

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

The same question I asked myself too. But as I figured out, focus() can't be applied to all type of elements. And because the sidenav is a custom angular directive it looks like, they can't be focused through the focus() function. That's why I trigger that focus handlers manually, and its working perfect. In my other PR, which already got reviewed by Elad, I'm using the same strategy to get that running (see #5943).

Quote MDN

  • The HTMLElement.focus() method sets focus on the specified element, if it can be focused.

Copy link
Member

Choose a reason for hiding this comment

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

Custom elements simply need a tabindex in order to receive focus:
http://codepen.io/jelbourn/pen/MKYpJm?editors=101

Copy link
Member Author

Choose a reason for hiding this comment

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

All right, thanks for that codepen demonstration.

}
});
}
Expand Down