@zayne-labs/toolkit-react 0.12.1 → 0.12.2
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/esm/hooks/useControllable.js +7 -10
- package/dist/esm/hooks/useControllable.js.map +1 -1
- package/dist/esm/hooks/useCopyToClipboard.d.ts +2 -2
- package/dist/esm/hooks/useDebounce.d.ts +4 -4
- package/dist/esm/hooks/useLocationState.d.ts +5 -5
- package/dist/esm/hooks/useSearch.d.ts +2 -2
- package/dist/esm/hooks/useSearchParams.d.ts +3 -3
- package/package.json +4 -4
|
@@ -51,24 +51,21 @@ const useControllableProp = (options) => {
|
|
|
51
51
|
*/
|
|
52
52
|
const useControllableState = (options) => {
|
|
53
53
|
const { defaultValue, equalityFn = Object.is, onChange: onChangeProp, value: valueProp } = options;
|
|
54
|
-
const
|
|
55
|
-
const
|
|
54
|
+
const stableOnchangeProp = useCallbackRef(onChangeProp);
|
|
55
|
+
const stableEqualityFn = useCallbackRef(equalityFn);
|
|
56
56
|
const [unControlledState, setUncontrolledState] = useState(defaultValue);
|
|
57
57
|
const isControlled = valueProp !== void 0;
|
|
58
58
|
const currentValue = isControlled ? valueProp : unControlledState;
|
|
59
59
|
const setValue = useCallback((newValue) => {
|
|
60
60
|
const nextValue = isFunction(newValue) ? newValue(currentValue) : newValue;
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
61
|
+
if (stableEqualityFn(currentValue, nextValue)) return;
|
|
62
|
+
stableOnchangeProp(nextValue);
|
|
63
|
+
if (isControlled) return;
|
|
66
64
|
setUncontrolledState(nextValue);
|
|
67
|
-
savedOnchangeProp(nextValue);
|
|
68
65
|
}, [
|
|
69
66
|
isControlled,
|
|
70
|
-
|
|
71
|
-
|
|
67
|
+
stableOnchangeProp,
|
|
68
|
+
stableEqualityFn,
|
|
72
69
|
currentValue
|
|
73
70
|
]);
|
|
74
71
|
return [currentValue, setValue];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useControllable.js","names":["setValue: StateSetter<TValue>"],"sources":["../../../src/hooks/useControllable.ts"],"sourcesContent":["\"use client\";\n\nimport { isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { useCallback, useMemo, useState } from \"react\";\nimport type { StateSetter } from \"@/utils\";\nimport { useCallbackRef } from \"./useCallbackRef\";\n\ntype UseControllablePropOptions<TProp> = {\n\tprop: TProp | undefined;\n\tstate: TProp;\n};\n\n/**\n * @description Given a prop value and state value, the useControllableProp hook is used to determine whether a component is controlled or uncontrolled, and also returns the computed value.\n */\nexport const useControllableProp = <TProp>(options: UseControllablePropOptions<TProp>) => {\n\tconst { prop, state } = options;\n\n\tconst isControlled = prop !== undefined;\n\n\tconst value = isControlled ? prop : state;\n\n\tconst result = useMemo<[isControlled: typeof isControlled, value: typeof value]>(\n\t\t() => [isControlled, value],\n\t\t[isControlled, value]\n\t);\n\n\treturn result;\n};\n\ntype UseControllableStateOptions<TValue> = {\n\tdefaultValue?: TValue | (() => TValue);\n\tequalityFn?: (prevState: TValue, nextValue: TValue) => boolean;\n\tonChange?: (value: TValue) => void;\n\tvalue?: TValue;\n};\n\n/**\n * @description React hook to manage state that can be either controlled or uncontrolled.\n * - When `options.value` is provided, the hook operates in controlled mode.\n * In this mode, `value` always equals `options.value` and `setValue` will\n * invoke `options.onChange(next)` without mutating internal state.\n * - When `options.value` is not provided, the hook operates in uncontrolled\n * mode, initializing internal state from `options.defaultValue` and updating\n * it via `setValue`.\n * - All updates are gated by `options.equalityFn(prev, next)` which defaults\n * to `Object.is`.\n *\n * @param options - Configuration options for the hook.\n * @param options.value - Controlled value. If defined, the state is controlled.\n * @param options.defaultValue - Initial value for the uncontrolled state. Can be a\n * function for lazy initialization or a direct value.\n * @param options.onChange - Callback fired when a new value is requested. In\n * controlled mode, this is invoked instead of updating internal state. In\n * uncontrolled mode, it is called after the internal state updates.\n * @param options.equalityFn - Predicate that decides whether to commit an\n * update given `prevState` and `nextValue`. If the values are equal, the update\n * is skipped. Defaults to `Object.is`.\n * @returns A tuple `[value, setValue]` just like React.useState.\n *\n * @example\n * // Uncontrolled usage\n * const [value, setValue] = useControllableState({ defaultValue: 0 });\n *\n * @example\n * // Controlled usage\n * const [value, setValue] = useControllableState({\n * value: props.value,\n * onChange: props.onChange,\n * });\n */\nexport const useControllableState = <TValue>(options: UseControllableStateOptions<TValue>) => {\n\tconst { defaultValue, equalityFn = Object.is, onChange: onChangeProp, value: valueProp } = options;\n\n\tconst
|
|
1
|
+
{"version":3,"file":"useControllable.js","names":["setValue: StateSetter<TValue>"],"sources":["../../../src/hooks/useControllable.ts"],"sourcesContent":["\"use client\";\n\nimport { isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { useCallback, useMemo, useState } from \"react\";\nimport type { StateSetter } from \"@/utils\";\nimport { useCallbackRef } from \"./useCallbackRef\";\n\ntype UseControllablePropOptions<TProp> = {\n\tprop: TProp | undefined;\n\tstate: TProp;\n};\n\n/**\n * @description Given a prop value and state value, the useControllableProp hook is used to determine whether a component is controlled or uncontrolled, and also returns the computed value.\n */\nexport const useControllableProp = <TProp>(options: UseControllablePropOptions<TProp>) => {\n\tconst { prop, state } = options;\n\n\tconst isControlled = prop !== undefined;\n\n\tconst value = isControlled ? prop : state;\n\n\tconst result = useMemo<[isControlled: typeof isControlled, value: typeof value]>(\n\t\t() => [isControlled, value],\n\t\t[isControlled, value]\n\t);\n\n\treturn result;\n};\n\ntype UseControllableStateOptions<TValue> = {\n\tdefaultValue?: TValue | (() => TValue);\n\tequalityFn?: (prevState: TValue, nextValue: TValue) => boolean;\n\tonChange?: (value: TValue) => void;\n\tvalue?: TValue;\n};\n\n/**\n * @description React hook to manage state that can be either controlled or uncontrolled.\n * - When `options.value` is provided, the hook operates in controlled mode.\n * In this mode, `value` always equals `options.value` and `setValue` will\n * invoke `options.onChange(next)` without mutating internal state.\n * - When `options.value` is not provided, the hook operates in uncontrolled\n * mode, initializing internal state from `options.defaultValue` and updating\n * it via `setValue`.\n * - All updates are gated by `options.equalityFn(prev, next)` which defaults\n * to `Object.is`.\n *\n * @param options - Configuration options for the hook.\n * @param options.value - Controlled value. If defined, the state is controlled.\n * @param options.defaultValue - Initial value for the uncontrolled state. Can be a\n * function for lazy initialization or a direct value.\n * @param options.onChange - Callback fired when a new value is requested. In\n * controlled mode, this is invoked instead of updating internal state. In\n * uncontrolled mode, it is called after the internal state updates.\n * @param options.equalityFn - Predicate that decides whether to commit an\n * update given `prevState` and `nextValue`. If the values are equal, the update\n * is skipped. Defaults to `Object.is`.\n * @returns A tuple `[value, setValue]` just like React.useState.\n *\n * @example\n * // Uncontrolled usage\n * const [value, setValue] = useControllableState({ defaultValue: 0 });\n *\n * @example\n * // Controlled usage\n * const [value, setValue] = useControllableState({\n * value: props.value,\n * onChange: props.onChange,\n * });\n */\nexport const useControllableState = <TValue>(options: UseControllableStateOptions<TValue>) => {\n\tconst { defaultValue, equalityFn = Object.is, onChange: onChangeProp, value: valueProp } = options;\n\n\tconst stableOnchangeProp = useCallbackRef(onChangeProp);\n\tconst stableEqualityFn = useCallbackRef(equalityFn);\n\n\tconst [unControlledState, setUncontrolledState] = useState(defaultValue as TValue);\n\n\tconst isControlled = valueProp !== undefined;\n\n\tconst currentValue = isControlled ? valueProp : unControlledState;\n\n\tconst setValue: StateSetter<TValue> = useCallback(\n\t\t(newValue) => {\n\t\t\tconst nextValue = isFunction(newValue) ? newValue(currentValue) : newValue;\n\n\t\t\tif (stableEqualityFn(currentValue, nextValue)) return;\n\n\t\t\t// == Always call onChangeProp whether the value is controlled or uncontrolled,\n\t\t\t// == just in case the onChangeProp is used to perform side effects\n\t\t\t// == without necessarily updating the controlled valueProp\n\t\t\tstableOnchangeProp(nextValue);\n\n\t\t\tif (isControlled) return;\n\n\t\t\tsetUncontrolledState(nextValue);\n\t\t},\n\t\t[isControlled, stableOnchangeProp, stableEqualityFn, currentValue]\n\t);\n\n\treturn [currentValue, setValue] as [value: typeof currentValue, setValue: typeof setValue];\n};\n"],"mappings":";;;;;;;;;;;AAeA,MAAa,uBAA8B,YAA+C;CACzF,MAAM,EAAE,MAAM,UAAU;CAExB,MAAM,eAAe,SAAS;CAE9B,MAAM,QAAQ,eAAe,OAAO;AAOpC,QALe,cACR,CAAC,cAAc,MAAM,EAC3B,CAAC,cAAc,MAAM,CACrB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CF,MAAa,wBAAgC,YAAiD;CAC7F,MAAM,EAAE,cAAc,aAAa,OAAO,IAAI,UAAU,cAAc,OAAO,cAAc;CAE3F,MAAM,qBAAqB,eAAe,aAAa;CACvD,MAAM,mBAAmB,eAAe,WAAW;CAEnD,MAAM,CAAC,mBAAmB,wBAAwB,SAAS,aAAuB;CAElF,MAAM,eAAe,cAAc;CAEnC,MAAM,eAAe,eAAe,YAAY;CAEhD,MAAMA,WAAgC,aACpC,aAAa;EACb,MAAM,YAAY,WAAW,SAAS,GAAG,SAAS,aAAa,GAAG;AAElE,MAAI,iBAAiB,cAAc,UAAU,CAAE;AAK/C,qBAAmB,UAAU;AAE7B,MAAI,aAAc;AAElB,uBAAqB,UAAU;IAEhC;EAAC;EAAc;EAAoB;EAAkB;EAAa,CAClE;AAED,QAAO,CAAC,cAAc,SAAS"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as react4 from "react";
|
|
2
2
|
import { AllowedClipboardItems, CopyToClipboardOptions } from "@zayne-labs/toolkit-core";
|
|
3
3
|
|
|
4
4
|
//#region src/hooks/useCopyToClipboard.d.ts
|
|
@@ -7,7 +7,7 @@ declare const useCopyToClipboard: (options?: CopyToClipboardOptions & {
|
|
|
7
7
|
}) => {
|
|
8
8
|
handleCopy: (valueToCopy: AllowedClipboardItems) => void;
|
|
9
9
|
hasCopied: boolean;
|
|
10
|
-
setValue:
|
|
10
|
+
setValue: react4.Dispatch<react4.SetStateAction<AllowedClipboardItems>>;
|
|
11
11
|
value: AllowedClipboardItems;
|
|
12
12
|
};
|
|
13
13
|
//#endregion
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as react6 from "react";
|
|
2
2
|
import { CallbackFn } from "@zayne-labs/toolkit-type-helpers";
|
|
3
3
|
|
|
4
4
|
//#region src/hooks/useDebounce.d.ts
|
|
@@ -11,13 +11,13 @@ declare const useDebouncedFn: <TParams>(callBackFn: CallbackFn<TParams>, delay:
|
|
|
11
11
|
cancelMaxWait(): void;
|
|
12
12
|
};
|
|
13
13
|
declare const useDebouncedState: <TValue>(defaultValue: TValue, delay: number | undefined) => readonly [TValue, {
|
|
14
|
-
(...params:
|
|
15
|
-
(params:
|
|
14
|
+
(...params: react6.SetStateAction<TValue>[]): void;
|
|
15
|
+
(params: react6.SetStateAction<TValue> | react6.SetStateAction<TValue>[], overrideOptions: {
|
|
16
16
|
$delay: number;
|
|
17
17
|
}): void;
|
|
18
18
|
cancel: () => void;
|
|
19
19
|
cancelMaxWait(): void;
|
|
20
|
-
},
|
|
20
|
+
}, react6.Dispatch<react6.SetStateAction<TValue>>];
|
|
21
21
|
//#endregion
|
|
22
22
|
export { useDebouncedFn, useDebouncedState };
|
|
23
23
|
//# sourceMappingURL=useDebounce.d.ts.map
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _zayne_labs_toolkit_core1 from "@zayne-labs/toolkit-core";
|
|
2
2
|
import { LocationInfo, LocationStoreApi, LocationStoreOptions } from "@zayne-labs/toolkit-core";
|
|
3
3
|
import { SelectorFn } from "@zayne-labs/toolkit-type-helpers";
|
|
4
4
|
|
|
5
5
|
//#region src/hooks/useLocationState.d.ts
|
|
6
6
|
type UseLocationResult<TSlice> = [state: TSlice, actions: LocationStoreApi];
|
|
7
|
-
declare const createUseLocationState: (options?: LocationStoreOptions) => Omit<
|
|
8
|
-
push: (url: string |
|
|
9
|
-
state?:
|
|
7
|
+
declare const createUseLocationState: (options?: LocationStoreOptions) => Omit<_zayne_labs_toolkit_core1.StoreApi<_zayne_labs_toolkit_core1.URLInfoObject>, "resetState" | "setState"> & {
|
|
8
|
+
push: (url: string | _zayne_labs_toolkit_core1.PartialURLInfo, options?: {
|
|
9
|
+
state?: _zayne_labs_toolkit_core1.PartialURLInfo["state"];
|
|
10
10
|
}) => void;
|
|
11
11
|
replace: LocationStoreApi["push"];
|
|
12
12
|
triggerPopstateEvent: (nextLocationState?: LocationInfo["state"]) => void;
|
|
13
|
-
} & (<TSlice =
|
|
13
|
+
} & (<TSlice = _zayne_labs_toolkit_core1.URLInfoObject>(selector?: SelectorFn<LocationInfo, TSlice>) => UseLocationResult<TSlice>);
|
|
14
14
|
declare const useLocationState: <TSlice = LocationInfo>(selector?: SelectorFn<LocationInfo, TSlice>, options?: LocationStoreOptions) => UseLocationResult<TSlice>;
|
|
15
15
|
//#endregion
|
|
16
16
|
export { createUseLocationState, useLocationState };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as react2 from "react";
|
|
2
2
|
|
|
3
3
|
//#region src/hooks/useSearch.d.ts
|
|
4
4
|
declare const useSearch: <TData>(initialData: TData[], delay?: number) => {
|
|
5
5
|
data: TData[];
|
|
6
6
|
isLoading: boolean;
|
|
7
7
|
query: string;
|
|
8
|
-
setQuery:
|
|
8
|
+
setQuery: react2.Dispatch<react2.SetStateAction<string>>;
|
|
9
9
|
};
|
|
10
10
|
//#endregion
|
|
11
11
|
export { useSearch };
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _zayne_labs_toolkit_core0 from "@zayne-labs/toolkit-core";
|
|
2
2
|
import { LocationStoreOptions, URLSearchParamsInit } from "@zayne-labs/toolkit-core";
|
|
3
3
|
|
|
4
4
|
//#region src/hooks/useSearchParams.d.ts
|
|
5
5
|
type UseSearchParamsOptions = LocationStoreOptions & {
|
|
6
6
|
action?: "push" | "replace";
|
|
7
7
|
};
|
|
8
|
-
declare const useSearchParams: <TSearchParams extends URLSearchParamsInit>(options?: UseSearchParamsOptions) => [searchParams: URLSearchParams, setSearchParams: (newQueryParams: TSearchParams | ((prev: URLSearchParams) => TSearchParams)) => void, triggerPopstateEvent: (nextLocationState?:
|
|
9
|
-
declare const useSearchParamsObject: <TSearchParams extends Record<string, string>>(options?: UseSearchParamsOptions) => [searchParamsObject: TSearchParams, setSearchParamsObject: (newQueryParams: TSearchParams | ((prev: TSearchParams) => TSearchParams)) => void, triggerPopstateEvent: (nextLocationState?:
|
|
8
|
+
declare const useSearchParams: <TSearchParams extends URLSearchParamsInit>(options?: UseSearchParamsOptions) => [searchParams: URLSearchParams, setSearchParams: (newQueryParams: TSearchParams | ((prev: URLSearchParams) => TSearchParams)) => void, triggerPopstateEvent: (nextLocationState?: _zayne_labs_toolkit_core0.LocationInfo["state"]) => void];
|
|
9
|
+
declare const useSearchParamsObject: <TSearchParams extends Record<string, string>>(options?: UseSearchParamsOptions) => [searchParamsObject: TSearchParams, setSearchParamsObject: (newQueryParams: TSearchParams | ((prev: TSearchParams) => TSearchParams)) => void, triggerPopstateEvent: (nextLocationState?: _zayne_labs_toolkit_core0.LocationInfo["state"]) => void];
|
|
10
10
|
//#endregion
|
|
11
11
|
export { useSearchParams, useSearchParamsObject };
|
|
12
12
|
//# sourceMappingURL=useSearchParams.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zayne-labs/toolkit-react",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.12.
|
|
4
|
+
"version": "0.12.2",
|
|
5
5
|
"description": "A collection of utility functions, types and composables used by my other projects. Nothing too fancy but can be useful.",
|
|
6
6
|
"author": "Ryan Zayne",
|
|
7
7
|
"license": "MIT",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@zayne-labs/toolkit-core": "0.12.
|
|
47
|
-
"@zayne-labs/toolkit-type-helpers": "0.12.
|
|
46
|
+
"@zayne-labs/toolkit-core": "0.12.2",
|
|
47
|
+
"@zayne-labs/toolkit-type-helpers": "0.12.2"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@arethetypeswrong/cli": "^0.18.2",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"@size-limit/esbuild-why": "^11.2.0",
|
|
53
53
|
"@size-limit/preset-small-lib": "^11.2.0",
|
|
54
54
|
"@total-typescript/ts-reset": "^0.6.1",
|
|
55
|
-
"@types/node": "^24.6.
|
|
55
|
+
"@types/node": "^24.6.2",
|
|
56
56
|
"@types/react": "^19.2.0",
|
|
57
57
|
"@zayne-labs/tsconfig": "0.10.4",
|
|
58
58
|
"concurrently": "^9.2.1",
|