@@ -29,7 +29,7 @@ export class FocusTrap {
29
29
this . _enabled = val ;
30
30
this . _startAnchor . tabIndex = this . _endAnchor . tabIndex = this . _enabled ? 0 : - 1 ;
31
31
}
32
- private _enabled : boolean = false ;
32
+ private _enabled : boolean = true ;
33
33
34
34
constructor (
35
35
private _element : HTMLElement ,
@@ -42,9 +42,7 @@ export class FocusTrap {
42
42
}
43
43
}
44
44
45
- /**
46
- * Destroys the focus trap by cleaning up the anchors.
47
- */
45
+ /** Destroys the focus trap by cleaning up the anchors. */
48
46
destroy ( ) {
49
47
if ( this . _startAnchor . parentNode ) {
50
48
this . _startAnchor . parentNode . removeChild ( this . _startAnchor ) ;
@@ -74,24 +72,22 @@ export class FocusTrap {
74
72
}
75
73
76
74
/**
77
- * Waits for microtask queue to empty, then focuses the first tabbable element within the focus
78
- * trap region.
75
+ * Waits for microtask queue to empty, then focuses
76
+ * the first tabbable element within the focus trap region.
79
77
*/
80
78
focusFirstTabbableElementWhenReady ( ) {
81
79
this . _ngZone . onMicrotaskEmpty . first ( ) . subscribe ( ( ) => this . focusFirstTabbableElement ( ) ) ;
82
80
}
83
81
84
82
/**
85
- * Waits for microtask queue to empty, then focuses the last tabbable element within the focus
86
- * trap region.
83
+ * Waits for microtask queue to empty, then focuses
84
+ * the last tabbable element within the focus trap region.
87
85
*/
88
86
focusLastTabbableElementWhenReady ( ) {
89
87
this . _ngZone . onMicrotaskEmpty . first ( ) . subscribe ( ( ) => this . focusLastTabbableElement ( ) ) ;
90
88
}
91
89
92
- /**
93
- * Focuses the first tabbable element within the focus trap region.
94
- */
90
+ /** Focuses the first tabbable element within the focus trap region. */
95
91
focusFirstTabbableElement ( ) {
96
92
let redirectToElement = this . _element . querySelector ( '[cdk-focus-start]' ) as HTMLElement ||
97
93
this . _getFirstTabbableElement ( this . _element ) ;
@@ -101,9 +97,7 @@ export class FocusTrap {
101
97
}
102
98
}
103
99
104
- /**
105
- * Focuses the last tabbable element within the focus trap region.
106
- */
100
+ /** Focuses the last tabbable element within the focus trap region. */
107
101
focusLastTabbableElement ( ) {
108
102
let focusTargets = this . _element . querySelectorAll ( '[cdk-focus-end]' ) ;
109
103
let redirectToElement : HTMLElement = null ;
@@ -154,24 +148,23 @@ export class FocusTrap {
154
148
return null ;
155
149
}
156
150
151
+ /** Creates an anchor element. */
157
152
private _createAnchor ( ) : HTMLElement {
158
153
let anchor = document . createElement ( 'div' ) ;
159
- anchor . tabIndex = 0 ;
154
+ anchor . tabIndex = this . _enabled ? 0 : - 1 ;
160
155
anchor . classList . add ( 'cdk-visually-hidden' ) ;
161
156
anchor . classList . add ( 'cdk-focus-trap-anchor' ) ;
162
157
return anchor ;
163
158
}
164
159
}
165
160
166
161
167
- /**
168
- * Factory that allows easy instantiation of focus traps.
169
- */
162
+ /** Factory that allows easy instantiation of focus traps. */
170
163
@Injectable ( )
171
164
export class FocusTrapFactory {
172
165
constructor ( private _checker : InteractivityChecker , private _ngZone : NgZone ) { }
173
166
174
- attach ( element : HTMLElement , deferAnchors = false ) : FocusTrap {
167
+ create ( element : HTMLElement , deferAnchors = false ) : FocusTrap {
175
168
return new FocusTrap ( element , this . _checker , this . _ngZone , deferAnchors ) ;
176
169
}
177
170
}
@@ -195,7 +188,7 @@ export class FocusTrapDeprecatedDirective implements OnDestroy, AfterContentInit
195
188
}
196
189
197
190
constructor ( private _elementRef : ElementRef , private _focusTrapFactory : FocusTrapFactory ) {
198
- this . focusTrap = this . _focusTrapFactory . attach ( this . _elementRef . nativeElement , true ) ;
191
+ this . focusTrap = this . _focusTrapFactory . create ( this . _elementRef . nativeElement , true ) ;
199
192
}
200
193
201
194
ngOnDestroy ( ) {
@@ -208,9 +201,7 @@ export class FocusTrapDeprecatedDirective implements OnDestroy, AfterContentInit
208
201
}
209
202
210
203
211
- /**
212
- * Directive for trapping focus within a region.
213
- */
204
+ /** Directive for trapping focus within a region. */
214
205
@Directive ( {
215
206
selector : '[cdkTrapFocus]'
216
207
} )
@@ -223,7 +214,7 @@ export class FocusTrapDirective implements OnDestroy, AfterContentInit {
223
214
set enabled ( val : boolean ) { this . focusTrap . enabled = val ; }
224
215
225
216
constructor ( private _elementRef : ElementRef , private _focusTrapFactory : FocusTrapFactory ) {
226
- this . focusTrap = this . _focusTrapFactory . attach ( this . _elementRef . nativeElement , true ) ;
217
+ this . focusTrap = this . _focusTrapFactory . create ( this . _elementRef . nativeElement , true ) ;
227
218
}
228
219
229
220
ngOnDestroy ( ) {
0 commit comments