Skip to content

Commit 29968b8

Browse files
crisbetokara
authored andcommitted
fix(dialog): capture previously focused element immediately (#3875)
1 parent 355f8b7 commit 29968b8

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/lib/dialog/dialog-container.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
Renderer,
77
ElementRef,
88
EventEmitter,
9+
Inject,
10+
Optional,
911
} from '@angular/core';
1012
import {
1113
animate,
@@ -15,6 +17,7 @@ import {
1517
transition,
1618
AnimationEvent,
1719
} from '@angular/animations';
20+
import {DOCUMENT} from '@angular/platform-browser';
1821
import {BasePortalHost, ComponentPortal, PortalHostDirective, TemplatePortal} from '../core';
1922
import {MdDialogConfig} from './dialog-config';
2023
import {MdDialogContentAlreadyAttachedError} from './dialog-errors';
@@ -57,6 +60,9 @@ export class MdDialogContainer extends BasePortalHost {
5760
/** Element that was focused before the dialog was opened. Save this to restore upon close. */
5861
private _elementFocusedBeforeDialogWasOpened: HTMLElement = null;
5962

63+
/** Reference to the global document object. */
64+
private _document: Document;
65+
6066
/** The dialog configuration. */
6167
dialogConfig: MdDialogConfig;
6268

@@ -69,9 +75,11 @@ export class MdDialogContainer extends BasePortalHost {
6975
constructor(
7076
private _renderer: Renderer,
7177
private _elementRef: ElementRef,
72-
private _focusTrapFactory: FocusTrapFactory) {
78+
private _focusTrapFactory: FocusTrapFactory,
79+
@Optional() @Inject(DOCUMENT) _document: any) {
7380

7481
super();
82+
this._document = _document;
7583
}
7684

7785
/**
@@ -83,6 +91,7 @@ export class MdDialogContainer extends BasePortalHost {
8391
throw new MdDialogContentAlreadyAttachedError();
8492
}
8593

94+
this._savePreviouslyFocusedElement();
8695
return this._portalHost.attachComponentPortal(portal);
8796
}
8897

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

107+
this._savePreviouslyFocusedElement();
98108
return this._portalHost.attachTemplatePortal(portal);
99109
}
100110

@@ -109,10 +119,18 @@ export class MdDialogContainer extends BasePortalHost {
109119
// If were to attempt to focus immediately, then the content of the dialog would not yet be
110120
// ready in instances where change detection has to run first. To deal with this, we simply
111121
// wait for the microtask queue to be empty.
112-
this._elementFocusedBeforeDialogWasOpened = document.activeElement as HTMLElement;
113122
this._focusTrap.focusFirstTabbableElementWhenReady();
114123
}
115124

125+
/**
126+
* Saves a reference to the element that was focused before the dialog was opened.
127+
*/
128+
private _savePreviouslyFocusedElement() {
129+
if (this._document) {
130+
this._elementFocusedBeforeDialogWasOpened = this._document.activeElement as HTMLElement;
131+
}
132+
}
133+
116134
/**
117135
* Callback, invoked whenever an animation on the host completes.
118136
* @docs-private

0 commit comments

Comments
 (0)