fix(dialog): prevent error when restoring focus on IE (#2771)

Fixes #2760.
This commit is contained in:
Kristiyan Kostadinov
2017-02-01 04:12:56 +01:00
committed by Kara
parent 60f03ed7dd
commit 153fcd3667
+9 -5
View File
@@ -5,6 +5,7 @@ import {
ViewEncapsulation,
NgZone,
OnDestroy,
Renderer,
} from '@angular/core';
import {BasePortalHost, ComponentPortal, PortalHostDirective, TemplatePortal} from '../core';
import {MdDialogConfig} from './dialog-config';
@@ -46,7 +47,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
/** Reference to the open dialog. */
dialogRef: MdDialogRef<any>;
constructor(private _ngZone: NgZone) {
constructor(private _ngZone: NgZone, private _renderer: Renderer) {
super();
}
@@ -90,9 +91,12 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
ngOnDestroy() {
// When the dialog is destroyed, return focus to the element that originally had it before
// the dialog was opened. Wait for the DOM to finish settling before changing the focus so
// that it doesn't end up back on the <body>.
this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
(this._elementFocusedBeforeDialogWasOpened as HTMLElement).focus();
});
// that it doesn't end up back on the <body>. Also note that we need the extra check, because
// IE can set the `activeElement` to null in some cases.
if (this._elementFocusedBeforeDialogWasOpened) {
this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
this._renderer.invokeElementMethod(this._elementFocusedBeforeDialogWasOpened, 'focus');
});
}
}
}