Skip to content

Commit 4327b38

Browse files
author
Guillaume Chau
committed
refactor: direction support
1 parent f70587a commit 4327b38

15 files changed

+610
-323
lines changed

README.md

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ There are several components provided by `vue-virtual-scroller`:
9999

100100
[RecycleScroller](#recyclescroller) is a component that only renders the visible item in your list. It also re-use components and dom elements to be the most efficient and performant possible.
101101

102-
[DynamicScroller](#dynamicscroller) is a component is using RecycleScroller under-the-hood and adds a dynamic height management feature on top of it. The main use case for this is **not knowing the height of the items** in advance: the Dynamic Scroller will automatically "discover" it when it renders new item as the user scrolls.
102+
[DynamicScroller](#dynamicscroller) is a component is using RecycleScroller under-the-hood and adds a dynamic size management feature on top of it. The main use case for this is **not knowing the size of the items** in advance: the Dynamic Scroller will automatically "discover" it when it renders new item as the user scrolls.
103103

104104
[DynamicScrollerItem](#dynamicscrolleritem) must wrap each item in a DynamicScroller to handle size computations.
105105

@@ -118,9 +118,10 @@ Use the scoped slot to render each item in the list:
118118
<RecycleScroller
119119
class="scroller"
120120
:items="list"
121-
:item-height="32"
121+
:item-size="32"
122+
key-field="id"
122123
>
123-
<div slot-scope="{ item }" class="user">
124+
<div v-slot="{ item }" class="user">
124125
{{ item.name }}
125126
</div>
126127
</RecycleScroller>
@@ -150,12 +151,12 @@ export default {
150151

151152
### Important notes
152153

153-
- ⚠️ You need to set the size of the virtual-scroller element and the items elements (for example, with CSS). Unless you are using [variable height mode](#variable-height-mode), all items should have the same height to prevent display glitches.
154-
- If the items are objects, the scroller needs to be able to identify them. By default it will look for an `id` field on the items. This can be configured with the `keyField` prop.
154+
- **⚠️ You need to set the size of the virtual-scroller element and the items elements (for example, with CSS). Unless you are using [variable size mode](#variable-size-mode), all items should have the same height (or width in horizontal mode) to prevent display glitches.**
155+
- **⚠️ If the items are objects, the scroller needs to be able to identify them. By default it will look for an `id` field on the items. This can be configured with the `keyField` prop if you are using another field name.**
155156
- It is not recommended to use functional components inside RecycleScroller since the components are reused (so it will actually be slower).
156157
- The components used in the list should expect `item` prop change without being re-created (use computed props or watchers to properly react to props changes!).
157158
- You don't need to set `key` on list content (but you should on all nested `<img>` elements to prevent load glitches).
158-
- The browsers have a height limitation on DOM elements, it means that currently the virtual scroller can't display more than ~500k items depending on the browser.
159+
- The browsers have a size limitation on DOM elements, it means that currently the virtual scroller can't display more than ~500k items depending on the browser.
159160
- Since DOM elements are reused for items, it's recommended to define hover styles using the provided `hover` class instead of the `:hover` state selector (e.g. `.vue-recycle-scroller__item-view.hover` or `.hover .some-element-inside-the-item-view`).
160161

161162
### How does it work?
@@ -165,7 +166,7 @@ export default {
165166
- For each type of item, a new pool is created so that the same components (and DOM trees) are reused for the same type.
166167
- Views can be deactivated if they go off-screen, and can be reused anytime for a newly visible item.
167168

168-
Here is what the internals of RecycleScroller look like:
169+
Here is what the internals of RecycleScroller look like in vertical mode:
169170

170171
```html
171172
<RecycleScroller>
@@ -194,9 +195,10 @@ When the user scrolls inside RecycleScroller, the views are mostly just moved ar
194195
### Props
195196

196197
- `items`: list of items you want to display in the scroller.
197-
- `itemHeight` (default: `null`): display height of the items in pixels used to calculate the scroll height and position. If it set to `null` (the default value), it will use [variable height mode](#variable-height-mode).
198-
- `minItemHeight`: minimum height used if the height of a item is unknown.
199-
- `heightField` (default: `'height'`): field used to get the item's height in variable height mode.
198+
- `direction` (default: `'vertical'`): scrolling direction, either `'vertical'` or `'horizontal'`.
199+
- `itemSize` (default: `null`): display height (or width in horizontal mode) of the items in pixels used to calculate the scroll size and position. If it set to `null` (the default value), it will use [variable size mode](#variable-size-mode).
200+
- `minItemSize`: minimum size used if the height (or width in horizontal mode) of a item is unknown.
201+
- `sizeField` (default: `'size'`): field used to get the item's size in variable size mode.
200202
- `typeField` (default: `'type'`): field used to differenciate different kinds of components in the list. For each distinct type, a pool of recycled items will be created.
201203
- `keyField` (default: `'id'`): field used to identify items and optimize render views management.
202204
- `pageMode` (default: `false`): enable [Page mode](#page-mode).
@@ -221,14 +223,32 @@ When the user scrolls inside RecycleScroller, the views are mostly just moved ar
221223

222224
```html
223225
<main>
224-
<slot name="before-container"></slot>
226+
<slot name="before"></slot>
225227
<wrapper>
226228
<!-- Reused view pools here -->
227229
</wrapper>
228-
<slot name="after-container"></slot>
230+
<slot name="after"></slot>
229231
</main>
230232
```
231233

234+
Example:
235+
236+
```html
237+
<RecycleScroller
238+
class="scroller"
239+
:items="list"
240+
:item-size="32"
241+
>
242+
<template #before>
243+
Hey! I'm a message displayed before the items!
244+
</template>
245+
246+
<div v-slot="{ item }" class="user">
247+
{{ item.name }}
248+
</div>
249+
</RecycleScroller>
250+
```
251+
232252
### Page mode
233253

234254
The page mode expand the virtual-scroller and use the page viewport to compute which items are visible. That way, you can use it in a big page with HTML elements before or after (like a header and a footer). Just set the `page-mode` props to `true`:
@@ -247,15 +267,15 @@ The page mode expand the virtual-scroller and use the page viewport to compute w
247267
</footer>
248268
```
249269

250-
### Variable height mode
270+
### Variable size mode
251271

252272
**⚠️ This mode can be performance heavy with a lot of items. Use with caution.**
253273

254-
If the `itemHeight` prop is not set or set to `null`, the virtual scroller will switch to Variable height mode. You then need to expose a number field on the item objects with the height of the item element.
274+
If the `itemSize` prop is not set or set to `null`, the virtual scroller will switch to Variable size mode. You then need to expose a number field on the item objects with the size of the item element.
255275

256-
**⚠️ You still need to set the height of the items with CSS correctly (with classes for example).**
276+
**⚠️ You still need to set the size of the items with CSS correctly (with classes for example).**
257277

258-
Use the `heightField` prop (default is `'height'`) to set the field used by the scroller to get the height for each item.
278+
Use the `sizeField` prop (default is `'size'`) to set the field used by the scroller to get the size for each item.
259279

260280
Example:
261281

@@ -264,17 +284,17 @@ const items = [
264284
{
265285
id: 1,
266286
label: 'Title',
267-
height: 64,
287+
size: 64,
268288
},
269289
{
270290
id: 2,
271291
label: 'Foo',
272-
height: 32,
292+
size: 32,
273293
},
274294
{
275295
id: 3,
276296
label: 'Bar',
277-
height: 32,
297+
size: 32,
278298
},
279299
]
280300
```
@@ -296,25 +316,25 @@ The `prerender` props can be set as the number of items to render on the server
296316
```html
297317
<RecycleScroller
298318
:items="items"
299-
:item-height="42"
319+
:item-size="42"
300320
:prerender="10"
301321
>
302322
```
303323

304324
## DynamicScroller
305325

306-
This works like RecycleScroller but can render items with unknown heights!
326+
This works like RecycleScroller but can render items with unknown sizes!
307327

308328
### Basic usage
309329

310330
```html
311331
<template>
312332
<DynamicScroller
313333
:items="items"
314-
:min-item-height="54"
334+
:min-item-size="54"
315335
class="scroller"
316336
>
317-
<template slot-scope="{ item, index, active }">
337+
<template v-slot="{ item, index, active }">
318338
<DynamicScrollerItem
319339
:item="item"
320340
:active="active"
@@ -354,15 +374,15 @@ export default {
354374

355375
### Important notes
356376

357-
- `minItemHeight` is required for the initial render of items.
377+
- `minItemSize` is required for the initial render of items.
358378
- `DynamicScroller` won't detect size changes on its own, but you can put values that can affect the item size with `size-dependencies` on [DynamicScrollerItem](#dynamicscrolleritem).
359-
- You don't need to have a `height` field on the items.
379+
- You don't need to have a `size` field on the items.
360380

361381
### Props
362382

363383
All the RecycleScroller props.
364384

365-
- It's not recommended to change `heightField` prop since all the height management is done internally.
385+
- It's not recommended to change `sizeField` prop since all the size management is done internally.
366386

367387
### Events
368388

dist/vue-virtual-scroller.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)