Skip to content

Commit fc9949a

Browse files
committed
fix(material/dialog): Make focus behavior consistent across zoneful/zoneless apps
1 parent e731203 commit fc9949a

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/cdk/dialog/dialog-container.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,17 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
283283
break;
284284
case true:
285285
case 'first-tabbable':
286-
this._focusTrap?.focusInitialElementWhenReady().then(focusedSuccessfully => {
287-
// If we weren't able to find a focusable element in the dialog, then focus the dialog
288-
// container instead.
289-
if (!focusedSuccessfully) {
290-
this._focusDialogContainer();
291-
}
292-
});
286+
afterNextRender(
287+
() => {
288+
const focusedSuccessfully = this._focusTrap?.focusInitialElement();
289+
// If we weren't able to find a focusable element in the dialog, then focus the dialog
290+
// container instead.
291+
if (!focusedSuccessfully) {
292+
this._focusDialogContainer();
293+
}
294+
},
295+
{injector: this._injector},
296+
);
293297
break;
294298
case 'first-heading':
295299
afterNextRender(

src/material/bottom-sheet/bottom-sheet.spec.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,8 @@ describe('MatBottomSheet', () => {
609609
});
610610

611611
viewContainerFixture.detectChanges();
612-
flushMicrotasks();
612+
flush();
613+
viewContainerFixture.detectChanges();
613614

614615
expect(document.activeElement!.tagName)
615616
.withContext('Expected bottom sheet container to be focused.')
@@ -658,7 +659,8 @@ describe('MatBottomSheet', () => {
658659
});
659660

660661
viewContainerFixture.detectChanges();
661-
flushMicrotasks();
662+
flush();
663+
viewContainerFixture.detectChanges();
662664

663665
let container = overlayContainerElement.querySelector(
664666
'.mat-bottom-sheet-container',
@@ -679,7 +681,8 @@ describe('MatBottomSheet', () => {
679681
});
680682

681683
viewContainerFixture.detectChanges();
682-
flushMicrotasks();
684+
flush();
685+
viewContainerFixture.detectChanges();
683686

684687
let firstHeader = overlayContainerElement.querySelector(
685688
'h1[tabindex="-1"]',
@@ -701,7 +704,8 @@ describe('MatBottomSheet', () => {
701704
});
702705

703706
viewContainerFixture.detectChanges();
704-
flushMicrotasks();
707+
flush();
708+
viewContainerFixture.detectChanges();
705709

706710
let firstParagraph = overlayContainerElement.querySelector(
707711
'p[tabindex="-1"]',
@@ -721,9 +725,9 @@ describe('MatBottomSheet', () => {
721725

722726
const bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
723727

724-
flushMicrotasks();
725728
viewContainerFixture.detectChanges();
726-
flushMicrotasks();
729+
flush();
730+
viewContainerFixture.detectChanges();
727731

728732
expect(document.activeElement!.id).not.toBe(
729733
'bottom-sheet-trigger',
@@ -758,9 +762,9 @@ describe('MatBottomSheet', () => {
758762
restoreFocus: false,
759763
});
760764

761-
flushMicrotasks();
762765
viewContainerFixture.detectChanges();
763-
flushMicrotasks();
766+
flush();
767+
viewContainerFixture.detectChanges();
764768

765769
expect(document.activeElement!.id).not.toBe(
766770
'bottom-sheet-trigger',
@@ -798,9 +802,9 @@ describe('MatBottomSheet', () => {
798802

799803
const bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
800804

801-
flushMicrotasks();
802805
viewContainerFixture.detectChanges();
803-
flushMicrotasks();
806+
flush();
807+
viewContainerFixture.detectChanges();
804808

805809
expect(document.activeElement!.id).not.toBe(
806810
'bottom-sheet-trigger',
@@ -815,9 +819,9 @@ describe('MatBottomSheet', () => {
815819
.withContext('Expected focus to be on the alternate button.')
816820
.toBe('other-button');
817821

818-
flushMicrotasks();
819822
viewContainerFixture.detectChanges();
820823
flush();
824+
viewContainerFixture.detectChanges();
821825

822826
expect(document.activeElement!.id)
823827
.withContext('Expected focus to stay on the alternate button.')

0 commit comments

Comments
 (0)