A virtual scrolling container that supports dynamic child elements with heights that could change over time. ie. dynamic subtrees / images / iframes / etc.
In most other implementations of virtual scrolling, rows are assumed to contain static information that won't change over time. This is problematic because in a highly dynamic real time system, rows could contain large subtrees that may await for data prior to rendering. This prevents most virtual scrollers from accurately determining a row height on mount. The same issues apply for loading images and iframes, which involves asynchronous asset loading and subsequent height change.
This virtual scroller takes into account of heights changing over time, and is designed to react and correct scroll positions when row heights change. Implementation of fake rows is similar to other approaches where we use a top and bottom spacer and only renders rows within the viewport + some configurable allowance.
npm install
this package and
var vscroll = require('react-vscroll');
import { VContainer } from 'react-vscroll';
export default function Container() {
return (
<VContainer
className={ 'a-container-class' }
style={ { display: 'flex', flexDirection: 'column' } }
items={ items }
initialHeight={ 80 }
scrollBottomThreshold={ 400 }
stickBottom={ true }
renderItem={ function(item){ return <div id={ item.id }>{ item.text }</div> } }
/>
)
}
-
git clone
this package andnpm install
. -
npm start
. This will run the example here. -
Browse to
http://localhost:5000
Data for the list of items you want to virtual render.
The virtual scrolling algorithm works by assuming an initial height, then reactively applying height corrections over time.
renderItem
is passed args (item)
, where item is the value obtained from iterating items
Number of pixels from the bottom of the scroller where stickBottom
will no longer work. This is useful for cases where we want to prevent a chat log from auto scrolling to the bottom if the user is viewing a past message somewhere at the top of the chat log.
Standard className
prop used for JSX components
Standard style
prop used for JSX components
Use this function for conditionally rendering a component if the scroller is not at the bottom. This is useful for rendering floating buttons like "Scroll to bottom".
Determines whether the chat log will attempt to stick to the bottom on mount / update. This is mostly useful for chat logs where the user is viewing chat messages from bottom to top.
This allows you to specify hooks to run when the virtual scroller has been mounted.
This allows you to specify hooks to run when the virtual scroller has been unmounted.
Controls the number of rows that are rendered outside the viewport.
Improvements are always welcome. Please follow these steps to contribute
-
For proposed changes, please create a pull request
-
For bugs and feature requests, please create an issue
Use of this software is subject to important terms and conditions as set forth in the LICENSE file