@aiszlab/relax 1.0.18 → 1.0.20

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.
Files changed (28) hide show
  1. package/dist/hooks/use-controlled-state.d.ts +13 -0
  2. package/dist/hooks/use-controlled-state.js +38 -0
  3. package/dist/{use-once-state.d.ts → hooks/use-once-state.d.ts} +1 -1
  4. package/dist/index.d.ts +19 -8
  5. package/dist/index.js +11 -8
  6. package/dist/utils/is-refable.d.ts +5 -0
  7. package/dist/utils/is-refable.js +34 -0
  8. package/dist/utils/is-state-getter.d.ts +14 -0
  9. package/dist/utils/is-state-getter.js +9 -0
  10. package/dist/utils/is-void.d.ts +5 -0
  11. package/dist/utils/is-void.js +9 -0
  12. package/package.json +3 -1
  13. package/dist/use-controlled-state.d.ts +0 -12
  14. package/dist/use-controlled-state.js +0 -33
  15. /package/dist/{use-boolean.d.ts → hooks/use-boolean.d.ts} +0 -0
  16. /package/dist/{use-boolean.js → hooks/use-boolean.js} +0 -0
  17. /package/dist/{use-debounce-callback.d.ts → hooks/use-debounce-callback.d.ts} +0 -0
  18. /package/dist/{use-debounce-callback.js → hooks/use-debounce-callback.js} +0 -0
  19. /package/dist/{use-image-loader.d.ts → hooks/use-image-loader.d.ts} +0 -0
  20. /package/dist/{use-image-loader.js → hooks/use-image-loader.js} +0 -0
  21. /package/dist/{use-media-query.d.ts → hooks/use-media-query.d.ts} +0 -0
  22. /package/dist/{use-mount.d.ts → hooks/use-mount.d.ts} +0 -0
  23. /package/dist/{use-mount.js → hooks/use-mount.js} +0 -0
  24. /package/dist/{use-mounted.d.ts → hooks/use-mounted.d.ts} +0 -0
  25. /package/dist/{use-mounted.js → hooks/use-mounted.js} +0 -0
  26. /package/dist/{use-once-state.js → hooks/use-once-state.js} +0 -0
  27. /package/dist/{use-timeout.d.ts → hooks/use-timeout.d.ts} +0 -0
  28. /package/dist/{use-timeout.js → hooks/use-timeout.js} +0 -0
@@ -0,0 +1,13 @@
1
+ import { type Dispatch, type SetStateAction } from 'react';
2
+ import { type State } from '../utils/is-state-getter';
3
+ interface Props<T> {
4
+ defaultState?: State<T>;
5
+ }
6
+ /**
7
+ * @author murukal
8
+ *
9
+ * @description
10
+ * controlled state
11
+ */
12
+ export declare const useControlledState: <T>(controlledState: State<T>, { defaultState }?: Props<T>) => [T, Dispatch<SetStateAction<T>>];
13
+ export {};
@@ -0,0 +1,38 @@
1
+ import { useState, useEffect } from 'react';
2
+ import { isStateGetter } from '../utils/is-state-getter.js';
3
+ import { isVoid } from '../utils/is-void.js';
4
+
5
+ /**
6
+ * @author murukal
7
+ *
8
+ * @description
9
+ * controlled state
10
+ */
11
+ const useControlledState = (controlledState, { defaultState } = {}) => {
12
+ /// initialize state
13
+ const [state, setState] = useState(() => {
14
+ if (isStateGetter(controlledState))
15
+ return controlledState();
16
+ if (isVoid(controlledState)) {
17
+ if (isVoid(defaultState))
18
+ return controlledState;
19
+ if (isStateGetter(defaultState))
20
+ return defaultState();
21
+ return defaultState;
22
+ }
23
+ return controlledState;
24
+ });
25
+ useEffect(() => {
26
+ // when state is not controlled
27
+ if (isVoid(controlledState))
28
+ return;
29
+ // if state is equal with value
30
+ if (controlledState === state)
31
+ return;
32
+ /// update inner state
33
+ setState(controlledState);
34
+ }, [controlledState]);
35
+ return [state, setState];
36
+ };
37
+
38
+ export { useControlledState };
@@ -4,4 +4,4 @@
4
4
  * @description
5
5
  * state always be same after first render
6
6
  */
