Skip to content

Commit 9473b16

Browse files
committed
fix(material/datepicker): resolve server-side rendering error (#26742)
Fixes that the datepicker was referring to the `window` which throws an error during server-side rendering. Fixes #26729. (cherry picked from commit fbca2e9)
1 parent 1bf2fc2 commit 9473b16

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/material/datepicker/calendar-body.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {Platform} from '@angular/cdk/platform';
910
import {
1011
ChangeDetectionStrategy,
1112
Component,
@@ -19,6 +20,7 @@ import {
1920
SimpleChanges,
2021
OnDestroy,
2122
AfterViewChecked,
23+
inject,
2224
} from '@angular/core';
2325
import {take} from 'rxjs/operators';
2426

@@ -71,6 +73,8 @@ let calendarBodyId = 1;
7173
changeDetection: ChangeDetectionStrategy.OnPush,
7274
})
7375
export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterViewChecked {
76+
private _platform = inject(Platform);
77+
7478
/**
7579
* Used to skip the next focus event when rendering the preview range.
7680
* We need a flag like this, because some browsers fire focus events asynchronously.
@@ -177,8 +181,11 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
177181
element.addEventListener('blur', this._leaveHandler, true);
178182
element.addEventListener('mousedown', this._mousedownHandler);
179183
element.addEventListener('touchstart', this._mousedownHandler);
180-
window.addEventListener('mouseup', this._mouseupHandler);
181-
window.addEventListener('touchend', this._touchendHandler);
184+
185+
if (this._platform.isBrowser) {
186+
window.addEventListener('mouseup', this._mouseupHandler);
187+
window.addEventListener('touchend', this._touchendHandler);
188+
}
182189
});
183190
}
184191

@@ -232,8 +239,11 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
232239
element.removeEventListener('blur', this._leaveHandler, true);
233240
element.removeEventListener('mousedown', this._mousedownHandler);
234241
element.removeEventListener('touchstart', this._mousedownHandler);
235-
window.removeEventListener('mouseup', this._mouseupHandler);
236-
window.removeEventListener('touchend', this._touchendHandler);
242+
243+
if (this._platform.isBrowser) {
244+
window.removeEventListener('mouseup', this._mouseupHandler);
245+
window.removeEventListener('touchend', this._touchendHandler);
246+
}
237247
}
238248

239249
/** Returns whether a cell is active. */

0 commit comments

Comments
 (0)