Skip to content

fix(dialog): focus recapturing not accounting for autoFocus option #19356

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
May 21, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/material/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ export class MatDialogContainer extends BasePortalOutlet {
/** Moves focus back into the dialog if it was moved out. */
_recaptureFocus() {
if (!this._containsFocus()) {
const focusWasTrapped = this._focusTrap.focusInitialElement();
const focusContainer = !this._config.autoFocus || !this._focusTrap.focusInitialElement();

if (!focusWasTrapped) {
if (focusContainer) {
this._elementRef.nativeElement.focus();
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/material/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,33 @@ describe('MatDialog', () => {
expect(document.activeElement).toBe(input, 'Expected input to stay focused after click');
}));

it('should recapture focus to the container when clicking on the backdrop with ' +
'autoFocus disabled', fakeAsync(() => {
dialog.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef,
autoFocus: false
});

viewContainerFixture.detectChanges();
flushMicrotasks();

let backdrop =
overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
let container =
overlayContainerElement.querySelector('.mat-dialog-container') as HTMLInputElement;

expect(document.activeElement).toBe(container, 'Expected container to be focused on open');

container.blur(); // Programmatic clicks might not move focus so we simulate it.
backdrop.click();
viewContainerFixture.detectChanges();
flush();

expect(document.activeElement)
.toBe(container, 'Expected container to stay focused after click');
}));

});

describe('hasBackdrop option', () => {
Expand Down