@aiszlab/relax 1.1.2 → 1.1.3
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.
|
@@ -6,5 +6,6 @@ import { Key } from 'react';
|
|
|
6
6
|
export declare const useScrollable: <P extends HTMLElement, C extends HTMLElement>() => {
|
|
7
7
|
groupRef: import("react").RefObject<P>;
|
|
8
8
|
itemRefs: import("react").MutableRefObject<Map<Key, C | null>>;
|
|
9
|
-
scrollTo: (
|
|
9
|
+
scrollTo: (to: number, duration?: number) => void;
|
|
10
|
+
to: (key: Key) => number;
|
|
10
11
|
};
|
|
@@ -8,17 +8,13 @@ const useScrollable = () => {
|
|
|
8
8
|
const groupRef = useRef(null);
|
|
9
9
|
const itemRefs = useRef(new Map());
|
|
10
10
|
const scroller = useRef(null);
|
|
11
|
-
const scrollTo = useCallback((
|
|
11
|
+
const scrollTo = useCallback((to, duration = 0) => {
|
|
12
12
|
if (scroller.current) {
|
|
13
13
|
cancelAnimationFrame(scroller.current);
|
|
14
14
|
}
|
|
15
15
|
const group = groupRef.current;
|
|
16
16
|
if (!group)
|
|
17
17
|
return;
|
|
18
|
-
const item = itemRefs.current.get(key);
|
|
19
|
-
if (!item)
|
|
20
|
-
return;
|
|
21
|
-
const to = item.offsetTop;
|
|
22
18
|
// if duration <= 0, jump immediately
|
|
23
19
|
if (duration <= 0) {
|
|
24
20
|
scroller.current = requestAnimationFrame(() => {
|
|
@@ -33,13 +29,20 @@ const useScrollable = () => {
|
|
|
33
29
|
group.scrollTop = group.scrollTop + step;
|
|
34
30
|
if (group.scrollTop === to)
|
|
35
31
|
return;
|
|
36
|
-
scrollTo(
|
|
32
|
+
scrollTo(to, duration - 10);
|
|
37
33
|
});
|
|
38
34
|
}, []);
|
|
35
|
+
const to = useCallback((key) => {
|
|
36
|
+
const item = itemRefs.current.get(key);
|
|
37
|
+
if (!item)
|
|
38
|
+
return 0;
|
|
39
|
+
return item.offsetTop;
|
|
40
|
+
}, []);
|
|
39
41
|
return {
|
|
40
42
|
groupRef,
|
|
41
43
|
itemRefs,
|
|
42
|
-
scrollTo
|
|
44
|
+
scrollTo,
|
|
45
|
+
to
|
|
43
46
|
};
|
|
44
47
|
};
|
|
45
48
|
|