6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { _getFocusedElementPierceShadowDom } from '@angular/cdk/platform' ;
9
+ import { Platform , _getFocusedElementPierceShadowDom } from '@angular/cdk/platform' ;
10
10
import { DOCUMENT } from '@angular/common' ;
11
11
import {
12
12
AfterContentInit ,
@@ -21,6 +21,7 @@ import {
21
21
SimpleChanges ,
22
22
OnChanges ,
23
23
booleanAttribute ,
24
+ inject ,
24
25
} from '@angular/core' ;
25
26
import { take } from 'rxjs/operators' ;
26
27
import { InteractivityChecker } from '../interactivity-checker/interactivity-checker' ;
@@ -415,10 +416,12 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
415
416
/** Whether the focus trap is active. */
416
417
@Input ( { alias : 'cdkTrapFocus' , transform : booleanAttribute } )
417
418
get enabled ( ) : boolean {
418
- return this . focusTrap . enabled ;
419
+ return this . focusTrap ? .enabled || false ;
419
420
}
420
421
set enabled ( value : boolean ) {
421
- this . focusTrap . enabled = value ;
422
+ if ( this . focusTrap ) {
423
+ this . focusTrap . enabled = value ;
424
+ }
422
425
}
423
426
424
427
/**
@@ -436,11 +439,15 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
436
439
*/
437
440
@Inject ( DOCUMENT ) _document : any ,
438
441
) {
439
- this . focusTrap = this . _focusTrapFactory . create ( this . _elementRef . nativeElement , true ) ;
442
+ const platform = inject ( Platform ) ;
443
+
444
+ if ( platform . isBrowser ) {
445
+ this . focusTrap = this . _focusTrapFactory . create ( this . _elementRef . nativeElement , true ) ;
446
+ }
440
447
}
441
448
442
449
ngOnDestroy ( ) {
443
- this . focusTrap . destroy ( ) ;
450
+ this . focusTrap ? .destroy ( ) ;
444
451
445
452
// If we stored a previously focused element when using autoCapture, return focus to that
446
453
// element now that the trapped region is being destroyed.
@@ -451,15 +458,15 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
451
458
}
452
459
453
460
ngAfterContentInit ( ) {
454
- this . focusTrap . attachAnchors ( ) ;
461
+ this . focusTrap ? .attachAnchors ( ) ;
455
462
456
463
if ( this . autoCapture ) {
457
464
this . _captureFocus ( ) ;
458
465
}
459
466
}
460
467
461
468
ngDoCheck ( ) {
462
- if ( ! this . focusTrap . hasAttached ( ) ) {
469
+ if ( this . focusTrap && ! this . focusTrap . hasAttached ( ) ) {
463
470
this . focusTrap . attachAnchors ( ) ;
464
471
}
465
472
}
@@ -471,14 +478,14 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
471
478
autoCaptureChange &&
472
479
! autoCaptureChange . firstChange &&
473
480
this . autoCapture &&
474
- this . focusTrap . hasAttached ( )
481
+ this . focusTrap ? .hasAttached ( )
475
482
) {
476
483
this . _captureFocus ( ) ;
477
484
}
478
485
}
479
486
480
487
private _captureFocus ( ) {
481
488
this . _previouslyFocusedElement = _getFocusedElementPierceShadowDom ( ) ;
482
- this . focusTrap . focusInitialElementWhenReady ( ) ;
489
+ this . focusTrap ? .focusInitialElementWhenReady ( ) ;
483
490
}
484
491
}
0 commit comments