@aiszlab/relax 1.2.71 → 1.2.73

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 @@
1
+ declare const clipboard: (value: string) => Promise<void>;
@@ -1,4 +1,4 @@
1
- export type Direction = "horizontal" | "vertical";
1
+ export type Orientation = "horizontal" | "vertical";
2
2
  interface ScrollBy {
3
3
  /**
4
4
  * @description
@@ -7,13 +7,13 @@ interface ScrollBy {
7
7
  duration: number;
8
8
  /**
9
9
  * @description
10
- * direction
10
+ * orientation
11
11
  */
12
- direction?: Direction;
12
+ orientation?: Orientation;
13
13
  }
14
14
  /**
15
15
  * @description
16
16
  * scroll to for wrapper element
17
17
  */
18
- export declare const scrollTo: (target: HTMLElement, to: number, { duration, direction }?: ScrollBy) => void;
18
+ export declare const scrollTo: (target: HTMLElement, to: number, { duration, orientation }?: ScrollBy) => void;
19
19
  export {};
@@ -18,8 +18,8 @@ var Scroller = /*#__PURE__*/function () {
18
18
  }
19
19
  }, {
20
20
  key: "currentAt",
21
- value: function currentAt(direction) {
22
- return direction === "vertical" ? "scrollTop" : "scrollLeft";
21
+ value: function currentAt(orientation) {
22
+ return orientation === "vertical" ? "scrollTop" : "scrollLeft";
23
23
  }
24
24
  }]);
25
25
  }();
@@ -34,15 +34,15 @@ var _scroller = {
34
34
  var _scrollTo = function scrollTo(target, to) {
35
35
  var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
36
36
  duration: 0,
37
- direction: "vertical"
37
+ orientation: "vertical"
38
38
  },
39
39
  _ref$duration = _ref.duration,
40
40
  duration = _ref$duration === void 0 ? 0 : _ref$duration,
41
- _ref$direction = _ref.direction,
42
- direction = _ref$direction === void 0 ? "vertical" : _ref$direction;
41
+ _ref$orientation = _ref.orientation,
42
+ orientation = _ref$orientation === void 0 ? "vertical" : _ref$orientation;
43
43
  var scroller = new Scroller();
44
44
  var scrolled = scroller.scrolled.get(target);
45
- var currentAtProperty = scroller.currentAt(direction);
45
+ var currentAtProperty = scroller.currentAt(orientation);
46
46
  if (scrolled) {
47
47
  cancelAnimationFrame(scrolled);
48
48
  scroller.scrolled["delete"](target);
@@ -64,7 +64,7 @@ var _scrollTo = function scrollTo(target, to) {
64
64
  if (target[currentAtProperty] === to) return;
65
65
  _scrollTo(target, to, {
66
66
  duration: duration - 10,
67
- direction: direction
67
+ orientation: orientation
68
68
  });
69
69
  }));
70
70
  };
@@ -1,8 +1,10 @@
1
- import { useRef, useCallback } from 'react';
1
+ import { useRef, useEffect, useCallback } from 'react';
2
2
 
