@aiszlab/relax 1.1.3 → 1.1.5

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