8
8
Renderer ,
9
9
ElementRef ,
10
10
EventEmitter ,
11
+ Inject ,
12
+ Optional ,
11
13
} from '@angular/core' ;
12
14
import {
13
15
animate ,
@@ -17,6 +19,7 @@ import {
17
19
transition ,
18
20
AnimationEvent ,
19
21
} from '@angular/animations' ;
22
+ import { DOCUMENT } from '@angular/platform-browser' ;
20
23
import { BasePortalHost , ComponentPortal , PortalHostDirective , TemplatePortal } from '../core' ;
21
24
import { MdDialogConfig } from './dialog-config' ;
22
25
import { MdDialogContentAlreadyAttachedError } from './dialog-errors' ;
@@ -77,7 +80,8 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
77
80
private _ngZone : NgZone ,
78
81
private _renderer : Renderer ,
79
82
private _elementRef : ElementRef ,
80
- private _focusTrapFactory : FocusTrapFactory ) {
83
+ private _focusTrapFactory : FocusTrapFactory ,
84
+ @Optional ( ) @Inject ( DOCUMENT ) private _document : Document | Document ) {
81
85
82
86
super ( ) ;
83
87
}
@@ -91,6 +95,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
91
95
throw new MdDialogContentAlreadyAttachedError ( ) ;
92
96
}
93
97
98
+ this . _savePreviouslyFocusedElement ( ) ;
94
99
return this . _portalHost . attachComponentPortal ( portal ) ;
95
100
}
96
101
@@ -103,12 +108,12 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
103
108
throw new MdDialogContentAlreadyAttachedError ( ) ;
104
109
}
105
110
111
+ this . _savePreviouslyFocusedElement ( ) ;
106
112
return this . _portalHost . attachTemplatePortal ( portal ) ;
107
113
}
108
114
109
115
/**
110
116
* Moves the focus inside the focus trap.
111
- * @private
112
117
*/
113
118
private _trapFocus ( ) {
114
119
if ( ! this . _focusTrap ) {
@@ -118,10 +123,18 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
118
123
// If were to attempt to focus immediately, then the content of the dialog would not yet be
119
124
// ready in instances where change detection has to run first. To deal with this, we simply
120
125
// wait for the microtask queue to be empty.
121
- this . _elementFocusedBeforeDialogWasOpened = document . activeElement as HTMLElement ;
122
126
this . _focusTrap . focusFirstTabbableElementWhenReady ( ) ;
123
127
}
124
128
129
+ /**
130
+ * Saves a reference to the element that was focused before the dialog was opened.
131
+ */
132
+ private _savePreviouslyFocusedElement ( ) {
133
+ if ( this . _document ) {
134
+ this . _elementFocusedBeforeDialogWasOpened = this . _document . activeElement as HTMLElement ;
135
+ }
136
+ }
137
+
125
138
/**
126
139
* Kicks off the leave animation.
127
140
* @docs -private
0 commit comments