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-base-virtual-list

v1.0.1

Published

React base virtual list component.

Downloads

318

Readme

react-base-virtual-list GitHub License NPM Version GitHub Actions Workflow Status npm bundle size

中文

React basic virtual list, supports common features and is easy to customize. Online Demo

Features

  • Supports common features and is easy to customize. Check Development.
  • Supports lists with fixed height or dynamic height items.
  • High performance. For lists with different item heights, it does not retrieve the height of each item.
  • Formats of exported files: typescript, cjs, esm, iife, iife source map. iife can be use by script tag in browser, see IIFE.

Installation

  • npm: npm install react-base-virtual-list
  • yarn: yarn add react-base-virtual-list
  • pnpm: pnpm add react-base-virtual-list
  • CDN: Check iife.

Usage

import { VirtualList } from "react-base-virtual-list";

export default function BaseExample() {
  const exampleData = [
    {
      headline: "in magna bibendum imperdiet",
      content: "Praesent blandit. Nam nulla.",
    },
    {
      headline: "non velit donec diam",
      content: "Aenean fermentum.",
    },
  ];
  return (
    <>
      <VirtualList
        items={exampleData}
        style={{ height: "600px", border: "1px solid #ccc", padding: "10px" }}
        renderItem={(item, index) => (
          <div key={index} style={{ marginBottom: "10px" }}>
            <h3>
              {index}. {item.headline}
            </h3>
            <div>
              <div
                style={{
                  float: "left",
                  width: "100px",
                  height: "100px",
                  background: "#f0f0f0",
                  borderRadius: "5px",
                  marginRight: "10px",
                }}
              ></div>
              {item.content}
            </div>
          </div>
        )}
      ></VirtualList>
    </>
  );
}

props (required)

  • items: Array. List data.
  • renderItem: (item, index: number) => ReactNode. Rendering function for each item in the list. Index is the index of the list item in the whole list.

props (optional)

  • itemSize: number. Estimated height of a single item in the list.
  • buffer: number. Additional space outside the visible area of the virtual list to render.
  • persistentIndices: number[]. Array of indices of items to be persistently rendered. This keeps the corresponding items rendered continuously without being removed due to being outside the rendering area. You can make them sticky by using CSS position:sticky.
  • listSize: number, default: 1000. Height of the visible area of the list. Only used before DOM creation, suitable for SSR.
  • triggerDistance: number. The min distance to trigger re-rendering when scrolling.
  • onScroll: React.UIEventHandler. Listen for the list's scroll event. Type is same with HTML native onscroll handle.
  • virtual: boolean. default: true. Whether to enable the virtual list feature. Render all items if disabled.
  • renderHead: () => ReactNode. Customize the persistent rendering content of the list header. Suitable for absolutely positioned, fixed positioned elements, and elements that do not take up too much space.
  • renderFoot: () => ReactNode. Customize the persistent rendering content of the list footer. Suitable for absolutely positioned, fixed positioned elements, and elements that do not take up too much space.
  • className: string. Add a CSS class to the list root element.
  • style: React.CSSProperties. Add CSS styles to the list root element.

Exposed Methods

First, use ref to obtain the exposed object.

import { useRef } from "react";
import { VirtualList, VirtualListHandle } from "react-base-virtual-list";

export default function BaseExample() {
  const ref = useRef<VirtualListHandle>(null);
  return (
    <>
      <VirtualList ref={ref}></VirtualList>
    </>
  );
}

Irrelevant parts are omitted in the above code. VirtualListHandle is a typescript type, please ignore it if you are using pure JS.

VirtualListHandle type code.

interface VirtualListHandle {
  scrollToIndex(
    index: number,
    block?: "start" | "end" | "center" | "nearest"
  ): void;
  getRootElement(): HTMLElement;
  forceUpdate(): void;
}

Then use the ref object to access the exposed methods.

  • scrollToIndex: (index:number, block = 'start'):void. Scroll to the specified index position. block is equal to the block option of the HTML native method scrollIntoView.
  • getRootElement: Get list HTML element。
  • forceUpdate: Forcefully re-render the list. This can be called after the visible area of the list changes.

Note

  • Remember to set the height of the list. Class, style, px, em, percentage, etc. are all acceptable.
  • Delayed loading, loading when scrolling to the bottom, etc. can be implemented using the exposed onScroll.

IIFE

The dist/index.iife.js file can be run in the browser. You can host it on your server and then use the script tag to include it. Before including it, you also need to include react, react-dom. The global variable exposed by this file is reactBaseVirtualList, you can access all the exports of this file through window.reactBaseVirtualList, and get the main component exported through window.reactBaseVirtualList.VirtualList.

You can also use the following third-party CDN url to include it.

  • unpkg: https://unpkg.com/react-base-virtual-list
  • jsdelivr: https://cdn.jsdelivr.net/npm/react-base-virtual-list

Development

  • lib: The main files, also the files that are packaged into the library. Running npm run build will package the files in this directory into the dist folder. The corresponding Vite configuration file is vite.build.ts.
  • src: The files used for development and debugging. Running npm run dev will run the code in this directory in the browser. Running npm run build:web will package the code in this directory into the dist folder. The corresponding Vite configuration file is vite.config.ts.
  • uno.config.ts: unocss configuration file. unocss only works in the src folder. With the current configuration, you can use Tailwindcss style class names.
  • .github/workflows/build.yml: Some automated actions performed when publishing to GitHub. You can delete or modify it.

Changelog

https://github.com/phphe/react-base-virtual-list/releases