Skip to content

refactor(cdk/dialog): Consolidate afterNextRender calls #29237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 25 additions & 43 deletions src/cdk/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,61 +257,43 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
return;
}

const element = this._elementRef.nativeElement;
// If were to attempt to focus immediately, then the content of the dialog would not yet be
// ready in instances where change detection has to run first. To deal with this, we simply
// wait for the microtask queue to be empty when setting focus when autoFocus isn't set to
// dialog. If the element inside the dialog can't be focused, then the container is focused
// so the user can't tab into other elements behind it.
const autoFocus = this._config.autoFocus;
switch (autoFocus) {
case false:
case 'dialog':
// Ensure that focus is on the dialog container. It's possible that a different
// component tried to move focus while the open animation was running. See:
// https://github.com/angular/components/issues/16215. Note that we only want to do this
// if the focus isn't inside the dialog already, because it's possible that the consumer
// turned off `autoFocus` in order to move focus themselves.
afterNextRender(
() => {
// wait until after the next render.
afterNextRender(
() => {
const element = this._elementRef.nativeElement;
switch (this._config.autoFocus) {
case false:
case 'dialog':
// Ensure that focus is on the dialog container. It's possible that a different
// component tried to move focus while the open animation was running. See:
// https://github.com/angular/components/issues/16215. Note that we only want to do this
// if the focus isn't inside the dialog already, because it's possible that the consumer
// turned off `autoFocus` in order to move focus themselves.
if (!this._containsFocus()) {
element.focus();
}
},
{injector: this._injector},
);
break;
case true:
case 'first-tabbable':
afterNextRender(
() => {
break;
case true:
case 'first-tabbable':
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(
() => {
break;
case 'first-heading':
this._focusByCssSelector('h1, h2, h3, h4, h5, h6, [role="heading"]');
},
{injector: this._injector},
);
break;
default:
afterNextRender(
() => {
this._focusByCssSelector(autoFocus!);
},
{injector: this._injector},
);
break;
}
break;
default:
this._focusByCssSelector(this._config.autoFocus!);
break;
}
},
{injector: this._injector},
);
}

/** Restores focus to the element that was focused before the dialog opened. */
Expand Down
Loading