@zayne-labs/toolkit-react 0.11.18 → 0.11.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.
- package/dist/esm/hooks/createCustomContext.d.ts +21 -0
- package/dist/esm/hooks/createCustomContext.js +25 -0
- package/dist/esm/hooks/createCustomContext.js.map +1 -0
- package/dist/esm/hooks/effects/useAfterMountEffect.d.ts +7 -0
- package/dist/esm/hooks/effects/useAfterMountEffect.js +19 -0
- package/dist/esm/hooks/effects/useAfterMountEffect.js.map +1 -0
- package/dist/esm/hooks/effects/useAsyncEffect.d.ts +5 -0
- package/dist/esm/hooks/effects/useAsyncEffect.js +20 -0
- package/dist/esm/hooks/effects/useAsyncEffect.js.map +1 -0
- package/dist/esm/hooks/effects/useEffectOnce.d.ts +5 -0
- package/dist/esm/hooks/effects/useEffectOnce.js +17 -0
- package/dist/esm/hooks/effects/useEffectOnce.js.map +1 -0
- package/dist/esm/hooks/effects/useLifeCycle.d.ts +13 -0
- package/dist/esm/hooks/effects/useLifeCycle.js +16 -0
- package/dist/esm/hooks/effects/useLifeCycle.js.map +1 -0
- package/dist/esm/hooks/effects/useMountEffect.d.ts +5 -0
- package/dist/esm/hooks/effects/useMountEffect.js +10 -0
- package/dist/esm/hooks/effects/useMountEffect.js.map +1 -0
- package/dist/esm/hooks/effects/useUnMountEffect.d.ts +7 -0
- package/dist/esm/hooks/effects/useUnMountEffect.js +8 -0
- package/dist/esm/hooks/effects/useUnMountEffect.js.map +1 -0
- package/dist/esm/hooks/index.d.ts +27 -2
- package/dist/esm/hooks/index.js +27 -2
- package/dist/esm/hooks/useAnimateElementRefs.d.ts +19 -0
- package/dist/esm/hooks/useAnimateElementRefs.js +54 -0
- package/dist/esm/hooks/useAnimateElementRefs.js.map +1 -0
- package/dist/esm/hooks/useAnimationInterval.d.ts +15 -0
- package/dist/esm/hooks/useAnimationInterval.js +27 -0
- package/dist/esm/hooks/useAnimationInterval.js.map +1 -0
- package/dist/esm/hooks/useCallbackRef.d.ts +13 -0
- package/dist/esm/hooks/useCallbackRef.js +19 -0
- package/dist/esm/hooks/useCallbackRef.js.map +1 -0
- package/dist/esm/hooks/useClickOutside.d.ts +14 -0
- package/dist/esm/hooks/useClickOutside.js +25 -0
- package/dist/esm/hooks/useClickOutside.js.map +1 -0
- package/dist/esm/hooks/useComposeRefs.d.ts +8 -0
- package/dist/esm/hooks/useComposeRefs.js +11 -0
- package/dist/esm/hooks/useComposeRefs.js.map +1 -0
- package/dist/esm/hooks/useConstant.d.ts +6 -0
- package/dist/esm/hooks/useConstant.js +17 -0
- package/dist/esm/hooks/useConstant.js.map +1 -0
- package/dist/esm/hooks/useControllable.d.ts +55 -0
- package/dist/esm/hooks/useControllable.js +79 -0
- package/dist/esm/hooks/useControllable.js.map +1 -0
- package/dist/esm/hooks/useCopyToClipboard.d.ts +15 -0
- package/dist/esm/hooks/useCopyToClipboard.js +49 -0
- package/dist/esm/hooks/useCopyToClipboard.js.map +1 -0
- package/dist/esm/hooks/useDebounce.d.ts +23 -0
- package/dist/esm/hooks/useDebounce.js +32 -0
- package/dist/esm/hooks/useDebounce.js.map +1 -0
- package/dist/esm/hooks/useDisclosure.d.ts +14 -0
- package/dist/esm/hooks/useDisclosure.js +42 -0
- package/dist/esm/hooks/useDisclosure.js.map +1 -0
- package/dist/esm/hooks/useIsHydrated.d.ts +28 -0
- package/dist/esm/hooks/useIsHydrated.js +39 -0
- package/dist/esm/hooks/useIsHydrated.js.map +1 -0
- package/dist/esm/hooks/useLocationState.d.ts +17 -0
- package/dist/esm/hooks/useLocationState.js +24 -0
- package/dist/esm/hooks/useLocationState.js.map +1 -0
- package/dist/esm/hooks/useScrollObserver.d.ts +11 -0
- package/dist/esm/hooks/useScrollObserver.js +44 -0
- package/dist/esm/hooks/useScrollObserver.js.map +1 -0
- package/dist/esm/hooks/useSearch.d.ts +12 -0
- package/dist/esm/hooks/useSearch.js +40 -0
- package/dist/esm/hooks/useSearch.js.map +1 -0
- package/dist/esm/hooks/useSearchParams.d.ts +18 -0
- package/dist/esm/hooks/useSearchParams.js +30 -0
- package/dist/esm/hooks/useSearchParams.js.map +1 -0
- package/dist/esm/hooks/useShallowCompare.d.ts +8 -0
- package/dist/esm/hooks/useShallowCompare.js +23 -0
- package/dist/esm/hooks/useShallowCompare.js.map +1 -0
- package/dist/esm/hooks/useStorageState.d.ts +20 -0
- package/dist/esm/hooks/useStorageState.js +54 -0
- package/dist/esm/hooks/useStorageState.js.map +1 -0
- package/dist/esm/hooks/useStore.d.ts +8 -0
- package/dist/esm/hooks/useStore.js +13 -0
- package/dist/esm/hooks/useStore.js.map +1 -0
- package/dist/esm/hooks/useThrottle.d.ts +15 -0
- package/dist/esm/hooks/useThrottle.js +27 -0
- package/dist/esm/hooks/useThrottle.js.map +1 -0
- package/dist/esm/hooks/useToggle.d.ts +6 -0
- package/dist/esm/hooks/useToggle.js +18 -0
- package/dist/esm/hooks/useToggle.js.map +1 -0
- package/dist/esm/utils/composeEventHandlers.d.ts +8 -0
- package/dist/esm/utils/composeEventHandlers.js +36 -0
- package/dist/esm/utils/composeEventHandlers.js.map +1 -0
- package/dist/esm/utils/composeRefs.d.ts +17 -0
- package/dist/esm/utils/composeRefs.js +28 -0
- package/dist/esm/utils/composeRefs.js.map +1 -0
- package/dist/esm/utils/getSlot/getSlot.d.ts +60 -0
- package/dist/esm/utils/getSlot/getSlot.js +78 -0
- package/dist/esm/utils/getSlot/getSlot.js.map +1 -0
- package/dist/esm/utils/getSlotMap/getSlotMap.d.ts +86 -0
- package/dist/esm/utils/getSlotMap/getSlotMap.js +87 -0
- package/dist/esm/utils/getSlotMap/getSlotMap.js.map +1 -0
- package/dist/esm/utils/index.d.ts +8 -1
- package/dist/esm/utils/index.js +6 -1
- package/dist/esm/utils/mergeProps.d.ts +20 -0
- package/dist/esm/utils/mergeProps.js +31 -0
- package/dist/esm/utils/mergeProps.js.map +1 -0
- package/dist/esm/utils/mergeTwoProps.d.ts +5 -0
- package/dist/esm/utils/mergeTwoProps.js +42 -0
- package/dist/esm/utils/mergeTwoProps.js.map +1 -0
- package/dist/esm/utils/types/common.d.ts +43 -0
- package/dist/esm/utils/types/polymorphism.d.ts +13 -0
- package/dist/esm/zustand/createReactZustandStore.js +8 -0
- package/dist/esm/zustand/createReactZustandStore.js.map +1 -0
- package/dist/esm/zustand/createZustandContext.d.ts +13 -0
- package/dist/esm/zustand/createZustandContext.js +21 -0
- package/dist/esm/zustand/createZustandContext.js.map +1 -0
- package/dist/esm/zustand/createZustandStoreWithCombine.d.ts +13 -0
- package/dist/esm/zustand/createZustandStoreWithCombine.js +14 -0
- package/dist/esm/zustand/createZustandStoreWithCombine.js.map +1 -0
- package/dist/esm/zustand/createZustandStoreWithSubscribe.d.ts +21 -0
- package/dist/esm/zustand/createZustandStoreWithSubscribe.js +16 -0
- package/dist/esm/zustand/createZustandStoreWithSubscribe.js.map +1 -0
- package/dist/esm/zustand/index.d.ts +4 -38
- package/dist/esm/zustand/index.js +4 -42
- package/package.json +6 -3
- package/dist/esm/hooks-iZivekUZ.js +0 -635
- package/dist/esm/hooks-iZivekUZ.js.map +0 -1
- package/dist/esm/index-DlScRgBk.d.ts +0 -228
- package/dist/esm/index-ouFKofvj.d.ts +0 -293
- package/dist/esm/utils-Bvs8tFDM.js +0 -279
- package/dist/esm/utils-Bvs8tFDM.js.map +0 -1
- package/dist/esm/zustand/index.js.map +0 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as react0 from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/createCustomContext.d.ts
|
|
4
|
+
type CustomContextOptions<TContextValue, TStrict extends boolean> = {
|
|
5
|
+
defaultValue?: TContextValue | null;
|
|
6
|
+
errorMessage?: string;
|
|
7
|
+
extension?: (contextValue: NoInfer<TContextValue> | null) => TContextValue | null;
|
|
8
|
+
hookName?: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
providerName?: string;
|
|
11
|
+
strict?: TStrict;
|
|
12
|
+
};
|
|
13
|
+
type UseCustomContext<TContextValue, TStrict extends boolean> = () => TStrict extends true ? TContextValue : TContextValue | null;
|
|
14
|
+
declare const createCustomContext: <TContextValue = null, TStrict extends boolean = true>(options?: CustomContextOptions<TContextValue, TStrict>) => [Provider: react0.Context<TContextValue>, useCustomContext: UseCustomContext<TContextValue, TStrict>];
|
|
15
|
+
declare class ContextError extends Error {
|
|
16
|
+
name: string;
|
|
17
|
+
}
|
|
18
|
+
declare const getErrorMessage: (hook: string, provider: string) => string;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { ContextError, CustomContextOptions, UseCustomContext, createCustomContext, getErrorMessage };
|
|
21
|
+
//# sourceMappingURL=createCustomContext.d.ts.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createContext, use } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/createCustomContext.ts
|
|
4
|
+
const createCustomContext = (options = {}) => {
|
|
5
|
+
const { defaultValue = null, errorMessage, extension, hookName = "useUnnamedContext", providerName = "UnnamedContextProvider", name = providerName.endsWith("Provider") ? providerName.slice(0, -8) : "UnnamedContext", strict = true } = options;
|
|
6
|
+
const Context = createContext(defaultValue);
|
|
7
|
+
Context.displayName = name;
|
|
8
|
+
const useCustomContext = () => {
|
|
9
|
+
const contextValue = use(Context);
|
|
10
|
+
const extendedContextValue = extension?.(contextValue) ?? contextValue;
|
|
11
|
+
if (strict && extendedContextValue === null) throw new ContextError(errorMessage ?? getErrorMessage(hookName, providerName));
|
|
12
|
+
return extendedContextValue;
|
|
13
|
+
};
|
|
14
|
+
return [Context, useCustomContext];
|
|
15
|
+
};
|
|
16
|
+
var ContextError = class extends Error {
|
|
17
|
+
name = "ContextError";
|
|
18
|
+
};
|
|
19
|
+
const getErrorMessage = (hook, provider) => {
|
|
20
|
+
return `${hook} returned "null". Did you forget to wrap the necessary components within ${provider}?`;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { ContextError, createCustomContext, getErrorMessage };
|
|
25
|
+
//# sourceMappingURL=createCustomContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createCustomContext.js","names":["useCustomContext: UseCustomContext<TContextValue, TStrict>"],"sources":["../../../src/hooks/createCustomContext.ts"],"sourcesContent":["import { createContext, use } from \"react\";\n\nexport type CustomContextOptions<TContextValue, TStrict extends boolean> = {\n\tdefaultValue?: TContextValue | null;\n\terrorMessage?: string;\n\textension?: (contextValue: NoInfer<TContextValue> | null) => TContextValue | null;\n\thookName?: string;\n\tname?: string;\n\tproviderName?: string;\n\tstrict?: TStrict;\n};\n\nexport type UseCustomContext<TContextValue, TStrict extends boolean> = () => TStrict extends true ?\n\tTContextValue\n:\tTContextValue | null;\n\nexport const createCustomContext = <TContextValue = null, TStrict extends boolean = true>(\n\toptions: CustomContextOptions<TContextValue, TStrict> = {}\n) => {\n\tconst {\n\t\tdefaultValue = null,\n\t\terrorMessage,\n\t\textension,\n\t\thookName = \"useUnnamedContext\",\n\t\tproviderName = \"UnnamedContextProvider\",\n\t\tname = providerName.endsWith(\"Provider\") ? providerName.slice(0, -8) : \"UnnamedContext\",\n\t\tstrict = true,\n\t} = options;\n\n\tconst Context = createContext<TContextValue>(defaultValue as TContextValue);\n\n\tContext.displayName = name;\n\n\tconst useCustomContext: UseCustomContext<TContextValue, TStrict> = () => {\n\t\tconst contextValue = use(Context);\n\n\t\tconst extendedContextValue = extension?.(contextValue) ?? contextValue;\n\n\t\tif (strict && extendedContextValue === null) {\n\t\t\tthrow new ContextError(errorMessage ?? getErrorMessage(hookName, providerName));\n\t\t}\n\n\t\treturn extendedContextValue as NonNullable<typeof extendedContextValue>;\n\t};\n\n\treturn [Context, useCustomContext] as [\n\t\tProvider: typeof Context,\n\t\tuseCustomContext: typeof useCustomContext,\n\t];\n};\n\nexport class ContextError extends Error {\n\toverride name = \"ContextError\";\n}\n\nexport const getErrorMessage = (hook: string, provider: string) => {\n\treturn `${hook} returned \"null\". Did you forget to wrap the necessary components within ${provider}?`;\n};\n"],"mappings":";;;AAgBA,MAAa,uBACZ,UAAwD,EAAE,KACtD;CACJ,MAAM,EACL,eAAe,MACf,cACA,WACA,WAAW,qBACX,eAAe,0BACf,OAAO,aAAa,SAAS,WAAW,GAAG,aAAa,MAAM,GAAG,GAAG,GAAG,kBACvE,SAAS,SACN;CAEJ,MAAM,UAAU,cAA6B,aAA8B;AAE3E,SAAQ,cAAc;CAEtB,MAAMA,yBAAmE;EACxE,MAAM,eAAe,IAAI,QAAQ;EAEjC,MAAM,uBAAuB,YAAY,aAAa,IAAI;AAE1D,MAAI,UAAU,yBAAyB,KACtC,OAAM,IAAI,aAAa,gBAAgB,gBAAgB,UAAU,aAAa,CAAC;AAGhF,SAAO;;AAGR,QAAO,CAAC,SAAS,iBAAiB;;AAMnC,IAAa,eAAb,cAAkC,MAAM;CACvC,AAAS,OAAO;;AAGjB,MAAa,mBAAmB,MAAc,aAAqB;AAClE,QAAO,GAAG,KAAK,2EAA2E,SAAS"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useCallbackRef } from "../useCallbackRef.js";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/effects/useAfterMountEffect.ts
|
|
5
|
+
const useAfterMountEffect = (callBackFn, deps) => {
|
|
6
|
+
const isFirstMount = useRef(true);
|
|
7
|
+
const stableCallback = useCallbackRef(callBackFn);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (isFirstMount.current) {
|
|
10
|
+
isFirstMount.current = false;
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
stableCallback();
|
|
14
|
+
}, deps);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { useAfterMountEffect };
|
|
19
|
+
//# sourceMappingURL=useAfterMountEffect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAfterMountEffect.js","names":["useAfterMountEffect: typeof useEffect"],"sources":["../../../../src/hooks/effects/useAfterMountEffect.ts"],"sourcesContent":["import { useEffect, useRef } from \"react\";\nimport { useCallbackRef } from \"../useCallbackRef\";\n\nconst useAfterMountEffect: typeof useEffect = (callBackFn, deps) => {\n\tconst isFirstMount = useRef(true);\n\tconst stableCallback = useCallbackRef(callBackFn);\n\n\tuseEffect(() => {\n\t\tif (isFirstMount.current) {\n\t\t\tisFirstMount.current = false;\n\t\t\treturn;\n\t\t}\n\n\t\tstableCallback();\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps -- stableCallback is stable\n\t}, deps);\n};\nexport { useAfterMountEffect };\n"],"mappings":";;;;AAGA,MAAMA,uBAAyC,YAAY,SAAS;CACnE,MAAM,eAAe,OAAO,KAAK;CACjC,MAAM,iBAAiB,eAAe,WAAW;AAEjD,iBAAgB;AACf,MAAI,aAAa,SAAS;AACzB,gBAAa,UAAU;AACvB;;AAGD,kBAAgB;IAEd,KAAK"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useCallbackRef } from "../useCallbackRef.js";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/effects/useAsyncEffect.ts
|
|
5
|
+
function useAsyncEffect(effect, deps) {
|
|
6
|
+
const stableEffectCallback = useCallbackRef(effect);
|
|
7
|
+
const [destroy, setDestroy] = useState();
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
const e = stableEffectCallback();
|
|
10
|
+
async function execute() {
|
|
11
|
+
setDestroy(await e);
|
|
12
|
+
}
|
|
13
|
+
execute();
|
|
14
|
+
return () => void destroy?.();
|
|
15
|
+
}, deps);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
export { useAsyncEffect };
|
|
20
|
+
//# sourceMappingURL=useAsyncEffect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAsyncEffect.js","names":[],"sources":["../../../../src/hooks/effects/useAsyncEffect.ts"],"sourcesContent":["import { useEffect, useState } from \"react\";\nimport { useCallbackRef } from \"../useCallbackRef\";\n\nexport function useAsyncEffect(\n\teffect: () => Promise<ReturnType<React.EffectCallback>>,\n\tdeps?: React.DependencyList\n) {\n\tconst stableEffectCallback = useCallbackRef(effect);\n\tconst [destroy, setDestroy] = useState<ReturnType<React.EffectCallback>>();\n\n\tuseEffect(() => {\n\t\tconst e = stableEffectCallback();\n\n\t\tasync function execute() {\n\t\t\tsetDestroy(await e);\n\t\t}\n\n\t\tvoid execute();\n\n\t\treturn () => void destroy?.();\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps -- stableEffectCallback is stable\n\t}, deps);\n}\n"],"mappings":";;;;AAGA,SAAgB,eACf,QACA,MACC;CACD,MAAM,uBAAuB,eAAe,OAAO;CACnD,MAAM,CAAC,SAAS,cAAc,UAA4C;AAE1E,iBAAgB;EACf,MAAM,IAAI,sBAAsB;EAEhC,eAAe,UAAU;AACxB,cAAW,MAAM,EAAE;;AAGpB,EAAK,SAAS;AAEd,eAAa,KAAK,WAAW;IAE3B,KAAK"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useCallbackRef } from "../useCallbackRef.js";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/effects/useEffectOnce.ts
|
|
5
|
+
const useEffectOnce = (callBackFn) => {
|
|
6
|
+
const stableCallback = useCallbackRef(callBackFn);
|
|
7
|
+
const effectGuardRef = useRef(false);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (effectGuardRef.current) return;
|
|
10
|
+
effectGuardRef.current = true;
|
|
11
|
+
return stableCallback();
|
|
12
|
+
}, []);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { useEffectOnce };
|
|
17
|
+
//# sourceMappingURL=useEffectOnce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useEffectOnce.js","names":[],"sources":["../../../../src/hooks/effects/useEffectOnce.ts"],"sourcesContent":["import { useEffect, useRef } from \"react\";\nimport { useCallbackRef } from \"../useCallbackRef\";\n\nconst useEffectOnce = (callBackFn: React.EffectCallback) => {\n\tconst stableCallback = useCallbackRef(callBackFn);\n\n\tconst effectGuardRef = useRef(false);\n\n\t// == savedCallback is always stable so no worries about re-execution of this effect\n\tuseEffect(() => {\n\t\tif (effectGuardRef.current) return;\n\n\t\teffectGuardRef.current = true;\n\n\t\treturn stableCallback();\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps -- stableCallback is stable\n\t}, []);\n};\n\nexport { useEffectOnce };\n"],"mappings":";;;;AAGA,MAAM,iBAAiB,eAAqC;CAC3D,MAAM,iBAAiB,eAAe,WAAW;CAEjD,MAAM,iBAAiB,OAAO,MAAM;AAGpC,iBAAgB;AACf,MAAI,eAAe,QAAS;AAE5B,iBAAe,UAAU;AAEzB,SAAO,gBAAgB;IAErB,EAAE,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/hooks/effects/useLifeCycle.d.ts
|
|
2
|
+
type Destructor = ReturnType<React.EffectCallback>;
|
|
3
|
+
type LifeCycleOptions = {
|
|
4
|
+
onMount?: () => void;
|
|
5
|
+
onUnmount?: Destructor;
|
|
6
|
+
};
|
|
7
|
+
declare const useLifeCycle: ({
|
|
8
|
+
onMount,
|
|
9
|
+
onUnmount
|
|
10
|
+
}: LifeCycleOptions) => void;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { Destructor, useLifeCycle };
|
|
13
|
+
//# sourceMappingURL=useLifeCycle.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useCallbackRef } from "../useCallbackRef.js";
|
|
2
|
+
import { useEffect } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/effects/useLifeCycle.ts
|
|
5
|
+
const useLifeCycle = ({ onMount, onUnmount }) => {
|
|
6
|
+
const stableOnMount = useCallbackRef(onMount);
|
|
7
|
+
const stableOnUnmount = useCallbackRef(onUnmount);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
stableOnMount();
|
|
10
|
+
return stableOnUnmount;
|
|
11
|
+
}, []);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { useLifeCycle };
|
|
16
|
+
//# sourceMappingURL=useLifeCycle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLifeCycle.js","names":[],"sources":["../../../../src/hooks/effects/useLifeCycle.ts"],"sourcesContent":["import { useEffect } from \"react\";\nimport { useCallbackRef } from \"../useCallbackRef\";\n\nexport type Destructor = ReturnType<React.EffectCallback>;\n\ntype LifeCycleOptions = {\n\tonMount?: () => void;\n\tonUnmount?: Destructor;\n};\n\nconst useLifeCycle = ({ onMount, onUnmount }: LifeCycleOptions) => {\n\tconst stableOnMount = useCallbackRef(onMount);\n\tconst stableOnUnmount = useCallbackRef(onUnmount);\n\n\tuseEffect(() => {\n\t\tstableOnMount();\n\n\t\treturn stableOnUnmount;\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps -- stableOnMount and stableOnUnmount are stable\n\t}, []);\n};\n\nexport { useLifeCycle };\n"],"mappings":";;;;AAUA,MAAM,gBAAgB,EAAE,SAAS,gBAAkC;CAClE,MAAM,gBAAgB,eAAe,QAAQ;CAC7C,MAAM,kBAAkB,eAAe,UAAU;AAEjD,iBAAgB;AACf,iBAAe;AAEf,SAAO;IAEL,EAAE,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useLifeCycle } from "./useLifeCycle.js";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/effects/useMountEffect.ts
|
|
4
|
+
const useMountEffect = (callBackFn) => {
|
|
5
|
+
useLifeCycle({ onMount: callBackFn });
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
export { useMountEffect };
|
|
10
|
+
//# sourceMappingURL=useMountEffect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMountEffect.js","names":[],"sources":["../../../../src/hooks/effects/useMountEffect.ts"],"sourcesContent":["import { useLifeCycle } from \"./useLifeCycle\";\n\nconst useMountEffect = (callBackFn: () => void) => {\n\tuseLifeCycle({ onMount: callBackFn });\n};\n\nexport { useMountEffect };\n"],"mappings":";;;AAEA,MAAM,kBAAkB,eAA2B;AAClD,cAAa,EAAE,SAAS,YAAY,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { useLifeCycle } from "./useLifeCycle.js";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/effects/useUnMountEffect.ts
|
|
4
|
+
const useUnmountEffect = (cleanUpFn) => useLifeCycle({ onUnmount: cleanUpFn });
|
|
5
|
+
|
|
6
|
+
//#endregion
|
|
7
|
+
export { useUnmountEffect };
|
|
8
|
+
//# sourceMappingURL=useUnMountEffect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useUnMountEffect.js","names":[],"sources":["../../../../src/hooks/effects/useUnMountEffect.ts"],"sourcesContent":["import { type Destructor, useLifeCycle } from \"./useLifeCycle\";\n\nconst useUnmountEffect = (cleanUpFn: Destructor) => useLifeCycle({ onUnmount: cleanUpFn });\n\nexport { useUnmountEffect };\n"],"mappings":";;;AAEA,MAAM,oBAAoB,cAA0B,aAAa,EAAE,WAAW,WAAW,CAAC"}
|
|
@@ -1,3 +1,28 @@
|
|
|
1
|
-
import { ContextError, CustomContextOptions, UseCustomContext, createCustomContext,
|
|
2
|
-
import "
|
|
1
|
+
import { ContextError, CustomContextOptions, UseCustomContext, createCustomContext, getErrorMessage } from "./createCustomContext.js";
|
|
2
|
+
import { useAfterMountEffect } from "./effects/useAfterMountEffect.js";
|
|
3
|
+
import { useAsyncEffect } from "./effects/useAsyncEffect.js";
|
|
4
|
+
import { useEffectOnce } from "./effects/useEffectOnce.js";
|
|
5
|
+
import { useLifeCycle } from "./effects/useLifeCycle.js";
|
|
6
|
+
import { useMountEffect } from "./effects/useMountEffect.js";
|
|
7
|
+
import { useUnmountEffect } from "./effects/useUnMountEffect.js";
|
|
8
|
+
import { useAnimateElementRefs } from "./useAnimateElementRefs.js";
|
|
9
|
+
import { useAnimationInterval } from "./useAnimationInterval.js";
|
|
10
|
+
import { useCallbackRef } from "./useCallbackRef.js";
|
|
11
|
+
import { useClickOutside } from "./useClickOutside.js";
|
|
12
|
+
import { useComposeRefs } from "./useComposeRefs.js";
|
|
13
|
+
import { useConstant, useLazyRef } from "./useConstant.js";
|
|
14
|
+
import { useControllableProp, useControllableState } from "./useControllable.js";
|
|
15
|
+
import { useCopyToClipboard } from "./useCopyToClipboard.js";
|
|
16
|
+
import { useDebouncedFn, useDebouncedState } from "./useDebounce.js";
|
|
17
|
+
import { useDisclosure } from "./useDisclosure.js";
|
|
18
|
+
import { useIsHydrated } from "./useIsHydrated.js";
|
|
19
|
+
import { createUseLocationState, useLocationState } from "./useLocationState.js";
|
|
20
|
+
import { useScrollObserver } from "./useScrollObserver.js";
|
|
21
|
+
import { useSearch } from "./useSearch.js";
|
|
22
|
+
import { useSearchParams, useSearchParamsObject } from "./useSearchParams.js";
|
|
23
|
+
import { useShallowCompSelector, useShallowCompValue } from "./useShallowCompare.js";
|
|
24
|
+
import { createUseStorageState, useStorageState } from "./useStorageState.js";
|
|
25
|
+
import { useStore } from "./useStore.js";
|
|
26
|
+
import { useThrottleByFrame, useThrottleBySetTimeout, useThrottleByTimer } from "./useThrottle.js";
|
|
27
|
+
import { useToggle } from "./useToggle.js";
|
|
3
28
|
export { ContextError, CustomContextOptions, UseCustomContext, createCustomContext, createUseLocationState, createUseStorageState, getErrorMessage, useAfterMountEffect, useAnimateElementRefs, useAnimationInterval, useAsyncEffect, useCallbackRef, useClickOutside, useComposeRefs, useConstant, useControllableProp, useControllableState, useCopyToClipboard, useDebouncedFn, useDebouncedState, useDisclosure, useEffectOnce, useIsHydrated, useLazyRef, useLifeCycle, useLocationState, useMountEffect, useScrollObserver, useSearch, useSearchParams, useSearchParamsObject, useShallowCompSelector, useShallowCompValue, useStorageState, useStore, useThrottleByFrame, useThrottleBySetTimeout, useThrottleByTimer, useToggle, useUnmountEffect };
|
package/dist/esm/hooks/index.js
CHANGED
|
@@ -1,4 +1,29 @@
|
|
|
1
|
-
import { ContextError, createCustomContext,
|
|
2
|
-
import "
|
|
1
|
+
import { ContextError, createCustomContext, getErrorMessage } from "./createCustomContext.js";
|
|
2
|
+
import { useCallbackRef } from "./useCallbackRef.js";
|
|
3
|
+
import { useAfterMountEffect } from "./effects/useAfterMountEffect.js";
|
|
4
|
+
import { useAsyncEffect } from "./effects/useAsyncEffect.js";
|
|
5
|
+
import { useEffectOnce } from "./effects/useEffectOnce.js";
|
|
6
|
+
import { useLifeCycle } from "./effects/useLifeCycle.js";
|
|
7
|
+
import { useMountEffect } from "./effects/useMountEffect.js";
|
|
8
|
+
import { useUnmountEffect } from "./effects/useUnMountEffect.js";
|
|
9
|
+
import { useAnimateElementRefs } from "./useAnimateElementRefs.js";
|
|
10
|
+
import { useAnimationInterval } from "./useAnimationInterval.js";
|
|
11
|
+
import { useClickOutside } from "./useClickOutside.js";
|
|
12
|
+
import { useComposeRefs } from "./useComposeRefs.js";
|
|
13
|
+
import { useConstant, useLazyRef } from "./useConstant.js";
|
|
14
|
+
import { useControllableProp, useControllableState } from "./useControllable.js";
|
|
15
|
+
import { useToggle } from "./useToggle.js";
|
|
16
|
+
import { useCopyToClipboard } from "./useCopyToClipboard.js";
|
|
17
|
+
import { useDebouncedFn, useDebouncedState } from "./useDebounce.js";
|
|
18
|
+
import { useDisclosure } from "./useDisclosure.js";
|
|
19
|
+
import { useIsHydrated } from "./useIsHydrated.js";
|
|
20
|
+
import { useStore } from "./useStore.js";
|
|
21
|
+
import { createUseLocationState, useLocationState } from "./useLocationState.js";
|
|
22
|
+
import { useScrollObserver } from "./useScrollObserver.js";
|
|
23
|
+
import { useSearch } from "./useSearch.js";
|
|
24
|
+
import { useSearchParams, useSearchParamsObject } from "./useSearchParams.js";
|
|
25
|
+
import { useShallowCompSelector, useShallowCompValue } from "./useShallowCompare.js";
|
|
26
|
+
import { createUseStorageState, useStorageState } from "./useStorageState.js";
|
|
27
|
+
import { useThrottleByFrame, useThrottleBySetTimeout, useThrottleByTimer } from "./useThrottle.js";
|
|
3
28
|
|
|
4
29
|
export { ContextError, createCustomContext, createUseLocationState, createUseStorageState, getErrorMessage, useAfterMountEffect, useAnimateElementRefs, useAnimationInterval, useAsyncEffect, useCallbackRef, useClickOutside, useComposeRefs, useConstant, useControllableProp, useControllableState, useCopyToClipboard, useDebouncedFn, useDebouncedState, useDisclosure, useEffectOnce, useIsHydrated, useLazyRef, useLifeCycle, useLocationState, useMountEffect, useScrollObserver, useSearch, useSearchParams, useSearchParamsObject, useShallowCompSelector, useShallowCompValue, useStorageState, useStore, useThrottleByFrame, useThrottleBySetTimeout, useThrottleByTimer, useToggle, useUnmountEffect };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { NonEmptyArray } from "@zayne-labs/toolkit-type-helpers";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useAnimateElementRefs.d.ts
|
|
4
|
+
type ElementsInfoArray<TTargetElement extends string> = NonEmptyArray<{
|
|
5
|
+
animationClass: string;
|
|
6
|
+
targetElement: TTargetElement;
|
|
7
|
+
}>;
|
|
8
|
+
/**
|
|
9
|
+
* This is a custom React hook that adds and removes animation classes to specified HTML elements.
|
|
10
|
+
* @param elementsInfoArray - An array of objects that contain information about the animation class and the target HTML element.
|
|
11
|
+
* @returns - An object containing the refs of the animated elements and a function to handle the initiation and removal animation.
|
|
12
|
+
*/
|
|
13
|
+
declare const useAnimateElementRefs: <TTargetElement extends string>(elementsInfoArray: ElementsInfoArray<TTargetElement>) => {
|
|
14
|
+
animatedElements: Record<TTargetElement, HTMLElement | null>;
|
|
15
|
+
handleElementsAnimation: () => void;
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { useAnimateElementRefs };
|
|
19
|
+
//# sourceMappingURL=useAnimateElementRefs.d.ts.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useCallbackRef } from "./useCallbackRef.js";
|
|
2
|
+
import { useCallback, useRef } from "react";
|
|
3
|
+
import { on } from "@zayne-labs/toolkit-core";
|
|
4
|
+
import { isArray } from "@zayne-labs/toolkit-type-helpers";
|
|
5
|
+
|
|
6
|
+
//#region src/hooks/useAnimateElementRefs.ts
|
|
7
|
+
const removeClass = (target, className) => () => target.classList.remove(className);
|
|
8
|
+
/**
|
|
9
|
+
* This is a custom React hook that adds and removes animation classes to specified HTML elements.
|
|
10
|
+
* @param elementsInfoArray - An array of objects that contain information about the animation class and the target HTML element.
|
|
11
|
+
* @returns - An object containing the refs of the animated elements and a function to handle the initiation and removal animation.
|
|
12
|
+
*/
|
|
13
|
+
const useAnimateElementRefs = (elementsInfoArray) => {
|
|
14
|
+
const elementsRef = useRef({});
|
|
15
|
+
const addAnimationClasses = useCallbackRef(() => {
|
|
16
|
+
if (!isArray(elementsInfoArray)) {
|
|
17
|
+
console.error("elementsInfo is not an Array");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
for (const { animationClass, targetElement } of elementsInfoArray) {
|
|
21
|
+
if (!elementsRef.current[targetElement]) {
|
|
22
|
+
console.error("ElementError", `"${targetElement}" element does not exist`);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
elementsRef.current[targetElement].classList.add(animationClass);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
const removeAnimationClasses = useCallbackRef(() => {
|
|
29
|
+
if (!isArray(elementsInfoArray)) {
|
|
30
|
+
console.error("elementsInfo is not an Array");
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
for (const { animationClass, targetElement } of elementsInfoArray) {
|
|
34
|
+
if (!elementsRef.current[targetElement]) {
|
|
35
|
+
console.error("ElementError", `"${targetElement}" element does not exist`);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
on("transitionend", elementsRef.current[targetElement], removeClass(elementsRef.current[targetElement], animationClass));
|
|
39
|
+
on("animationend", elementsRef.current[targetElement], removeClass(elementsRef.current[targetElement], animationClass));
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
const handleElementsAnimation = useCallback(() => {
|
|
43
|
+
addAnimationClasses();
|
|
44
|
+
removeAnimationClasses();
|
|
45
|
+
}, [addAnimationClasses, removeAnimationClasses]);
|
|
46
|
+
return {
|
|
47
|
+
animatedElements: elementsRef.current,
|
|
48
|
+
handleElementsAnimation
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
export { useAnimateElementRefs };
|
|
54
|
+
//# sourceMappingURL=useAnimateElementRefs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAnimateElementRefs.js","names":[],"sources":["../../../src/hooks/useAnimateElementRefs.ts"],"sourcesContent":["import { on } from \"@zayne-labs/toolkit-core\";\nimport { type NonEmptyArray, isArray } from \"@zayne-labs/toolkit-type-helpers\";\nimport { useCallback, useRef } from \"react\";\nimport { useCallbackRef } from \"./useCallbackRef\";\n\ntype ElementsInfoArray<TTargetElement extends string> = NonEmptyArray<{\n\tanimationClass: string;\n\ttargetElement: TTargetElement;\n}>;\n\nconst removeClass = (target: HTMLElement, className: string) => () => target.classList.remove(className);\n\n/**\n * This is a custom React hook that adds and removes animation classes to specified HTML elements.\n * @param elementsInfoArray - An array of objects that contain information about the animation class and the target HTML element.\n * @returns - An object containing the refs of the animated elements and a function to handle the initiation and removal animation.\n */\n\nconst useAnimateElementRefs = <TTargetElement extends string>(\n\telementsInfoArray: ElementsInfoArray<TTargetElement>\n) => {\n\tconst elementsRef = useRef<Record<TTargetElement, HTMLElement | null>>({} as never);\n\n\tconst addAnimationClasses = useCallbackRef(() => {\n\t\tif (!isArray(elementsInfoArray)) {\n\t\t\tconsole.error(\"elementsInfo is not an Array\");\n\t\t\treturn;\n\t\t}\n\n\t\tfor (const { animationClass, targetElement } of elementsInfoArray) {\n\t\t\tif (!elementsRef.current[targetElement]) {\n\t\t\t\tconsole.error(\"ElementError\", `\"${targetElement}\" element does not exist`);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\telementsRef.current[targetElement].classList.add(animationClass);\n\t\t}\n\t});\n\n\tconst removeAnimationClasses = useCallbackRef(() => {\n\t\tif (!isArray(elementsInfoArray)) {\n\t\t\tconsole.error(\"elementsInfo is not an Array\");\n\t\t\treturn;\n\t\t}\n\n\t\tfor (const { animationClass, targetElement } of elementsInfoArray) {\n\t\t\tif (!elementsRef.current[targetElement]) {\n\t\t\t\tconsole.error(\"ElementError\", `\"${targetElement}\" element does not exist`);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\ton(\n\t\t\t\t\"transitionend\",\n\t\t\t\telementsRef.current[targetElement],\n\t\t\t\tremoveClass(elementsRef.current[targetElement], animationClass)\n\t\t\t);\n\n\t\t\ton(\n\t\t\t\t\"animationend\",\n\t\t\t\telementsRef.current[targetElement],\n\t\t\t\tremoveClass(elementsRef.current[targetElement], animationClass)\n\t\t\t);\n\t\t}\n\t});\n\n\t// Add animation classes to elements and remove them after the animation ends\n\tconst handleElementsAnimation = useCallback(() => {\n\t\taddAnimationClasses();\n\n\t\tremoveAnimationClasses();\n\t}, [addAnimationClasses, removeAnimationClasses]);\n\n\treturn { animatedElements: elementsRef.current, handleElementsAnimation };\n};\n\nexport { useAnimateElementRefs };\n"],"mappings":";;;;;;AAUA,MAAM,eAAe,QAAqB,oBAA4B,OAAO,UAAU,OAAO,UAAU;;;;;;AAQxG,MAAM,yBACL,sBACI;CACJ,MAAM,cAAc,OAAmD,EAAE,CAAU;CAEnF,MAAM,sBAAsB,qBAAqB;AAChD,MAAI,CAAC,QAAQ,kBAAkB,EAAE;AAChC,WAAQ,MAAM,+BAA+B;AAC7C;;AAGD,OAAK,MAAM,EAAE,gBAAgB,mBAAmB,mBAAmB;AAClE,OAAI,CAAC,YAAY,QAAQ,gBAAgB;AACxC,YAAQ,MAAM,gBAAgB,IAAI,cAAc,0BAA0B;AAC1E;;AAGD,eAAY,QAAQ,eAAe,UAAU,IAAI,eAAe;;GAEhE;CAEF,MAAM,yBAAyB,qBAAqB;AACnD,MAAI,CAAC,QAAQ,kBAAkB,EAAE;AAChC,WAAQ,MAAM,+BAA+B;AAC7C;;AAGD,OAAK,MAAM,EAAE,gBAAgB,mBAAmB,mBAAmB;AAClE,OAAI,CAAC,YAAY,QAAQ,gBAAgB;AACxC,YAAQ,MAAM,gBAAgB,IAAI,cAAc,0BAA0B;AAC1E;;AAGD,MACC,iBACA,YAAY,QAAQ,gBACpB,YAAY,YAAY,QAAQ,gBAAgB,eAAe,CAC/D;AAED,MACC,gBACA,YAAY,QAAQ,gBACpB,YAAY,YAAY,QAAQ,gBAAgB,eAAe,CAC/D;;GAED;CAGF,MAAM,0BAA0B,kBAAkB;AACjD,uBAAqB;AAErB,0BAAwB;IACtB,CAAC,qBAAqB,uBAAuB,CAAC;AAEjD,QAAO;EAAE,kBAAkB,YAAY;EAAS;EAAyB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AnimationIntervalOptions } from "@zayne-labs/toolkit-core";
|
|
2
|
+
import { Prettify } from "@zayne-labs/toolkit-type-helpers";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/useAnimationInterval.d.ts
|
|
5
|
+
type AnimationOptions = Prettify<AnimationIntervalOptions & {
|
|
6
|
+
intervalDuration: number | null;
|
|
7
|
+
onAnimation: () => void;
|
|
8
|
+
}>;
|
|
9
|
+
declare const useAnimationInterval: (options: AnimationOptions) => {
|
|
10
|
+
start: () => void;
|
|
11
|
+
stop: () => void;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { useAnimationInterval };
|
|
15
|
+
//# sourceMappingURL=useAnimationInterval.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useCallbackRef } from "./useCallbackRef.js";
|
|
2
|
+
import { useEffect, useMemo } from "react";
|
|
3
|
+
import { setAnimationInterval } from "@zayne-labs/toolkit-core";
|
|
4
|
+
|
|
5
|
+
//#region src/hooks/useAnimationInterval.ts
|
|
6
|
+
const useAnimationInterval = (options) => {
|
|
7
|
+
const { intervalDuration, onAnimation, once } = options;
|
|
8
|
+
const latestCallback = useCallbackRef(onAnimation);
|
|
9
|
+
const { start, stop } = useMemo(() => setAnimationInterval(latestCallback, intervalDuration, { once }), [
|
|
10
|
+
intervalDuration,
|
|
11
|
+
latestCallback,
|
|
12
|
+
once
|
|
13
|
+
]);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (intervalDuration === null) return;
|
|
16
|
+
start();
|
|
17
|
+
return stop;
|
|
18
|
+
}, [intervalDuration]);
|
|
19
|
+
return {
|
|
20
|
+
start,
|
|
21
|
+
stop
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { useAnimationInterval };
|
|
27
|
+
//# sourceMappingURL=useAnimationInterval.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAnimationInterval.js","names":[],"sources":["../../../src/hooks/useAnimationInterval.ts"],"sourcesContent":["import { type AnimationIntervalOptions, setAnimationInterval } from \"@zayne-labs/toolkit-core\";\nimport type { Prettify } from \"@zayne-labs/toolkit-type-helpers\";\nimport { useEffect, useMemo } from \"react\";\nimport { useCallbackRef } from \"./useCallbackRef\";\n\ntype AnimationOptions = Prettify<\n\tAnimationIntervalOptions & {\n\t\tintervalDuration: number | null;\n\t\tonAnimation: () => void;\n\t}\n>;\n\nconst useAnimationInterval = (options: AnimationOptions) => {\n\tconst { intervalDuration, onAnimation, once } = options;\n\n\tconst latestCallback = useCallbackRef(onAnimation);\n\n\tconst { start, stop } = useMemo(\n\t\t() => setAnimationInterval(latestCallback, intervalDuration, { once }),\n\t\t[intervalDuration, latestCallback, once]\n\t);\n\n\tuseEffect(() => {\n\t\tif (intervalDuration === null) return;\n\n\t\tstart();\n\n\t\treturn stop;\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps -- start and stop are stable\n\t}, [intervalDuration]);\n\n\treturn { start, stop };\n};\n\nexport { useAnimationInterval };\n"],"mappings":";;;;;AAYA,MAAM,wBAAwB,YAA8B;CAC3D,MAAM,EAAE,kBAAkB,aAAa,SAAS;CAEhD,MAAM,iBAAiB,eAAe,YAAY;CAElD,MAAM,EAAE,OAAO,SAAS,cACjB,qBAAqB,gBAAgB,kBAAkB,EAAE,MAAM,CAAC,EACtE;EAAC;EAAkB;EAAgB;EAAK,CACxC;AAED,iBAAgB;AACf,MAAI,qBAAqB,KAAM;AAE/B,SAAO;AAEP,SAAO;IAEL,CAAC,iBAAiB,CAAC;AAEtB,QAAO;EAAE;EAAO;EAAM"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AnyFunction } from "@zayne-labs/toolkit-type-helpers";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useCallbackRef.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns a stable function that always points to the latest version of the callback function.
|
|
7
|
+
* @param callbackFn - The function to reference
|
|
8
|
+
* @returns a stable function that always points to the latest version of the callback function
|
|
9
|
+
*/
|
|
10
|
+
declare const useCallbackRef: <TCallback = AnyFunction>(callbackFn: TCallback | undefined) => TCallback;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { useCallbackRef };
|
|
13
|
+
//# sourceMappingURL=useCallbackRef.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useCallback, useInsertionEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useCallbackRef.ts
|
|
4
|
+
/**
|
|
5
|
+
* Returns a stable function that always points to the latest version of the callback function.
|
|
6
|
+
* @param callbackFn - The function to reference
|
|
7
|
+
* @returns a stable function that always points to the latest version of the callback function
|
|
8
|
+
*/
|
|
9
|
+
const useCallbackRef = (callbackFn) => {
|
|
10
|
+
const callbackRef = useRef(callbackFn);
|
|
11
|
+
useInsertionEffect(() => {
|
|
12
|
+
callbackRef.current = callbackFn;
|
|
13
|
+
}, [callbackFn]);
|
|
14
|
+
return useCallback((...params) => callbackRef.current?.(...params), []);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { useCallbackRef };
|
|
19
|
+
//# sourceMappingURL=useCallbackRef.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCallbackRef.js","names":[],"sources":["../../../src/hooks/useCallbackRef.ts"],"sourcesContent":["import type { AnyFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { useCallback, useInsertionEffect, useRef } from \"react\";\n\n/**\n * Returns a stable function that always points to the latest version of the callback function.\n * @param callbackFn - The function to reference\n * @returns a stable function that always points to the latest version of the callback function\n */\n\nconst useCallbackRef = <TCallback = AnyFunction>(callbackFn: TCallback | undefined) => {\n\tconst callbackRef = useRef(callbackFn);\n\n\tuseInsertionEffect(() => {\n\t\t// == Doing this instead updating it during render cuz according to Dan Abramov, render should be pure\n\t\tcallbackRef.current = callbackFn;\n\t}, [callbackFn]);\n\n\tconst savedCallback = useCallback(\n\t\t// eslint-disable-next-line ts-eslint/no-unnecessary-condition -- callbackRef.current can be null in some cases\n\t\t(...params: unknown[]) => (callbackRef.current as AnyFunction)?.(...params) as unknown,\n\t\t[]\n\t);\n\n\treturn savedCallback as TCallback;\n};\n\nexport { useCallbackRef };\n"],"mappings":";;;;;;;;AASA,MAAM,kBAA2C,eAAsC;CACtF,MAAM,cAAc,OAAO,WAAW;AAEtC,0BAAyB;AAExB,cAAY,UAAU;IACpB,CAAC,WAAW,CAAC;AAQhB,QANsB,aAEpB,GAAG,WAAuB,YAAY,UAA0B,GAAG,OAAO,EAC3E,EAAE,CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as react0 from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useClickOutside.d.ts
|
|
4
|
+
type UseClickOutsideOptions<TElement extends HTMLElement> = {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
onClick: (event: MouseEvent | TouchEvent) => void;
|
|
7
|
+
ref?: Array<React.RefObject<TElement>> | React.RefObject<TElement>;
|
|
8
|
+
};
|
|
9
|
+
declare const useClickOutside: <TElement extends HTMLElement>(options: UseClickOutsideOptions<TElement>) => {
|
|
10
|
+
ref: react0.RefObject<TElement | null>;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { useClickOutside };
|
|
14
|
+
//# sourceMappingURL=useClickOutside.d.ts.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useCallbackRef } from "./useCallbackRef.js";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { onClickOutside, toArray } from "@zayne-labs/toolkit-core";
|
|
4
|
+
|
|
5
|
+
//#region src/hooks/useClickOutside.ts
|
|
6
|
+
const useClickOutside = (options) => {
|
|
7
|
+
const innerRef = useRef(null);
|
|
8
|
+
const { enabled = true, onClick, ref: refOrRefArray = innerRef } = options;
|
|
9
|
+
const savedOnClick = useCallbackRef(onClick);
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if (!enabled) return;
|
|
12
|
+
const elementArray = toArray(refOrRefArray).map((ref) => ref.current);
|
|
13
|
+
const cleanup = onClickOutside(elementArray, savedOnClick);
|
|
14
|
+
return () => cleanup();
|
|
15
|
+
}, [
|
|
16
|
+
enabled,
|
|
17
|
+
refOrRefArray,
|
|
18
|
+
savedOnClick
|
|
19
|
+
]);
|
|
20
|
+
return { ref: innerRef };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { useClickOutside };
|
|
25
|
+
//# sourceMappingURL=useClickOutside.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useClickOutside.js","names":[],"sources":["../../../src/hooks/useClickOutside.ts"],"sourcesContent":["import { onClickOutside, toArray } from \"@zayne-labs/toolkit-core\";\nimport { useEffect, useRef } from \"react\";\nimport { useCallbackRef } from \"./useCallbackRef\";\n\ntype UseClickOutsideOptions<TElement extends HTMLElement> = {\n\tenabled?: boolean;\n\tonClick: (event: MouseEvent | TouchEvent) => void;\n\tref?: Array<React.RefObject<TElement>> | React.RefObject<TElement>;\n};\n\nconst useClickOutside = <TElement extends HTMLElement>(options: UseClickOutsideOptions<TElement>) => {\n\tconst innerRef = useRef<TElement>(null);\n\n\tconst { enabled = true, onClick, ref: refOrRefArray = innerRef } = options;\n\n\tconst savedOnClick = useCallbackRef(onClick);\n\n\tuseEffect(() => {\n\t\tif (!enabled) return;\n\n\t\tconst elementArray = toArray(refOrRefArray).map((ref) => ref.current);\n\n\t\tconst cleanup = onClickOutside(elementArray, savedOnClick);\n\n\t\treturn () => cleanup();\n\t}, [enabled, refOrRefArray, savedOnClick]);\n\n\treturn {\n\t\tref: innerRef,\n\t};\n};\n\nexport { useClickOutside };\n"],"mappings":";;;;;AAUA,MAAM,mBAAiD,YAA8C;CACpG,MAAM,WAAW,OAAiB,KAAK;CAEvC,MAAM,EAAE,UAAU,MAAM,SAAS,KAAK,gBAAgB,aAAa;CAEnE,MAAM,eAAe,eAAe,QAAQ;AAE5C,iBAAgB;AACf,MAAI,CAAC,QAAS;EAEd,MAAM,eAAe,QAAQ,cAAc,CAAC,KAAK,QAAQ,IAAI,QAAQ;EAErE,MAAM,UAAU,eAAe,cAAc,aAAa;AAE1D,eAAa,SAAS;IACpB;EAAC;EAAS;EAAe;EAAa,CAAC;AAE1C,QAAO,EACN,KAAK,UACL"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PossibleRef } from "../utils/composeRefs.js";
|
|
2
|
+
import * as react3 from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/useComposeRefs.d.ts
|
|
5
|
+
declare const useComposeRefs: <TRef extends HTMLElement>(...refs: Array<PossibleRef<TRef>>) => react3.RefCallback<TRef>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { useComposeRefs };
|
|
8
|
+
//# sourceMappingURL=useComposeRefs.d.ts.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { composeRefs } from "../utils/composeRefs.js";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/useComposeRefs.ts
|
|
5
|
+
const useComposeRefs = (...refs) => {
|
|
6
|
+
return useMemo(() => composeRefs(...refs), refs);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
export { useComposeRefs };
|
|
11
|
+
//# sourceMappingURL=useComposeRefs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useComposeRefs.js","names":[],"sources":["../../../src/hooks/useComposeRefs.ts"],"sourcesContent":["import { useMemo } from \"react\";\nimport { composeRefs, type PossibleRef } from \"@/utils\";\n\nconst useComposeRefs = <TRef extends HTMLElement>(...refs: Array<PossibleRef<TRef>>) => {\n\t// eslint-disable-next-line react-hooks/exhaustive-deps -- Allow\n\tconst mergedRef = useMemo(() => composeRefs(...refs), refs);\n\n\treturn mergedRef;\n};\n\nexport { useComposeRefs };\n"],"mappings":";;;;AAGA,MAAM,kBAA4C,GAAG,SAAmC;AAIvF,QAFkB,cAAc,YAAY,GAAG,KAAK,EAAE,KAAK"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
//#region src/hooks/useConstant.d.ts
|
|
2
|
+
declare const useConstant: <TResult>(initFn: () => TResult) => TResult;
|
|
3
|
+
declare const useLazyRef: <TResult>(initFn: () => TResult) => React.RefObject<TResult>;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { useConstant, useLazyRef };
|
|
6
|
+
//# sourceMappingURL=useConstant.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useRef } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useConstant.ts
|
|
4
|
+
const useConstant = (initFn) => {
|
|
5
|
+
const resultRef = useRef(null);
|
|
6
|
+
if (resultRef.current === null) resultRef.current = initFn();
|
|
7
|
+
return resultRef.current;
|
|
8
|
+
};
|
|
9
|
+
const useLazyRef = (initFn) => {
|
|
10
|
+
const resultRef = useRef(null);
|
|
11
|
+
if (resultRef.current === null) resultRef.current = initFn();
|
|
12
|
+
return resultRef;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { useConstant, useLazyRef };
|
|
17
|
+
//# sourceMappingURL=useConstant.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useConstant.js","names":[],"sources":["../../../src/hooks/useConstant.ts"],"sourcesContent":["import { useRef } from \"react\";\n\nexport const useConstant = <TResult>(initFn: () => TResult): TResult => {\n\tconst resultRef = useRef<TResult | null>(null);\n\n\t// eslint-disable-next-line ts-eslint/prefer-nullish-coalescing -- The current case is justified since it's optimizable by the react compiler\n\tif (resultRef.current === null) {\n\t\tresultRef.current = initFn();\n\t}\n\n\treturn resultRef.current;\n};\n\nexport const useLazyRef = <TResult>(initFn: () => TResult): React.RefObject<TResult> => {\n\tconst resultRef = useRef<TResult>(null as never);\n\n\t// eslint-disable-next-line ts-eslint/prefer-nullish-coalescing -- The current case is justified since it's optimizable by the react compiler\n\tif (resultRef.current === null) {\n\t\tresultRef.current = initFn();\n\t}\n\n\treturn resultRef;\n};\n"],"mappings":";;;AAEA,MAAa,eAAwB,WAAmC;CACvE,MAAM,YAAY,OAAuB,KAAK;AAG9C,KAAI,UAAU,YAAY,KACzB,WAAU,UAAU,QAAQ;AAG7B,QAAO,UAAU;;AAGlB,MAAa,cAAuB,WAAoD;CACvF,MAAM,YAAY,OAAgB,KAAc;AAGhD,KAAI,UAAU,YAAY,KACzB,WAAU,UAAU,QAAQ;AAG7B,QAAO"}
|