npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-virtual-dynamic-list

v0.5.13

Published

Yes, yet another React Virtual List... Why?

Downloads

254

Readme

React Virtual Dynamic List

Yes, yet another React Virtual List... Why?

  • Provides dynamic item heights
  • Only measures what it draws, estimates the rest and keeps everything smooth by adjusting scroll position when sizes are discovered later
  • Does not need to measure intermediate items when large scrolling, massively improving performance
  • Does not need an accurate estimated item height
  • Allows items to change size at any time
  • Allows natural browser layout of the components on screen within their standard container, no individual item positioning
  • Works in environments that don't constantly fire scroll events (e.g. iOS)

This component uses a O(Ln2) algorithm to work out the position of items and caches all of this for maximum performance even when scrolling huge distances.

Supports list of up to 1,000,000 pixels in height (due to browser limitations on pixel heights). Provides events that enable any number of items.

Installation

npm i react-virtual-dynamic-list

Usage

    <Virtual items={someCollection} renderItem={item=>(<div>{item.id}</div>)}/>

or

    <Virtual items={100000} renderItem={item=>(<div>Item number {item + 1}</div>)}/>

Parameters

(Required) items - array | number of items

Provides the items that will be rendered, if an array is used, the contents are passed to the renderItem function as context, otherwise the index is passed

(Required) renderItem - function (item|index, index)

A function to render the item. The first parameter is the item or the item's index. The second is always the index.

scrollTop - the scroll position of the component in pixels (default to 0)

scrollToItem - scroll to show the specified index at the top of the display

Wrapper - the wrapper for items in the grid (defaults to <div/>)

If you need your items to render properly inside a wrapper component then you can provide it here.

Your component must apply the style prop passed to it and render children

Holder - the overall holder for the grid (defaults to <div/>)

If want to provider a component to render the whole of the virtual list you can provide it here.

Your component must apply the style prop passed to it, take a ref via forwardRef and apply it to the root and render children

useAnimation - should animation be used to help position items e.g. for iOS (defaults to true)

Animation is used in addition to scrolling. A very minor overhead.

overscan - the number of component heights to apply as overscan (defaults to 1)

Provides a number of pages of overscan

expectedHeight - the expected height, can be very rough (defaults to 64)

Heights are worked out from averages after the first render, so something rough is fine.

onInit - function({getPositionOf, getHeightOf, getItemFromPosition, itemCache})

Provides access to some api functions that can be useful for modifying the list. Often you will cache these for later use.

onSize - function ({averageHeight, height, item})

Provides useful information on item sizes so you can resize the component, the most useful being averageHeight of an item based on the items drawn.

getPositionOf(item) - returns the position of an item
getHeightOf(item) - returns the height of an item (may be estimated if not measured)
getItemFromPosition(position) - gets the item that is at a scroll position
itemCache - a Map containing the cache of all currently rendered items, you can clear this

if you like at any time.

onScroll - function({items, start, last, scrollPos, max, scroller})

Provides an event that can modify the scroll. You may change items in this function. If you insert things above you are probably going to have to update the scroll position afterwards.

scroller - the element being scrolled

you might want to fiddle with scrollTop when you add things (especially if going upwards)

items - the items being rendererd
start - the first item being rendered (off screen above)
last - the last item being rendererd (off screen below)
max - the last item that has been rendered ever (useful for loading more)
Also contains the parameters passed to onInit
    function onScroll({max, items}) {
        if (max > items.length - 15) {
            items.push(...Array.from({length: 15}, (_, index) => ({
                id: index + items.length,
                height: Math.random() * 98 + 32 | 0,
                color: rgb(Math.random() * 112 + 143,
                    Math.random() * 112 + 143, Math.random() * 112 + 143)
            })))
        }
    }

OTHER PROPERTIES

Are passed to the wrapping div that does the scrolling (this is not Wrapper, that holds the actual items).

    <Virtual items={1000} renderItem={i=>(<div>{i}</div>)} width={80} height={200}/>

Sets the size of the rendered div to 80 x 200

ScrollIndicatorHolder

This is a component you can use as a Holder for the Virtual component. It uses shadows to indicate that scrolling is possible.

You can pass a shadow parameter throw to it via the Virtual.

    <Virtual shadow={'0 0 32px 14px black'} items={[...items]} Holder={ScrollIndicatorHolder} renderItem={item => {
        return <Item item={item}/>
    }}/>

useMeasurement

Provides the ability to measure a component using a resizeObserver - so it will redraw on resize too which is handy. ResizeObserver is pony filled.


    const [size, ref] = useMeasurement() 
    return <div ref={ref}>Something is {size.width} x {size.height} at {size.left}, {size.top} </div>