Skip to content

Commit 6afa61f

Browse files
committed
delay 1 more macrotask
1 parent a4e15fa commit 6afa61f

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/cdk/dialog/dialog-container.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,21 +270,25 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
270270
break;
271271
case true:
272272
case 'first-tabbable':
273-
let timeoutRef: {};
274273
// TODO(mmalerba): Once hybrid mode is enabled in g3, we can change this to just
275-
// `afterNextRender`.
274+
// `afterNextRender(() => setTimeout(() =>`.
276275
new Promise<void>(r => {
277276
afterNextRender(r, {injector: this._injector});
278-
timeoutRef = setTimeout(r);
279-
}).then(() => {
280-
clearTimeout(timeoutRef as number);
281-
const focusedSuccessfully = this._focusTrap?.focusInitialElement();
282-
// If we weren't able to find a focusable element in the dialog, then focus the dialog
283-
// container instead.
284-
if (!focusedSuccessfully) {
285-
this._focusDialogContainer();
286-
}
287-
});
277+
setTimeout(r);
278+
}).then(() =>
279+
this._ngZone.runOutsideAngular(() =>
280+
// Wait for one more setTimeout in case there are subsequent microtask change detection
281+
// cycles (e.g. from ngModel).
282+
setTimeout(() => {
283+
const focusedSuccessfully = this._focusTrap?.focusInitialElement();
284+
// If we weren't able to find a focusable element in the dialog, then focus the
285+
// dialog container instead.
286+
if (!focusedSuccessfully) {
287+
this._focusDialogContainer();
288+
}
289+
}),
290+
),
291+
);
288292
break;
289293
case 'first-heading':
290294
this._focusByCssSelector('h1, h2, h3, h4, h5, h6, [role="heading"]');

0 commit comments

Comments
 (0)