MCPcopy Create free account
hub / github.com/angular/components / focusFirstItem

Method focusFirstItem

src/material/menu/menu.ts:399–421  ·  view source on GitHub ↗

* Focus the first item in the menu. * @param origin Action from which the focus originated. Used to set the correct styling.

(origin: FocusOrigin = 'program')

Source from the content-addressed store, hash-verified

397 * @param origin Action from which the focus originated. Used to set the correct styling.
398 */
399 focusFirstItem(origin: FocusOrigin = 'program'): void {
400 // Wait for `afterNextRender` to ensure iOS VoiceOver screen reader focuses the first item (#24735).
401 this._firstItemFocusRef?.destroy();
402 this._firstItemFocusRef = afterNextRender(
403 () => {
404 const menuPanel = this._resolvePanel();
405
406 // If an item in the menuPanel is already focused, avoid overriding the focus.
407 if (!menuPanel || !menuPanel.contains(document.activeElement)) {
408 const manager = this._keyManager;
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 menuPanel panel so keyboard events like Escape still work. Also this will
413 // give _some_ feedback to screen readers.
414 if (!manager.activeItem && menuPanel) {
415 menuPanel.focus();
416 }
417 }
418 },
419 {injector: this._injector},
420 );
421 }
422
423 /**
424 * Resets the active item in the menu. This is used when the menu is opened, allowing

Callers

nothing calls this directly

Calls 5

_resolvePanelMethod · 0.95
setFirstItemActiveMethod · 0.80
setFocusOriginMethod · 0.80
destroyMethod · 0.65
focusMethod · 0.65

Tested by

no test coverage detected