@aiszlab/relax 1.1.0 → 1.1.2
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.d.ts +4 -0
- package/dist/hooks/use-refs.js +21 -0
- package/dist/hooks/use-scrollable.js +46 -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 };
|
|
@@ -0,0 +1,46 @@
|
|
|
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((key, duration = 0) => {
|
|
12
|
+
if (scroller.current) {
|
|
13
|
+
cancelAnimationFrame(scroller.current);
|
|
14
|
+
}
|
|
15
|
+
const group = groupRef.current;
|
|
16
|
+
if (!group)
|
|
17
|
+
return;
|
|
18
|
+
const item = itemRefs.current.get(key);
|
|
19
|
+
if (!item)
|
|
20
|
+
return;
|
|
21
|
+
const to = item.offsetTop;
|
|
22
|
+
// if duration <= 0, jump immediately
|
|
23
|
+
if (duration <= 0) {
|
|
24
|
+
scroller.current = requestAnimationFrame(() => {
|
|
25
|
+
group.scrollTop = to;
|
|
26
|
+
});
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
// animate
|
|
30
|
+
const difference = to - group.scrollTop;
|
|
31
|
+
const step = (difference / duration) * 10;
|
|
32
|
+
scroller.current = requestAnimationFrame(() => {
|
|
33
|
+
group.scrollTop = group.scrollTop + step;
|
|
34
|
+
if (group.scrollTop === to)
|
|
35
|
+
return;
|
|
36
|
+
scrollTo(key, duration - 10);
|
|
37
|
+
});
|
|
38
|
+
}, []);
|
|
39
|
+
return {
|
|
40
|
+
groupRef,
|
|
41
|
+
itemRefs,
|
|
42
|
+
scrollTo
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
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';
|