Skip to content

Commit 0396c73

Browse files
committed
fix(material/menu): focus the first item when opening menu on iOS VoiceOver
When opening the menu using the iOS VoiceOver screen reader, focus the first item in the menu. Previously, the first menu item would focus on other screen readers like desktop VoiceOver but not with iOS VoiceOver. Waiting until `onStable` seems to fix this. Fixes #24735
1 parent 9bc596f commit 0396c73

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

src/material-experimental/mdc-menu/menu.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ describe('MDC-based MatMenu', () => {
11511151
fixture.detectChanges();
11521152

11531153
fixture.componentInstance.trigger.menuOpened.subscribe(() => {
1154+
flush();
11541155
(document.querySelectorAll('.mat-mdc-menu-panel [mat-menu-item]')[3] as HTMLElement).focus();
11551156
});
11561157
fixture.componentInstance.trigger.openMenu();

src/material/menu/menu.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,7 @@ describe('MatMenu', () => {
11471147
fixture.detectChanges();
11481148

11491149
fixture.componentInstance.trigger.menuOpened.subscribe(() => {
1150+
flush();
11501151
(document.querySelectorAll('.mat-menu-panel [mat-menu-item]')[3] as HTMLElement).focus();
11511152
});
11521153
fixture.componentInstance.trigger.openMenu();

src/material/menu/menu.ts

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -391,42 +391,32 @@ export class _MatMenuBase
391391
* @param origin Action from which the focus originated. Used to set the correct styling.
392392
*/
393393
focusFirstItem(origin: FocusOrigin = 'program'): void {
394-
// When the content is rendered lazily, it takes a bit before the items are inside the DOM.
395-
if (this.lazyContent) {
396-
this._ngZone.onStable.pipe(take(1)).subscribe(() => this._focusFirstItem(origin));
397-
} else {
398-
this._focusFirstItem(origin);
399-
}
400-
}
401-
402-
/**
403-
* Actual implementation that focuses the first item. Needs to be separated
404-
* out so we don't repeat the same logic in the public `focusFirstItem` method.
405-
*/
406-
private _focusFirstItem(origin: FocusOrigin) {
407-
const manager = this._keyManager;
394+
// Wait for `onStable` to ensure iOS VoiceOver screen reader focuses the first item (#24735).
395+
this._ngZone.onStable.pipe(take(1)).subscribe(() => {
396+
const manager = this._keyManager;
408397

409-
manager.setFocusOrigin(origin).setFirstItemActive();
410-
411-
// If there's no active item at this point, it means that all the items are disabled.
412-
// Move focus to the menu panel so keyboard events like Escape still work. Also this will
413-
// give _some_ feedback to screen readers.
414-
if (!manager.activeItem && this._directDescendantItems.length) {
415-
let element = this._directDescendantItems.first!._getHostElement().parentElement;
416-
417-
// Because the `mat-menu` is at the DOM insertion point, not inside the overlay, we don't
418-
// have a nice way of getting a hold of the menu panel. We can't use a `ViewChild` either
419-
// because the panel is inside an `ng-template`. We work around it by starting from one of
420-
// the items and walking up the DOM.
421-
while (element) {
422-
if (element.getAttribute('role') === 'menu') {
423-
element.focus();
424-
break;
425-
} else {
426-
element = element.parentElement;
398+
manager.setFocusOrigin(origin).setFirstItemActive();
399+
400+
// If there's no active item at this point, it means that all the items are disabled.
401+
// Move focus to the menu panel so keyboard events like Escape still work. Also this will
402+
// give _some_ feedback to screen readers.
403+
if (!manager.activeItem && this._directDescendantItems.length) {
404+
let element = this._directDescendantItems.first!._getHostElement().parentElement;
405+
406+
// Because the `mat-menu` is at the DOM insertion point, not inside the overlay, we don't
407+
// have a nice way of getting a hold of the menu panel. We can't use a `ViewChild` either
408+
// because the panel is inside an `ng-template`. We work around it by starting from one of
409+
// the items and walking up the DOM.
410+
while (element) {
411+
if (element.getAttribute('role') === 'menu') {
412+
element.focus();
413+
break;
414+
} else {
415+
element = element.parentElement;
416+
}
427417
}
428418
}
429-
}
419+
});
430420
}
431421

432422
/**

0 commit comments

Comments
 (0)