Skip to content

fix(dialog): capture previously focused element immediately #3875

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 2 commits into from
Apr 21, 2017
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
22 changes: 20 additions & 2 deletions src/lib/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
Renderer,
ElementRef,
EventEmitter,
Inject,
Optional,
} from '@angular/core';
import {
animate,
Expand All @@ -15,6 +17,7 @@ import {
transition,
AnimationEvent,
} from '@angular/animations';
import {DOCUMENT} from '@angular/platform-browser';
import {BasePortalHost, ComponentPortal, PortalHostDirective, TemplatePortal} from '../core';
import {MdDialogConfig} from './dialog-config';
import {MdDialogContentAlreadyAttachedError} from './dialog-errors';
Expand Down Expand Up @@ -57,6 +60,9 @@ export class MdDialogContainer extends BasePortalHost {
/** Element that was focused before the dialog was opened. Save this to restore upon close. */
private _elementFocusedBeforeDialogWasOpened: HTMLElement = null;

/** Reference to the global document object. */
private _document: Document;

/** The dialog configuration. */
dialogConfig: MdDialogConfig;

Expand All @@ -69,9 +75,11 @@ export class MdDialogContainer extends BasePortalHost {
constructor(
private _renderer: Renderer,
private _elementRef: ElementRef,
private _focusTrapFactory: FocusTrapFactory) {
private _focusTrapFactory: FocusTrapFactory,
@Optional() @Inject(DOCUMENT) _document: any) {

super();
this._document = _document;
}

/**
Expand All @@ -83,6 +91,7 @@ export class MdDialogContainer extends BasePortalHost {
throw new MdDialogContentAlreadyAttachedError();
}

this._savePreviouslyFocusedElement();
return this._portalHost.attachComponentPortal(portal);
}

Expand All @@ -95,6 +104,7 @@ export class MdDialogContainer extends BasePortalHost {
throw new MdDialogContentAlreadyAttachedError();
}

this._savePreviouslyFocusedElement();
return this._portalHost.attachTemplatePortal(portal);
}

Expand All @@ -109,10 +119,18 @@ export class MdDialogContainer extends BasePortalHost {
// 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.
this._elementFocusedBeforeDialogWasOpened = document.activeElement as HTMLElement;
this._focusTrap.focusFirstTabbableElementWhenReady();
}

/**
* Saves a reference to the element that was focused before the dialog was opened.
*/
private _savePreviouslyFocusedElement() {
if (this._document) {
this._elementFocusedBeforeDialogWasOpened = this._document.activeElement as HTMLElement;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way to capture this in a unit test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was tricky to write a test that fails when we expect it to since we trigger the change detection and animations manually.


/**
* Callback, invoked whenever an animation on the host completes.
* @docs-private
Expand Down