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