Skip to content

Commit 3b7181c

Browse files
committed
feat:first
0 parents  commit 3b7181c

26 files changed

+4286
-0
lines changed

.eslintrc.cjs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
root: true,
3+
env: { browser: true, es2020: true },
4+
extends: [
5+
'eslint:recommended',
6+
'plugin:@typescript-eslint/recommended',
7+
'plugin:react-hooks/recommended',
8+
],
9+
ignorePatterns: ['dist', '.eslintrc.cjs'],
10+
parser: '@typescript-eslint/parser',
11+
plugins: ['react-refresh'],
12+
rules: {
13+
'react-refresh/only-export-components': [
14+
'warn',
15+
{ allowConstantExport: true },
16+
],
17+
},
18+
}

.github/workflows/build.yml

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Release with Changelog, Build Demo, Deploy to Github Pages, Publish to NPM
2+
# concat workflows because it is not easy to run trigger multiple. https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow
3+
permissions:
4+
contents: write
5+
on:
6+
push:
7+
tags:
8+
- "v*"
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout Repository
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Set up Node.js
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version: "20.x"
24+
registry-url: "https://registry.npmjs.org"
25+
26+
- name: Install
27+
run: npm install
28+
29+
- name: Release with Changelog
30+
run: npx changelogithub # or [email protected] if ensure the stable result
31+
continue-on-error: true # failed when only tag pushed
32+
env:
33+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
34+
35+
- name: Build Demo
36+
run: npm run build:web
37+
38+
- name: Extra Files
39+
run: |
40+
# Switch to the generated directory
41+
cd dist
42+
43+
# Set custom domain for GitHub Pages
44+
# echo "youdomain.com" > CNAME
45+
46+
# Required to bypass Jekyll on GitHub Pages
47+
echo "" > .nojekyll
48+
49+
- name: Deploy
50+
uses: JamesIves/github-pages-deploy-action@v4
51+
with:
52+
single-commit: true
53+
branch: gh-pages
54+
clean: true
55+
folder: dist
56+
- name: Remove Dist
57+
run: rm -rf dist
58+
- name: Build Library
59+
run: npm run build
60+
- run: rm -rf node_modules && npm ci
61+
- run: npm publish
62+
env:
63+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 phphe
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# @phphe/react-base-virtual-list ![GitHub License](https://img.shields.io/github/license/phphe/react-base-virtual-list) ![NPM Version](https://img.shields.io/npm/v/@phphe/react-base-virtual-list) ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/phphe/react-base-virtual-list/build.yml) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40phphe%2Freact-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

Comments
 (0)