7
- export declare const useOnceState: <T>(initialState: T | (() => T)) => T;
7
+ export declare const useOnceState: <T>(initialState: State<T>) => T;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,19 @@
1
- export { useBoolean } from './use-boolean';
2
- export { useDebounceCallback } from './use-debounce-callback';
3
- export { useImageLoader } from './use-image-loader';
4
- export { useMount } from './use-mount';
5
- export { useMounted } from './use-mounted';
6
- export { useTimeout } from './use-timeout';
7
- export { useControlledState } from './use-controlled-state';
8
- export { useOnceState } from './use-once-state';
1
+ /**
2
+ * @description
3
+ * hooks
4
+ */
5
+ export { useBoolean } from './hooks/use-boolean';
6
+ export { useDebounceCallback } from './hooks/use-debounce-callback';
7
+ export { useImageLoader } from './hooks/use-image-loader';
8
+ export { useMount } from './hooks/use-mount';
9
+ export { useMounted } from './hooks/use-mounted';
10
+ export { useTimeout } from './hooks/use-timeout';
11
+ export { useControlledState } from './hooks/use-controlled-state';
12
+ export { useOnceState } from './hooks/use-once-state';
13
+ /**
14
+ * @description
15
+ * utils
16
+ */
17
+ export { isRefable } from './utils/is-refable';
18
+ export { isVoid } from './utils/is-void';
19
+ export { isStateGetter } from './utils/is-state-getter';
package/dist/index.js CHANGED
@@ -1,8 +1,11 @@
1
- export { useBoolean } from './use-boolean.js';
2
- export { useDebounceCallback } from './use-debounce-callback.js';
3
- export { useImageLoader } from './use-image-loader.js';
4
- export { useMount } from './use-mount.js';
5
- export { useMounted } from './use-mounted.js';
6
- export { useTimeout } from './use-timeout.js';
7
- export { useControlledState } from './use-controlled-state.js';
8
- export { useOnceState } from './use-once-state.js';
1
+ export { useBoolean } from './hooks/use-boolean.js';
2
+ export { useDebounceCallback } from './hooks/use-debounce-callback.js';
3
+ export { useImageLoader } from './hooks/use-image-loader.js';
4
+ export { useMount } from './hooks/use-mount.js';
5
+ export { useMounted } from './hooks/use-mounted.js';
6
+ export { useTimeout } from './hooks/use-timeout.js';
7
+ export { useControlledState } from './hooks/use-controlled-state.js';
8
+ export { useOnceState } from './hooks/use-once-state.js';
9
+ export { isRefable } from './utils/is-refable.js';
10
+ export { isVoid } from './utils/is-void.js';
11
+ export { isStateGetter } from './utils/is-state-getter.js';
@@ -0,0 +1,5 @@
1
+ import { type ReactNode } from 'react';
2
+ /**
3
+ * @description
4
+ */
5
+ export declare const isRefable: (node: ReactNode) => boolean;
@@ -0,0 +1,34 @@
1
+ import { isValidElement } from 'react';
2
+ import { isFragment, isMemo } from 'react-is';
3
+
4
+ /**
5
+ * @description
6
+ */
7
+ const isRefable = (node) => {
8
+ if (!isValidElement(node)) {
9
+ return false;
10
+ }
11
+ if (isFragment(node)) {
12
+ return false;
13
+ }
14
+ return _RefableElement(node);
15
+ };
16
+ /**
17
+ * @description
18
+ * refable element
19
+ */
20
+ const _RefableElement = (element) => {
21
+ var _a, _b;
22
+ const type = isMemo(element) ? element.type.type : element.type;
23
+ // Function component node
24
+ if (typeof type === 'function' && !((_a = type.prototype) === null || _a === void 0 ? void 0 : _a.render)) {
25
+ return false;
26
+ }
27
+ // Class component
28
+ if (typeof element === 'function' && !((_b = element.prototype) === null || _b === void 0 ? void 0 : _b.render)) {
29
+ return false;
30
+ }
31
+ return true;
32
+ };
33
+
34
+ export { isRefable };
@@ -0,0 +1,14 @@
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>;
9
+ /**
10
+ * @description
11
+ * is state getter
12
+ */
13
+ export declare const isStateGetter: <T>(state: State<T>) => state is StateGetter<T>;
14
+ export {};
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @description
3
+ * is state getter
4
+ */
5
+ const isStateGetter = (state) => {
6
+ return typeof state === 'function';
7
+ };
8
+
9
+ export { isStateGetter };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @description
3
+ * is undefined
4
+ */
5
+ export declare const isVoid: (value: unknown) => value is undefined;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @description
3
+ * is undefined
4
+ */
5
+ const isVoid = (value) => {
6
+ return value === void 0;
7
+ };
8
+
9
+ export { isVoid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -11,6 +11,7 @@
11
11
  "pub": "npm run clean:build && npm run build && npm publish"
12
12
  },
13
13
  "dependencies": {
14
+ "react-is": "^18.2.0",
14
15
  "rxjs": "^7.8.1"
15
16
  },
16
17
  "devDependencies": {
@@ -22,6 +23,7 @@
22
23
  "@rollup/plugin-typescript": "^11.1.1",
23
24
  "@types/react": "^18.2.12",
24
25
  "@types/react-dom": "^18.2.5",
26
+ "@types/react-is": "^18.2.2",
25
27
  "rollup": "^3.27.0",
26
28
  "typescript": "^5.1.3"
27
29
  },
@@ -1,12 +0,0 @@
1
- import { type Dispatch, type SetStateAction } from 'react';
2
- interface Props<T> {
3
- defaultState: T;
4
- }
5
- /**
6
- * @author murukal
7
- *
8
- * @description
9
- * controlled state
10
- */
11
- export declare const useControlledState: <T>(controlledState?: T | (() => T) | undefined, props?: Props<T> | undefined) => [T | undefined, Dispatch<SetStateAction<T | undefined>>];
12
- export {};
@@ -1,33 +0,0 @@
1
- import { useState, useEffect } from 'react';
2
-
3
- /**
4
- * @author murukal
5
- *
6
- * @description
7
- * controlled state
8
- */
9
- const useControlledState = (controlledState, props) => {
10
- const [state, setState] = useState(() => {
11
- if (controlledState === void 0) {
12
- return props === null || props === void 0 ? void 0 : props.defaultState;
13
- }
14
- if (typeof controlledState === 'function') {
15
- return controlledState();
16
- }
17
- return controlledState;
18
- });
19
- useEffect(() => {
20
- // when state is not controlled
21
- if (controlledState === void 0) {
22
- return;
23
- }
24
- // if state is equal with value
25
- if (controlledState === state) {
26
- return;
27
- }
28
- setState(controlledState);
29
- }, [controlledState]);
30
- return [state, setState];
31
- };
32
-
33
- export { useControlledState };
File without changes
File without changes
File without changes
File without changes
File without changes