0.0.5 • Published 7 months ago

solidjs-virtualizedlist v0.0.5

Weekly downloads
-
License
-
Repository
github
Last release
7 months ago

SolidJs Virtualized List

A lightweight, highly efficient virtualized list component for SolidJs that supports both horizontal and vertical rows, allowing you to efficiently render large datasets with ease.

Features

  1. Supports both horizontal and vertical rows.
  2. Efficiently handles large datasets by rendering only the visible elements.
  3. Provides a scrollTo function to programmatically scroll to a specific item.
npm i solidjs-virtualizedlist
Usage Example
import { createSignal } from "solid-js";
import { render } from "solid-js/web";
import { VirtualizedList } from "./virtualized-list.jsx";

const root = document.getElementById("root");

// Generate data for the list
let data = Array.from({ length: 1000000 }).map((v, i) => i + 1);

let scrollTo; // Declare a variable to hold the scrollTo function

const [scrollToKey, setScrollToKey] = createSignal(0); // Create a signal for the scroll target index

render(
  () => (
    <div>
      <VirtualizedList
        options={{
          containerSize: 500, // Height of the visible container
          dataLength: data.length, // Total length of the dataset
          cellHeightWidth: 20, // Height of each item in the list
          overscan: 10, // Number of extra items to render above and below the visible area
          direction: "y", // Direction of the list (vertical in this case)
          scrollTo: (cb) => (scrollTo = cb), // Callback to set the scrollTo function
          onScroll: (e) => {
            console.log(e); // Event handler for scroll events
          },
        }}
        parentContainerProps={{
          style: {
            width: "250px",
          },
        }}
        scrollContainerProps={{}}
      >
        {({ index }) => {
          return data[index] ? (
            <p style={{ height: "100%" }}>!{data[index]}</p> // Rendered item in the list
          ) : null;
        }}
      </VirtualizedList>
      <input
        type="number"
        placeholder="Index"
        onChange={(e) => setScrollToKey(() => e.target.value)}
      />
      <button onClick={() => scrollTo(+scrollToKey())}>Scroll</button> // Button
      to trigger scroll
    </div>
  ),
  root
);
PropertyTypeDescription
children({ index }: { index: number }) => JSX.ElementFunction that takes an index and returns a React element to be rendered.
optionsVirtualizedListOptionsConfiguration options for the virtualized list.
parentContainerPropsReact.DetailedHTMLProps<React.HTMLAttributes, HTMLDivElement> (optional)HTML attributes for the parent container element.
scrollContainerPropsReact.DetailedHTMLProps<React.HTMLAttributes, HTMLDivElement> (optional)HTML attributes for the scroll container element.

VirtualizedListOptions

PropertyTypeDescription
containerSizenumberHeight of the visible container.
overscannumber (optional)Number of extra items to render above and below the visible area.
dataLengthnumberTotal length of the dataset.
cellHeightWidthnumberHeight or Width of each item in the list.
direction"x" | "y" (optional, default: "y")Direction of the list (horizontal or vertical).
scrollTo(cb: (index: number) => void) => void (optional)Callback to set the scrollTo function.
debouncenumber (optional)Debounce value for scroll events (milliseconds).
onScroll(event: Event) => void (optional)Event handler for scroll events.
0.0.5

7 months ago

0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago

0.0.0

7 months ago