|
| 1 | +<h1 align="center">π§» infinite-scroll-hook (React)</h1> |
| 2 | + |
1 | 3 | <p align="center"> |
2 | | - <img src="logo.png" alt="logo" width="550px" /> |
| 4 | + <a href="https://github.com/iamyoki/infinite-scroll-hook/actions/workflows/test.yml"><img src="https://github.com/iamyoki/infinite-scroll-hook/actions/workflows/test.yml/badge.svg" alt="π§ͺ Run Tests"></a> |
| 5 | + <a href="https://github.com/iamyoki/infinite-scroll-hook/actions/workflows/release.yml"><img src="https://github.com/iamyoki/infinite-scroll-hook/actions/workflows/release.yml/badge.svg" alt="π Release The Package"></a> |
| 6 | + <a href="https://github.com/iamyoki/infinite-scroll-hook"> |
| 7 | + <img src="https://img.shields.io/bundlephobia/minzip/infinite-scroll-hook?color=%237B68EE&label=Minizipped%20Size" alt="transition hook"> |
| 8 | + </a> |
| 9 | + <a href="https://github.com/iamyoki/infinite-scroll-hook"> |
| 10 | + <img src="https://img.shields.io/github/license/iamyoki/infinite-scroll-hook?color=Turquoise" alt=""> |
| 11 | + </a> |
3 | 12 | </p> |
4 | 13 |
|
5 | | -<br /> |
6 | | -<h1 align="center">π qhlab-library-template</h1> |
| 14 | +<br> |
| 15 | +<p align="center"><strong>Scroll down to load more never been so easy!</strong></p> |
| 16 | +<br> |
7 | 17 |
|
8 | 18 | <p align="center"> |
9 | | - <a href="https://github.com/iamyoki/transition-hook/actions/workflows/test.yml"><img src="https://github.com/iamyoki/transition-hook/actions/workflows/test.yml/badge.svg" alt="π§ͺ Run Tests"></a> |
10 | | - <a href="https://github.com/iamyoki/transition-hook/actions/workflows/release.yml"><img src="https://github.com/iamyoki/transition-hook/actions/workflows/release.yml/badge.svg" alt="π Release The Package"></a> |
11 | | - <a href="https://github.com/iamyoki/transition-hook"> |
12 | | - <img src="https://img.shields.io/bundlephobia/minzip/transition-hook?color=%237B68EE&label=Minizipped%20Size" alt="transition hook"> |
13 | | - </a> |
14 | | - <a href="https://github.com/iamyoki/transition-hook"> |
15 | | - <img src="https://img.shields.io/github/license/iamyoki/transition-hook?color=Turquoise" alt=""> |
| 19 | + <a href="https://codesandbox.io/s/try-infinite-scroll-hook-jo6uxc?file=/src/App.jsx" target="_blank"> |
| 20 | + <img src="gifs/code.png" alt="example"> |
16 | 21 | </a> |
17 | 22 | </p> |
18 | 23 |
|
19 | 24 | <br> |
20 | | -<p align="center"><strong>A template make make open-source library never been so easy.</strong></p> |
| 25 | + |
| 26 | +<p align="center"> |
| 27 | + <a href="https://codesandbox.io/s/try-infinite-scroll-hook-jo6uxc?file=/src/App.jsx">See Example in Codesandbox</a> |
| 28 | +</p> |
| 29 | + |
21 | 30 | <br> |
22 | 31 |
|
23 | 32 | - [Installation](#installation) |
24 | 33 | - [Usage](#usage) |
| 34 | + - [Simple usage](#simple-usage) |
| 35 | + - [Offset](#offset) |
| 36 | + - [Stop load more](#stop-load-more) |
25 | 37 | - [API Reference](#api-reference) |
26 | 38 | - [License](#license) |
27 | 39 |
|
| 40 | +<br> |
| 41 | + |
28 | 42 | ## Installation |
29 | 43 |
|
30 | 44 | Install with yarn |
31 | 45 |
|
32 | 46 | ```bash |
33 | | -yarn add qhlab-library-template |
| 47 | +yarn add infinite-scroll-hook |
34 | 48 | ``` |
35 | 49 |
|
36 | 50 | Or install with npm |
37 | 51 |
|
38 | 52 | ```bash |
39 | | -npm install qhlab-library-template --save |
| 53 | +npm install infinite-scroll-hook --save |
40 | 54 | ``` |
41 | 55 |
|
42 | 56 | ## Usage |
43 | 57 |
|
44 | | -> Write some thing nice. |
| 58 | +### Simple usage |
| 59 | + |
| 60 | +```jsx |
| 61 | +export default function App() { |
| 62 | + const [list, setList] = useState([...Array(11).keys()]) |
| 63 | + const { containerRef, isLoading } = useInfiniteScroll({ |
| 64 | + async onLoadMore() { |
| 65 | + const res = await fetchList(list.slice(-1)[0]) |
| 66 | + setList(list.concat(res)) |
| 67 | + }, |
| 68 | + }) |
| 69 | + |
| 70 | + return ( |
| 71 | + <div className="App"> |
| 72 | + <List ref={containerRef}> |
| 73 | + {list.map((n) => ( |
| 74 | + <Item key={n}>{n}</Item> |
| 75 | + ))} |
| 76 | + {isLoading && <Loading>Loading ...</Loading>} |
| 77 | + </List> |
| 78 | + </div> |
| 79 | + ) |
| 80 | + |
| 81 | +``` |
| 82 | +
|
| 83 | +### Offset |
| 84 | +
|
| 85 | +Will load more while scrolling hit to bottom offset '200px' |
| 86 | +
|
| 87 | +```jsx |
| 88 | +const { containerRef, isLoading } = useInfiniteScroll({offset: '200px'}) |
| 89 | +... |
| 90 | +``` |
| 91 | +
|
| 92 | +All css size units available |
| 93 | +
|
| 94 | +- `offset: 200px` β
|
| 95 | +- `offset: 20%` β
|
| 96 | +- `offset: 20vh` β
|
| 97 | +- `offset: 20cm` β
|
| 98 | +- ... |
| 99 | +
|
| 100 | +### Stop load more |
| 101 | +
|
| 102 | +Stops when finished |
| 103 | +
|
| 104 | +```jsx |
| 105 | +const { containerRef, isLoading } = useInfiniteScroll({shouldStop: isFetchEnd}) |
| 106 | +... |
| 107 | + |
| 108 | +return ( |
| 109 | + <div className="App"> |
| 110 | + <List ref={containerRef}> |
| 111 | + {list.map((n) => ( |
| 112 | + <Item key={n}>{n}</Item> |
| 113 | + ))} |
| 114 | + {(isLoading || !isFetchEnd) && <Loading>Loading ...</Loading>} |
| 115 | + </List> |
| 116 | + </div> |
| 117 | + ) |
| 118 | +``` |
45 | 119 |
|
46 | 120 | ## API Reference |
47 | 121 |
|
48 | | -> Write some thing nice. |
| 122 | +```js |
| 123 | + const {containerRef, isLoading} = useTransition(options) |
| 124 | +``` |
| 125 | +
|
| 126 | +| Parameters | Type | Description | |
| 127 | +| :--------- | :---------------------------------------------------------------------------- | :------------------------ | |
| 128 | +| `options` | `{ offset?: string; shouldStop?: boolean; onLoadMore?: () => Promise<void> }` | This is the option object | |
| 129 | +
|
| 130 | +<br> |
| 131 | +
|
| 132 | +| Returns | Type | Description | |
| 133 | +| :------------- | :----------------------- | :-------------------------------------------------- | |
| 134 | +| `containerRef` | `LegacyRef<HTMLElement>` | The ref object attach to your jsx container element | |
| 135 | +| `isLoading` | `boolean` | Whether is loading or not | |
49 | 136 |
|
50 | 137 | ## License |
51 | 138 |
|
|
0 commit comments