Skip to content

Commit 26cbb30

Browse files
committed
docs: en doc
1 parent 87cb1ca commit 26cbb30

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

README.md

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,136 @@
22

33
[中文](README_CN.md)
44

5-
TODO
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

README_CN.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export default function BaseExample() {
7575
- `buffer`: `number`. 虚拟列表可见区域外额外渲染的空间。
7676
- `persistentIndices`: `number[]`. 持久化渲染的项的索引数组。使对应索引的项持续渲染而不会因为在渲染区域外而被删除。你再使用 css 的`position:sticky`就可以使其黏着显示。
7777
- `listSize`: `number`, 默认值: 1000. 列表的可见区域高度。仅用于 DOM 创建前使用,适用于 SSR.
78+
- `triggerDistance`: `number`. 滚动时触发重新渲染的距离。
79+
- `onScroll`: `typeof document.onscroll`. 监听列表的 scorll 事件。类型与 HTML 原生 onscroll 监听器相同。
7880
- `className`: `string`. 附加 css class 到根元素。
7981
- `style`: `React.CSSProperties`. 附加 css style 到根元素。
8082

@@ -115,9 +117,10 @@ interface VirtualListHandle {
115117
- `scrollToIndex`: `(index:number, block = 'start'):void`. 滚动到指定索引位置。`block`等于 HTML 原生方法`scrollIntoView``block`选项。
116118
- `forceUpdate`: 强制重新渲染列表。可以再列表可见区域变换后调用此方法。
117119

118-
## 注意点
120+
## 注意
119121

120122
- 记得给列表设置高度。class, style, px, em, 百分比等都可以。
123+
- 延迟加载,滚动到底部时加载等功能可以靠暴露的`onScroll`实现。
121124

122125
## iife
123126

0 commit comments

Comments
 (0)