Skip to content

Commit acbf3c8

Browse files
crisbetotinayuangao
authored andcommitted
fix(dialog): server-side rendering error when attempting to trap focus (#9698)
Fixes an error that is being thrown when trying to open a dialog on the server. Also adds a dialog sanity check to the kitchen sink. Relates to #9066.
1 parent 959a8a1 commit acbf3c8

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/lib/dialog/dialog-container.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,13 @@ export class MatDialogContainer extends BasePortalOutlet {
163163
if (this._document) {
164164
this._elementFocusedBeforeDialogWasOpened = this._document.activeElement as HTMLElement;
165165

166-
// Move focus onto the dialog immediately in order to prevent the user from accidentally
167-
// opening multiple dialogs at the same time. Needs to be async, because the element
168-
// may not be focusable immediately.
169-
Promise.resolve().then(() => this._elementRef.nativeElement.focus());
166+
// Note that there is no focus method when rendering on the server.
167+
if (this._elementRef.nativeElement.focus) {
168+
// Move focus onto the dialog immediately in order to prevent the user from accidentally
169+
// opening multiple dialogs at the same time. Needs to be async, because the element
170+
// may not be focusable immediately.
171+
Promise.resolve().then(() => this._elementRef.nativeElement.focus());
172+
}
170173
}
171174
}
172175

src/universal-app/kitchen-sink/kitchen-sink.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,23 @@ import {
3636
MatTooltipModule,
3737
MatStepperModule,
3838
MatSnackBar,
39+
MatDialog,
3940
} from '@angular/material';
4041
import {
4142
CdkTableModule,
4243
DataSource
4344
} from '@angular/cdk/table';
45+
import {Overlay} from '@angular/cdk/overlay';
4446

4547
import {of as observableOf} from 'rxjs/observable/of';
4648

49+
50+
@Component({
51+
template: `<button>Do the thing</button>`
52+
})
53+
export class TestDialog {}
54+
55+
4756
@Component({
4857
selector: 'kitchen-sink',
4958
templateUrl: './kitchen-sink.html',
@@ -60,9 +69,13 @@ export class KitchenSink {
6069
disconnect: () => {}
6170
};
6271

63-
constructor(snackBar: MatSnackBar) {
72+
constructor(snackBar: MatSnackBar, dialog: MatDialog, overlay: Overlay) {
6473
// Open a snack bar to do a basic sanity check of the overlays.
6574
snackBar.open('Hello there');
75+
76+
// TODO(crisbeto): use the noop scroll strategy until
77+
// the fixes for the block scroll strategy get in.
78+
dialog.open(TestDialog, {scrollStrategy: overlay.scrollStrategies.noop()});
6679
}
6780

6881
}
@@ -109,7 +122,8 @@ export class KitchenSink {
109122
CdkTableModule
110123
],
111124
bootstrap: [KitchenSink],
112-
declarations: [KitchenSink],
125+
declarations: [KitchenSink, TestDialog],
126+
entryComponents: [TestDialog],
113127
})
114128
export class KitchenSinkClientModule { }
115129

0 commit comments

Comments
 (0)