* Focus the first item in the menu. * @param origin Action from which the focus originated. Used to set the correct styling.
(origin: FocusOrigin = 'program')
| 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 |
nothing calls this directly
no test coverage detected