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

virtual-scroll-component

v1.0.4

Published

A container component for virtualized scroll.

Downloads

145

Readme

Virtual Scroll Component

Virtual and infinite scrolling for a list of elements. A wrapper component to be used in the React framework.

Motivation

Rendering hundreds of elements in DOM can be slow, especially if your elements are non-trivial. Instead, we can simply render elements as necessary when they are scrolled into view. This is called virtual scrolling.

As you can imagine, rendering only view-able elements can cause unnecessary document scrolling as the position of HTML elements are usually relative. We can fix this by wrapping a container with fixed height around every element, and only render the child element when necessary.

Installation

Install through npm or yarn.

npm install virtual-scroll-component 
yarn add virtual-scroll-component

Usage

import VirtualScroll from 'virtual-scroll-component';

Props

rows

Use rows to pass an array of component instances to be placed in a virtualized scroll container. This can also be done with props.children, but do note that any arguments passed to props.children takes precedence over rows.

<VirtualScroll rows={[<div/>, <div/>]}/>
<VirtualScroll rows={[<span/>]}>
    <div/> // div will be used, and not span
</VirtualScroll>

rowHeight

Use rowHeight to set the height of each row. Accepts an integer and is interpreted in CSS pixels.

<VirtualScroll rowHeight={100}/> 

onLastRow

Use onLastRow to set a function to be called every time the last row is in view. You can pass a function which increases the number of component instances, thereby creating an infinite scroll.

const [rows, setRows] = useState([<Child/>]);
const handleLastRow = () => {
    setRows([...rows, <Child/>])
}

return (<VirtualScroll rows={rows} onLastRow={handleLastRow}/>);

className

Use className to add a CSS class onto the VirtualScroll component. By default, elements are rendered vertically. Use className to override default styles.

<VirtualScroll className={"custom-class-name"}/>

props.children

Use props.children to pass a list of component instances to be placed in a virtualized scroll container. This can also be done with rows, but do note that any arguments passed to props.children takes precedence over rows.

<VirtualScroll>
    <div/>
    <div/>
</VirtualScroll>

Pitfalls

This package does not support elements with varying heights.

Development

There are some scripts available in package.json.

  • yarn start - to watch for file changes and update automatically with webpack
  • yarn build - to build the package and store in ./dist/index.js
  • yarn jest - to run unit tests

I welcome any from of participation, so feel free to submit an issue or make a pull request.

Acknowledgement

Big thanks to BP mishra for his guidance throughout this project!