@aiszlab/relax 1.1.4 → 1.1.6

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.
@@ -0,0 +1,19 @@
1
+ export type Direction = 'horizontal' | 'vertical';
2
+ interface ScrollBy {
3
+ /**
4
+ * @description
5
+ * duration
6
+ */
7
+ duration: number;
8
+ /**
9
+ * @description
10
+ * direction
11
+ */
12
+ direction?: Direction;
13
+ }
14
+ /**
15
+ * @description
16
+ * scroll to for wrapper element
17
+ */
18
+ export declare const scrollTo: (target: HTMLElement, to: number, { duration, direction }?: ScrollBy) => void;
19
+ export {};
@@ -0,0 +1,60 @@
1
+ import { __classPrivateFieldSet, __classPrivateFieldGet } from '../node_modules/tslib/tslib.es6.js';
2
+
3
+ var _Scroller_scrolled, _Scroller_scroller;
4
+ class Scroller {
5
+ constructor() {
6
+ var _a;
7
+ _Scroller_scrolled.set(this, new Map()
8
+ // singleton mode
9
+ );
10
+ // singleton mode
11
+ _Scroller_scroller.set(this, null);
12
+ return (__classPrivateFieldSet(this, _Scroller_scroller, (_a = __classPrivateFieldGet(this, _Scroller_scroller, "f")) !== null && _a !== void 0 ? _a : this, "f"));
13
+ }
14
+ get scrolled() {
15
+ return __classPrivateFieldGet(this, _Scroller_scrolled, "f");
16
+ }
17
+ currentAt(direction) {
18
+ return direction === 'vertical' ? 'scrollTop' : 'scrollLeft';
19
+ }
20
+ }
21
+ _Scroller_scrolled = new WeakMap(), _Scroller_scroller = new WeakMap();
22
+ /**
23
+ * @description
24
+ * scroll to for wrapper element
25
+ */
26
+ const scrollTo = (target, to, { duration = 0, direction = 'vertical' } = {
27
+ duration: 0,
28
+ direction: 'vertical'
29
+ }) => {
30
+ const scroller = new Scroller();
31
+ const scrolled = scroller.scrolled.get(target);
32
+ const currentAtProperty = scroller.currentAt(direction);
33
+ if (scrolled) {
34
+ cancelAnimationFrame(scrolled);
35
+ scroller.scrolled.delete(target);
36
+ }
37
+ // if duration <= 0, jump immediately
38
+ if (duration <= 0) {
39
+ scroller.scrolled.set(target, requestAnimationFrame(() => {
40
+ target[currentAtProperty] = to;
41
+ }));
42
+ return;
43
+ }
44
+ // animate
45
+ const currentAt = target[currentAtProperty];
46
+ const difference = to - currentAt;
47
+ const step = (difference / duration) * 10;
48
+ scroller.scrolled.set(target, requestAnimationFrame(() => {
49
+ target[currentAtProperty] = currentAt + step;
50
+ // over end, stop any animation
51
+ if (target[currentAtProperty] === to)
52
+ return;
53
+ scrollTo(target, to, {
54
+ duration: duration - 10,
55
+ direction
56
+ });
57
+ }));
58
+ };
59
+
60
+ export { scrollTo };
@@ -1,15 +1,15 @@
1
- import { Key } from 'react';
2
- type Direction = 'horizontal' | 'vertical';
3
- interface Props {
1
+ import { type Key } from 'react';
2
+ import { type Direction } from '../dom/scroll-to';
3
+ interface Dependencies {
4
4
  direction?: Direction;
5
5
  }
6
6
  /**
7
7
  * @description
8
8
  * scrollable hook
9
9
  */
10
- export declare const useScrollable: <P extends HTMLElement, C extends HTMLElement>({ direction }?: Props) => {
11
- groupRef: import("react").RefObject<P>;
12
- itemRefs: import("react").MutableRefObject<Map<Key, C | null>>;
10
+ export declare const useScrollable: <P extends HTMLElement, C extends HTMLElement>({ direction }?: Dependencies) => {
11
+ targetRef: import("react").RefObject<P>;
12
+ triggerRefs: import("react").MutableRefObject<Map<Key, C | null>>;
13
13
  scrollTo: (to: number, duration?: number) => void;
14
14
  to: (key: Key) => number;
15
15
  };
@@ -1,66 +1,34 @@
1
1
  import { useRef, useCallback } from 'react';
2
+ import { scrollTo } from '../dom/scroll-to.js';
2
3
 
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
- ]);
19
4
  /**
20
5
  * @description
21
6
  * scrollable hook
22
7
  */
23
8
  const useScrollable = ({ direction = 'vertical' } = {}) => {
24
- const groupRef = useRef(null);
25
- const itemRefs = useRef(new Map());
26
- const scroller = useRef(null);
27
- const scrollTo = useCallback((to, duration = 0) => {
28
- if (scroller.current) {
29
- cancelAnimationFrame(scroller.current);
30
- }
31
- const group = groupRef.current;
32
- if (!group)
9
+ const targetRef = useRef(null);
10
+ const triggerRefs = useRef(new Map());
11
+ const scrollTo$1 = useCallback((to, duration = 0) => {
12
+ const target = targetRef.current;
13
+ if (!target)
33
14
  return;
34
- const triggerProperty = PROPERTY.get(direction).trigger;
35
- // if duration <= 0, jump immediately
36
- if (duration <= 0) {
37
- scroller.current = requestAnimationFrame(() => {
38
- group[triggerProperty] = to;
39
- });
40
- return;
41
- }
42
- // animate
43
- const currentAt = group[triggerProperty];
44
- const difference = to - currentAt;
45
- const step = (difference / duration) * 10;
46
- scroller.current = requestAnimationFrame(() => {
47
- group[triggerProperty] = currentAt + step;
48
- if (group[triggerProperty] === to)
49
- return;
50
- scrollTo(to, duration - 10);
15
+ // use animated scroll
16
+ scrollTo(target, to, {
17
+ duration,
18
+ direction
51
19
  });
52
20
  }, [direction]);
53
21
  const to = useCallback((key) => {
54
- const item = itemRefs.current.get(key);
55
- if (!item)
22
+ const trigger = triggerRefs.current.get(key);
23
+ if (!trigger)
56
24
  return 0;
57
25
  // different direction, use different property
58
- return item[PROPERTY.get(direction).target];
26
+ return direction === 'vertical' ? trigger.offsetTop : trigger.offsetLeft;
59
27
  }, [direction]);
60
28
  return {
61
- groupRef,
62
- itemRefs,
63
- scrollTo,
29
+ targetRef,
30
+ triggerRefs,
31
+ scrollTo: scrollTo$1,
64
32
  to
65
33
  };
66
34
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",