@@ -116,7 +116,7 @@ export class MatDialogContainer extends BasePortalOutlet {
116
116
throwMatDialogContentAlreadyAttachedError ( ) ;
117
117
}
118
118
119
- this . _savePreviouslyFocusedElement ( ) ;
119
+ this . _setupFocusTrap ( ) ;
120
120
return this . _portalOutlet . attachComponentPortal ( portal ) ;
121
121
}
122
122
@@ -129,7 +129,7 @@ export class MatDialogContainer extends BasePortalOutlet {
129
129
throwMatDialogContentAlreadyAttachedError ( ) ;
130
130
}
131
131
132
- this . _savePreviouslyFocusedElement ( ) ;
132
+ this . _setupFocusTrap ( ) ;
133
133
return this . _portalOutlet . attachTemplatePortal ( portal ) ;
134
134
}
135
135
@@ -144,14 +144,14 @@ export class MatDialogContainer extends BasePortalOutlet {
144
144
throwMatDialogContentAlreadyAttachedError ( ) ;
145
145
}
146
146
147
- this . _savePreviouslyFocusedElement ( ) ;
147
+ this . _setupFocusTrap ( ) ;
148
148
return this . _portalOutlet . attachDomPortal ( portal ) ;
149
149
}
150
150
151
151
/** Moves focus back into the dialog if it was moved out. */
152
152
_recaptureFocus ( ) {
153
153
if ( ! this . _containsFocus ( ) ) {
154
- const focusWasTrapped = this . _getFocusTrap ( ) . focusInitialElement ( ) ;
154
+ const focusWasTrapped = this . _focusTrap . focusInitialElement ( ) ;
155
155
156
156
if ( ! focusWasTrapped ) {
157
157
this . _elementRef . nativeElement . focus ( ) ;
@@ -165,7 +165,7 @@ export class MatDialogContainer extends BasePortalOutlet {
165
165
// ready in instances where change detection has to run first. To deal with this, we simply
166
166
// wait for the microtask queue to be empty.
167
167
if ( this . _config . autoFocus ) {
168
- this . _getFocusTrap ( ) . focusInitialElementWhenReady ( ) ;
168
+ this . _focusTrap . focusInitialElementWhenReady ( ) ;
169
169
} else if ( ! this . _containsFocus ( ) ) {
170
170
// Otherwise ensure that focus is on the dialog container. It's possible that a different
171
171
// component tried to move focus while the open animation was running. See:
@@ -200,8 +200,15 @@ export class MatDialogContainer extends BasePortalOutlet {
200
200
}
201
201
}
202
202
203
- /** Saves a reference to the element that was focused before the dialog was opened. */
204
- private _savePreviouslyFocusedElement ( ) {
203
+ /**
204
+ * Sets up the focus trand and saves a reference to the
205
+ * element that was focused before the dialog was opened.
206
+ */
207
+ private _setupFocusTrap ( ) {
208
+ if ( ! this . _focusTrap ) {
209
+ this . _focusTrap = this . _focusTrapFactory . create ( this . _elementRef . nativeElement ) ;
210
+ }
211
+
205
212
if ( this . _document ) {
206
213
this . _elementFocusedBeforeDialogWasOpened = this . _document . activeElement as HTMLElement ;
207
214
@@ -222,15 +229,6 @@ export class MatDialogContainer extends BasePortalOutlet {
222
229
return element === activeElement || element . contains ( activeElement ) ;
223
230
}
224
231
225
- /** Gets the focus trap associated with the dialog. */
226
- private _getFocusTrap ( ) {
227
- if ( ! this . _focusTrap ) {
228
- this . _focusTrap = this . _focusTrapFactory . create ( this . _elementRef . nativeElement ) ;
229
- }
230
-
231
- return this . _focusTrap ;
232
- }
233
-
234
232
/** Callback, invoked whenever an animation on the host completes. */
235
233
_onAnimationDone ( event : AnimationEvent ) {
236
234
if ( event . toState === 'enter' ) {
0 commit comments