@@ -43,6 +43,7 @@ import {throwMatMenuMissingError} from './menu-errors';
4343import { MatMenuItem } from './menu-item' ;
4444import { MatMenuPanel } from './menu-panel' ;
4545import { MenuPositionX , MenuPositionY } from './menu-positions' ;
46+ import { FocusMonitor , FocusOrigin } from '@angular/cdk/a11y' ;
4647
4748/** Injection token that determines the scroll handling while the menu is open. */
4849export const MAT_MENU_SCROLL_STRATEGY =
@@ -130,7 +131,9 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
130131 @Inject ( MAT_MENU_SCROLL_STRATEGY ) private _scrollStrategy ,
131132 @Optional ( ) private _parentMenu : MatMenu ,
132133 @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 ) {
134137
135138 if ( _menuItemInstance ) {
136139 _menuItemInstance . _triggersSubmenu = this . triggersSubmenu ( ) ;
@@ -207,9 +210,16 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
207210 this . menu . close . emit ( ) ;
208211 }
209212
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+ }
213223 }
214224
215225 /** Closes the menu and does the necessary cleanup. */
@@ -274,8 +284,12 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
274284 // We should reset focus if the user is navigating using a keyboard or
275285 // if we have a top-level trigger which might cause focus to be lost
276286 // 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.
278290 this . focus ( ) ;
291+ } else if ( ! this . triggersSubmenu ( ) ) {
292+ this . focus ( 'mouse' ) ;
279293 }
280294
281295 this . _openedByMouse = false ;
0 commit comments