Skip to content

Commit 153fcd3

Browse files
crisbetokara
authored andcommitted
fix(dialog): prevent error when restoring focus on IE (#2771)
Fixes #2760.
1 parent 60f03ed commit 153fcd3

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/lib/dialog/dialog-container.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ViewEncapsulation,
66
NgZone,
77
OnDestroy,
8+
Renderer,
89
} from '@angular/core';
910
import {BasePortalHost, ComponentPortal, PortalHostDirective, TemplatePortal} from '../core';
1011
import {MdDialogConfig} from './dialog-config';
@@ -46,7 +47,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
4647
/** Reference to the open dialog. */
4748
dialogRef: MdDialogRef<any>;
4849

49-
constructor(private _ngZone: NgZone) {
50+
constructor(private _ngZone: NgZone, private _renderer: Renderer) {
5051
super();
5152
}
5253

@@ -90,9 +91,12 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
9091
ngOnDestroy() {
9192
// When the dialog is destroyed, return focus to the element that originally had it before
9293
// the dialog was opened. Wait for the DOM to finish settling before changing the focus so
93-
// that it doesn't end up back on the <body>.
94-
this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
95-
(this._elementFocusedBeforeDialogWasOpened as HTMLElement).focus();
96-
});
94+
// that it doesn't end up back on the <body>. Also note that we need the extra check, because
95+
// IE can set the `activeElement` to null in some cases.
96+
if (this._elementFocusedBeforeDialogWasOpened) {
97+
this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
98+
this._renderer.invokeElementMethod(this._elementFocusedBeforeDialogWasOpened, 'focus');
99+
});
100+
}
97101
}
98102
}

0 commit comments

Comments
 (0)