-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix(menu): keyboard controls not working if all items are disabled #16572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -320,13 +320,35 @@ export class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatMenuItem> | |
| * @param origin Action from which the focus originated. Used to set the correct styling. | ||
| */ | ||
| focusFirstItem(origin: FocusOrigin = 'program'): void { | ||
| const manager = this._keyManager; | ||
|
|
||
| // When the content is rendered lazily, it takes a bit before the items are inside the DOM. | ||
| if (this.lazyContent) { | ||
| this._ngZone.onStable.asObservable() | ||
| .pipe(take(1)) | ||
| .subscribe(() => this._keyManager.setFocusOrigin(origin).setFirstItemActive()); | ||
| .subscribe(() => manager.setFocusOrigin(origin).setFirstItemActive()); | ||
| } else { | ||
| this._keyManager.setFocusOrigin(origin).setFirstItemActive(); | ||
| manager.setFocusOrigin(origin).setFirstItemActive(); | ||
| } | ||
|
|
||
| // If there's no active item at this point, it means that all the items are disabled. | ||
| // Move focus to the menu panel so keyboard events like Escape still work. Also this will | ||
| // give _some_ feedback to screen readers. | ||
| if (!manager.activeItem && this._directDescendantItems.length) { | ||
| let element = this._directDescendantItems.first._getHostElement().parentElement; | ||
|
|
||
| // Because the `mat-menu` is at the DOM insertion point, not inside the overlay, we don't | ||
| // have a nice way of getting a hold of the menu panel. We can't use a `ViewChild` either | ||
| // because the panel is inside an `ng-template`. We work around it by starting from one of | ||
| // the items and walking up the DOM. | ||
| while (element) { | ||
| if (element.getAttribute('role') === 'menu') { | ||
| element.focus(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: it felt a little weird to have this logic here since the method is called |
||
| break; | ||
| } else { | ||
| element = element.parentElement; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ported this test over, because it got in through a very old PR that got in recently which was opened before the MDC menu existed.