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

@devoinc/virtual-list

v1.0.0

Published

## Description Library allows you to display a virtual infinite list of items by only showing visible items on screen. This is very useful for very long list of items. The most important features are that the library provides dynamic item heights, only me

Downloads

4

Readme

Virtual List

Description

Library allows you to display a virtual infinite list of items by only showing visible items on screen. This is very useful for very long list of items. The most important features are that the library provides dynamic item heights, only measures what it draws, it doesn't need an accurate estimated item height and it allows items to change size at any time. This library, due to how it is implemented, substantially improves performance on occasions when the collection is modified over time, such as in streaming processes.

Installation

npm i @devoinc/virtual-list

Usage

    <VirtualList items={theItemList} renderItem={item=>(<div>{item.id}</div>)}/>

or

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

Props type

(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

  • 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.

(Optional)

  • 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 html tag 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 (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.

    • 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.
  • 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.

  • 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).

    <VirtualList items={1000} renderItem={item=>(<div>{item}</div>)} width={200} height={400}/>
  • ScrollIndicatorHolder

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

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

    <VirtualList 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>