3
3
  var useEvent = function useEvent(callback) {
4
4
  var ref = useRef(callback);
5
- ref.current = callback;
5
+ useEffect(function () {
6
+ ref.current = callback;
7
+ });
6
8
  // @ts-ignore
7
9
  return useCallback(function () {
8
10
  return ref.current.apply(ref, arguments);
@@ -1,8 +1,14 @@
1
- import { type DOMAttributes } from "react";
1
+ import { type PointerEventHandler } from "react";
2
2
  type UseHoverBy<T> = {
3
- onEnter?: DOMAttributes<T>["onPointerEnter"];
4
- onLeave?: DOMAttributes<T>["onPointerLeave"];
3
+ onEnter?: PointerEventHandler<T>;
4
+ onLeave?: PointerEventHandler<T>;
5
5
  };
6
- type UsedHover<T> = [boolean, Required<Pick<DOMAttributes<T>, "onPointerEnter" | "onPointerLeave">>];
6
+ type UsedHover<T> = [
7
+ boolean,
8
+ {
9
+ onPointerEnter: PointerEventHandler<T>;
10
+ onPointerLeave: PointerEventHandler<T>;
11
+ }
12
+ ];
7
13
  export declare const useHover: <T extends Element = Element>(props?: UseHoverBy<T>) => UsedHover<T>;
8
14
  export {};
@@ -1,7 +1,7 @@
1
1
  import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
2
- import { useCallback } from 'react';
3
2
  import { useBoolean } from './use-boolean.js';
4
3
  import { chain } from '../utils/chain.js';
4
+ import { useEvent } from './use-event.js';
5
5
 
6
6
  var useHover = function useHover(props) {
7
7
  var _useBoolean = useBoolean(false),
@@ -10,12 +10,12 @@ var useHover = function useHover(props) {
10
10
  _useBoolean2$ = _useBoolean2[1],
11
11
  turnOn = _useBoolean2$.turnOn,
12
12
  turnOff = _useBoolean2$.turnOff;
13
- var onPointerEnter = useCallback(function (e) {
14
- chain(props === null || props === void 0 ? void 0 : props.onEnter, turnOn)(e);
15
- }, [props === null || props === void 0 ? void 0 : props.onEnter]);
16
- var onPointerLeave = useCallback(function (e) {
17
- chain(props === null || props === void 0 ? void 0 : props.onLeave, turnOff)(e);
18
- }, [props === null || props === void 0 ? void 0 : props.onLeave]);
13
+ var onPointerEnter = useEvent(function (event) {
14
+ chain(props === null || props === void 0 ? void 0 : props.onEnter, turnOn)(event);
15
+ });
16
+ var onPointerLeave = useEvent(function (event) {
17
+ chain(props === null || props === void 0 ? void 0 : props.onLeave, turnOff)(event);
18
+ });
19
19
  return [isHovered, {
20
20
  onPointerEnter: onPointerEnter,
21
21
  onPointerLeave: onPointerLeave
@@ -0,0 +1,25 @@
1
+ import { type RefObject } from "react";
2
+ type UseInfiniteScrollProps = {
3
+ /**
4
+ * @description
5
+ * hasMore
6
+ */
7
+ hasMore?: boolean;
8
+ /**
9
+ * @description
10
+ * distance
11
+ */
12
+ distance?: number;
13
+ /**
14
+ * @description
15
+ * load more handler
16
+ */
17
+ onLoadMore?: () => void;
18
+ };
19
+ type UsedInfiniteScroll = [loadable: RefObject<HTMLElement>, scrollable: RefObject<HTMLElement>];
20
+ /**
21
+ * @description
22
+ * use infinite scroll
23
+ */
24
+ export declare const useInfiniteScroll: ({ hasMore, distance, onLoadMore, }: UseInfiniteScrollProps) => UsedInfiniteScroll;
25
+ export {};
@@ -0,0 +1,62 @@
1
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
2
+ import { useRef, useEffect } from 'react';
3
+ import { useEvent } from './use-event.js';
4
+ import { debounce } from '../utils/debounce.js';
5
+
6
+ /**
7
+ * @description
8
+ * use infinite scroll
9
+ */
10
+ var useInfiniteScroll = function useInfiniteScroll(_ref) {
11
+ var _ref$hasMore = _ref.hasMore,
12
+ hasMore = _ref$hasMore === void 0 ? true : _ref$hasMore,
13
+ _ref$distance = _ref.distance,
14
+ distance = _ref$distance === void 0 ? 0 : _ref$distance,
15
+ onLoadMore = _ref.onLoadMore;
16
+ var loadable = useRef(null);
17
+ var scrollable = useRef(null);
18
+ var loadMore = useEvent(function () {
19
+ onLoadMore === null || onLoadMore === void 0 || onLoadMore();
20
+ });
21
+ useEffect(function () {
22
+ var loader = loadable.current;
23
+ var scroller = scrollable.current;
24
+ if (!hasMore) return;
25
+ if (!scroller) return;
26
+ // use loader if loader is assigned
27
+ if (!!loader) {
28
+ var options = {
29
+ root: scroller,
30
+ rootMargin: "0px 0px ".concat(distance, "px 0px"),
31
+ threshold: 0.1
32
+ };
33
+ var observer = new IntersectionObserver(function (entries) {
34
+ var _entries = _slicedToArray(entries, 1),
35
+ entry = _entries[0];
36
+ if (!entry.isIntersecting) return;
37
+ loadMore();
38
+ }, options);
39
+ observer.observe(loader);
40
+ return function () {
41
+ observer.disconnect();
42
+ };
43
+ }
44
+ // listen scroll event, when loader is not assigned
45
+ var _debounce = debounce(function () {
46
+ if (scroller.scrollHeight - scroller.scrollTop > scroller.clientHeight + distance) {
47
+ return;
48
+ }
49
+ loadMore();
50
+ }, 100),
51
+ next = _debounce.next,
52
+ abort = _debounce.abort;
53
+ scroller.addEventListener("scroll", next);
54
+ return function () {
55
+ scroller.removeEventListener("scroll", next);
56
+ abort();
57
+ };
58
+ }, [hasMore]);
59
+ return [loadable, scrollable];
60
+ };
61
+
62
+ export { useInfiniteScroll };
@@ -0,0 +1,13 @@
1
+ type UseIsMountedProps = {
2
+ /**
3
+ * @description
4
+ * after mount, react will rerender current component
5
+ */
6
+ rerender?: boolean;
7
+ };
8
+ /**
9
+ * @description
10
+ * `useIsMounted`
11
+ */
12
+ export declare const useIsMounted: ({ rerender }?: UseIsMountedProps) => () => boolean;
13
+ export {};
@@ -0,0 +1,33 @@
1
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
2
+ import { useState, useRef } from 'react';
3
+ import { useMounted } from './use-mounted.js';
4
+
5
+ /**
6
+ * @description
7
+ * `useIsMounted`
8
+ */
9
+ var useIsMounted = function useIsMounted() {
10
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
11
+ _ref$rerender = _ref.rerender,
12
+ rerender = _ref$rerender === void 0 ? false : _ref$rerender;
13
+ var _useState = useState(false),
14
+ _useState2 = _slicedToArray(_useState, 2),
15
+ setIsMounted = _useState2[1];
16
+ var isMountedRef = useRef(false);
17
+ useMounted(function () {
18
+ isMountedRef.current = true;
19
+ if (rerender) {
20
+ setIsMounted(true);
21
+ }
22
+ return function () {
23
+ isMountedRef.current = false;
24
+ };
25
+ });
26
+ // only use callback
27
+ // avoid parent component or hooks change isMountedRef.current
28
+ return function () {
29
+ return isMountedRef.current;
30
+ };
31
+ };
32
+
33
+ export { useIsMounted };
@@ -0,0 +1,2 @@
1
+ import { type UseStorageStateBy } from "./use-storage-state";
2
+ export declare const useLocalStorageState: (key: string, useBy?: UseStorageStateBy) => import("./use-storage-state").UsedStorageState;
@@ -0,0 +1,7 @@
1
+ import { useStorageState } from './use-storage-state.js';
2
+
3
+ var useLocalStorageState = function useLocalStorageState(key, useBy) {
4
+ return useStorageState(key, localStorage, useBy);
5
+ };
6
+
7
+ export { useLocalStorageState };
@@ -1,13 +1,13 @@
1
1
  import { type Key } from "react";
2
- import { type Direction } from "../dom/scroll-to";
3
- interface Dependencies {
4
- direction?: Direction;
2
+ import { type Orientation } from "../dom/scroll-to";
3
+ interface UseScrollableProps {
4
+ orientation?: Orientation;
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, }?: Dependencies) => {
10
+ export declare const useScrollable: <P extends HTMLElement, C extends HTMLElement>({ orientation, }?: UseScrollableProps) => {
11
11
  targetRef: import("react").RefObject<P>;
12
12
  triggerRefs: import("react").MutableRefObject<Map<Key, C | null>>;
13
13
  scrollTo: (to: number, duration?: number) => void;
@@ -7,8 +7,8 @@ import { scrollTo as _scrollTo } from '../dom/scroll-to.js';
7
7
  */
8
8
  var useScrollable = function useScrollable() {
9
9
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
10
- _ref$direction = _ref.direction,
11
- direction = _ref$direction === void 0 ? "vertical" : _ref$direction;
10
+ _ref$orientation = _ref.orientation,
11
+ orientation = _ref$orientation === void 0 ? "vertical" : _ref$orientation;
12
12
  var targetRef = useRef(null);
13
13
  var triggerRefs = useRef(new Map());
14
14
  var scrollTo = useCallback(function (to) {
@@ -18,16 +18,16 @@ var useScrollable = function useScrollable() {
18
18
  // use animated scroll
19
19
  _scrollTo(target, to, {
20
20
  duration: duration,
21
- direction: direction
21
+ orientation: orientation
22
22
  });
23
- }, [direction]);
23
+ }, [orientation]);
24
24
  var to = useCallback(function (key) {
25
25
  var trigger = triggerRefs.current.get(key);
26
26
  if (!trigger) return 0;
27
- // different direction, use different property
28
- return direction === "vertical" ? trigger.offsetTop : trigger.offsetLeft;
29
- }, [direction]);
30
- /// set trigger
27
+ // different orientation, use different property
28
+ return orientation === "vertical" ? trigger.offsetTop : trigger.offsetLeft;
29
+ }, [orientation]);
30
+ // set trigger
31
31
  var setTrigger = useCallback(function (key, trigger) {
32
32
  triggerRefs.current.set(key, trigger);
33
33
  }, []);
@@ -0,0 +1,2 @@
1
+ import { type UseStorageStateBy } from "./use-storage-state";
2
+ export declare const useSessionStorageState: (key: string, useBy?: UseStorageStateBy) => import("./use-storage-state").UsedStorageState;
@@ -0,0 +1,7 @@
1
+ import { useStorageState } from './use-storage-state.js';
2
+
3
+ var useSessionStorageState = function useSessionStorageState(key, useBy) {
4
+ return useStorageState(key, sessionStorage, useBy);
5
+ };
6
+
7
+ export { useSessionStorageState };
@@ -0,0 +1,5 @@
1
+ export type UsedStorageState = [string | null, (value: string | null) => void];
2
+ export type UseStorageStateBy = {
3
+ listen?: boolean;
4
+ };
5
+ export declare const useStorageState: (key: string, storage: Storage, { listen }?: UseStorageStateBy) => UsedStorageState;
@@ -0,0 +1,47 @@
1
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
2
+ import { useState, useEffect, useCallback } from 'react';
3
+ import { isNull } from '../is/is-null.js';
4
+
5
+ var useStorageState = function useStorageState(key, storage) {
6
+ var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
7
+ _ref$listen = _ref.listen,
8
+ listen = _ref$listen === void 0 ? true : _ref$listen;
9
+ var _useState = useState(function () {
10
+ return storage.getItem(key);
11
+ }),
12
+ _useState2 = _slicedToArray(_useState, 2),
13
+ state = _useState2[0],
14
+ setState = _useState2[1];
15
+ // effect sync state after key change
16
+ useEffect(function () {
17
+ setState(storage.getItem(key));
18
+ }, [key]);
19
+ useEffect(function () {
20
+ var onStorageChange = function onStorageChange(event) {
21
+ if (event.key !== key) return;
22
+ if (event.storageArea !== storage) return;
23
+ };
24
+ // only listen with flag = `true`
25
+ if (listen) {
26
+ window.addEventListener("storage", onStorageChange);
27
+ }
28
+ return function () {
29
+ window.removeEventListener("storage", onStorageChange);
30
+ };
31
+ }, [listen]);
32
+ // change handler
33
+ var setter = useCallback(function (value) {
34
+ if (listen) {
35
+ if (isNull(value)) {
36
+ storage.removeItem(key);
37
+ } else {
38
+ storage.setItem(key, value);
39
+ }
40
+ return;
41
+ }
42
+ setState(value);
43
+ }, [key, listen]);
44
+ return [state, setter];
45
+ };
46
+
47
+ export { useStorageState };
@@ -1,19 +1,13 @@
1
- import { useRef, useEffect } from 'react';
2
- import { useMounted } from './use-mounted.js';
1
+ import { useEffect } from 'react';
3
2
  import { effect } from '../utils/effect.js';
3
+ import { useIsMounted } from './use-is-mounted.js';
4
4
 
5
5
  var useUpdateEffect = function useUpdateEffect(callback, deps) {
6
- var isMounted = useRef(false);
7
6
  useEffect(function () {
8
- if (!isMounted.current) return;
7
+ if (!isMounted()) return;
9
8
  return effect(callback);
10
9
  }, deps);
11
- useMounted(function () {
12
- isMounted.current = true;
13
- return function () {
14
- isMounted.current = false;
15
- };
16
- });
10
+ var isMounted = useIsMounted();
17
11
  };
18
12
 
19
13
  export { useUpdateEffect };
package/dist/index.d.ts CHANGED
@@ -30,6 +30,10 @@ export { useMediaQuery } from "./hooks/use-media-query";
30
30
  export { useClickAway } from "./hooks/use-click-away";
31
31
  export { useUnmount } from "./hooks/use-unmount";
32
32
  export { useTimer } from "./hooks/use-timer";
33
+ export { useLocalStorageState } from "./hooks/use-local-storage-state";
34
+ export { useSessionStorageState } from "./hooks/use-session-storage-state";
35
+ export { useIsMounted } from "./hooks/use-is-mounted";
36
+ export { useInfiniteScroll } from "./hooks/use-infinite-scroll";
33
37
  /**
34
38
  * @description
35
39
  * is
package/dist/index.js CHANGED
@@ -26,6 +26,10 @@ export { useMediaQuery } from './hooks/use-media-query.js';
26
26
  export { useClickAway } from './hooks/use-click-away.js';
27
27
  export { useUnmount } from './hooks/use-unmount.js';
28
28
  export { useTimer } from './hooks/use-timer.js';
29
+ export { useLocalStorageState } from './hooks/use-local-storage-state.js';
30
+ export { useSessionStorageState } from './hooks/use-session-storage-state.js';
31
+ export { useIsMounted } from './hooks/use-is-mounted.js';
32
+ export { useInfiniteScroll } from './hooks/use-infinite-scroll.js';
29
33
  export { isRefable } from './is/is-refable.js';
30
34
  export { isUndefined } from './is/is-undefined.js';
31
35
  export { isNull } from './is/is-null.js';
@@ -0,0 +1 @@
1
+ export declare const isNumber: (value: unknown) => value is number;
@@ -0,0 +1 @@
1
+ export declare const isString: (value: unknown) => value is string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.2.71",
3
+ "version": "1.2.73",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,2 +0,0 @@
1
- import { type UseStorageBy } from "./use-storage";
2
- export declare const useLocalStorage: (key: string, useBy?: UseStorageBy) => import("./use-storage").UsedStorage;
@@ -1,2 +0,0 @@
1
- import { type UseStorageBy } from "./use-storage";
2
- export declare const useSessionStorage: (key: string, useBy?: UseStorageBy) => import("./use-storage").UsedStorage;
@@ -1,5 +0,0 @@
1
- export type UsedStorage = [string | null, (value: string | null) => void];
2
- export type UseStorageBy = {
3
- listen?: boolean;
4
- };
5
- export declare const useStorage: (key: string, storage: Storage, { listen }?: UseStorageBy) => UsedStorage;