Skip to content

docs(virtual-scroll): add initial documentation #11496

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

Merged
merged 6 commits into from
Jun 19, 2018

Conversation

mmalerba
Copy link
Contributor

fixes #10123

@mmalerba mmalerba requested review from amcdnl, jelbourn and crisbeto May 24, 2018 17:29
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label May 24, 2018
The easiest way to provide a strategy is with one of the directives `itemSize` or `autosize`.

For some example usages,
[see the demo app](https://github.com/angular/material2/tree/master/src/demo-app/virtual-scroll).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a TODO here to add live examples once this lands in stable?


### Creating items in the viewport
`*cdkVirtualFor` should be used instead of `*ngFor` to create items in the
`<cdk-virtual-scroll-viewport>`. `*cdkVirtualFor` supports most of the same features as `*ngFor`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it mention which features it doesn't support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote that because I'm naturally hedgey, but I think it supports all of the same features as *ngFor, updated to say that.


#### View recycling
In order to improve performance `*cdkVirtualFor` saves the views it creates in a cache when they are
no longer needed. This way the next time a new view is needed once can be recycled from the cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "once" seems out of place here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, was supposed to read "one"

In order to improve performance `*cdkVirtualFor` saves the views it creates in a cache when they are
no longer needed. This way the next time a new view is needed once can be recycled from the cache
rather than being created from scratch. The size of this cache can be adjusted using the
`templateCacheSize` input. The cache size defaults to `20` and caching can be disabled by setting it
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should avoid specifying defaults in the docs since they could change and we'll probably forget to update them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its useful for people to know. I'll put it in the jsdoc rather than here though.

connect process.

### Scrolling over fixed size items
If you're scrolling over a list of items that are all the same fixed size, you can use the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention the advantages/disadvantages of using a fixed-size viewport?

@@ -435,6 +436,7 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
* The number of pixels worth of buffer to shoot for when rendering new items.
* If the actual amount turns out to be less it will not necessarily trigger an additional
* rendering cycle (as long as the amount of buffer is still greater than `minBufferPx`).
* The default add buffer px is 200.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults to 200px

@@ -425,6 +425,7 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
/**
* The minimum amount of buffer rendered beyond the viewport (in pixels).
* If the amount of buffer dips below this number, more items will be rendered.
* The default minBufferPx is 100.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults to 100px

/** The number of extra elements to render on either side of the scrolling viewport. */
/**
* The number of extra elements to render on either side of the scrolling viewport.
* The default buffer size is 5.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults to 5 elements

@@ -109,6 +109,10 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
}
}

/**
* The size of the cache used to store templates that are not being used for re-use later.
* Setting the cache size to `0` will disable caching. The default cache size is 20.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults to 20 templates

**Warning: this component is still experimental. It may have bugs and the API may change at any
time**

`<cdk-virtual-scroll-viewport>` is used to display a scrolling list containing a large number of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would shorten the lede here and move the bits about the strategies to their own section immediately after this. The section for strategies should talk about why you would use one or the other.

Also some editing for brevity and passive voice:

`<cdk-virtual-scroll-viewport>` displays large lists of elements performantly by only
rendering the items that appear on-screen. This components works with `*cdkVirtualFor`,
a virtualized replacement for `*ngFor`.

</cdk-virtual-scroll-viewport>
```

The `autosize` strategy allows the buffer to be configured through two inputs `minBufferPx` and
Copy link
Member

@jelbourn jelbourn May 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would break up the paragraph structure a bit here:

The `autosize` strategy is configured through two inputs: `minBufferPx` and `addBufferPx`.

**`minBufferPx`** determines the minimum space outside virtual scrolling viewport that will be filled
with content. Increasing this will increase the amount of content a user will see before more content
must be rendered. However, too large a value will cause more content to be rendered than is necessary.

**`addBufferPx`** determines the amount of content that will be added incrementally as
the viewport is scrolled. This should be greater than the size of `minBufferPx` so that one
"render" is needed at a time.

**Note: The fixed size strategy will likely be changed to allow specifying a separate
`minBufferPx` and `addBufferPx` like the autosize strategy**

The fixed size viewport strategy is less flexible than the autosize strategy because it requires all
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just explain this once near the beginning of the doc; it's currently mentioned a few times throughout

Because the auto size strategy needs to measure the size of the elements, its performance may not
be as good as the fixed size strategy.

### Setting the viewport orientation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just #### Viewport orientation ?

`vertical` which virtualizes scrolling along the y-axis. It can be set to `horizontal` to virtualize
scrolling along the x-axis. If you use this option you need to make sure that the content is
actually laid out so that it extends along the x-axis. To do this you may want to target CSS at
`.cdk-virtual-scroll-content-wrapper` which is the wrapper element that contains the rendered
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better/possible to recommend styling their own element instead of a class built into the virtual scroller?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's eventually going to be possible to specify that element themselves, or just allow it to be implicitly created if they don't specify. That's part of the work for making it work with <table>, <ul>, etc.

be as good as the fixed size strategy.

### Setting the viewport orientation
The orientation of the viewport can be adjusted by using the `orientation` input. It defaults to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The virtual-scroll viewport defaults to a vertical orientation, but can also be set to
`orientation="horizontal"`. When changing the orientation, ensure that the item are laid
out horizontally via CSS.

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like some of the comments from the first pass still apply

time**

`<cdk-virtual-scroll-viewport>` displays large lists of elements performantly by only
rendering the items that appear on-screen. This components works with `*cdkVirtualFor`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appear -> fit (just occurred to me on second reading)

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you push your commits? Still seeing text w/ comments on it

@mmalerba mmalerba added target: minor This PR is targeted for the next minor release pr: merge safe labels May 29, 2018
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker labels Jun 1, 2018
amcdnl
amcdnl previously requested changes Jun 3, 2018
Copy link
Contributor

@amcdnl amcdnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few nits

**Warning: this component is still experimental. It may have bugs and the API may change at any
time**

`<cdk-virtual-scroll-viewport>` displays large lists of elements performantly by only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to explain virtual scroll a little bit. I don't think people understand the difference between virtual scroll and infinite scroll. A idea gist.

Virtual scrolling enables developers to render large lists of data in a performant way. Loading hundreds of elements can be slow in any browser, virtual scrolling enables a performant want to simulate all items are rendered by making the height of the container element the total number of elements to be rendered and then only rendering the items in view. Virtual scroll is different from strategies like infinite scroll where it renders a set amount of elements and then when you hit the bottom renders the rest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made some slight tweaks and merged with my sentence above

[see the demo app](https://github.com/angular/material2/tree/master/src/demo-app/virtual-scroll).

### Creating items in the viewport
`*cdkVirtualFor` replaces `*ngFor` inside of a `<cdk-virtual-scroll-viewport>`, supporting the exact
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The replacement is mentioned right above this. Perhaps remove from one or the other?

connect process.

### Scrolling over fixed size items
When all items are the same fixed size,, you can use the `FixedSizeVirtualScrollStrategy`. This can
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two commas: fixed size,,


```html
<cdk-virtual-scroll-viewport itemSize="50">
<div *cdkVirtualFor="let item of items;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd be surprised ....

</cdk-virtual-scroll-viewport>
```

A `trackBy` function can be specified and works the same as the `*ngFor` `trackBy`. The `index`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this trackBy is just randomly mentioned at the end here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because trackBy isn't a context variable, so it didn't fit in the part above, but I do want to mention that it's still supported

portion.

#### View recycling
To improve rendering performance, `*cdkVirtualFor` caches previously created views after
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth mentioning things like portals that are attached to the VCR can cause issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might even put this at the end too. Its a implementation detail rather than something important for the user to know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be important for the user to know depending on their use case, some components may not deal well with having their templates recycled.

It does sound like a good idea to mention somewhere that the ViewContainer is managed by the virtual-scroller and so any components that attempt to do things with it are going to run into issues. I'm not sure where the best place for this is... In the future I also think we could mark the views that are managed by the virtual scroll in order to play better with other components that may modify the ViewContainer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably put it at the bottom as a note.

content.

```html
<cdk-virtual-scroll-viewport autosize orientation="horizontal">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd generally discourage users from using autosize, thus removing it as the default on examples like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, we don't want users putting autosize just because its less typing. If they know the itemSize it's going to be more performant.

</cdk-virtual-scroll-viewport>
```

The fixed size strategy also supports setting the buffer size, i.e. the number of items rendered
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the default buffer size?


```html
<cdk-virtual-scroll-viewport itemSize="50">
...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the examples should show the for in them, people like to just copy/paste.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add full working embedded example code when I move this out of cdk-experimental

```

#### Specifying data
`*cdkVirtualFor` accepts data from an `Array`, `Observable<Array>`, or `DataSource`. The
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd show an example code snippet here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add that once we move it into the cdk proper and have actual embedded examples

@andrewseguin andrewseguin removed the action: merge The PR is ready for merge by the caretaker label Jun 7, 2018
@mmalerba
Copy link
Contributor Author

@jelbourn @amcdnl PTAL

@mmalerba mmalerba dismissed amcdnl’s stale review June 18, 2018 23:51

addressed comments

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jelbourn jelbourn added the action: merge The PR is ready for merge by the caretaker label Jun 19, 2018
@jelbourn jelbourn added the docs This issue is related to documentation label Jun 19, 2018
@jelbourn jelbourn merged commit 93b5e89 into angular:master Jun 19, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement docs This issue is related to documentation target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

virtual-scroll: add docs
7 participants