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