Skip to content

Commit 25d455d

Browse files
committed
fix(cdk/scrolling): fix scrolling in appendOnly mode
The rendered content offset is incorrectly set in appendOnly. It should be set to 0
1 parent 03485cd commit 25d455d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,39 @@ describe('CdkVirtualScrollViewport', () => {
10061006

10071007
expect(viewport.getRenderedRange()).toEqual({start: 0, end: 200});
10081008
}));
1009+
1010+
it('rendered offset should always start at 0', fakeAsync(() => {
1011+
finishInit(fixture);
1012+
triggerScroll(viewport, testComponent.itemSize + 5);
1013+
fixture.detectChanges();
1014+
flush();
1015+
1016+
expect(viewport.getOffsetToRenderedContentStart())
1017+
.withContext('should have 0px offset as we are using appendOnly')
1018+
.toBe(0);
1019+
}));
1020+
1021+
it('should set content offset to bottom of content', fakeAsync(() => {
1022+
finishInit(fixture);
1023+
const contentSize = viewport.measureRenderedContentSize();
1024+
1025+
expect(contentSize).toBeGreaterThan(0);
1026+
1027+
viewport.setRenderedContentOffset(contentSize + 10, 'to-end');
1028+
fixture.detectChanges();
1029+
flush();
1030+
1031+
expect(viewport.getOffsetToRenderedContentStart()).toBe(0);
1032+
}));
1033+
1034+
it('should set content offset to top of content', fakeAsync(() => {
1035+
finishInit(fixture);
1036+
viewport.setRenderedContentOffset(10, 'to-start');
1037+
fixture.detectChanges();
1038+
flush();
1039+
1040+
expect(viewport.getOffsetToRenderedContentStart()).toBe(0);
1041+
}));
10091042
});
10101043
});
10111044

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
321321
const axis = isHorizontal ? 'X' : 'Y';
322322
const axisDirection = isHorizontal && isRtl ? -1 : 1;
323323
let transform = `translate${axis}(${Number(axisDirection * offset)}px)`;
324+
// in appendOnly, we always start from the top
325+
offset = this.appendOnly && to === 'to-start' ? 0 : offset;
324326
this._renderedContentOffset = offset;
325327
if (to === 'to-end') {
326328
transform += ` translate${axis}(-100%)`;

0 commit comments

Comments
 (0)