From 6dd1689b519abf287098d30f7698fc37197e3db0 Mon Sep 17 00:00:00 2001 From: Miles Malerba Date: Mon, 10 Jun 2024 09:08:55 -0700 Subject: [PATCH] fix(material/dialog): Make focus behavior consistent across zoneful/zoneless apps (#29192) (cherry picked from commit 3b32d0e7c95b358d30f8b7e6b0570ab8ba815a06) --- src/cdk/dialog/dialog-container.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/cdk/dialog/dialog-container.ts b/src/cdk/dialog/dialog-container.ts index aeb1c9a742ba..ab77ef8e9554 100644 --- a/src/cdk/dialog/dialog-container.ts +++ b/src/cdk/dialog/dialog-container.ts @@ -283,13 +283,17 @@ export class CdkDialogContainer break; case true: case 'first-tabbable': - this._focusTrap?.focusInitialElementWhenReady().then(focusedSuccessfully => { - // If we weren't able to find a focusable element in the dialog, then focus the dialog - // container instead. - if (!focusedSuccessfully) { - this._focusDialogContainer(); - } - }); + afterNextRender( + () => { + const focusedSuccessfully = this._focusTrap?.focusInitialElement(); + // If we weren't able to find a focusable element in the dialog, then focus the dialog + // container instead. + if (!focusedSuccessfully) { + this._focusDialogContainer(); + } + }, + {injector: this._injector}, + ); break; case 'first-heading': afterNextRender(