File tree Expand file tree Collapse file tree 3 files changed +45
-3
lines changed Expand file tree Collapse file tree 3 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -188,6 +188,28 @@ describe('MDC-based MatMenu', () => {
188
188
expect ( document . activeElement ) . not . toBe ( triggerEl ) ;
189
189
} ) ) ;
190
190
191
+ it ( 'should be able to move focus in the closed event' , fakeAsync ( ( ) => {
192
+ const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
193
+ const instance = fixture . componentInstance ;
194
+ fixture . detectChanges ( ) ;
195
+ const triggerEl = instance . triggerEl . nativeElement ;
196
+ const button = document . createElement ( 'button' ) ;
197
+ button . setAttribute ( 'tabindex' , '0' ) ;
198
+ document . body . appendChild ( button ) ;
199
+
200
+ triggerEl . click ( ) ;
201
+ fixture . detectChanges ( ) ;
202
+
203
+ const subscription = instance . trigger . menuClosed . subscribe ( ( ) => button . focus ( ) ) ;
204
+ instance . trigger . closeMenu ( ) ;
205
+ fixture . detectChanges ( ) ;
206
+ tick ( 500 ) ;
207
+
208
+ expect ( document . activeElement ) . toBe ( button ) ;
209
+ document . body . removeChild ( button ) ;
210
+ subscription . unsubscribe ( ) ;
211
+ } ) ) ;
212
+
191
213
it ( 'should restore focus to the trigger immediately once the menu is closed' , ( ) => {
192
214
const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
193
215
fixture . detectChanges ( ) ;
Original file line number Diff line number Diff line change @@ -277,9 +277,9 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
277
277
}
278
278
279
279
const menu = this . menu ;
280
-
281
280
this . _closingActionsSubscription . unsubscribe ( ) ;
282
281
this . _overlayRef . detach ( ) ;
282
+ this . _restoreFocus ( ) ;
283
283
284
284
if ( menu instanceof MatMenu ) {
285
285
menu . _resetAnimation ( ) ;
@@ -308,8 +308,6 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
308
308
menu . lazyContent . detach ( ) ;
309
309
}
310
310
}
311
-
312
- this . _restoreFocus ( ) ;
313
311
}
314
312
315
313
/**
Original file line number Diff line number Diff line change @@ -190,6 +190,28 @@ describe('MatMenu', () => {
190
190
expect ( document . activeElement ) . not . toBe ( triggerEl ) ;
191
191
} ) ) ;
192
192
193
+ it ( 'should be able to move focus in the closed event' , fakeAsync ( ( ) => {
194
+ const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
195
+ const instance = fixture . componentInstance ;
196
+ fixture . detectChanges ( ) ;
197
+ const triggerEl = instance . triggerEl . nativeElement ;
198
+ const button = document . createElement ( 'button' ) ;
199
+ button . setAttribute ( 'tabindex' , '0' ) ;
200
+ document . body . appendChild ( button ) ;
201
+
202
+ triggerEl . click ( ) ;
203
+ fixture . detectChanges ( ) ;
204
+
205
+ const subscription = instance . trigger . menuClosed . subscribe ( ( ) => button . focus ( ) ) ;
206
+ instance . trigger . closeMenu ( ) ;
207
+ fixture . detectChanges ( ) ;
208
+ tick ( 500 ) ;
209
+
210
+ expect ( document . activeElement ) . toBe ( button ) ;
211
+ document . body . removeChild ( button ) ;
212
+ subscription . unsubscribe ( ) ;
213
+ } ) ) ;
214
+
193
215
it ( 'should restore focus to the trigger immediately once the menu is closed' , ( ) => {
194
216
const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
195
217
fixture . detectChanges ( ) ;
You can’t perform that action at this time.
0 commit comments