@os-design/use-load-next 1.0.26 → 1.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +12 -5
- package/src/index.ts +69 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@os-design/use-load-next",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.28",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"repository": "git@gitlab.com:os-team/libs/os-design.git",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
@@ -14,7 +14,14 @@
|
|
|
14
14
|
"./package.json": "./package.json"
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
|
-
"dist"
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"!**/*.test.ts",
|
|
20
|
+
"!**/*.test.tsx",
|
|
21
|
+
"!**/__tests__",
|
|
22
|
+
"!**/*.stories.tsx",
|
|
23
|
+
"!**/*.stories.mdx",
|
|
24
|
+
"!**/*.example.tsx"
|
|
18
25
|
],
|
|
19
26
|
"sideEffects": false,
|
|
20
27
|
"scripts": {
|
|
@@ -29,8 +36,8 @@
|
|
|
29
36
|
"access": "public"
|
|
30
37
|
},
|
|
31
38
|
"dependencies": {
|
|
32
|
-
"@os-design/use-event": "^1.0.
|
|
33
|
-
"@os-design/use-size": "^1.0.
|
|
39
|
+
"@os-design/use-event": "^1.0.15",
|
|
40
|
+
"@os-design/use-size": "^1.0.16"
|
|
34
41
|
},
|
|
35
42
|
"devDependencies": {
|
|
36
43
|
"@types/react-relay": "^14.1.3",
|
|
@@ -43,5 +50,5 @@
|
|
|
43
50
|
"react-relay": ">=13",
|
|
44
51
|
"relay-runtime": ">=13"
|
|
45
52
|
},
|
|
46
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "e5d8409760608145d2c738aa5789d0465ae5416f"
|
|
47
54
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import useEvent from '@os-design/use-event';
|
|
2
|
+
import useSize from '@os-design/use-size';
|
|
3
|
+
import { RefObject, useCallback } from 'react';
|
|
4
|
+
import { LoadMoreFn } from 'react-relay';
|
|
5
|
+
import { OperationType } from 'relay-runtime';
|
|
6
|
+
|
|
7
|
+
interface PaginationProps {
|
|
8
|
+
hasNext: boolean;
|
|
9
|
+
isLoadingNext: boolean;
|
|
10
|
+
loadNext: LoadMoreFn<OperationType>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface Options {
|
|
14
|
+
count?: number;
|
|
15
|
+
threshold?: number;
|
|
16
|
+
container?: Element | RefObject<Element>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const getElement = (el: Element | RefObject<Element>) =>
|
|
20
|
+
el instanceof Element ? el : el.current;
|
|
21
|
+
|
|
22
|
+
const useLoadNext = (
|
|
23
|
+
paginationProps: PaginationProps,
|
|
24
|
+
options: Options = {}
|
|
25
|
+
): void => {
|
|
26
|
+
const { hasNext, isLoadingNext, loadNext } = paginationProps;
|
|
27
|
+
const { count = 60, threshold = 500, container } = options;
|
|
28
|
+
|
|
29
|
+
const size = useSize(container);
|
|
30
|
+
|
|
31
|
+
const getScrollHeight = useCallback(() => {
|
|
32
|
+
if (!container) return document.body.scrollHeight;
|
|
33
|
+
const containerElement = getElement(container);
|
|
34
|
+
if (!containerElement) return 0;
|
|
35
|
+
return containerElement.scrollHeight;
|
|
36
|
+
}, [container]);
|
|
37
|
+
|
|
38
|
+
const getScrollTop = useCallback(() => {
|
|
39
|
+
if (!container) return window.pageYOffset;
|
|
40
|
+
const containerElement = getElement(container);
|
|
41
|
+
if (!containerElement) return 0;
|
|
42
|
+
return containerElement.scrollTop;
|
|
43
|
+
}, [container]);
|
|
44
|
+
|
|
45
|
+
const scrollListener = useCallback(() => {
|
|
46
|
+
const maxScrollTopBeforeLoad = getScrollHeight() - size.height - threshold;
|
|
47
|
+
if (getScrollTop() >= maxScrollTopBeforeLoad && hasNext && !isLoadingNext) {
|
|
48
|
+
loadNext(count);
|
|
49
|
+
}
|
|
50
|
+
}, [
|
|
51
|
+
getScrollHeight,
|
|
52
|
+
getScrollTop,
|
|
53
|
+
size.height,
|
|
54
|
+
threshold,
|
|
55
|
+
hasNext,
|
|
56
|
+
isLoadingNext,
|
|
57
|
+
loadNext,
|
|
58
|
+
count,
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
useEvent(
|
|
62
|
+
container ||
|
|
63
|
+
((typeof window !== 'undefined' ? window : undefined) as never),
|
|
64
|
+
'scroll',
|
|
65
|
+
scrollListener
|
|
66
|
+
);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export default useLoadNext;
|