Skip to content

fix(cdk-experimental/dialog): don't move focus if it was moved during close animation #17320

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
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
18 changes: 16 additions & 2 deletions src/cdk-experimental/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export function throwDialogContentAlreadyAttachedError() {
},
})
export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
private _document: Document;

/** State of the dialog animation. */
_state: 'void' | 'enter' | 'exit' = 'enter';

Expand Down Expand Up @@ -120,11 +122,13 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
private _elementRef: ElementRef<HTMLElement>,
private _focusTrapFactory: FocusTrapFactory,
private _changeDetectorRef: ChangeDetectorRef,
@Optional() @Inject(DOCUMENT) private _document: any,
@Optional() @Inject(DOCUMENT) _document: any,
/** The dialog configuration. */
public _config: DialogConfig) {
super();

this._document = _document;

// We use a Subject with a distinctUntilChanged, rather than a callback attached to .done,
// because some browsers fire the done event twice and we don't want to emit duplicate events.
// See: https://github.com/angular/angular/issues/24084
Expand Down Expand Up @@ -248,7 +252,17 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
const toFocus = this._elementFocusedBeforeDialogWasOpened;
// We need the extra check, because IE can set the `activeElement` to null in some cases.
if (toFocus && typeof toFocus.focus === 'function') {
toFocus.focus();
const activeElement = this._document.activeElement;
const element = this._elementRef.nativeElement;

// Make sure that focus is still inside the dialog or is on the body (usually because a
// non-focusable element like the backdrop was clicked) before moving it. It's possible that
// the consumer moved it themselves before the animation was done, in which case we shouldn't
// do anything.
if (!activeElement || activeElement === this._document.body || activeElement === element ||
element.contains(activeElement)) {
toFocus.focus();
}
}
}
}
40 changes: 40 additions & 0 deletions src/cdk-experimental/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,46 @@ describe('Dialog', () => {
document.body.removeChild(button);
}));

it('should not move focus if it was moved outside the dialog while animating', fakeAsync(() => {
// Create a element that has focus before the dialog is opened.
const button = document.createElement('button');
const otherButton = document.createElement('button');
const body = document.body;
button.id = 'dialog-trigger';
otherButton.id = 'other-button';
body.appendChild(button);
body.appendChild(otherButton);
button.focus();

const dialogRef = dialog.openFromComponent(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

flushMicrotasks();
viewContainerFixture.detectChanges();
flushMicrotasks();

expect(document.activeElement!.id)
.not.toBe('dialog-trigger', 'Expected the focus to change when dialog was opened.');

// Start the closing sequence and move focus out of dialog.
dialogRef.close();
otherButton.focus();

expect(document.activeElement!.id)
.toBe('other-button', 'Expected focus to be on the alternate button.');

flushMicrotasks();
viewContainerFixture.detectChanges();
flush();

expect(document.activeElement!.id)
.toBe('other-button', 'Expected focus to stay on the alternate button.');

body.removeChild(button);
body.removeChild(otherButton);
}));

it('should allow the consumer to shift focus in afterClosed', fakeAsync(() => {
// Create a element that has focus before the dialog is opened.
let button = document.createElement('button');
Expand Down