@candidstartup/react-virtual-scroll
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

NPM Type Definitions NPM Version NPM bundle size

@candidstartup/react-virtual-scroll

React virtual scrolling components for lists and grids following the same philosophy as react-window. Written in TypeScript using modern React. Scalable to trillions of rows and columns.

Interface

The interface is similar to react-window with two main changes. First, the components are functional rather than class based. Second, rather than having fixed and variable size variants of each component, the components are passed an ItemOffsetMapping object as a prop. This is an interface that the components query to determine the size and position of each item.

The interface is designed to ensure that the components have no scaling issues even with trillions of rows or columns. The package includes basic fixed and variable size item mappings.

Implementation

Most of the logic is implemented by custom hooks that are used by both VirtualList and VirtualGrid controls. The logic that manages scrolling in a single dimension is implemented in useVirtualScroll. It's based on an improved version of SlickGrid's paged scrolling algorithm. One instance of the hook is used by VirtualList and two instances by VirtualGrid.

VirtualList Example

import { VirtualList, useVariableSizeItemOffsetMapping } from '@candidstartup/react-virtual-scroll';

const mapping = useVariableSizeItemOffsetMapping(30, [50]);
const list = React.createRef();

...

<VirtualList
  ref={list}
  height={240}
  itemCount={1000000000000}
  itemOffsetMapping={mapping}
  useIsScrolling={true}
  width={600}>
  {Row}
</VirtualList>

Check out the full sample or try it out on CodeSandbox

VirtualGrid Example

import { VirtualList, useVariableSizeItemOffsetMapping, useFixedSizeItemOffsetMapping } from '@candidstartup/react-virtual-scroll';

const rowMapping = useVariableSizeItemOffsetMapping(30, [50]);
const columnMapping = useFixedSizeItemOffsetMapping(280);
const grid = React.createRef();

...

<VirtualGrid
  ref={grid}
  height={240}
  rowCount={1000000000000}
  rowOffsetMapping={rowMapping}
  columnCount={1000000000000}
  columnOffsetMapping={columnMapping}
  width={600}>
  {Cell}
</VirtualGrid> 

Check out the full sample or try it out on CodeSandbox

More

Want to know more? Check out my blog

Package Sidebar

Install

npm i @candidstartup/react-virtual-scroll

Weekly Downloads

9

Version

0.2.0

License

BSD-3-Clause

Unpacked Size

69.2 kB

Total Files

6

Last publish

Collaborators

  • timwiegand