@vef-framework/hooks 2.0.10 → 2.1.0
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/cjs/index.cjs +1 -1
- package/dist/cjs/lib.cjs +1 -1
- package/dist/cjs/use-authorized-items/index.cjs +1 -1
- package/dist/cjs/use-breakpoints/index.cjs +1 -1
- package/dist/cjs/use-check-permission/index.cjs +1 -1
- package/dist/cjs/use-data-dict-query/index.cjs +1 -1
- package/dist/cjs/use-data-options/index.cjs +1 -1
- package/dist/cjs/use-deep-callback/index.cjs +1 -1
- package/dist/cjs/use-deep-compare/index.cjs +1 -1
- package/dist/cjs/use-deep-effect/index.cjs +1 -1
- package/dist/cjs/use-deep-isomorphic-effect/index.cjs +1 -1
- package/dist/cjs/use-deep-layout-effect/index.cjs +1 -1
- package/dist/cjs/use-deep-memo/index.cjs +1 -1
- package/dist/cjs/use-document-event/index.cjs +1 -1
- package/dist/cjs/use-emitter-event/index.cjs +1 -1
- package/dist/cjs/use-has-fetching/index.cjs +1 -1
- package/dist/cjs/use-has-mutating/index.cjs +1 -1
- package/dist/cjs/use-is-authorized/index.cjs +1 -1
- package/dist/cjs/use-latest/index.cjs +1 -1
- package/dist/cjs/use-raf-state/index.cjs +1 -1
- package/dist/cjs/use-shallow-callback/index.cjs +1 -1
- package/dist/cjs/use-shallow-compare/index.cjs +1 -1
- package/dist/cjs/use-shallow-effect/index.cjs +1 -1
- package/dist/cjs/use-shallow-isomorphic-effect/index.cjs +1 -1
- package/dist/cjs/use-shallow-layout-effect/index.cjs +1 -1
- package/dist/cjs/use-shallow-memo/index.cjs +1 -1
- package/dist/cjs/use-singleton/index.cjs +1 -1
- package/dist/cjs/use-viewport-size/index.cjs +1 -0
- package/dist/es/index.js +29 -85
- package/dist/es/lib.js +14 -60
- package/dist/es/use-authorized-items/index.js +11 -14
- package/dist/es/use-breakpoints/index.js +47 -53
- package/dist/es/use-check-permission/index.js +8 -11
- package/dist/es/use-data-dict-query/index.js +21 -25
- package/dist/es/use-data-options/index.js +46 -78
- package/dist/es/use-deep-callback/index.js +8 -7
- package/dist/es/use-deep-compare/index.js +15 -19
- package/dist/es/use-deep-effect/index.js +8 -7
- package/dist/es/use-deep-isomorphic-effect/index.js +8 -8
- package/dist/es/use-deep-layout-effect/index.js +7 -6
- package/dist/es/use-deep-memo/index.js +8 -7
- package/dist/es/use-document-event/index.js +20 -15
- package/dist/es/use-emitter-event/index.js +11 -15
- package/dist/es/use-has-fetching/index.js +11 -10
- package/dist/es/use-has-mutating/index.js +10 -9
- package/dist/es/use-is-authorized/index.js +8 -11
- package/dist/es/use-latest/index.js +7 -6
- package/dist/es/use-raf-state/index.js +14 -13
- package/dist/es/use-shallow-callback/index.js +8 -7
- package/dist/es/use-shallow-compare/index.js +15 -19
- package/dist/es/use-shallow-effect/index.js +8 -7
- package/dist/es/use-shallow-isomorphic-effect/index.js +8 -8
- package/dist/es/use-shallow-layout-effect/index.js +8 -7
- package/dist/es/use-shallow-memo/index.js +8 -7
- package/dist/es/use-singleton/index.js +7 -6
- package/dist/es/use-viewport-size/index.js +34 -0
- package/dist/types/index.d.ts +3 -2
- package/dist/types/lib.d.ts +1 -0
- package/dist/types/src/index.d.ts +27 -0
- package/dist/types/src/lib.d.ts +7 -0
- package/dist/types/src/use-authorized-items/index.d.ts +22 -0
- package/dist/types/src/use-breakpoints/index.d.ts +55 -0
- package/dist/types/src/use-check-permission/index.d.ts +8 -0
- package/dist/types/src/use-data-dict-query/index.d.ts +15 -0
- package/dist/types/src/use-data-options/index.d.ts +86 -0
- package/dist/types/src/use-deep-callback/index.d.ts +11 -0
- package/dist/types/src/use-deep-compare/index.d.ts +9 -0
- package/dist/types/src/use-deep-effect/index.d.ts +10 -0
- package/dist/types/src/use-deep-isomorphic-effect/index.d.ts +11 -0
- package/dist/types/src/use-deep-layout-effect/index.d.ts +10 -0
- package/dist/types/src/use-deep-memo/index.d.ts +11 -0
- package/dist/types/src/use-document-event/index.d.ts +19 -0
- package/dist/types/src/use-emitter-event/index.d.ts +17 -0
- package/dist/types/src/use-has-fetching/index.d.ts +8 -0
- package/dist/types/src/use-has-mutating/index.d.ts +7 -0
- package/dist/types/src/use-is-authorized/index.d.ts +10 -0
- package/dist/types/src/use-latest/index.d.ts +21 -0
- package/dist/types/src/use-raf-state/index.d.ts +23 -0
- package/dist/types/src/use-shallow-callback/index.d.ts +14 -0
- package/dist/types/src/use-shallow-compare/index.d.ts +13 -0
- package/dist/types/src/use-shallow-effect/index.d.ts +13 -0
- package/dist/types/src/use-shallow-isomorphic-effect/index.d.ts +13 -0
- package/dist/types/src/use-shallow-layout-effect/index.d.ts +13 -0
- package/dist/types/src/use-shallow-memo/index.d.ts +14 -0
- package/dist/types/src/use-singleton/index.d.ts +18 -0
- package/dist/types/src/use-viewport-size/index.d.ts +20 -0
- package/dist/types/use-authorized-items/index.d.ts +5 -5
- package/dist/types/use-breakpoints/index.d.ts +15 -7
- package/dist/types/use-check-permission/index.d.ts +2 -2
- package/dist/types/use-data-dict-query/index.d.ts +1 -1
- package/dist/types/use-data-options/index.d.ts +8 -2
- package/dist/types/use-deep-callback/index.d.ts +7 -5
- package/dist/types/use-deep-compare/index.d.ts +4 -3
- package/dist/types/use-deep-effect/index.d.ts +5 -4
- package/dist/types/use-deep-isomorphic-effect/index.d.ts +6 -4
- package/dist/types/use-deep-layout-effect/index.d.ts +5 -4
- package/dist/types/use-deep-memo/index.d.ts +6 -4
- package/dist/types/use-document-event/index.d.ts +15 -4
- package/dist/types/use-emitter-event/index.d.ts +13 -5
- package/dist/types/use-is-authorized/index.d.ts +2 -2
- package/dist/types/use-latest/index.d.ts +18 -5
- package/dist/types/use-raf-state/index.d.ts +18 -3
- package/dist/types/use-shallow-callback/index.d.ts +10 -5
- package/dist/types/use-shallow-compare/index.d.ts +8 -3
- package/dist/types/use-shallow-effect/index.d.ts +8 -4
- package/dist/types/use-shallow-isomorphic-effect/index.d.ts +8 -4
- package/dist/types/use-shallow-layout-effect/index.d.ts +8 -4
- package/dist/types/use-shallow-memo/index.d.ts +8 -3
- package/dist/types/use-singleton/index.d.ts +14 -7
- package/dist/types/use-viewport-size/index.d.ts +20 -0
- package/package.json +8 -7
|
@@ -29,19 +29,27 @@ export interface UseBreakpointsOptions<T extends string = string> {
|
|
|
29
29
|
/**
|
|
30
30
|
* Get initial value in effect instead of on mount
|
|
31
31
|
* This helps avoid hydration mismatches in SSR
|
|
32
|
+
*
|
|
32
33
|
* @default false
|
|
33
34
|
*/
|
|
34
35
|
getInitialValueInEffect?: boolean;
|
|
35
36
|
}
|
|
36
37
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @param breakpoints - Breakpoint configuration object
|
|
40
|
-
* @param options - Hook options
|
|
41
|
-
* @returns Current breakpoint information
|
|
38
|
+
* Tracks active responsive breakpoints based on window width using media queries.
|
|
39
|
+
* Returns the current breakpoint and all matching breakpoints.
|
|
42
40
|
*
|
|
41
|
+
* @param breakpoints - Breakpoint configuration object mapping names to min-width values.
|
|
42
|
+
* @param options - Hook options for SSR and hydration handling.
|
|
43
|
+
* @returns Current breakpoint information including name, value, and all matches.
|
|
43
44
|
* @remarks
|
|
44
|
-
*
|
|
45
|
-
*
|
|
45
|
+
* Uses `min-width` media queries, so include a breakpoint starting from 0 to cover all viewport sizes.
|
|
46
|
+
* @example
|
|
47
|
+
* ```tsx
|
|
48
|
+
* const breakpoints = { xs: 0, sm: 640, md: 768, lg: 1024, xl: 1280 };
|
|
49
|
+
* const { current, matches } = useBreakpoints(breakpoints);
|
|
50
|
+
*
|
|
51
|
+
* // current: 'md' (largest matching breakpoint)
|
|
52
|
+
* // matches: ['xs', 'sm', 'md'] (all matching breakpoints)
|
|
53
|
+
* ```
|
|
46
54
|
*/
|
|
47
55
|
export declare function useBreakpoints<T extends string>(breakpoints: Breakpoints<T>, options?: UseBreakpointsOptions<T>): UseBreakpointsResult<T>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { PermissionCheckMode } from '@vef-framework/core';
|
|
2
2
|
import { MaybeArray } from '@vef-framework/shared';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Hook that returns a function to check permissions imperatively.
|
|
5
5
|
*
|
|
6
|
-
* @returns
|
|
6
|
+
* @returns A function that checks if the user has the specified permissions.
|
|
7
7
|
*/
|
|
8
8
|
export declare function useCheckPermission(): (permTokens?: MaybeArray<string>, checkMode?: PermissionCheckMode) => boolean;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DataOption, UseQueryOptions, UseQueryResult } from '@vef-framework/core';
|
|
2
2
|
import { Except } from '@vef-framework/shared';
|
|
3
|
-
export type UseDataDictQueryOptions<TData = DataOption> = Except<UseQueryOptions<DataOption[], TData[]>, "queryFn" | "queryKey" | "queryHash" | "queryKeyHashFn"> & {
|
|
3
|
+
export type UseDataDictQueryOptions<TData = DataOption> = Except<UseQueryOptions<DataOption[], TData[], string>, "queryFn" | "queryKey" | "queryHash" | "queryKeyHashFn"> & {
|
|
4
4
|
dataDictKey?: string;
|
|
5
5
|
onFetch?: (data: TData[]) => void;
|
|
6
6
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DataOption, DataOptionWithPinyin, UseQueryOptions, UseQueryResult } from '@vef-framework/core';
|
|
2
|
-
import { Key, MaybeUndefined,
|
|
2
|
+
import { Key, MaybeUndefined, Simplify } from '@vef-framework/shared';
|
|
3
3
|
/**
|
|
4
4
|
* Field extractor - can be a string path (e.g., "user.name") or a function
|
|
5
5
|
*/
|
|
@@ -10,26 +10,31 @@ export type FieldExtractor<TData, TValue> = string | ((item: TData) => TValue);
|
|
|
10
10
|
export interface DataOptionsFieldMapping<TData = unknown> {
|
|
11
11
|
/**
|
|
12
12
|
* Field mapping for label
|
|
13
|
+
*
|
|
13
14
|
* @default "label"
|
|
14
15
|
*/
|
|
15
16
|
labelKey?: FieldExtractor<TData, string>;
|
|
16
17
|
/**
|
|
17
18
|
* Field mapping for value
|
|
19
|
+
*
|
|
18
20
|
* @default "value"
|
|
19
21
|
*/
|
|
20
22
|
valueKey?: FieldExtractor<TData, Key>;
|
|
21
23
|
/**
|
|
22
24
|
* Field mapping for disabled
|
|
25
|
+
*
|
|
23
26
|
* @default "disabled"
|
|
24
27
|
*/
|
|
25
28
|
disabledKey?: FieldExtractor<TData, MaybeUndefined<boolean>>;
|
|
26
29
|
/**
|
|
27
30
|
* Field mapping for description
|
|
31
|
+
*
|
|
28
32
|
* @default "description"
|
|
29
33
|
*/
|
|
30
34
|
descriptionKey?: FieldExtractor<TData, MaybeUndefined<string>>;
|
|
31
35
|
/**
|
|
32
36
|
* Field mapping for children
|
|
37
|
+
*
|
|
33
38
|
* @default "children"
|
|
34
39
|
*/
|
|
35
40
|
childrenKey?: FieldExtractor<TData, MaybeUndefined<TData[]>>;
|
|
@@ -67,13 +72,14 @@ export type UseDataOptionsQueryOptions<TQueryFnData = unknown, TData = TQueryFnD
|
|
|
67
72
|
/**
|
|
68
73
|
* Return type for useDataOptionsQuery hook
|
|
69
74
|
*/
|
|
70
|
-
export type UseDataOptionsQueryResult<TData, TOption> =
|
|
75
|
+
export type UseDataOptionsQueryResult<TData, TOption> = Simplify<Omit<UseQueryResult<TData[]>, "data"> & {
|
|
71
76
|
options: TOption[];
|
|
72
77
|
}>;
|
|
73
78
|
export declare function useDataOptionsQuery<TQueryFnData = unknown, TData = TQueryFnData, TParams = never>(config: UseDataOptionsQueryOptions<TQueryFnData, TData, TParams>): UseDataOptionsQueryResult<TData, DataOption<TData>>;
|
|
74
79
|
export declare function useDataOptionsQuery<TQueryFnData = unknown, TData = TQueryFnData, TParams = never>(options: UseDataOptionsQueryOptions<TQueryFnData, TData, TParams> & {
|
|
75
80
|
/**
|
|
76
81
|
* Whether to add pinyin fields for label and description
|
|
82
|
+
*
|
|
77
83
|
* @default false
|
|
78
84
|
*/
|
|
79
85
|
withPinyin: boolean;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { DependencyList } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Memoizes a callback function with deep comparison of dependencies.
|
|
4
|
+
* Unlike `useCallback`, this performs deep equality checks on dependency values,
|
|
5
|
+
* preventing unnecessary callback recreation when objects/arrays have the same content.
|
|
4
6
|
*
|
|
5
|
-
* @param callback - The callback to
|
|
6
|
-
* @param dependencies - The
|
|
7
|
-
* @returns The memoized callback.
|
|
7
|
+
* @param callback - The callback function to memoize.
|
|
8
|
+
* @param dependencies - The dependency array to deeply compare for changes.
|
|
9
|
+
* @returns The memoized callback that only changes when dependencies are deeply different.
|
|
8
10
|
*/
|
|
9
|
-
export declare function useDeepCallback<T extends
|
|
11
|
+
export declare function useDeepCallback<T extends (...args: any[]) => any>(callback: T, dependencies: DependencyList): T;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { DependencyList } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Performs deep comparison of dependency arrays to detect changes.
|
|
4
|
+
* Unlike React's default shallow comparison, this compares nested objects and arrays by value.
|
|
4
5
|
*
|
|
5
|
-
* @param dependencies - The
|
|
6
|
-
* @returns
|
|
6
|
+
* @param dependencies - The dependency array to track for deep changes.
|
|
7
|
+
* @returns A memoized array containing a signal value that changes only when dependencies are deeply different.
|
|
7
8
|
*/
|
|
8
9
|
export declare function useDeepCompare(dependencies?: DependencyList): readonly [number];
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { DependencyList, EffectCallback } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Runs an effect with deep comparison of dependencies.
|
|
4
|
+
* Unlike `useEffect`, this performs deep equality checks on dependency values,
|
|
5
|
+
* preventing unnecessary effect execution when objects/arrays have the same content.
|
|
4
6
|
*
|
|
5
|
-
* @param effect - The effect to
|
|
6
|
-
* @param dependencies - The
|
|
7
|
-
* @returns The effect.
|
|
7
|
+
* @param effect - The effect callback to execute.
|
|
8
|
+
* @param dependencies - The dependency array to deeply compare for changes.
|
|
8
9
|
*/
|
|
9
10
|
export declare function useDeepEffect(effect: EffectCallback, dependencies?: DependencyList): void;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { DependencyList, EffectCallback } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Runs an isomorphic effect with deep comparison of dependencies.
|
|
4
|
+
* Uses `useLayoutEffect` on the client and `useEffect` on the server.
|
|
5
|
+
* Unlike `useIsomorphicEffect`, this performs deep equality checks on dependency values,
|
|
6
|
+
* preventing unnecessary effect execution when objects/arrays have the same content.
|
|
4
7
|
*
|
|
5
|
-
* @param effect - The effect to
|
|
6
|
-
* @param dependencies - The
|
|
7
|
-
* @returns The effect.
|
|
8
|
+
* @param effect - The effect callback to execute.
|
|
9
|
+
* @param dependencies - The dependency array to deeply compare for changes.
|
|
8
10
|
*/
|
|
9
11
|
export declare function useDeepIsomorphicEffect(effect: EffectCallback, dependencies?: DependencyList): void;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { DependencyList, EffectCallback } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Runs a layout effect with deep comparison of dependencies.
|
|
4
|
+
* Unlike `useLayoutEffect`, this performs deep equality checks on dependency values,
|
|
5
|
+
* preventing unnecessary effect execution when objects/arrays have the same content.
|
|
4
6
|
*
|
|
5
|
-
* @param effect - The effect to
|
|
6
|
-
* @param dependencies - The
|
|
7
|
-
* @returns The effect.
|
|
7
|
+
* @param effect - The effect callback to execute synchronously after DOM mutations.
|
|
8
|
+
* @param dependencies - The dependency array to deeply compare for changes.
|
|
8
9
|
*/
|
|
9
10
|
export declare function useDeepLayoutEffect(effect: EffectCallback, dependencies?: DependencyList): void;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { DependencyList } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Memoizes a computed value with deep comparison of dependencies.
|
|
4
|
+
* Unlike `useMemo`, this performs deep equality checks on dependency values,
|
|
5
|
+
* preventing unnecessary recomputation when objects/arrays have the same content.
|
|
4
6
|
*
|
|
5
|
-
* @param factory - The factory
|
|
6
|
-
* @param dependencies - The
|
|
7
|
-
* @returns The memoized value.
|
|
7
|
+
* @param factory - The factory function that computes the memoized value.
|
|
8
|
+
* @param dependencies - The dependency array to deeply compare for changes.
|
|
9
|
+
* @returns The memoized value that only recomputes when dependencies are deeply different.
|
|
8
10
|
*/
|
|
9
11
|
export declare function useDeepMemo<T>(factory: () => T, dependencies: DependencyList): T;
|
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Attaches an event listener to the document with automatic cleanup.
|
|
3
|
+
* The listener function is always called with the latest version, preventing stale closures.
|
|
3
4
|
*
|
|
4
|
-
* @param type -
|
|
5
|
-
* @param listener -
|
|
6
|
-
* @param options -
|
|
5
|
+
* @param type - Event type to listen for (e.g., 'click', 'keydown').
|
|
6
|
+
* @param listener - Event handler function.
|
|
7
|
+
* @param options - Event listener options (capture, passive, once).
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* function Component() {
|
|
11
|
+
* useDocumentEvent('keydown', (event) => {
|
|
12
|
+
* if (event.key === 'Escape') {
|
|
13
|
+
* closeModal();
|
|
14
|
+
* }
|
|
15
|
+
* });
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
7
18
|
*/
|
|
8
19
|
export declare function useDocumentEvent<TType extends string>(type: TType, listener: TType extends keyof DocumentEventMap ? (this: Document, event: DocumentEventMap[TType]) => void : (this: Document, event: CustomEvent) => void, options?: boolean | AddEventListenerOptions): void;
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { EventHandler, EventType, EventEmitter } from '@vef-framework/shared';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Subscribes to an EventEmitter event with automatic cleanup.
|
|
4
4
|
*
|
|
5
|
-
* @param emitter -
|
|
6
|
-
* @param eventType -
|
|
7
|
-
* @param eventListener -
|
|
5
|
+
* @param emitter - EventEmitter instance to subscribe to.
|
|
6
|
+
* @param eventType - Event type to listen for.
|
|
7
|
+
* @param eventListener - Event handler function.
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* function Component({ emitter }) {
|
|
11
|
+
* useEmitterEvent(emitter, 'data', (data) => {
|
|
12
|
+
* console.log('Received:', data);
|
|
13
|
+
* });
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
8
16
|
*/
|
|
9
|
-
export declare function useEmitterEvent<TEvents extends Record<EventType,
|
|
17
|
+
export declare function useEmitterEvent<TEvents extends Record<EventType, any>>(emitter: EventEmitter<TEvents>, eventType: keyof TEvents, eventListener: EventHandler<TEvents[keyof TEvents]>): void;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { PermissionCheckMode } from '@vef-framework/core';
|
|
2
2
|
import { MaybeArray } from '@vef-framework/shared';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Hook to check if the user is authorized based on permission tokens.
|
|
5
5
|
*
|
|
6
6
|
* @param permTokens - The permission tokens to check.
|
|
7
|
-
* @param checkMode - The check mode to use.
|
|
7
|
+
* @param checkMode - The check mode to use (default: "any").
|
|
8
8
|
* @returns Whether the user is authorized to access the resource identified by the permission token.
|
|
9
9
|
*/
|
|
10
10
|
export declare function useIsAuthorized(permTokens?: MaybeArray<string>, checkMode?: PermissionCheckMode): boolean;
|
|
@@ -1,8 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MutableRefObject } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Returns a ref that always contains the latest value.
|
|
4
|
+
* Useful for accessing current props/state in callbacks without re-creating them.
|
|
4
5
|
*
|
|
5
|
-
* @param value - The value to
|
|
6
|
-
* @returns
|
|
6
|
+
* @param value - The value to track.
|
|
7
|
+
* @returns A ref object containing the latest value.
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* function Component({ onChange }) {
|
|
11
|
+
* const onChangeRef = useLatest(onChange);
|
|
12
|
+
*
|
|
13
|
+
* useEffect(() => {
|
|
14
|
+
* const handler = () => onChangeRef.current();
|
|
15
|
+
* window.addEventListener('resize', handler);
|
|
16
|
+
* return () => window.removeEventListener('resize', handler);
|
|
17
|
+
* }, []); // No need to include onChange in deps
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
7
20
|
*/
|
|
8
|
-
export declare function useLatest<T>(value: T):
|
|
21
|
+
export declare function useLatest<T>(value: T): MutableRefObject<T>;
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import { Dispatch, SetStateAction } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* State hook that batches updates using requestAnimationFrame.
|
|
4
|
+
* Useful for high-frequency updates (e.g., scroll, resize, mouse move) to prevent excessive re-renders.
|
|
4
5
|
*
|
|
5
|
-
* @param initialState -
|
|
6
|
-
* @returns
|
|
6
|
+
* @param initialState - Initial state value or lazy initializer function.
|
|
7
|
+
* @returns Tuple of [state, setState] similar to useState.
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* function Component() {
|
|
11
|
+
* const [position, setPosition] = useRafState({ x: 0, y: 0 });
|
|
12
|
+
*
|
|
13
|
+
* useEffect(() => {
|
|
14
|
+
* const handleMove = (e: MouseEvent) => {
|
|
15
|
+
* setPosition({ x: e.clientX, y: e.clientY });
|
|
16
|
+
* };
|
|
17
|
+
* window.addEventListener('mousemove', handleMove);
|
|
18
|
+
* return () => window.removeEventListener('mousemove', handleMove);
|
|
19
|
+
* }, []);
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
7
22
|
*/
|
|
8
23
|
export declare function useRafState<T>(initialState: T | (() => T)): [T, Dispatch<SetStateAction<T>>];
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { DependencyList } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Memoizes a callback function with shallow comparison of dependencies.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Similar to React's useCallback, but uses shallow equality comparison for the
|
|
6
|
+
* dependency array instead of reference equality. This is useful when your
|
|
7
|
+
* dependencies are objects or arrays that may have the same content but
|
|
8
|
+
* different references.
|
|
9
|
+
*
|
|
10
|
+
* @param callback - The callback function to memoize.
|
|
11
|
+
* @param dependencies - Dependency array to compare shallowly.
|
|
12
|
+
* @returns The memoized callback function.
|
|
8
13
|
*/
|
|
9
|
-
export declare function useShallowCallback<T extends
|
|
14
|
+
export declare function useShallowCallback<T extends (...args: any[]) => any>(callback: T, dependencies: DependencyList): T;
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { DependencyList } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Performs shallow comparison of dependency arrays for React hooks.
|
|
4
|
+
* Returns a stable reference that only changes when dependencies are not shallowly equal.
|
|
4
5
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
6
|
+
* This is useful for optimizing hooks like useEffect, useMemo, and useCallback
|
|
7
|
+
* when you want to compare object/array dependencies by their shallow content
|
|
8
|
+
* rather than by reference identity.
|
|
9
|
+
*
|
|
10
|
+
* @param dependencies - The dependency array to compare.
|
|
11
|
+
* @returns A tuple containing a signal number that increments when dependencies change.
|
|
7
12
|
*/
|
|
8
13
|
export declare function useShallowCompare(dependencies?: DependencyList): readonly [number];
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { DependencyList, EffectCallback } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Runs an effect with shallow comparison of dependencies.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Similar to React's useEffect, but uses shallow equality comparison for the
|
|
6
|
+
* dependency array instead of reference equality. This is useful when your
|
|
7
|
+
* dependencies are objects or arrays that may have the same content but
|
|
8
|
+
* different references.
|
|
9
|
+
*
|
|
10
|
+
* @param effect - The effect function to run.
|
|
11
|
+
* @param dependencies - Dependency array to compare shallowly.
|
|
8
12
|
*/
|
|
9
13
|
export declare function useShallowEffect(effect: EffectCallback, dependencies?: DependencyList): void;
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { DependencyList, EffectCallback } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Runs an isomorphic effect with shallow comparison of dependencies.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Similar to useIsomorphicEffect (useLayoutEffect on client, useEffect on server),
|
|
6
|
+
* but uses shallow equality comparison for the dependency array instead of reference
|
|
7
|
+
* equality. This is useful when your dependencies are objects or arrays that may
|
|
8
|
+
* have the same content but different references.
|
|
9
|
+
*
|
|
10
|
+
* @param effect - The effect function to run.
|
|
11
|
+
* @param dependencies - Dependency array to compare shallowly.
|
|
8
12
|
*/
|
|
9
13
|
export declare function useShallowIsomorphicEffect(effect: EffectCallback, dependencies?: DependencyList): void;
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { DependencyList, EffectCallback } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Runs a layout effect with shallow comparison of dependencies.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Similar to React's useLayoutEffect, but uses shallow equality comparison for the
|
|
6
|
+
* dependency array instead of reference equality. This is useful when your
|
|
7
|
+
* dependencies are objects or arrays that may have the same content but
|
|
8
|
+
* different references.
|
|
9
|
+
*
|
|
10
|
+
* @param effect - The effect function to run synchronously after DOM mutations.
|
|
11
|
+
* @param dependencies - Dependency array to compare shallowly.
|
|
8
12
|
*/
|
|
9
13
|
export declare function useShallowLayoutEffect(effect: EffectCallback, dependencies?: DependencyList): void;
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { DependencyList } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Memoizes a value with shallow comparison of dependencies.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Similar to React's useMemo, but uses shallow equality comparison for the
|
|
6
|
+
* dependency array instead of reference equality. This is useful when your
|
|
7
|
+
* dependencies are objects or arrays that may have the same content but
|
|
8
|
+
* different references.
|
|
9
|
+
*
|
|
10
|
+
* @param factory - Function that returns the value to memoize.
|
|
11
|
+
* @param dependencies - Dependency array to compare shallowly.
|
|
7
12
|
* @returns The memoized value.
|
|
8
13
|
*/
|
|
9
14
|
export declare function useShallowMemo<T>(factory: () => T, dependencies: DependencyList): T;
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { RefObject } from 'react';
|
|
2
|
-
export interface SingletonRefObject<T> extends RefObject<T> {
|
|
3
|
-
readonly current: T;
|
|
4
|
-
}
|
|
5
2
|
/**
|
|
6
|
-
*
|
|
3
|
+
* Creates a singleton value that persists across renders and is initialized only once.
|
|
4
|
+
* The initializer function is called only on the first render.
|
|
7
5
|
*
|
|
8
|
-
* @param initializer -
|
|
9
|
-
* @returns
|
|
6
|
+
* @param initializer - Function that creates the singleton value.
|
|
7
|
+
* @returns A ref object containing the singleton value.
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* function Component() {
|
|
11
|
+
* const emitter = useSingleton(() => new EventEmitter());
|
|
12
|
+
* const id = useSingleton(() => Math.random());
|
|
13
|
+
*
|
|
14
|
+
* // emitter.current and id.current are stable across renders
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
10
17
|
*/
|
|
11
|
-
export declare function useSingleton<T>(initializer: () => T):
|
|
18
|
+
export declare function useSingleton<T>(initializer: () => T): RefObject<T>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface ViewportSize {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Tracks the current viewport dimensions (window.innerWidth/innerHeight).
|
|
7
|
+
* Automatically updates on window resize and orientation change events.
|
|
8
|
+
* Uses requestAnimationFrame to batch updates and prevent excessive re-renders.
|
|
9
|
+
*
|
|
10
|
+
* @returns Current viewport size object with width and height.
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* function Component() {
|
|
14
|
+
* const { width, height } = useViewportSize();
|
|
15
|
+
*
|
|
16
|
+
* return <div>Viewport: {width}x{height}</div>;
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function useViewportSize(): ViewportSize;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vef-framework/hooks",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0
|
|
4
|
+
"version": "2.1.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Hooks for VEF framework",
|
|
7
7
|
"author": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"sideEffects": false,
|
|
21
21
|
"exports": {
|
|
22
22
|
".": {
|
|
23
|
-
"
|
|
23
|
+
"vef": "./src/index.ts",
|
|
24
24
|
"import": {
|
|
25
25
|
"types": "./dist/types/index.d.ts",
|
|
26
26
|
"default": "./dist/es/index.js"
|
|
@@ -48,13 +48,14 @@
|
|
|
48
48
|
"react": ">=19"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"@vef-framework/shared": "2.0
|
|
51
|
+
"@ai-sdk/react": "3.0.140",
|
|
52
|
+
"@mantine/hooks": "^8.3.18",
|
|
53
|
+
"react-hotkeys-hook": "^5.2.4",
|
|
54
|
+
"@vef-framework/shared": "2.1.0",
|
|
55
|
+
"@vef-framework/core": "2.1.0"
|
|
55
56
|
},
|
|
56
57
|
"devDependencies": {
|
|
57
|
-
"react": "^19.2.
|
|
58
|
+
"react": "^19.2.4"
|
|
58
59
|
},
|
|
59
60
|
"scripts": {
|
|
60
61
|
"clean": "rimraf dist",
|