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

@x1337/rlv

v0.0.3

Published

List Virtualization is a React component for efficiently rendering large lists by only rendering the visible items, improving performance and reducing memory consumption.

Downloads

5

Readme

List Virtualization

List Virtualization is a React component for efficiently rendering large lists by only rendering the visible items, improving performance and reducing memory consumption.

Installation

You can install the List Virtualization component via npm:

npm install @x1337/rlv

Usage

Basic Example

import React, { useState, useEffect } from 'react';
import { ListVirtualization } from '@x1337/rlv';

function MyComponent() {
  const [items, setItems] = useState([]);
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    // Fetch initial items
    fetchItems();
  }, []);

  const fetchItems = async () => {
    setLoading(true);
    // Simulate fetching items from an API
    await new Promise((resolve) => setTimeout(resolve, 1000));
    const newItems = [...items, ...Array.from({ length: 10 }, (_, index) => `Item ${items.length + index}`)];
    setItems(newItems);
    setLoading(false);
  };

  return (
    <div>
      <h1>Virtualized List Example</h1>
      <ListVirtualization
        items={items}
        itemHeight={50}
        numberOfItems={100}
        loadingIndicator={loading && <div>Loading...</div>}
        fetchItems={fetchItems}
        containerStyle={{ height: '300px', border: '1px solid #ccc' }}
        elementWrapperStyle={{ padding: '5px', borderBottom: '1px solid #eee' }}
        itemRenderer={(item, index) => <div>{item}</div>}
        onReachEnd={() => {
          if (!loading) {
            fetchItems();
          }
        }}
      />
    </div>
  );
}

export default MyComponent;

Advanced Example with Dynamic Item Heights

import React, { useState, useEffect } from 'react';
import { ListVirtualization } from '@x1337/rlv';

function MyComponent() {
  const [items, setItems] = useState([]);
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    // Fetch initial items
    fetchItems();
  }, []);

  const fetchItems = async () => {
    setLoading(true);
    // Simulate fetching items from an API
    await new Promise((resolve) => setTimeout(resolve, 1000));
    const newItems = [...items, ...Array.from({ length: 10 }, (_, index) => `Item ${items.length + index}`)];
    setItems(newItems);
    setLoading(false);
  };

  const calculateItemHeight = (index) => {
    // Simulate dynamic item heights
    return Math.floor(Math.random() * 100) + 50; // Random height between 50 and 150
  };

  return (
    <div>
      <h1>Virtualized List Example with Dynamic Heights</h1>
      <ListVirtualization
        items={items}
        itemHeight={calculateItemHeight}
        numberOfItems={100}
        loadingIndicator={loading && <div>Loading...</div>}
        fetchItems={fetchItems}
        containerStyle={{ height: '300px', border: '1px solid #ccc' }}
        elementWrapperStyle={{ padding: '5px', borderBottom: '1px solid #eee' }}
        itemRenderer={(item, index) => <div>{item}</div>}
        onReachEnd={() => {
          if (!loading) {
            fetchItems();
          }
        }}
      />
    </div>
  );
}

export default MyComponent;

Example with Custom Container Style and Loading Indicator

<ListVirtualization
  items={items}
  itemHeight={50}
  numberOfItems={100}
  loadingIndicator={<Spinner />}
  containerStyle={{ border: '1px solid #ccc', padding: '10px' }}
  itemRenderer={(item, index) => <div>{item}</div>}
/>

Example with Dynamic Item Heights

<ListVirtualization
  items={items}
  itemHeight={(index) => Math.floor(Math.random() * 100) + 50}
  numberOfItems={100}
  loadingIndicator={<div>Loading...</div>}
  itemRenderer={(item, index) => <div>{item}</div>}
/>

Example with Initial Scroll Position

<ListVirtualization
  items={items}
  itemHeight={50}
  numberOfItems={100}
  initialScrollTop={200}
  loadingIndicator={<div>Loading...</div>}
  itemRenderer={(item, index) => <div>{item}</div>}
/>

Example with Custom Key Getter

<ListVirtualization
  items={items}
  itemHeight={50}
  numberOfItems={100}
  getKey={(index) => `item-\${index}`}
  loadingIndicator={<div>Loading...</div>}
  itemRenderer={(item, index) => <div>{item}</div>}
/>

Example with onReachEnd Callback

<ListVirtualization
  items={items}
  itemHeight={50}
  numberOfItems={100}
  onReachEnd={() => console.log('Reached end of list')}
  loadingIndicator={<div>Loading...</div>}
  itemRenderer={(item, index) => <div>{item}</div>}
/>

Example with Fetch Items Function

<ListVirtualization
  items={items}
  itemHeight={50}
  numberOfItems={100}
  fetchItems={() => fetchMoreItems()}
  loadingIndicator={<div>Loading...</div>}
  itemRenderer={(item, index) => <div>{item}</div>}
/>

Props

  • items (Array): Array of items to render.
  • itemHeight (Number | Function): Height of each item or a function to calculate the height dynamically.
  • numberOfItems (Number): Total number of items.
  • containerStyle (Object): Custom styles for the container.
  • elementWrapperStyle (Object): Custom styles for the element wrapper.
  • loadingIndicator (ReactNode): Loading indicator to show when new items are being loaded.
  • itemRenderer (Function): Custom renderer for each item.
  • initialScrollTop (Number): Initial scroll position.
  • getKey (Function): Function to get a unique key for each item.
  • onReachEnd (Function): Callback when scrolling reaches the end of the list.
  • fetchItems (Function): Function to fetch more items when scrolling reaches the end.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.