File tree 3 files changed +45
-10
lines changed 3 files changed +45
-10
lines changed Original file line number Diff line number Diff line change @@ -179,6 +179,25 @@ describe('MatMenu', () => {
179
179
expect ( document . activeElement ) . not . toBe ( triggerEl ) ;
180
180
} ) ) ;
181
181
182
+ it ( 'should restore focus to the trigger immediately once the menu is closed' , ( ) => {
183
+ const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
184
+ fixture . detectChanges ( ) ;
185
+ const triggerEl = fixture . componentInstance . triggerEl . nativeElement ;
186
+
187
+ // A click without a mousedown before it is considered a keyboard open.
188
+ triggerEl . click ( ) ;
189
+ fixture . detectChanges ( ) ;
190
+
191
+ expect ( overlayContainerElement . querySelector ( '.mat-mdc-menu-panel' ) ) . toBeTruthy ( ) ;
192
+
193
+ fixture . componentInstance . trigger . closeMenu ( ) ;
194
+ fixture . detectChanges ( ) ;
195
+ // Note: don't add a `tick` here since we're testing
196
+ // that focus is restored before the animation is done.
197
+
198
+ expect ( document . activeElement ) . toBe ( triggerEl ) ;
199
+ } ) ;
200
+
182
201
it ( 'should be able to set a custom class on the backdrop' , fakeAsync ( ( ) => {
183
202
const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
184
203
Original file line number Diff line number Diff line change @@ -298,18 +298,20 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
298
298
. subscribe ( {
299
299
next : ( ) => menu . lazyContent ! . detach ( ) ,
300
300
// No matter whether the content got re-attached, reset the menu.
301
- complete : ( ) => this . _resetMenu ( )
301
+ complete : ( ) => this . _setIsMenuOpen ( false )
302
302
} ) ;
303
303
} else {
304
- this . _resetMenu ( ) ;
304
+ this . _setIsMenuOpen ( false ) ;
305
305
}
306
306
} else {
307
- this . _resetMenu ( ) ;
307
+ this . _setIsMenuOpen ( false ) ;
308
308
309
309
if ( menu . lazyContent ) {
310
310
menu . lazyContent . detach ( ) ;
311
311
}
312
312
}
313
+
314
+ this . _restoreFocus ( ) ;
313
315
}
314
316
315
317
/**
@@ -339,13 +341,8 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
339
341
}
340
342
}
341
343
342
- /**
343
- * This method resets the menu when it's closed, most importantly restoring
344
- * focus to the menu trigger if the menu was opened via the keyboard.
345
- */
346
- private _resetMenu ( ) : void {
347
- this . _setIsMenuOpen ( false ) ;
348
-
344
+ /** Restores focus to the element that was focused before the menu was open. */
345
+ private _restoreFocus ( ) {
349
346
// We should reset focus if the user is navigating using a keyboard or
350
347
// if we have a top-level trigger which might cause focus to be lost
351
348
// when clicking on the backdrop.
Original file line number Diff line number Diff line change @@ -181,6 +181,25 @@ describe('MatMenu', () => {
181
181
expect ( document . activeElement ) . not . toBe ( triggerEl ) ;
182
182
} ) ) ;
183
183
184
+ it ( 'should restore focus to the trigger immediately once the menu is closed' , ( ) => {
185
+ const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
186
+ fixture . detectChanges ( ) ;
187
+ const triggerEl = fixture . componentInstance . triggerEl . nativeElement ;
188
+
189
+ // A click without a mousedown before it is considered a keyboard open.
190
+ triggerEl . click ( ) ;
191
+ fixture . detectChanges ( ) ;
192
+
193
+ expect ( overlayContainerElement . querySelector ( '.mat-menu-panel' ) ) . toBeTruthy ( ) ;
194
+
195
+ fixture . componentInstance . trigger . closeMenu ( ) ;
196
+ fixture . detectChanges ( ) ;
197
+ // Note: don't add a `tick` here since we're testing
198
+ // that focus is restored before the animation is done.
199
+
200
+ expect ( document . activeElement ) . toBe ( triggerEl ) ;
201
+ } ) ;
202
+
184
203
it ( 'should be able to set a custom class on the backdrop' , fakeAsync ( ( ) => {
185
204
const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
186
205
You can’t perform that action at this time.
0 commit comments