@aiszlab/relax 1.1.1 → 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.
- package/dist/hooks/use-refs.js +21 -0
- package/dist/hooks/use-scrollable.d.ts +2 -1
- package/dist/hooks/use-scrollable.js +49 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { isFunction } from '../is/is-function.js';
|
|
3
|
+
|
|
4
|
+
const mount = (ref, trigger) => {
|
|
5
|
+
if (isFunction(ref)) {
|
|
6
|
+
ref(trigger);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
ref.current = trigger;
|
|
10
|
+
};
|
|
11
|
+
const useRefs = (...refs) => {
|
|
12
|
+
return useMemo(() => {
|
|
13
|
+
return (trigger) => {
|
|
14
|
+
refs.forEach((ref) => {
|
|
15
|
+
mount(ref, trigger);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
}, refs);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { useRefs };
|
|
@@ -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
|
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useRef, useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description
|
|
5
|
+
* scrollable hook
|
|
6
|
+
*/
|
|
7
|
+
const useScrollable = () => {
|
|
8
|
+
const groupRef = useRef(null);
|
|
9
|
+
const itemRefs = useRef(new Map());
|
|
10
|
+
const scroller = useRef(null);
|
|
11
|
+
const scrollTo = useCallback((to, duration = 0) => {
|
|
12
|
+
if (scroller.current) {
|
|
13
|
+
cancelAnimationFrame(scroller.current);
|
|
14
|
+
}
|
|
15
|
+
const group = groupRef.current;
|
|
16
|
+
if (!group)
|
|
17
|
+
return;
|
|
18
|
+
// if duration <= 0, jump immediately
|
|
19
|
+
if (duration <= 0) {
|
|
20
|
+
scroller.current = requestAnimationFrame(() => {
|
|
21
|
+
group.scrollTop = to;
|
|
22
|
+
});
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
// animate
|
|
26
|
+
const difference = to - group.scrollTop;
|
|
27
|
+
const step = (difference / duration) * 10;
|
|
28
|
+
scroller.current = requestAnimationFrame(() => {
|
|
29
|
+
group.scrollTop = group.scrollTop + step;
|
|
30
|
+
if (group.scrollTop === to)
|
|
31
|
+
return;
|
|
32
|
+
scrollTo(to, duration - 10);
|
|
33
|
+
});
|
|
34
|
+
}, []);
|
|
35
|
+
const to = useCallback((key) => {
|
|
36
|
+
const item = itemRefs.current.get(key);
|
|
37
|
+
if (!item)
|
|
38
|
+
return 0;
|
|
39
|
+
return item.offsetTop;
|
|
40
|
+
}, []);
|
|
41
|
+
return {
|
|
42
|
+
groupRef,
|
|
43
|
+
itemRefs,
|
|
44
|
+
scrollTo,
|
|
45
|
+
to
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export { useScrollable };
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export { useControlledState } from './hooks/use-controlled-state';
|
|
|
12
12
|
export { useOnceState } from './hooks/use-once-state';
|
|
13
13
|
export { useScrollLocker } from './hooks/use-scroll-locker';
|
|
14
14
|
export { useForceUpdate } from './hooks/use-force-update';
|
|
15
|
+
export { useScrollable } from './hooks/use-scrollable';
|
|
16
|
+
export { useRefs } from './hooks/use-refs';
|
|
15
17
|
/**
|
|
16
18
|
* @description
|
|
17
19
|
* is
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,8 @@ export { useControlledState } from './hooks/use-controlled-state.js';
|
|
|
8
8
|
export { useOnceState } from './hooks/use-once-state.js';
|
|
9
9
|
export { useScrollLocker } from './hooks/use-scroll-locker.js';
|
|
10
10
|
export { useForceUpdate } from './hooks/use-force-update.js';
|
|
11
|
+
export { useScrollable } from './hooks/use-scrollable.js';
|
|
12
|
+
export { useRefs } from './hooks/use-refs.js';
|
|
11
13
|
export { isRefable } from './is/is-refable.js';
|
|
12
14
|
export { isUndefined } from './is/is-undefined.js';
|
|
13
15
|
export { isStateGetter } from './is/is-state-getter.js';
|