@aiszlab/relax 1.2.25 → 1.2.26

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,14 +1,19 @@
1
- /// <reference types="react" />
1
+ import { type Dispatch, type SetStateAction } from 'react';
2
+ import type { State } from "../types";
3
+ type UsedBoolean = [
4
+ boolean,
5
+ {
6
+ turnOn: () => void;
7
+ turnOff: () => void;
8
+ toggle: () => void;
9
+ setBoolean: Dispatch<SetStateAction<boolean>>;
10
+ }
11
+ ];
2
12
  /**
3
13
  * @author murukal
4
14
  *
5
15
  * @description
6
- * boolean state
16
+ * boolean state, in relax, we already create some api to easy use
7
17
  */
8
- export declare const useBoolean: (initialValue?: boolean) => {
9
- isOn: boolean;
10
- turnOn: () => void;
11
- turnOff: () => void;
12
- toggle: () => void;
13
- setIsOn: import("react").Dispatch<import("react").SetStateAction<boolean>>;
14
- };
18
+ export declare const useBoolean: (initialState?: State<boolean>) => UsedBoolean;
19
+ export {};
@@ -4,20 +4,19 @@ import { useState, useCallback } from 'react';
4
4
  * @author murukal
5
5
  *
6
6
  * @description
7
- * boolean state
7
+ * boolean state, in relax, we already create some api to easy use
8
8
  */
