@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.
- package/dist/hooks/use-boolean.d.ts +14 -9
- package/dist/hooks/use-boolean.mjs +9 -10
- package/dist/hooks/use-controlled-state.d.ts +1 -1
- package/dist/hooks/use-counter.d.ts +19 -15
- package/dist/hooks/use-counter.mjs +21 -23
- package/dist/hooks/use-default.d.ts +1 -1
- package/dist/hooks/use-default.mjs +2 -9
- package/dist/hooks/use-focus.d.ts +9 -0
- package/dist/hooks/use-hover.d.ts +9 -5
- package/dist/hooks/use-hover.mjs +7 -11
- package/dist/hooks/use-image-loader.d.ts +2 -2
- package/dist/hooks/use-reactive.d.ts +1 -1
- package/dist/hooks/use-scroll-locker.mjs +9 -10
- package/dist/hooks/use-timeout.d.ts +1 -1
- package/dist/hooks/use-timeout.mjs +4 -5
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/is/is-state-getter.d.ts +1 -9
- package/dist/types/index.d.ts +1 -0
- package/dist/types/state.d.ts +10 -0
- package/dist/utils/to-array.mjs +12 -0
- package/dist/utils/to-function.d.ts +1 -0
- package/dist/utils/to-function.mjs +11 -0
- package/package.json +2 -2
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
|
|
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: (
|
|
9
|
-
|
|
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 = (
|
|
10
|
-
const [isOn, setIsOn] = useState(
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
14
|
+
return [isOn, {
|
|
15
|
+
turnOn,
|
|
16
|
+
turnOff,
|
|
17
|
+
toggle,
|
|
18
|
+
setBoolean: setIsOn
|
|
19
|
+
}];
|
|
21
20
|
};
|
|
22
21
|
|
|
23
22
|
export { useBoolean };
|
|
@@ -1,34 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
-
|
|
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
|
|
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 {
|
|
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 = (
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
|
|
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
|
|
19
|
-
|
|
23
|
+
const subtract = useCallback((step = 1) => {
|
|
24
|
+
_setCount((prev) => Math.max(min, prev - step));
|
|
20
25
|
}, [min]);
|
|
21
26
|
const first = useCallback(() => {
|
|
22
|
-
|
|
27
|
+
_setCount(min);
|
|
23
28
|
}, [min]);
|
|
24
29
|
const last = useCallback(() => {
|
|
25
|
-
|
|
30
|
+
_setCount(max);
|
|
26
31
|
}, [max]);
|
|
27
32
|
const reset = useCallback(() => {
|
|
28
|
-
|
|
29
|
-
}, [
|
|
30
|
-
|
|
31
|
-
|
|
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,5 +1,5 @@
|
|
|
1
1
|
import { useMemo } from 'react';
|
|
2
|
-
import {
|
|
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 };
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
type UsedHover = [
|
|
2
|
+
boolean,
|
|
3
|
+
{
|
|
4
|
+
onPointerEnter: () => void;
|
|
5
|
+
onPointerLeave: () => void;
|
|
6
|
+
}
|
|
7
|
+
];
|
|
8
|
+
export declare const useHover: () => UsedHover;
|
|
9
|
+
export {};
|
package/dist/hooks/use-hover.mjs
CHANGED
|
@@ -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 {
|
|
6
|
-
const onPointerEnter =
|
|
5
|
+
const [isHovered, { turnOn, turnOff }] = useBoolean(false);
|
|
6
|
+
const onPointerEnter = useCallback(() => {
|
|
7
7
|
turnOn();
|
|
8
|
-
});
|
|
9
|
-
const onPointerLeave =
|
|
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
|
|
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 }:
|
|
11
|
+
export declare const useImageLoader: ({ src }: Props) => Status;
|
|
12
12
|
export {};
|
|
@@ -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
|
|
11
|
-
|
|
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(
|
|
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
|
|
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, (
|
|
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
|
-
|
|
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(() => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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
|
|
11
|
+
const timer = setTimeout(handler, duration);
|
|
13
12
|
return () => {
|
|
14
|
-
if (!timer
|
|
13
|
+
if (!timer)
|
|
15
14
|
return;
|
|
16
|
-
clearTimeout(timer
|
|
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
|
|
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 {};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const toFunction: <T extends Function>(value: unknown) => T;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiszlab/relax",
|
|
3
|
-
"version": "1.2.
|
|
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.
|
|
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",
|