Skip to content

Commit 9c0e00b

Browse files
committed
don't recalulate height/width during every change detection
1 parent 63aab21 commit 9c0e00b

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/cdk/scrolling/virtual-scroll-viewport.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@
1010
so that the scrollbar captures the size of the entire data set.
1111
-->
1212
<div class="cdk-virtual-scroll-spacer"
13-
[style.width]="orientation == 'horizontal' ? _totalContentSize + 'px' : ''"
14-
[style.height]="orientation == 'vertical' ? _totalContentSize + 'px' : ''"></div>
13+
[style.width]="_totalContentWidth" [style.height]="_totalContentHeight"></div>

src/cdk/scrolling/virtual-scroll-viewport.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,17 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
7171
private _renderedRangeSubject = new Subject<ListRange>();
7272

7373
/** The direction the viewport scrolls. */
74-
@Input() orientation: 'horizontal' | 'vertical' = 'vertical';
74+
@Input()
75+
get orientation() {
76+
return this._orientation;
77+
}
78+
set orientation(orientation: 'horizontal' | 'vertical') {
79+
if (this._orientation !== orientation) {
80+
this._orientation = orientation;
81+
this._calculateSpacerSize();
82+
}
83+
}
84+
private _orientation: 'horizontal' | 'vertical' = 'vertical';
7585

7686
// Note: we don't use the typical EventEmitter here because we need to subscribe to the scroll
7787
// strategy lazily (i.e. only if the user is actually listening to the events). We do this because
@@ -92,7 +102,13 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
92102
/**
93103
* The total size of all content (in pixels), including content that is not currently rendered.
94104
*/
95-
_totalContentSize = 0;
105+
private _totalContentSize = 0;
106+
107+
/** A string representing the `style.width` property value to be used for the spacer element. */
108+
_totalContentWidth = '';
109+
110+
/** A string representing the `style.height` property value to be used for the spacer element. */
111+
_totalContentHeight = '';
96112

97113
/**
98114
* The CSS transform applied to the rendered subset of items so that they appear within the bounds
@@ -232,6 +248,7 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
232248
setTotalContentSize(size: number) {
233249
if (this._totalContentSize !== size) {
234250
this._totalContentSize = size;
251+
this._calculateSpacerSize();
235252
this._markChangeDetectionNeeded();
236253
}
237254
}
@@ -390,4 +407,12 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
390407
fn();
391408
}
392409
}
410+
411+
/** Calculates the `style.width` and `style.height` for the spacer element. */
412+
private _calculateSpacerSize() {
413+
this._totalContentHeight =
414+
this.orientation === 'horizontal' ? '' : `${this._totalContentSize}px`;
415+
this._totalContentWidth =
416+
this.orientation === 'horizontal' ? `${this._totalContentSize}px` : '';
417+
}
393418
}

tools/public_api_guard/cdk/scrolling.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ export declare type CdkVirtualForOfContext<T> = {
9090

9191
export declare class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, OnDestroy {
9292
_contentWrapper: ElementRef<HTMLElement>;
93-
_totalContentSize: number;
93+
_totalContentHeight: string;
94+
_totalContentWidth: string;
9495
elementRef: ElementRef<HTMLElement>;
9596
orientation: 'horizontal' | 'vertical';
9697
renderedRangeStream: Observable<ListRange>;

0 commit comments

Comments
 (0)