|
| 1 | +# @phphe/react-base-virtual-list     |
| 2 | + |
| 3 | +[中文](README_CN.md) |
| 4 | + |
| 5 | +React Basic Virtual List. [Online Demo](https://phphe.github.io/react-base-virtual-list/) |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Supports lists with different item heights. |
| 10 | +- Simple and easy to extend, only contains common features. |
| 11 | +- High performance. For lists with different item heights, it does not retrieve the height of each item. |
| 12 | +- Exported files include TypeScript definition files, CJS files, ES files, IIFE files, and IIFE source maps. The IIFE file can be use by `script` tag in browser, see [IIFE](#iife). |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```sh |
| 17 | +npm install @phphe/react-base-virtual-list --save |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +```tsx |
| 23 | +import { VirtualList } from "@phphe/react-base-virtual-list"; |
| 24 | + |
| 25 | +export default function BaseExample() { |
| 26 | + const exampleData = [ |
| 27 | + { |
| 28 | + headline: "in magna bibendum imperdiet", |
| 29 | + content: "Praesent blandit. Nam nulla.", |
| 30 | + }, |
| 31 | + { |
| 32 | + headline: "non velit donec diam", |
| 33 | + content: "Aenean fermentum.", |
| 34 | + }, |
| 35 | + ]; |
| 36 | + return ( |
| 37 | + <> |
| 38 | + <VirtualList |
| 39 | + items={exampleData} |
| 40 | + style={{ height: "600px", border: "1px solid #ccc", padding: "10px" }} |
| 41 | + renderItem={(item, index) => ( |
| 42 | + <div key={index} style={{ marginBottom: "10px" }}> |
| 43 | + <h3> |
| 44 | + {index}. {item.headline} |
| 45 | + </h3> |
| 46 | + <div> |
| 47 | + <div |
| 48 | + style={{ |
| 49 | + float: "left", |
| 50 | + width: "100px", |
| 51 | + height: "100px", |
| 52 | + background: "#f0f0f0", |
| 53 | + borderRadius: "5px", |
| 54 | + marginRight: "10px", |
| 55 | + }} |
| 56 | + ></div> |
| 57 | + {item.content} |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + )} |
| 61 | + ></VirtualList> |
| 62 | + </> |
| 63 | + ); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## props (required) |
| 68 | + |
| 69 | +- `items`: `Array`. List data. |
| 70 | +- `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. |
| 71 | + |
| 72 | +## props (optional) |
| 73 | + |
| 74 | +- `itemSize`: `number`. Estimated height of a single item in the list. |
| 75 | +- `buffer`: `number`. Additional space outside the visible area of the virtual list to render. |
| 76 | +- `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`. |
| 77 | +- `listSize`: `number`, default: 1000. Height of the visible area of the list. Only used before DOM creation, suitable for SSR. |
| 78 | +- `triggerDistance`: `number`. The min distance to trigger re-rendering when scrolling. |
| 79 | +- `onScroll`: `typeof document.onscroll`. Listen for the list's scroll event. Type is same with HTML native onscroll handle. |
| 80 | +- `className`: `string`. Add a CSS class to the list root element. |
| 81 | +- `style`: `React.CSSProperties`. Add CSS styles to the list root element. |
| 82 | + |
| 83 | +## Exposed Methods |
| 84 | + |
| 85 | +First, use `ref` to obtain the exposed object. |
| 86 | + |
| 87 | +```tsx |
| 88 | +import { useRef } from "react"; |
| 89 | +import { VirtualList, VirtualListHandle } from "@phphe/react-base-virtual-list"; |
| 90 | + |
| 91 | +export default function BaseExample() { |
| 92 | + const ref = useRef<VirtualListHandle>(null); |
| 93 | + return ( |
| 94 | + <> |
| 95 | + <VirtualList ref={ref}></VirtualList> |
| 96 | + </> |
| 97 | + ); |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Irrelevant parts are omitted in the above code. `VirtualListHandle` is a `typescript` type, please ignore it if you are using pure JS. |
| 102 | + |
| 103 | +`VirtualListHandle` type code. |
| 104 | + |
| 105 | +```ts |
| 106 | +interface VirtualListHandle { |
| 107 | + scrollToIndex( |
| 108 | + index: number, |
| 109 | + block?: "start" | "end" | "center" | "nearest" |
| 110 | + ): void; |
| 111 | + forceUpdate(): void; |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +Then use the `ref` object to access the exposed methods. |
| 116 | + |
| 117 | +- `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`. |
| 118 | +- `forceUpdate`: Forcefully re-render the list. This can be called after the visible area of the list changes. |
| 119 | + |
| 120 | +## Note |
| 121 | + |
| 122 | +- Remember to set the height of the list. Class, style, px, em, percentage, etc. are all acceptable. |
| 123 | +- Delayed loading, loading when scrolling to the bottom, etc. can be implemented using the exposed `onScroll`. |
| 124 | + |
| 125 | +## IIFE |
| 126 | + |
| 127 | +The `dist/index.iife.js` file can be run in the browser. |
| 128 | +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`. |
| 129 | + |
| 130 | +You can also use the following third-party CDN url to include it. |
| 131 | + |
| 132 | +- unpkg: https://unpkg.com/@phphe/react-base-virtual-list |
| 133 | +- jsdelivr: https://cdn.jsdelivr.net/npm/@phphe/react-base-virtual-list |
| 134 | + |
| 135 | +## Changelog |
| 136 | + |
| 137 | +https://github.com/phphe/react-base-virtual-list/releases |
0 commit comments