-
Notifications
You must be signed in to change notification settings - Fork 6.8k
virtual scroll: implement scrollTo functions #11498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
868bbf9
a31a4c8
017a131
2527306
05c29b9
dce1cad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,4 +37,7 @@ export interface VirtualScrollStrategy { | |
|
||
/** Called when the offset of the rendered items changed. */ | ||
onRenderedOffsetChanged(); | ||
|
||
/** Get the offset for the given index. */ | ||
getScrollOffsetForIndex(index: number); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we may want to put the whole |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ import {animationFrameScheduler, fromEvent, Observable, Subject} from 'rxjs'; | |
import {sampleTime, take, takeUntil} from 'rxjs/operators'; | ||
import {CdkVirtualForOf} from './virtual-for-of'; | ||
import {VIRTUAL_SCROLL_STRATEGY, VirtualScrollStrategy} from './virtual-scroll-strategy'; | ||
import { supportsSmoothScroll } from '@angular/cdk/platform'; | ||
|
||
|
||
/** Checks if the given ranges are equal. */ | ||
|
@@ -246,7 +247,40 @@ export class CdkVirtualScrollViewport implements OnInit, OnDestroy { | |
} | ||
} | ||
|
||
/** Sets the scroll offset on the viewport. */ | ||
/** Scrolls to the offset on the viewport. */ | ||
scrollToOffset(offset: number, options = { smooth: false, lazy: false }) { | ||
const viewportElement = this.elementRef.nativeElement; | ||
const top = this.orientation === 'vertical' ? offset : 0; | ||
const left = this.orientation === 'horizontal' ? offset : 0; | ||
|
||
let shouldScroll = true; | ||
if (options.lazy) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this logic is quite right because the rendered content may be larger than the viewport. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your right. I changed it to measure the viewport size rather than content. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if lazy scrolling is a common enough use case to be something we want to include in the API. It's pretty easy to implement on top if we just provide the basic functionality |
||
const currentOffset = this.measureScrollOffset(); | ||
const currentOffsetEnd = currentOffset + this.getViewportSize(); | ||
shouldScroll = offset < currentOffset || offset > currentOffsetEnd; | ||
} | ||
|
||
if (shouldScroll) { | ||
if (options.smooth && supportsSmoothScroll()) { | ||
viewportElement.scrollTo({ left, top, behavior: 'smooth' }); | ||
} else { | ||
if (this.orientation === 'vertical') { | ||
viewportElement.scrollTop = top; | ||
} else { | ||
viewportElement.scrollLeft = left; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** Scroll the viewport to the specified index. */ | ||
scrollToIndex(index: number, options = { smooth: false, lazy: false }) { | ||
const contentSize = this.measureRenderedContentSize(); | ||
const offset = this._scrollStrategy.getScrollOffsetForIndex(index); | ||
this.scrollToOffset(offset, options); | ||
} | ||
|
||
/** Internal method to set the scroll offset on the viewport. */ | ||
setScrollOffset(offset: number) { | ||
// Rather than setting the offset immediately, we batch it up to be applied along with other DOM | ||
// writes during the next change detection cycle. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's say "not currently supported", this sounds like it never will be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean that the scrollToIndex will not be supported for autosize in initial version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The autosize strategy will probably stay in cdk-experimental a little longer than the fixed size strategy, while we work out some of the bugs. This will be implemented before it goes into cdk