9
- const useBoolean = (initialValue) => {
10
- const [isOn, setIsOn] = useState(initialValue || false);
9
+ const useBoolean = (initialState) => {
10
+ const [isOn, setIsOn] = useState(initialState !== null && initialState !== void 0 ? initialState : false);
11
11
  const turnOn = useCallback(() => setIsOn(true), []);
12
12
  const turnOff = useCallback(() => setIsOn(false), []);
13
13
  const toggle = useCallback(() => setIsOn((_isOn) => !_isOn), []);
14
- return {
15
- isOn,
16
- turnOn,
17
- turnOff,
18
- toggle,
19
- setIsOn
20
- };
14
+ return [isOn, {
15
+ turnOn,
16
+ turnOff,
17
+ toggle,
18
+ setBoolean: setIsOn
19
+ }];
21
20
  };
22
21
 
23
22
  export { useBoolean };
@@ -1,5 +1,5 @@
1
1
  import { type Dispatch, type SetStateAction } from 'react';
2
- import { type State } from '../is/is-state-getter';
2
+ import type { State } from "../types";
3
3
  interface Dependencies<T> {
4
4
  defaultState?: State<T>;
5
5
  }
@@ -1,34 +1,38 @@
1
- /// <reference types="react" />
2
- interface Props {
1
+ import { type Dispatch, type SetStateAction } from 'react';
2
+ import type { State } from '../types';
3
+ type Props = {
3
4
  /**
4
5
  * @description
5
6
  * initial value
6
7
  */
7
- initialValue?: number;
8
+ initialState?: State<number>;
8
9
  /**
9
10
  * @description
10
- * max
11
+ * max: count will not be greater than max
11
12
  */
12
13
  max?: number;
13
14
  /**
14
15
  * @description
15
- * min
16
+ * min: count will not be smaller than min
16
17
  */
17
18
  min?: number;
18
- }
19
+ };
20
+ type UsedCounter = [
21
+ number,
22
+ {
23
+ add: (step?: number) => void;
24
+ subtract: (step?: number) => void;
25
+ first: () => void;
26
+ last: () => void;
27
+ reset: () => void;
28
+ setCount: Dispatch<SetStateAction<number>>;
29
+ }
30
+ ];
19
31
  /**
20
32
  * @author murukal
21
33
  *
22
34
  * @description
23
35
  * a number counter with some useful apis
24
36
  */
25
- export declare const useCounter: ({ max, min, ...props }?: Props) => {
26
- count: number;
27
- next: (step?: number) => void;
28
- prev: (step?: number) => void;
29
- first: () => void;
30
- last: () => void;
31
- reset: () => void;
32
- setCount: import("react").Dispatch<import("react").SetStateAction<number>>;
33
- };
37
+ export declare const useCounter: (initialState?: State<number>, { max, min }?: Props) => UsedCounter;
34
38
  export {};
@@ -1,6 +1,12 @@
1
- import { __rest } from '../node_modules/tslib/tslib.es6.mjs';
2
- import { useMemo, useState, useCallback } from 'react';
1
+ import { useState, useCallback, useMemo } from 'react';
3
2
  import { clamp } from '../utils/clamp.mjs';
3
+ import '../utils/debounce.mjs';
4
+ import 'rxjs';
5
+ import { useDefault } from './use-default.mjs';
6
+ import './use-scroll-locker.mjs';
7
+ import '../dom/scroll-to.mjs';
8
+ import './use-toggleable.mjs';
9
+ import 'react-is';
4
10
 
5
11
  /**
6
12
  * @author murukal
@@ -8,34 +14,26 @@ import { clamp } from '../utils/clamp.mjs';
8
14
  * @description
9
15
  * a number counter with some useful apis
10
16
  */
11
- const useCounter = (_a = { initialValue: 0, max: Infinity, min: 0 }) => {
12
- var { max = Infinity, min = 0 } = _a, props = __rest(_a, ["max", "min"]);
13
- const initialValue = useMemo(() => { var _a; return clamp((_a = props.initialValue) !== null && _a !== void 0 ? _a : 0, min, max); }, [max, min, props.initialValue]);
14
- const [count, setCount] = useState(initialValue);
15
- const next = useCallback((step = 1) => {
16
- setCount((prev) => Math.min(max, prev + step));
17
+ const useCounter = (initialState, { max = Infinity, min = 0 } = { max: Infinity, min: 0 }) => {
18
+ const defaultState = useDefault(initialState !== null && initialState !== void 0 ? initialState : 0);
19
+ const [_count, _setCount] = useState(defaultState);
20
+ const add = useCallback((step = 1) => {
21
+ _setCount((prev) => Math.min(max, prev + step));
17
22
  }, [max]);
18
- const prev = useCallback((step = 1) => {
19
- setCount((prev) => Math.max(min, prev - step));
23
+ const subtract = useCallback((step = 1) => {
24
+ _setCount((prev) => Math.max(min, prev - step));
20
25
  }, [min]);
21
26
  const first = useCallback(() => {
22
- setCount(min);
27
+ _setCount(min);
23
28
  }, [min]);
24
29
  const last = useCallback(() => {
25
- setCount(max);
30
+ _setCount(max);
26
31
  }, [max]);
27
32
  const reset = useCallback(() => {
28
- setCount(initialValue);
29
- }, [initialValue]);
30
- return {
31
- count,
32
- next,
33
- prev,
34
- first,
35
- last,
36
- reset,
37
- setCount
38
- };
33
+ _setCount(defaultState);
34
+ }, []);
35
+ const count = useMemo(() => clamp(_count, min, max), [min, max]);
36
+ return [count, { add, subtract, first, last, reset, setCount: _setCount }];
39
37
  };
40
38
 
41
39
  export { useCounter };
@@ -1,4 +1,4 @@
1
- import { type State } from '../is/is-state-getter';
1
+ import type { State } from '../types';
2
2
  /**
3
3
  * @author murukal
4
4
  *
@@ -1,5 +1,5 @@
1
1
  import { useMemo } from 'react';
2
- import { isStateGetter } from '../is/is-state-getter.mjs';
2
+ import { toFunction } from '../utils/to-function.mjs';
3
3
 
4
4
  /**
5
5
  * @author murukal
@@ -8,14 +8,7 @@ import { isStateGetter } from '../is/is-state-getter.mjs';
8
8
  * state always be same after first render
9
9
  */
10
10
  const useDefault = (initialState) => {
11
- return useMemo(() => {
12
- if (isStateGetter(initialState)) {
13
- return initialState();
14
- }
15
- else {
16
- return initialState;
17
- }
18
- }, []);
11
+ return useMemo(toFunction(initialState), []);
19
12
  };
20
13
 
21
14
  export { useDefault };
@@ -0,0 +1,9 @@
1
+ type UsedFocus = [
2
+ boolean,
3
+ {
4
+ onFocus: () => void;
5
+ onBlur: () => void;
6
+ }
7
+ ];
8
+ export declare const useFoucs: () => UsedFocus;
9
+ export {};
@@ -1,5 +1,9 @@
1
- export declare const useHover: () => {
2
- isHovered: boolean;
3
- onPointerEnter: () => void;
4
- onPointerLeave: () => void;
5
- };
1
+ type UsedHover = [
2
+ boolean,
3
+ {
4
+ onPointerEnter: () => void;
5
+ onPointerLeave: () => void;
6
+ }
7
+ ];
8
+ export declare const useHover: () => UsedHover;
9
+ export {};
@@ -1,19 +1,15 @@
1
+ import { useCallback } from 'react';
1
2
  import { useBoolean } from './use-boolean.mjs';
2
- import { useEvent } from './use-event.mjs';
3
3
 
4
4
  const useHover = () => {
5
- const { isOn: isHovered, turnOff, turnOn } = useBoolean(false);
6
- const onPointerEnter = useEvent(() => {
5
+ const [isHovered, { turnOn, turnOff }] = useBoolean(false);
6
+ const onPointerEnter = useCallback(() => {
7
7
  turnOn();
8
- });
9
- const onPointerLeave = useEvent(() => {
8
+ }, []);
9
+ const onPointerLeave = useCallback(() => {
10
10
  turnOff();
11
- });
12
- return {
13
- isHovered,
14
- onPointerEnter,
15
- onPointerLeave
16
- };
11
+ }, []);
12
+ return [isHovered, { onPointerEnter, onPointerLeave }];
17
13
  };
18
14
 
19
15
  export { useHover };
@@ -1,5 +1,5 @@
1
1
  type Status = 'none' | 'loading' | 'error' | 'loaded';
2
- interface Dependencies {
2
+ interface Props {
3
3
  src: string;
4
4
  }
5
5
  /**
@@ -8,5 +8,5 @@ interface Dependencies {
8
8
  * @description
9
9
  * image loader
10
10
  */
11
- export declare const useImageLoader: ({ src }: Dependencies) => Status;
11
+ export declare const useImageLoader: ({ src }: Props) => Status;
12
12
  export {};
@@ -1,4 +1,4 @@
1
- import { State } from '../is/is-state-getter';
1
+ import type { State } from '../types';
2
2
  /**
3
3
  * @description
4
4
  * use reactive
@@ -3,25 +3,22 @@ import { useId, useLayoutEffect } from 'react';
3
3
  import { isOverflow } from '../is/is-overflow.mjs';
4
4
  import { isStyleElement } from '../is/is-style-element.mjs';
5
5
 
6
- var _ScrollLocker_scrollLocker, _ScrollLocker_barSize;
6
+ var _a, _ScrollLocker_scrollLocker, _ScrollLocker_barSize;
7
7
  const isComputable = (value) => /^(.*)px$/.test(value);
8
8
  class ScrollLocker {
9
9
  constructor() {
10
- var _a;
11
- // singleton mode
12
- _ScrollLocker_scrollLocker.set(this, null
13
- // bar size
14
- );
10
+ var _b;
11
+ var _c;
15
12
  // bar size
16
13
  _ScrollLocker_barSize.set(this, null);
17
- return (__classPrivateFieldSet(this, _ScrollLocker_scrollLocker, (_a = __classPrivateFieldGet(this, _ScrollLocker_scrollLocker, "f")) !== null && _a !== void 0 ? _a : this, "f"));
14
+ return (__classPrivateFieldSet(_c = _a, _a, (_b = __classPrivateFieldGet(_c, _a, "f", _ScrollLocker_scrollLocker)) !== null && _b !== void 0 ? _b : this, "f", _ScrollLocker_scrollLocker));
18
15
  }
19
16
  get barSize() {
20
- var _a;
17
+ var _b;
21
18
  if (__classPrivateFieldGet(this, _ScrollLocker_barSize, "f"))
22
19
  return __classPrivateFieldGet(this, _ScrollLocker_barSize, "f");
23
20
  const { width, height } = getComputedStyle(document.body, '::-webkit-scrollbar');
24
- return (__classPrivateFieldSet(this, _ScrollLocker_barSize, (_a = __classPrivateFieldGet(this, _ScrollLocker_barSize, "f")) !== null && _a !== void 0 ? _a : {
21
+ return (__classPrivateFieldSet(this, _ScrollLocker_barSize, (_b = __classPrivateFieldGet(this, _ScrollLocker_barSize, "f")) !== null && _b !== void 0 ? _b : {
25
22
  width: isComputable(width) ? width : '0',
26
23
  height: isComputable(height) ? height : '0'
27
24
  }, "f"));
@@ -64,7 +61,9 @@ class ScrollLocker {
64
61
  this.container.removeChild(locked);
65
62
  }
66
63
  }
67
- _ScrollLocker_scrollLocker = new WeakMap(), _ScrollLocker_barSize = new WeakMap();
64
+ _a = ScrollLocker, _ScrollLocker_barSize = new WeakMap();
65
+ // singleton mode
66
+ _ScrollLocker_scrollLocker = { value: null };
68
67
  const useScrollLocker = (isLock) => {
69
68
  const id = useId();
70
69
  useLayoutEffect(() => {
@@ -7,5 +7,5 @@ interface Options {
7
7
  * @description
8
8
  * timeout effect
9
9
  */
10
- export declare const useTimeout: (handler: UnderlyingSinkCloseCallback, { duration }: Options) => void;
10
+ export declare const useTimeout: (handler: Function, { duration }: Options) => void;
11
11
  export {};
@@ -1,4 +1,4 @@
1
- import { useRef, useEffect } from 'react';
1
+ import { useEffect } from 'react';
2
2
 
3
3
  /**
4
4
  * @author murukal
@@ -7,13 +7,12 @@ import { useRef, useEffect } from 'react';
7
7
  * timeout effect
8
8
  */
9
9
  const useTimeout = (handler, { duration }) => {
10
- const timer = useRef(null);
11
10
  useEffect(() => {
12
- timer.current = setTimeout(handler, duration);
11
+ const timer = setTimeout(handler, duration);
13
12
  return () => {
14
- if (!timer.current)
13
+ if (!timer)
15
14
  return;
16
- clearTimeout(timer.current);
15
+ clearTimeout(timer);
17
16
  };
18
17
  }, [duration]);
19
18
  };
package/dist/index.d.ts CHANGED
@@ -48,3 +48,5 @@ export { range } from './utils/range';
48
48
  export { clamp } from './utils/clamp';
49
49
  export { chain } from './utils/chain';
50
50
  export { debounce } from './utils/debounce';
51
+ export { toArray } from './utils/to-array';
52
+ export { toFunction } from './utils/to-function';
package/dist/index.mjs CHANGED
@@ -36,3 +36,5 @@ export { range } from './utils/range.mjs';
36
36
  export { clamp } from './utils/clamp.mjs';
37
37
  export { chain } from './utils/chain.mjs';
38
38
  export { debounce } from './utils/debounce.mjs';
39
+ export { toArray } from './utils/to-array.mjs';
40
+ export { toFunction } from './utils/to-function.mjs';
@@ -1,14 +1,6 @@
1
- type StateGetter<T> = () => T;
2
- /**
3
- * @description
4
- * state setter
5
- *
6
- * used by initial state, default state, controlled state
7
- */
8
- export type State<T> = T | StateGetter<T>;
1
+ import type { State, StateGetter } from "../types";
9
2
  /**
10
3
  * @description
11
4
  * is state getter
12
5
  */
13
6
  export declare const isStateGetter: <T>(state: State<T>) => state is StateGetter<T>;
14
- export {};
@@ -4,3 +4,4 @@ export type { Partialable } from './partialable';
4
4
  export type { RequiredIn } from './required-in';
5
5
  export type { ThenableEffectCallback } from './thenable-effect-callback';
6
6
  export type { First } from './first';
7
+ export type { State, StateGetter } from './state';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @description
3
+ * in react, define some initial state always use a value getter
4
+ */
5
+ export type StateGetter<T> = () => T;
6
+ /**
7
+ * @description
8
+ * value or state setter
9
+ */
10
+ export type State<T> = T | StateGetter<T>;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @description
3
+ * convert any type data to a array
4
+ */
5
+ const toArray = (value) => {
6
+ if (Array.isArray(value)) {
7
+ return value;
8
+ }
9
+ return [value];
10
+ };
11
+
12
+ export { toArray };
@@ -0,0 +1 @@
1
+ export declare const toFunction: <T extends Function>(value: unknown) => T;
@@ -0,0 +1,11 @@
1
+ import { isFunction } from '../is/is-function.mjs';
2
+
3
+ const toFunction = (value) => {
4
+ const _isFunction = isFunction(value);
5
+ if (_isFunction) {
6
+ return value;
7
+ }
8
+ return (() => value);
9
+ };
10
+
11
+ export { toFunction };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.2.25",
3
+ "version": "1.2.26",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "exports": {
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "devDependencies": {
28
28
  "@babel/core": "^7.22.5",
29
- "@babel/preset-env": "^7.22.5",
29
+ "@babel/preset-env": "^7.24.0",
30
30
  "@babel/preset-typescript": "^7.22.5",
31
31
  "@rollup/plugin-babel": "^6.0.3",
32
32
  "@rollup/plugin-node-resolve": "^15.1.0",