MCPcopy Index your code
hub / github.com/angular/components / focusFirstItem

Method focusFirstItem

src/material/menu/menu.ts:397–419  ·  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

395 * @param origin Action from which the focus originated. Used to set the correct styling.
396 */
397 focusFirstItem(origin: FocusOrigin = 'program'): void {
398 // Wait for `afterNextRender` to ensure iOS VoiceOver screen reader focuses the first item (#24735).
399 this._firstItemFocusRef?.destroy();
400 this._firstItemFocusRef = afterNextRender(
401 () => {
402 const menuPanel = this._resolvePanel();
403
404 // If an item in the menuPanel is already focused, avoid overriding the focus.
405 if (!menuPanel || !menuPanel.contains(document.activeElement)) {
406 const manager = this._keyManager;
407 manager.setFocusOrigin(origin).setFirstItemActive();
408
409 // If there's no active item at this point, it means that all the items are disabled.
410 // Move focus to the menuPanel panel so keyboard events like Escape still work. Also this will
411 // give _some_ feedback to screen readers.
412 if (!manager.activeItem && menuPanel) {
413 menuPanel.focus();
414 }
415 }
416 },
417 {injector: this._injector},
418 );
419 }
420
421 /**
422 * 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