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