Skip to content

Commit b875350

Browse files
committed
refactor(cdk/dialog): Remove use of focusInitialElementWhenReady
1 parent 5799c14 commit b875350

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/cdk/dialog/dialog-container.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import {
3737
ViewChild,
3838
ViewEncapsulation,
3939
inject,
40+
afterNextRender,
41+
Injector,
4042
} from '@angular/core';
4143
import {DialogConfig} from './dialog-config';
4244

@@ -102,6 +104,8 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
102104

103105
protected readonly _changeDetectorRef = inject(ChangeDetectorRef);
104106

107+
private _injector = inject(Injector);
108+
105109
constructor(
106110
protected _elementRef: ElementRef,
107111
protected _focusTrapFactory: FocusTrapFactory,
@@ -266,13 +270,17 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
266270
break;
267271
case true:
268272
case 'first-tabbable':
269-
this._focusTrap?.focusInitialElementWhenReady().then(focusedSuccessfully => {
270-
// If we weren't able to find a focusable element in the dialog, then focus the dialog
271-
// container instead.
272-
if (!focusedSuccessfully) {
273-
this._focusDialogContainer();
274-
}
275-
});
273+
afterNextRender(
274+
() => {
275+
const focusedSuccessfully = this._focusTrap?.focusInitialElement();
276+
// If we weren't able to find a focusable element in the dialog, then focus the dialog
277+
// container instead.
278+
if (!focusedSuccessfully) {
279+
this._focusDialogContainer();
280+
}
281+
},
282+
{injector: this._injector},
283+
);
276284
break;
277285
case 'first-heading':
278286
this._focusByCssSelector('h1, h2, h3, h4, h5, h6, [role="heading"]');

0 commit comments

Comments
 (0)