Skip to content

Commit 6af8a35

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

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,6 @@ describe('MatBottomSheet', () => {
725725

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

728-
flush();
729728
viewContainerFixture.detectChanges();
730729
flush();
731730
viewContainerFixture.detectChanges();
@@ -763,7 +762,6 @@ describe('MatBottomSheet', () => {
763762
restoreFocus: false,
764763
});
765764

766-
flush();
767765
viewContainerFixture.detectChanges();
768766
flush();
769767
viewContainerFixture.detectChanges();
@@ -804,7 +802,6 @@ describe('MatBottomSheet', () => {
804802

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

807-
flush();
808805
viewContainerFixture.detectChanges();
809806
flush();
810807
viewContainerFixture.detectChanges();
@@ -822,9 +819,9 @@ describe('MatBottomSheet', () => {
822819
.withContext('Expected focus to be on the alternate button.')
823820
.toBe('other-button');
824821

825-
flushMicrotasks();
826822
viewContainerFixture.detectChanges();
827823
flush();
824+
viewContainerFixture.detectChanges();
828825

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

0 commit comments

Comments
 (0)