Skip to content

Commit 2021596

Browse files
Merge pull request #5681 from SaadBazaz:dialog-ssr
PiperOrigin-RevId: 652911593
2 parents 7ec70c4 + 78c9cda commit 2021596

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

dialog/internal/dialog.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ export class Dialog extends dialogBaseClass {
150150
private escapePressedWithoutCancel = false;
151151
// This TreeWalker is used to walk through a dialog's children to find
152152
// focusable elements. TreeWalker is faster than `querySelectorAll('*')`.
153-
private readonly treewalker = document.createTreeWalker(
154-
this,
155-
NodeFilter.SHOW_ELEMENT,
156-
);
153+
// We check for isServer because there isn't a "document" during an SSR
154+
// run.
155+
private readonly treewalker = isServer
156+
? null
157+
: document.createTreeWalker(this, NodeFilter.SHOW_ELEMENT);
157158

158159
constructor() {
159160
super();
@@ -559,7 +560,13 @@ export class Dialog extends dialogBaseClass {
559560
// won't actually reach here.
560561
}
561562

562-
private getFirstAndLastFocusableChildren() {
563+
private getFirstAndLastFocusableChildren():
564+
| [HTMLElement, HTMLElement]
565+
| [null, null] {
566+
if (!this.treewalker) {
567+
return [null, null];
568+
}
569+
563570
let firstFocusableChild: HTMLElement | null = null;
564571
let lastFocusableChild: HTMLElement | null = null;
565572

0 commit comments

Comments
 (0)