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 : any ) {
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,6 +108,7 @@ 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
@@ -118,10 +124,19 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
118
124
// If were to attempt to focus immediately, then the content of the dialog would not yet be
119
125
// ready in instances where change detection has to run first. To deal with this, we simply
120
126
// wait for the microtask queue to be empty.
121
- this . _elementFocusedBeforeDialogWasOpened = document . activeElement as HTMLElement ;
122
127
this . _focusTrap . focusFirstTabbableElementWhenReady ( ) ;
123
128
}
124
129
130
+ /**
131
+ * Saves a reference to the element that was focused before the dialog was opened.
132
+ * @private
133
+ */
134
+ private _savePreviouslyFocusedElement ( ) {
135
+ if ( this . _document ) {
136
+ this . _elementFocusedBeforeDialogWasOpened = this . _document . activeElement as HTMLElement ;
137
+ }
138
+ }
139
+
125
140
/**
126
141
* Kicks off the leave animation.
127
142
* @docs -private
0 commit comments