Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/cdk/menu/menu-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {_IdGenerator, FocusKeyManager, FocusOrigin} from '@angular/cdk/a11y';
import {_IdGenerator, FocusKeyManager, FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {Directionality} from '@angular/cdk/bidi';
import {
AfterContentInit,
Expand Down Expand Up @@ -43,7 +43,6 @@ import {PointerFocusTracker} from './pointer-focus-tracker';
'[id]': 'id',
'[attr.aria-orientation]': 'orientation',
'[attr.data-cdk-menu-stack-id]': 'menuStack.id',
'(focus)': 'focusFirstItem()',
'(focusin)': 'menuStack.setHasFocus(true)',
'(focusout)': 'menuStack.setHasFocus(false)',
},
Expand All @@ -52,6 +51,7 @@ export abstract class CdkMenuBase
extends CdkMenuGroup
implements Menu, AfterContentInit, OnDestroy
{
private _focusMonitor = inject(FocusMonitor);
protected ngZone = inject(NgZone);
private _renderer = inject(Renderer2);

Expand Down Expand Up @@ -108,13 +108,15 @@ export abstract class CdkMenuBase
this.menuStack.push(this);
}
this._setKeyManager();
this._handleFocus();
this._subscribeToMenuStackHasFocus();
this._subscribeToMenuOpen();
this._subscribeToMenuStackClosed();
this._setUpPointerTracker();
}

ngOnDestroy() {
this._focusMonitor.stopMonitoring(this.nativeElement);
this.keyManager?.destroy();
this.destroyed.next();
this.destroyed.complete();
Expand Down Expand Up @@ -231,4 +233,18 @@ export abstract class CdkMenuBase
this.menuAim.initialize(this, this.pointerTracker!);
}
}

/** Handles focus landing on the host element of the menu. */
private _handleFocus() {
this._focusMonitor
.monitor(this.nativeElement, false)
.pipe(takeUntil(this.destroyed))
.subscribe(origin => {
// Don't forward focus on mouse interactions, because it can
// mess with the user's scroll position. See #30130.
if (origin !== null && origin !== 'mouse') {
this.focusFirstItem(origin);
}
});
}
}
Loading