Skip to content

Commit 3acbf26

Browse files
crisbetotinayuangao
authored andcommitted
fix(block-scroll-strategy): server-side rendering error (#9665)
Fixes a server-side rendering error in the `BlockScrollStrategy`, because it was referring to the global `document`. Relates to #9066.
1 parent f75fa15 commit 3acbf26

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/cdk/overlay/scroll/block-scroll-strategy.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ export class BlockScrollStrategy implements ScrollStrategy {
1616
private _previousHTMLStyles = { top: '', left: '' };
1717
private _previousScrollPosition: { top: number, left: number };
1818
private _isEnabled = false;
19+
private _document: Document;
1920

20-
constructor(private _viewportRuler: ViewportRuler) { }
21+
constructor(private _viewportRuler: ViewportRuler, document: any) {
22+
this._document = document;
23+
}
2124

2225
/** Attaches this scroll strategy to an overlay. */
2326
attach() { }
2427

2528
/** Blocks page-level scroll while the attached overlay is open. */
2629
enable() {
2730
if (this._canBeEnabled()) {
28-
const root = document.documentElement;
31+
const root = this._document.documentElement;
2932

3033
this._previousScrollPosition = this._viewportRuler.getViewportScrollPosition();
3134

@@ -45,8 +48,8 @@ export class BlockScrollStrategy implements ScrollStrategy {
4548
/** Unblocks page-level scroll while the attached overlay is open. */
4649
disable() {
4750
if (this._isEnabled) {
48-
const html = document.documentElement;
49-
const body = document.body;
51+
const html = this._document.documentElement;
52+
const body = this._document.body;
5053
const previousHtmlScrollBehavior = html.style['scrollBehavior'] || '';
5154
const previousBodyScrollBehavior = body.style['scrollBehavior'] || '';
5255

@@ -71,11 +74,13 @@ export class BlockScrollStrategy implements ScrollStrategy {
7174
// Since the scroll strategies can't be singletons, we have to use a global CSS class
7275
// (`cdk-global-scrollblock`) to make sure that we don't try to disable global
7376
// scrolling multiple times.
74-
if (document.documentElement.classList.contains('cdk-global-scrollblock') || this._isEnabled) {
77+
const html = this._document.documentElement;
78+
79+
if (html.classList.contains('cdk-global-scrollblock') || this._isEnabled) {
7580
return false;
7681
}
7782

78-
const body = document.body;
83+
const body = this._document.body;
7984
const viewport = this._viewportRuler.getViewportSize();
8085
return body.scrollHeight > viewport.height || body.scrollWidth > viewport.width;
8186
}

src/cdk/overlay/scroll/scroll-strategy-options.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {Injectable, NgZone} from '@angular/core';
8+
import {Injectable, NgZone, Inject} from '@angular/core';
99
import {CloseScrollStrategy, CloseScrollStrategyConfig} from './close-scroll-strategy';
1010
import {NoopScrollStrategy} from './noop-scroll-strategy';
1111
import {BlockScrollStrategy} from './block-scroll-strategy';
1212
import {ScrollDispatcher} from '@angular/cdk/scrolling';
1313
import {ViewportRuler} from '@angular/cdk/scrolling';
14+
import {DOCUMENT} from '@angular/common';
1415
import {
1516
RepositionScrollStrategy,
1617
RepositionScrollStrategyConfig,
@@ -25,10 +26,15 @@ import {
2526
*/
2627
@Injectable()
2728
export class ScrollStrategyOptions {
29+
private _document: Document;
30+
2831
constructor(
2932
private _scrollDispatcher: ScrollDispatcher,
3033
private _viewportRuler: ViewportRuler,
31-
private _ngZone: NgZone) { }
34+
private _ngZone: NgZone,
35+
@Inject(DOCUMENT) document: any) {
36+
this._document = document;
37+
}
3238

3339
/** Do nothing on scroll. */
3440
noop = () => new NoopScrollStrategy();
@@ -41,7 +47,7 @@ export class ScrollStrategyOptions {
4147
this._ngZone, this._viewportRuler, config)
4248

4349
/** Block scrolling. */
44-
block = () => new BlockScrollStrategy(this._viewportRuler);
50+
block = () => new BlockScrollStrategy(this._viewportRuler, this._document);
4551

4652
/**
4753
* Update the overlay's position on scroll.

0 commit comments

Comments
 (0)