@aiszlab/relax 1.1.3 → 1.1.4

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.
@@ -1,11 +1,16 @@
1
1
  import { Key } from 'react';
2
+ type Direction = 'horizontal' | 'vertical';
3
+ interface Props {
4
+ direction?: Direction;
5
+ }
2
6
  /**
3
7
  * @description
4
8
  * scrollable hook
5
9
  */
6
- export declare const useScrollable: <P extends HTMLElement, C extends HTMLElement>() => {
10
+ export declare const useScrollable: <P extends HTMLElement, C extends HTMLElement>({ direction }?: Props) => {
7
11
  groupRef: import("react").RefObject<P>;
8
12
  itemRefs: import("react").MutableRefObject<Map<Key, C | null>>;
9
13
  scrollTo: (to: number, duration?: number) => void;
10
14
  to: (key: Key) => number;
11
15
  };
16
+ export {};
@@ -1,10 +1,26 @@
1
1
  import { useRef, useCallback } from 'react';
2
2
 
3
+ const PROPERTY = new Map([
4
+ [
5
+ 'vertical',
6
+ {
7
+ trigger: 'scrollTop',
8
+ target: 'offsetTop'
9
+ }
10
+ ],
11
+ [
12
+ 'horizontal',
13
+ {
14
+ trigger: 'scrollLeft',
15
+ target: 'offsetLeft'
16
+ }
17
+ ]
18
+ ]);
3
19
  /**
4
20
  * @description
5
21
  * scrollable hook
6
22
  */
7
- const useScrollable = () => {
23
+ const useScrollable = ({ direction = 'vertical' } = {}) => {
8
24
  const groupRef = useRef(null);
9
25
  const itemRefs = useRef(new Map());
10
26
  const scroller = useRef(null);
@@ -15,29 +31,32 @@ const useScrollable = () => {
15
31
  const group = groupRef.current;
16
32
  if (!group)
17
33
  return;
34
+ const triggerProperty = PROPERTY.get(direction).trigger;
18
35
  // if duration <= 0, jump immediately
19
36
  if (duration <= 0) {
20
37
  scroller.current = requestAnimationFrame(() => {
21
- group.scrollTop = to;
38
+ group[triggerProperty] = to;
22
39
  });
23
40
  return;
24
41
  }
25
42
  // animate
26
- const difference = to - group.scrollTop;
43
+ const currentAt = group[triggerProperty];
44
+ const difference = to - currentAt;
27
45
  const step = (difference / duration) * 10;
28
46
  scroller.current = requestAnimationFrame(() => {
29
- group.scrollTop = group.scrollTop + step;
30
- if (group.scrollTop === to)
47
+ group[triggerProperty] = currentAt + step;
48
+ if (group[triggerProperty] === to)
31
49
  return;
32
50
  scrollTo(to, duration - 10);
33
51
  });
34
- }, []);
52
+ }, [direction]);
35
53
  const to = useCallback((key) => {
36
54
  const item = itemRefs.current.get(key);
37
55
  if (!item)
38
56
  return 0;
39
- return item.offsetTop;
40
- }, []);
57
+ // different direction, use different property
58
+ return item[PROPERTY.get(direction).target];
59
+ }, [direction]);
41
60
  return {
42
61
  groupRef,
43
62
  itemRefs,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",