@@ -14,11 +14,12 @@ import {
14
14
OnDestroy ,
15
15
AfterContentInit ,
16
16
Injectable ,
17
+ Inject ,
17
18
} from '@angular/core' ;
18
19
import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
19
- import { Platform } from '@angular/cdk/platform' ;
20
20
import { first } from 'rxjs/operators/first' ;
21
21
import { InteractivityChecker } from './interactivity-checker' ;
22
+ import { DOCUMENT } from '@angular/common' ;
22
23
23
24
24
25
/**
@@ -45,9 +46,9 @@ export class FocusTrap {
45
46
46
47
constructor (
47
48
private _element : HTMLElement ,
48
- private _platform : Platform ,
49
49
private _checker : InteractivityChecker ,
50
50
private _ngZone : NgZone ,
51
+ private _document : Document ,
51
52
deferAnchors = false ) {
52
53
53
54
if ( ! deferAnchors ) {
@@ -73,11 +74,6 @@ export class FocusTrap {
73
74
* in the constructor, but can be deferred for cases like directives with `*ngIf`.
74
75
*/
75
76
attachAnchors ( ) : void {
76
- // If we're not on the browser, there can be no focus to trap.
77
- if ( ! this . _platform . isBrowser ) {
78
- return ;
79
- }
80
-
81
77
if ( ! this . _startAnchor ) {
82
78
this . _startAnchor = this . _createAnchor ( ) ;
83
79
}
@@ -144,10 +140,6 @@ export class FocusTrap {
144
140
* @returns The boundary element.
145
141
*/
146
142
private _getRegionBoundary ( bound : 'start' | 'end' ) : HTMLElement | null {
147
- if ( ! this . _platform . isBrowser ) {
148
- return null ;
149
- }
150
-
151
143
// Contains the deprecated version of selector, for temporary backwards comparability.
152
144
let markers = this . _element . querySelectorAll ( `[cdk-focus-region-${ bound } ], ` +
153
145
`[cdkFocusRegion${ bound } ], ` +
@@ -175,10 +167,6 @@ export class FocusTrap {
175
167
* @returns Whether focus was moved successfuly.
176
168
*/
177
169
focusInitialElement ( ) : boolean {
178
- if ( ! this . _platform . isBrowser ) {
179
- return false ;
180
- }
181
-
182
170
// Contains the deprecated version of selector, for temporary backwards comparability.
183
171
const redirectToElement = this . _element . querySelector ( `[cdk-focus-initial], ` +
184
172
`[cdkFocusInitial]` ) as HTMLElement ;
@@ -271,7 +259,7 @@ export class FocusTrap {
271
259
272
260
/** Creates an anchor element. */
273
261
private _createAnchor ( ) : HTMLElement {
274
- let anchor = document . createElement ( 'div' ) ;
262
+ const anchor = this . _document . createElement ( 'div' ) ;
275
263
anchor . tabIndex = this . _enabled ? 0 : - 1 ;
276
264
anchor . classList . add ( 'cdk-visually-hidden' ) ;
277
265
anchor . classList . add ( 'cdk-focus-trap-anchor' ) ;
@@ -292,10 +280,15 @@ export class FocusTrap {
292
280
/** Factory that allows easy instantiation of focus traps. */
293
281
@Injectable ( )
294
282
export class FocusTrapFactory {
283
+ private _document : Document ;
284
+
295
285
constructor (
296
286
private _checker : InteractivityChecker ,
297
- private _platform : Platform ,
298
- private _ngZone : NgZone ) { }
287
+ private _ngZone : NgZone ,
288
+ @Inject ( DOCUMENT ) _document : any ) {
289
+
290
+ this . _document = _document ;
291
+ }
299
292
300
293
/**
301
294
* Creates a focus-trapped region around the given element.
@@ -306,7 +299,7 @@ export class FocusTrapFactory {
306
299
*/
307
300
create ( element : HTMLElement , deferCaptureElements : boolean = false ) : FocusTrap {
308
301
return new FocusTrap (
309
- element , this . _platform , this . _checker , this . _ngZone , deferCaptureElements ) ;
302
+ element , this . _checker , this . _ngZone , this . _document , deferCaptureElements ) ;
310
303
}
311
304
}
312
305
@@ -349,6 +342,8 @@ export class FocusTrapDeprecatedDirective implements OnDestroy, AfterContentInit
349
342
exportAs : 'cdkTrapFocus' ,
350
343
} )
351
344
export class CdkTrapFocus implements OnDestroy , AfterContentInit {
345
+ private _document : Document ;
346
+
352
347
/** Underlying FocusTrap instance. */
353
348
focusTrap : FocusTrap ;
354
349
@@ -372,7 +367,9 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit {
372
367
constructor (
373
368
private _elementRef : ElementRef ,
374
369
private _focusTrapFactory : FocusTrapFactory ,
375
- private _platform : Platform ) {
370
+ @Inject ( DOCUMENT ) _document : any ) {
371
+
372
+ this . _document = _document ;
376
373
this . focusTrap = this . _focusTrapFactory . create ( this . _elementRef . nativeElement , true ) ;
377
374
}
378
375
@@ -390,8 +387,8 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit {
390
387
ngAfterContentInit ( ) {
391
388
this . focusTrap . attachAnchors ( ) ;
392
389
393
- if ( this . autoCapture && this . _platform . isBrowser ) {
394
- this . _previouslyFocusedElement = document . activeElement as HTMLElement ;
390
+ if ( this . autoCapture ) {
391
+ this . _previouslyFocusedElement = this . _document . activeElement as HTMLElement ;
395
392
this . focusTrap . focusInitialElementWhenReady ( ) ;
396
393
}
397
394
}
0 commit comments