@@ -14,7 +14,7 @@ import {
14
14
InteractivityChecker ,
15
15
} from '@angular/cdk/a11y' ;
16
16
import { OverlayRef } from '@angular/cdk/overlay' ;
17
- import { _getFocusedElementPierceShadowDom } from '@angular/cdk/platform' ;
17
+ import { Platform , _getFocusedElementPierceShadowDom } from '@angular/cdk/platform' ;
18
18
import {
19
19
BasePortalOutlet ,
20
20
CdkPortalOutlet ,
@@ -36,6 +36,7 @@ import {
36
36
Optional ,
37
37
ViewChild ,
38
38
ViewEncapsulation ,
39
+ inject ,
39
40
} from '@angular/core' ;
40
41
import { DialogConfig } from './dialog-config' ;
41
42
@@ -72,13 +73,14 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
72
73
extends BasePortalOutlet
73
74
implements OnDestroy
74
75
{
76
+ private _platform = inject ( Platform ) ;
75
77
protected _document : Document ;
76
78
77
79
/** The portal outlet inside of this container into which the dialog content will be loaded. */
78
80
@ViewChild ( CdkPortalOutlet , { static : true } ) _portalOutlet : CdkPortalOutlet ;
79
81
80
82
/** The class that traps and manages focus within the dialog. */
81
- private _focusTrap : FocusTrap ;
83
+ private _focusTrap : FocusTrap | null = null ;
82
84
83
85
/** Element that was focused before the dialog was opened. Save this to restore upon close. */
84
86
private _elementFocusedBeforeDialogWasOpened : HTMLElement | null = null ;
@@ -248,7 +250,7 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
248
250
break ;
249
251
case true :
250
252
case 'first-tabbable' :
251
- this . _focusTrap . focusInitialElementWhenReady ( ) . then ( focusedSuccessfully => {
253
+ this . _focusTrap ? .focusInitialElementWhenReady ( ) . then ( focusedSuccessfully => {
252
254
// If we weren't able to find a focusable element in the dialog, then focus the dialog
253
255
// container instead.
254
256
if ( ! focusedSuccessfully ) {
@@ -328,12 +330,14 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
328
330
329
331
/** Sets up the focus trap. */
330
332
private _initializeFocusTrap ( ) {
331
- this . _focusTrap = this . _focusTrapFactory . create ( this . _elementRef . nativeElement ) ;
333
+ if ( this . _platform . isBrowser ) {
334
+ this . _focusTrap = this . _focusTrapFactory . create ( this . _elementRef . nativeElement ) ;
332
335
333
- // Save the previously focused element. This element will be re-focused
334
- // when the dialog closes.
335
- if ( this . _document ) {
336
- this . _elementFocusedBeforeDialogWasOpened = _getFocusedElementPierceShadowDom ( ) ;
336
+ // Save the previously focused element. This element will be re-focused
337
+ // when the dialog closes.
338
+ if ( this . _document ) {
339
+ this . _elementFocusedBeforeDialogWasOpened = _getFocusedElementPierceShadowDom ( ) ;
340
+ }
337
341
}
338
342
}
339
343
0 commit comments