@@ -43,6 +43,7 @@ import {throwMatMenuMissingError} from './menu-errors';
43
43
import { MatMenuItem } from './menu-item' ;
44
44
import { MatMenuPanel } from './menu-panel' ;
45
45
import { MenuPositionX , MenuPositionY } from './menu-positions' ;
46
+ import { FocusMonitor , FocusOrigin } from '@angular/cdk/a11y' ;
46
47
47
48
/** Injection token that determines the scroll handling while the menu is open. */
48
49
export const MAT_MENU_SCROLL_STRATEGY =
@@ -130,7 +131,9 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
130
131
@Inject ( MAT_MENU_SCROLL_STRATEGY ) private _scrollStrategy ,
131
132
@Optional ( ) private _parentMenu : MatMenu ,
132
133
@Optional ( ) @Self ( ) private _menuItemInstance : MatMenuItem ,
133
- @Optional ( ) private _dir : Directionality ) {
134
+ @Optional ( ) private _dir : Directionality ,
135
+ // TODO(crisbeto): make the _focusMonitor required when doing breaking changes.
136
+ private _focusMonitor ?: FocusMonitor ) {
134
137
135
138
if ( _menuItemInstance ) {
136
139
_menuItemInstance . _triggersSubmenu = this . triggersSubmenu ( ) ;
@@ -207,9 +210,16 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
207
210
this . menu . close . emit ( ) ;
208
211
}
209
212
210
- /** Focuses the menu trigger. */
211
- focus ( ) {
212
- this . _element . nativeElement . focus ( ) ;
213
+ /**
214
+ * Focuses the menu trigger.
215
+ * @param origin Source of the menu trigger's focus.
216
+ */
217
+ focus ( origin : FocusOrigin = 'program' ) {
218
+ if ( this . _focusMonitor ) {
219
+ this . _focusMonitor . focusVia ( this . _element . nativeElement , origin ) ;
220
+ } else {
221
+ this . _element . nativeElement . focus ( ) ;
222
+ }
213
223
}
214
224
215
225
/** Closes the menu and does the necessary cleanup. */
@@ -274,8 +284,12 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
274
284
// We should reset focus if the user is navigating using a keyboard or
275
285
// if we have a top-level trigger which might cause focus to be lost
276
286
// when clicking on the backdrop.
277
- if ( ! this . _openedByMouse || ! this . triggersSubmenu ( ) ) {
287
+ if ( ! this . _openedByMouse ) {
288
+ // Note that the focus style will show up both for `program` and
289
+ // `keyboard` so we don't have to specify which one it is.
278
290
this . focus ( ) ;
291
+ } else if ( ! this . triggersSubmenu ( ) ) {
292
+ this . focus ( 'mouse' ) ;
279
293
}
280
294
281
295
this . _openedByMouse = false ;
0 commit comments