cdk-virtual-scroll-viewport - support window scroll
Bug, feature request, or proposal:
Feature request
What is the expected behavior?
Allow the scroll of the cdk-virtual-scroll-viewport to be controlled by the top level window scrollbar.
See example at the link below. (Go to that page and click on parent scroll to see desired behavior) https://rintoj.github.io/ngx-virtual-scroller
Another example of window scrolling in the context of virtual scrolling https://bvaughn.github.io/react-virtualized/#/components/WindowScroller
What is the current behavior?
Specifying a parent scroll does not seem to be supported, from what I can tell.
What is the use-case or motivation for changing an existing behavior?
Having a single top level window/page scroll is standard in a lot of SPAs. The virtual-scroll would be a lot more useful in these cases, if it could use the same scroll bar, otherwise often times you end up with two scrollbars (one for the virt-scroll and the other for the page, or else a smaller inset container for the virt-scroll with a scrollbar), which is less than ideal.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular: 7.0.1 Material: 7.0.2
Is there anything else we should know?
That would be very useful!
+1
This should definitely be implemented soon!
mark to track status. This will be awesome!
@mmalerba I did a small POC to support both window and closest relative parent virtual scrolling.
Actually it's not yet ready for merge. It's still missing tests, examples, and even implementations... But at least it gives a proposal for the implementation. I would be really interested to get your feedback.
In my use-case it fits well. Hope you appreciate.
Here is a demo:

Kind regards,
@rmedaer sure, I can take a look. Can you link me to the branch where you're working?
Yeah ! I committed on master branch in my fork. I know it's bad but again my plan was just to do a test and it became probably more ..
The dev-app (demo) contains an example.
@rmedaer Overall I like the approach, though parts of the implementation I'm not too sure about. I also think we could make the nested viewport more flexible by either making a directive that can be used to mark the parent, or using an @Input() that lets the user specify the scrolling container (e.g. viewportRoot=".some-ancestor-element"), rather than just assuming it's the direct parent.
@jelbourn Do you have any thoughts about this proposal?
Could you summarize the approach?
Actually, my approach is to decouple the viewport and its container. Then I provide somehow the container to the viewport which will use its API to calculate scroll offset and so on. Here is the container's interface:
export interface VirtualScrollContainerRef {
measureScrollOffset(from: 'top' | 'left' | 'right' | 'bottom' | 'start' | 'end'): number;
measureContainerSize(orientation: 'horizontal' | 'vertical'): number;
getViewportSize(contentSize: number): string | null;
scrollTo(options: ExtendedScrollToOptions): any;
elementScrolled(): Observable<Event>;
}
As you can see this interface is implementing more or less the same then CdkScrollable (there is no need of ElementRef).
In my POC I "provide" the container ref to the viewport using a directive that you have to apply on viewport. But as @mmalerba said, we could also use a DOM reference. We could even create a directive that you apply on parent (container) element which has to scroll.
Currently my prototype has 3 ContainerRef directives:
- CdkVirtualScrollDefaultViewport: same behavior then current impl; we are scrolling into the viewport.
- CdkVirtualScrollNestedViewport: use the closest relative parent as container => works well with
offsetTopproperty. - CdkVirtualScrollWindowViewport: use window as container. This one cannot use implementation from CdkScrollable.
This feature is really needed in order to use the Virtual-scroll in real apps.... We would really appreciate if it would be implemented soon.
@hezzyg You can use this module. It's already supported window scroll.
@hezzyg otherwise if @mmalerba and/or @jelbourn are OK with my approach, I can deliver a patch ASAP. But I'm still waiting their (no)go to do it (or not). ;-)
@hezzyg otherwise if @mmalerba and/or @jelbourn are OK with my approach, I can deliver a patch ASAP. But I'm still waiting their (no)go to do it (or not). ;-)
Thanks @rmedaer . I do prefer a solution integrated into Angular Material for better support in the future. So I hope your suggestion will get there asap :-)
@hezzyg You can use this module. It's already supported
windowscroll.
I'll have a look. thanks @vahidvdn
@rmedaer yes I'd like to do this, please send a PR and mention me on it, thanks!
This would be a really useful feature. We are unable to use this at the moment because we have a scrollbar at a different level.
@rmedaer @mmalerba
@hezzyg otherwise if @mmalerba and/or @jelbourn are OK with my approach, I can deliver a patch ASAP. But I'm still waiting their (no)go to do it (or not). ;-)
@rmedaer yes I'd like to do this, please send a PR and mention me on it, thanks!
Is there a pull request for the patch? I really need to implement a window scrolling CDK viewport so I'd be grateful for any help!
@mmalerba the PR #14287 is simply introducing the virtual adapter but is there any PR to support scrolling at parent level or window level for cdk-virtual-scroll-viewport?
@jeetparikh if it's really urgent, you can take a look at this commit. It's the POC I made to test virtual scrolling on window. Btw I don't have so much time to do a PR for now but at least you can already play with it !