Skip to content

Commit

Permalink
refactor(cdk/dialog): Remove use of focusInitialElementWhenReady
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba committed Mar 15, 2024
1 parent 5799c14 commit b875350
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/cdk/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
ViewChild,
ViewEncapsulation,
inject,
afterNextRender,
Injector,
} from '@angular/core';
import {DialogConfig} from './dialog-config';

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

protected readonly _changeDetectorRef = inject(ChangeDetectorRef);

private _injector = inject(Injector);

constructor(
protected _elementRef: ElementRef,
protected _focusTrapFactory: FocusTrapFactory,
Expand Down Expand Up @@ -266,13 +270,17 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
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':
this._focusByCssSelector('h1, h2, h3, h4, h5, h6, [role="heading"]');
Expand Down

0 comments on commit b875350

Please sign in to comment.