Skip to content

Commit cd80df3

Browse files
author
Claudéric Demers
authored
Merge pull request #55 from clauderic/sticky-headers
Add support for sticky items
2 parents 6d3e8a3 + e881d57 commit cd80df3

File tree

5 files changed

+192
-44
lines changed

5 files changed

+192
-44
lines changed

README.md

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
</div>
44

55
# react-tiny-virtual-list
6+
67
> A tiny but mighty list virtualization library, with zero dependencies 💪
78
89
[![npm version](https://img.shields.io/npm/v/react-tiny-virtual-list.svg)](https://www.npmjs.com/package/react-tiny-virtual-list)
@@ -13,28 +14,30 @@
1314
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/clauderic/react-tiny-virtual-list/blob/master/LICENSE)
1415
[![Gitter](https://badges.gitter.im/clauderic/react-tiny-virtual-list.svg)](https://gitter.im/clauderic/react-tiny-virtual-list)
1516

16-
* **Tiny & dependency free** – Only 3kb gzipped
17-
* **Render millions of items**, without breaking a sweat
18-
* **Scroll to index** or **set the initial scroll offset**
19-
* **Supports fixed** or **variable** heights/widths
20-
* **Vertical** or **Horizontal** lists
17+
- **Tiny & dependency free** – Only 3kb gzipped
18+
- **Render millions of items**, without breaking a sweat
19+
- **Scroll to index** or **set the initial scroll offset**
20+
- **Supports fixed** or **variable** heights/widths
21+
- **Vertical** or **Horizontal** lists
2122

2223
Check out the [demo](https://clauderic.github.io/react-tiny-virtual-list/) for some examples, or take it for a test drive right away in [Code Sandbox](https://codesandbox.io/s/kymm7z9qr).
2324

24-
Getting Started
25-
---------------
25+
## Getting Started
2626

2727
Using [npm](https://www.npmjs.com/):
28+
2829
```
2930
npm install react-tiny-virtual-list --save
3031
```
3132

3233
ES6, CommonJS, and UMD builds are available with each distribution. For example:
34+
3335
```js
3436
import VirtualList from 'react-tiny-virtual-list';
3537
```
3638

3739
You can also use a global-friendly UMD build:
40+
3841
```html
3942
<script src="react-tiny-virtual-list/umd/react-tiny-virtual-list.js"></script>
4043
<script>
@@ -43,8 +46,7 @@ var VirtualList = window.VirtualList;
4346
</script>
4447
```
4548

46-
Example usage
47-
-------------
49+
## Example usage
4850

4951
```js
5052
import React from 'react';
@@ -70,45 +72,53 @@ render(
7072
```
7173

7274
### Prop Types
73-
| Property | Type | Required? | Description |
74-
|:------------------|:------------------|:----------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
75-
| width | Number or String* || Width of List. This property will determine the number of rendered items when scrollDirection is `'horizontal'`. |
76-
| height | Number or String* || Height of List. This property will determine the number of rendered items when scrollDirection is `'vertical'`. |
77-
| itemCount | Number || The number of items you want to render |
78-
| renderItem | Function || Responsible for rendering an item given it's index: `({index: number, style: Object}): React.PropTypes.node`. The returned element must handle key and style. |
79-
| itemSize | || Either a fixed height/width (depending on the scrollDirection), an array containing the heights of all the items in your list, or a function that returns the height of an item given its index: `(index: number): number` |
80-
| scrollDirection | String | | Whether the list should scroll vertically or horizontally. One of `'vertical'` (default) or `'horizontal'`. |
81-
| scrollOffset | Number | | Can be used to control the scroll offset; Also useful for setting an initial scroll offset |
82-
| scrollToIndex | Number | | Item index to scroll to (by forcefully scrolling if necessary) |
83-
| scrollToAlignment | String | | Used in combination with `scrollToIndex`, this prop controls the alignment of the scrolled to item. One of: `'start'`, `'center'`, `'end'` or `'auto'`. Use `'start'` to always align items to the top of the container and `'end'` to align them bottom. Use `'center`' to align them in the middle of the container. `'auto'` scrolls the least amount possible to ensure that the specified `scrollToIndex` item is fully visible. |
84-
| overscanCount | Number | | Number of extra buffer items to render above/below the visible items. Tweaking this can help reduce scroll flickering on certain browsers/devices. |
85-
| estimatedItemSize | Number | | Used to estimate the total size of the list before all of its items have actually been measured. The estimated total height is progressively adjusted as items are rendered. |
86-
| onItemsRendered | Function | | Callback invoked with information about the slice of rows/columns that were just rendered. It has the following signature: `({startIndex: number, stopIndex: number})`. |
87-
| onScroll | Function | | Callback invoked whenever the scroll offset changes within the inner scrollable region. It has the following signature: `(scrollTop: number, event: React.UIEvent<HTMLDivElement>)`. |
88-
89-
*\* Width may only be a string when `scrollDirection` is `'vertical'`. Similarly, Height may only be a string if `scrollDirection` is `'horizontal'`*
75+
76+
| Property | Type | Required? | Description |
77+
| :---------------- | :----------------- | :-------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
78+
| width | Number \| String\* || Width of List. This property will determine the number of rendered items when scrollDirection is `'horizontal'`. |
79+
| height | Number \| String\* || Height of List. This property will determine the number of rendered items when scrollDirection is `'vertical'`. |
80+
| itemCount | Number || The number of items you want to render |
81+
| renderItem | Function || Responsible for rendering an item given it's index: `({index: number, style: Object}): React.PropTypes.node`. The returned element must handle key and style. |
82+
| itemSize | || Either a fixed height/width (depending on the scrollDirection), an array containing the heights of all the items in your list, or a function that returns the height of an item given its index: `(index: number): number` |
83+
| scrollDirection | String | | Whether the list should scroll vertically or horizontally. One of `'vertical'` (default) or `'horizontal'`. |
84+
| scrollOffset | Number | | Can be used to control the scroll offset; Also useful for setting an initial scroll offset |
85+
| scrollToIndex | Number | | Item index to scroll to (by forcefully scrolling if necessary) x |
86+
| scrollToAlignment | String | | Used in combination with `scrollToIndex`, this prop controls the alignment of the scrolled to item. One of: `'start'`, `'center'`, `'end'` or `'auto'`. Use `'start'` to always align items to the top of the container and `'end'` to align them bottom. Use `'center`' to align them in the middle of the container. `'auto'` scrolls the least amount possible to ensure that the specified `scrollToIndex` item is fully visible. |
87+
| stickyIndices | Number[] | | An array of indexes (eg. `[0, 10, 25, 30]`) to make certain items in the list sticky (`position: sticky`) |
88+
| overscanCount | Number | | Number of extra buffer items to render above/below the visible items. Tweaking this can help reduce scroll flickering on certain browsers/devices. |
89+
| estimatedItemSize | Number | | Used to estimate the total size of the list before all of its items have actually been measured. The estimated total height is progressively adjusted as items are rendered. |
90+
| onItemsRendered | Function | | Callback invoked with information about the slice of rows/columns that were just rendered. It has the following signature: `({startIndex: number, stopIndex: number})`. |
91+
| onScroll | Function | | Callback invoked whenever the scroll offset changes within the inner scrollable region. It has the following signature: `(scrollTop: number, event: React.UIEvent<HTMLDivElement>)`. |
92+
93+
_\* Width may only be a string when `scrollDirection` is `'vertical'`. Similarly, Height may only be a string if `scrollDirection` is `'horizontal'`_
9094

9195
### Public Methods
9296

9397
#### recomputeSizes (index: number)
98+
9499
This method force recomputes the item sizes after the specified index (these are normally cached).
95100

96101
`VirtualList` has no way of knowing when its underlying data has changed, since it only receives a itemSize property. If the itemSize is a `number`, this isn't an issue, as it can compare before and after values and automatically call `recomputeSizes` internally.
97-
However, if you're passing a function to `itemSize`, that type of comparison is error prone. In that event, you'll need to call `recomputeSizes` manually to inform the `VirtualList` that the size of its items has changed.
102+
However, if you're passing a function to `itemSize`, that type of comparison is error prone. In that event, you'll need to call `recomputeSizes` manually to inform the `VirtualList` that the size of its items has changed.
103+
104+
### Common Issues with PureComponent
98105

99-
### Common Issues with PureComponent
100106
`react-tiny-virtual-list` uses [PureComponent](https://reactjs.org/docs/react-api.html#reactpurecomponent), so it only updates when it's props change. Therefore, if only the order of your data changes (eg `['a','b','c']` => `['d','e','f']`), `react-tiny-virtual-list` has no way to know your data has changed and that it needs to re-render.
101107

102108
You can force it to re-render by calling [forceUpdate](https://reactjs.org/docs/react-component.html#forceupdate) on it or by passing it an extra prop that will change every time your data changes.
103109

104110
## Reporting Issues
111+
105112
Found an issue? Please [report it](https://github.com/clauderic/react-tiny-virtual-list/issues) along with any relevant details to reproduce it. If you can, please provide a live demo replicating the issue you're describing. You can [fork this Code Sandbox](https://codesandbox.io/s/kymm7z9qr) as a starting point.
106113

107114
## Contributions
115+
108116
Feature requests / pull requests are welcome, though please take a moment to make sure your contributions fits within the scope of the project. [Learn how to contribute](https://github.com/clauderic/react-tiny-virtual-list/blob/master/CONTRIBUTING.md)
109117

110118
## Acknowledgments
119+
111120
This library draws inspiration from [react-virtualized](https://github.com/bvaughn/react-virtualized), and is meant as a bare-minimum replacement for the [List](https://github.com/bvaughn/react-virtualized/blob/master/docs/List.md) component. If you're looking for a tiny, lightweight and dependency-free list virtualization library that supports variable heights, you're in the right place! If you're looking for something that supports more use-cases, I highly encourage you to check out [react-virtualized](https://github.com/bvaughn/react-virtualized) instead, it's a fantastic library ❤️
112121

113122
## License
123+
114124
react-tiny-virtual-list is available under the MIT License.

0 commit comments

Comments
 (0)