@siberiacancode/reactuse 0.1.0 → 0.2.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/helpers/createContext/createContext.cjs +2 -0
- package/dist/cjs/helpers/createContext/createContext.cjs.map +1 -0
- package/dist/cjs/helpers/createStore/createStore.cjs +2 -0
- package/dist/cjs/helpers/createStore/createStore.cjs.map +1 -0
- package/dist/cjs/hooks/useClipboard/useClipboard.cjs.map +1 -1
- package/dist/cjs/hooks/useDidUpdate/useDidUpdate.cjs.map +1 -1
- package/dist/cjs/hooks/useDropZone/useDropZone.cjs +2 -0
- package/dist/cjs/hooks/useDropZone/useDropZone.cjs.map +1 -0
- package/dist/cjs/hooks/useEventSource/useEventSource.cjs +2 -0
- package/dist/cjs/hooks/useEventSource/useEventSource.cjs.map +1 -0
- package/dist/cjs/hooks/useField/useField.cjs +1 -1
- package/dist/cjs/hooks/useField/useField.cjs.map +1 -1
- package/dist/cjs/hooks/useGamepad/useGamepad.cjs.map +1 -1
- package/dist/cjs/hooks/useHover/useHover.cjs +1 -1
- package/dist/cjs/hooks/useHover/useHover.cjs.map +1 -1
- package/dist/cjs/hooks/useIntersectionObserver/useIntersectionObserver.cjs.map +1 -1
- package/dist/cjs/hooks/useMemory/useMemory.cjs +1 -1
- package/dist/cjs/hooks/useMemory/useMemory.cjs.map +1 -1
- package/dist/cjs/hooks/useQuery/useQuery.cjs +1 -1
- package/dist/cjs/hooks/useQuery/useQuery.cjs.map +1 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/esm/helpers/createContext/createContext.mjs +38 -0
- package/dist/esm/helpers/createContext/createContext.mjs.map +1 -0
- package/dist/esm/helpers/createStore/createStore.mjs +32 -0
- package/dist/esm/helpers/createStore/createStore.mjs.map +1 -0
- package/dist/esm/hooks/useClipboard/useClipboard.mjs.map +1 -1
- package/dist/esm/hooks/useDidUpdate/useDidUpdate.mjs.map +1 -1
- package/dist/esm/hooks/useDropZone/useDropZone.mjs +50 -0
- package/dist/esm/hooks/useDropZone/useDropZone.mjs.map +1 -0
- package/dist/esm/hooks/useEventSource/useEventSource.mjs +52 -0
- package/dist/esm/hooks/useEventSource/useEventSource.mjs.map +1 -0
- package/dist/esm/hooks/useField/useField.mjs +1 -3
- package/dist/esm/hooks/useField/useField.mjs.map +1 -1
- package/dist/esm/hooks/useGamepad/useGamepad.mjs +7 -2
- package/dist/esm/hooks/useGamepad/useGamepad.mjs.map +1 -1
- package/dist/esm/hooks/useHover/useHover.mjs +9 -6
- package/dist/esm/hooks/useHover/useHover.mjs.map +1 -1
- package/dist/esm/hooks/useIntersectionObserver/useIntersectionObserver.mjs.map +1 -1
- package/dist/esm/hooks/useMemory/useMemory.mjs +14 -10
- package/dist/esm/hooks/useMemory/useMemory.mjs.map +1 -1
- package/dist/esm/hooks/useQuery/useQuery.mjs +27 -29
- package/dist/esm/hooks/useQuery/useQuery.mjs.map +1 -1
- package/dist/esm/index.mjs +323 -315
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/helpers/createContext/createContext.d.ts +48 -0
- package/dist/types/helpers/createStore/createStore.d.ts +31 -0
- package/dist/types/helpers/index.d.ts +2 -0
- package/dist/types/hooks/index.d.ts +2 -0
- package/dist/types/hooks/useClipboard/useClipboard.d.ts +1 -1
- package/dist/types/hooks/useDropZone/useDropZone.d.ts +80 -0
- package/dist/types/hooks/useEventSource/useEventSource.d.ts +47 -0
- package/dist/types/hooks/useHover/useHover.d.ts +11 -4
- package/dist/types/hooks/useIntersectionObserver/useIntersectionObserver.d.ts +1 -1
- package/dist/types/hooks/useQuery/useQuery.d.ts +0 -1
- package/dist/types/index.d.ts +1 -0
- package/package.json +7 -11
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { JSX, ReactNode } from 'react';
|
|
2
|
+
/** The create context options type */
|
|
3
|
+
export interface CreateContextOptions {
|
|
4
|
+
/** Display name for the context (useful for debugging) */
|
|
5
|
+
name?: string;
|
|
6
|
+
/** Whether to throw an error if context is used outside of Provider */
|
|
7
|
+
strict?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** The context value type */
|
|
10
|
+
export interface ContextValue<Value> {
|
|
11
|
+
/** The context value */
|
|
12
|
+
value: Value | undefined;
|
|
13
|
+
/** The context set function */
|
|
14
|
+
set: (value: Value) => void;
|
|
15
|
+
}
|
|
16
|
+
/** The provider props type */
|
|
17
|
+
export interface ProviderProps<Value> {
|
|
18
|
+
/** The children */
|
|
19
|
+
children: ReactNode;
|
|
20
|
+
/** The initial value */
|
|
21
|
+
initialValue?: Value;
|
|
22
|
+
}
|
|
23
|
+
/** The create context return type */
|
|
24
|
+
export interface CreateContextReturn<Value> {
|
|
25
|
+
/** The context instance */
|
|
26
|
+
instance: React.Context<ContextValue<Value>>;
|
|
27
|
+
/** The provider component */
|
|
28
|
+
Provider: (props: ProviderProps<Value>) => JSX.Element;
|
|
29
|
+
/** The selector hook */
|
|
30
|
+
useSelect: {
|
|
31
|
+
<Selected>(selector: (value: Value) => Selected): Selected;
|
|
32
|
+
(): ContextValue<Value>;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @name createContext
|
|
37
|
+
* @description - Creates a typed context with additional utilities
|
|
38
|
+
* @category Helpers
|
|
39
|
+
*
|
|
40
|
+
* @template Value - The type of value that will be stored in the context
|
|
41
|
+
* @param {Value | undefined} [defaultValue] - Default value for the context
|
|
42
|
+
* @param {CreateContextOptions<Value>} [options] - Additional options for context creation
|
|
43
|
+
* @returns {CreateContextReturn<Value>} Object containing context utilities and components
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const { useSelect, instance, Provider } = createContext<number>(0);
|
|
47
|
+
*/
|
|
48
|
+
export declare const createContext: <Value>(defaultValue?: Value | undefined, options?: CreateContextOptions) => CreateContextReturn<Value>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type SetStateAction<Value> = ((prev: Value) => Value) | Value;
|
|
2
|
+
type StateCreator<Value> = (set: (action: SetStateAction<Value>) => void, get: () => Value) => Value;
|
|
3
|
+
export interface StoreApi<Value> {
|
|
4
|
+
getInitialState: () => Value;
|
|
5
|
+
getState: () => Value;
|
|
6
|
+
setState: (action: SetStateAction<Value>) => void;
|
|
7
|
+
subscribe: (listener: (state: Value, prevState: Value) => void) => () => void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* @name createStore
|
|
11
|
+
* @description - Creates a store with state management capabilities
|
|
12
|
+
* @category Helpers
|
|
13
|
+
*
|
|
14
|
+
* @template Value - The type of the store state
|
|
15
|
+
*
|
|
16
|
+
* @param {StateCreator<Value>} createState - Function that initializes the store state
|
|
17
|
+
* @returns {StoreApi<Value>} - Object containing store methods and hook for accessing state
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const { set, get, use, subscribe } = createStore((set) => ({
|
|
21
|
+
* count: 0,
|
|
22
|
+
* increment: () => set(state => ({ count: state.count + 1 }))
|
|
23
|
+
* }));
|
|
24
|
+
*/
|
|
25
|
+
export declare function createStore<Value>(createState: StateCreator<Value> | Value): {
|
|
26
|
+
set: (action: SetStateAction<Value>) => void;
|
|
27
|
+
get: () => Value;
|
|
28
|
+
use: <Selected>(selector: (state: Value) => Selected) => Selected;
|
|
29
|
+
subscribe: (listener: (state: Value, prevState: Value) => void) => () => boolean;
|
|
30
|
+
};
|
|
31
|
+
export {};
|
|
@@ -26,9 +26,11 @@ export * from './useDocumentEvent/useDocumentEvent';
|
|
|
26
26
|
export * from './useDocumentTitle/useDocumentTitle';
|
|
27
27
|
export * from './useDocumentVisibility/useDocumentVisibility';
|
|
28
28
|
export * from './useDoubleClick/useDoubleClick';
|
|
29
|
+
export * from './useDropZone/useDropZone';
|
|
29
30
|
export * from './useElementSize/useElementSize';
|
|
30
31
|
export * from './useEvent/useEvent';
|
|
31
32
|
export * from './useEventListener/useEventListener';
|
|
33
|
+
export * from './useEventSource/useEventSource';
|
|
32
34
|
export * from './useEyeDropper/useEyeDropper';
|
|
33
35
|
export * from './useFavicon/useFavicon';
|
|
34
36
|
export * from './useField/useField';
|
|
@@ -21,6 +21,6 @@ export interface UseCopyToClipboardParams {
|
|
|
21
21
|
* @returns {UseCopyToClipboardReturn} An object containing the boolean state value and utility functions to manipulate the state
|
|
22
22
|
*
|
|
23
23
|
* @example
|
|
24
|
-
* const {
|
|
24
|
+
* const { value, copy } = useClipboard();
|
|
25
25
|
*/
|
|
26
26
|
export declare const useClipboard: (params?: UseCopyToClipboardParams) => UseCopyToClipboardReturn;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { HookTarget } from '../../utils/helpers';
|
|
2
|
+
import { StateRef } from '../useRefState/useRefState';
|
|
3
|
+
export type DropZoneDataTypes = ((types: string[]) => boolean) | string[];
|
|
4
|
+
export interface UseDropZoneOptions {
|
|
5
|
+
/** The data types for drop zone */
|
|
6
|
+
dataTypes?: DropZoneDataTypes;
|
|
7
|
+
/** The multiple mode for drop zone */
|
|
8
|
+
multiple?: boolean;
|
|
9
|
+
/** The on drop callback */
|
|
10
|
+
onDrop?: (files: File[] | null, event: DragEvent) => void;
|
|
11
|
+
/** The on enter callback */
|
|
12
|
+
onEnter?: (event: DragEvent) => void;
|
|
13
|
+
/** The on leave callback */
|
|
14
|
+
onLeave?: (event: DragEvent) => void;
|
|
15
|
+
/** The on over callback */
|
|
16
|
+
onOver?: (event: DragEvent) => void;
|
|
17
|
+
}
|
|
18
|
+
export interface UseDropZoneReturn {
|
|
19
|
+
/** The files that was dropped in drop zone */
|
|
20
|
+
files: File[] | null;
|
|
21
|
+
/** The over drop zone status */
|
|
22
|
+
overed: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface UseDropZone {
|
|
25
|
+
(target: HookTarget, callback?: (files: File[] | null, event: DragEvent) => void): UseDropZoneReturn;
|
|
26
|
+
<Target extends Element>(callback?: (files: File[] | null, event: DragEvent) => void, target?: never): UseDropZoneReturn & {
|
|
27
|
+
ref: StateRef<Target>;
|
|
28
|
+
};
|
|
29
|
+
(target: HookTarget, options?: UseDropZoneOptions): UseDropZoneReturn;
|
|
30
|
+
<Target extends Element>(options?: UseDropZoneOptions, target?: never): UseDropZoneReturn & {
|
|
31
|
+
ref: StateRef<Target>;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @name useDropZone
|
|
36
|
+
* @description - Hook that provides drop zone functionality
|
|
37
|
+
* @category Elements
|
|
38
|
+
*
|
|
39
|
+
* @overload
|
|
40
|
+
* @template Target The target element
|
|
41
|
+
* @param {Target} target The target element drop zone's
|
|
42
|
+
* @param {DataTypes} [options.dataTypes] The data types
|
|
43
|
+
* @param {boolean} [options.multiple] The multiple mode
|
|
44
|
+
* @param {(files: File[] | null, event: DragEvent) => void} [options.onDrop] The on drop callback function
|
|
45
|
+
* @param {(event: DragEvent) => void} [options.onEnter] The on enter callback function
|
|
46
|
+
* @param {(event: DragEvent) => void} [options.onLeave] The on leave callback function
|
|
47
|
+
* @param {(event: DragEvent) => void} [options.onOver] The on over callback function
|
|
48
|
+
* @returns {[boolean, File[] | null]} The object with drop zone states
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const { overed, files } = useDropZone(ref, options);
|
|
52
|
+
*
|
|
53
|
+
* @overload
|
|
54
|
+
* @param {Target} target The target element drop zone's
|
|
55
|
+
* @param {(files: File[] | null, event: DragEvent) => void} [callback] The callback function to be invoked on drop
|
|
56
|
+
* @returns {[boolean, File[] | null]} The object with drop zone states
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* const { overed, files } = useDropZone(ref, () => console.log('callback'));
|
|
60
|
+
*
|
|
61
|
+
* @overload
|
|
62
|
+
* @param {DataTypes} [options.dataTypes] The data types
|
|
63
|
+
* @param {boolean} [options.multiple] The multiple mode
|
|
64
|
+
* @param {(files: File[] | null, event: DragEvent) => void} [options.onDrop] The on drop callback function
|
|
65
|
+
* @param {(event: DragEvent) => void} [options.onEnter] The on enter callback function
|
|
66
|
+
* @param {(event: DragEvent) => void} [options.onLeave] The on leave callback function
|
|
67
|
+
* @param {(event: DragEvent) => void} [options.onOver] The on over callback function
|
|
68
|
+
* @returns {[StateRef<Target>, boolean, File[] | null]} The object with drop zone states and ref
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const { ref, overed, files } = useDropZone(options);
|
|
72
|
+
*
|
|
73
|
+
* @overload
|
|
74
|
+
* @param {(files: File[] | null, event: DragEvent) => void} [callback] The callback function to be invoked on drop
|
|
75
|
+
* @returns {[StateRef<Target>, boolean, File[] | null]} The object with drop zone states and ref
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* const { ref, overed, files } = useDropZone(() => console.log('callback'));
|
|
79
|
+
*/
|
|
80
|
+
export declare const useDropZone: UseDropZone;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** The use event source options type */
|
|
2
|
+
export interface UseEventSourceOptions<QueryData, Data> extends EventSourceInit {
|
|
3
|
+
/** Immediately open the connection when calling this hook */
|
|
4
|
+
immediately?: boolean;
|
|
5
|
+
placeholderData?: (() => Data) | Data;
|
|
6
|
+
retry?: boolean | number;
|
|
7
|
+
retryDelay?: ((retry: number, event: Event) => number) | number;
|
|
8
|
+
onError?: (error: Event) => void;
|
|
9
|
+
onMessage?: (event: Event & {
|
|
10
|
+
data?: Data;
|
|
11
|
+
}) => void;
|
|
12
|
+
onOpen?: () => void;
|
|
13
|
+
select?: (data: QueryData) => Data;
|
|
14
|
+
}
|
|
15
|
+
/** The use event source return type */
|
|
16
|
+
interface UseEventSourceReturn<Data = any> {
|
|
17
|
+
/** The latest data received via the EventSource */
|
|
18
|
+
data?: Data;
|
|
19
|
+
/** The current error */
|
|
20
|
+
error?: Event;
|
|
21
|
+
/** The instance of the EventSource */
|
|
22
|
+
instance?: EventSource;
|
|
23
|
+
isConnecting: boolean;
|
|
24
|
+
isError: boolean;
|
|
25
|
+
isOpen: boolean;
|
|
26
|
+
/** Closes the EventSource connection gracefully */
|
|
27
|
+
close: () => void;
|
|
28
|
+
/** Reopen the EventSource connection */
|
|
29
|
+
open: () => void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @name useEventSource
|
|
33
|
+
* @description - Hook that provides a reactive wrapper for event source
|
|
34
|
+
* @category Browser
|
|
35
|
+
*
|
|
36
|
+
* @browserapi EventSource https://developer.mozilla.org/en-US/docs/Web/API/EventSource
|
|
37
|
+
*
|
|
38
|
+
* @param {string | URL} url The URL of the EventSource
|
|
39
|
+
* @param {string[]} [events=[]] List of events to listen to
|
|
40
|
+
* @param {UseEventSourceOptions} [options={}] Configuration options
|
|
41
|
+
* @returns {UseEventSourceReturn<Data>} The EventSource state and controls
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const { instance, data, isConnecting, isOpen, isError, close, open } = useEventSource('url', ['message']);
|
|
45
|
+
*/
|
|
46
|
+
export declare const useEventSource: <QueryData = any, Data = QueryData>(url: string | URL, events?: string[], options?: UseEventSourceOptions<QueryData, Data>) => UseEventSourceReturn<Data>;
|
|
47
|
+
export {};
|
|
@@ -7,11 +7,18 @@ export interface UseHoverOptions {
|
|
|
7
7
|
/** The on leave callback */
|
|
8
8
|
onLeave?: (event: Event) => void;
|
|
9
9
|
}
|
|
10
|
+
export interface UseHoverReturn {
|
|
11
|
+
value: boolean;
|
|
12
|
+
}
|
|
10
13
|
export interface UseHover {
|
|
11
14
|
(target: HookTarget, callback?: (event: Event) => void): boolean;
|
|
12
15
|
(target: HookTarget, options?: UseHoverOptions): boolean;
|
|
13
|
-
<Target extends Element>(callback?: (event: Event) => void, target?: never):
|
|
14
|
-
|
|
16
|
+
<Target extends Element>(callback?: (event: Event) => void, target?: never): {
|
|
17
|
+
ref: StateRef<Target>;
|
|
18
|
+
} & UseHoverReturn;
|
|
19
|
+
<Target extends Element>(options?: UseHoverOptions, target?: never): {
|
|
20
|
+
ref: StateRef<Target>;
|
|
21
|
+
} & UseHoverReturn;
|
|
15
22
|
}
|
|
16
23
|
/**
|
|
17
24
|
* @name useHover
|
|
@@ -38,7 +45,7 @@ export interface UseHover {
|
|
|
38
45
|
* @overload
|
|
39
46
|
* @template Target The target element
|
|
40
47
|
* @param {(event: Event) => void} [callback] The callback function to be invoked on mouse enter
|
|
41
|
-
* @returns {
|
|
48
|
+
* @returns {{ ref: StateRef<Target> } & UseHoverReturn} The state of the hover
|
|
42
49
|
*
|
|
43
50
|
* @example
|
|
44
51
|
* const [ref, hovering] = useHover(() => console.log('callback'));
|
|
@@ -47,7 +54,7 @@ export interface UseHover {
|
|
|
47
54
|
* @template Target The target element
|
|
48
55
|
* @param {(event: Event) => void} [options.onEntry] The callback function to be invoked on mouse enter
|
|
49
56
|
* @param {(event: Event) => void} [options.onLeave] The callback function to be invoked on mouse leave
|
|
50
|
-
* @returns {
|
|
57
|
+
* @returns {{ ref: StateRef<Target> } & UseHoverReturn} The state of the hover
|
|
51
58
|
*
|
|
52
59
|
* @example
|
|
53
60
|
* const [ref, hovering] = useHover(options);
|
|
@@ -29,7 +29,7 @@ export interface UseIntersectionObserver {
|
|
|
29
29
|
* @param {boolean} [options.enabled=true] The IntersectionObserver options
|
|
30
30
|
* @param {((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined} [options.onChange] The callback to execute when intersection is detected
|
|
31
31
|
* @param {HookTarget} [options.root] The root element to observe
|
|
32
|
-
* @returns {UseIntersectionObserverReturn} An object containing the state
|
|
32
|
+
* @returns {UseIntersectionObserverReturn} An object containing the state
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
35
35
|
* const { ref, entry, inView } = useIntersectionObserver();
|
package/dist/types/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@siberiacancode/reactuse",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The ultimate collection of react hooks",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "SIBERIA CAN CODE 🧊",
|
|
@@ -68,24 +68,20 @@
|
|
|
68
68
|
"@testing-library/dom": "^10.4.0",
|
|
69
69
|
"@testing-library/react": "^16.3.0",
|
|
70
70
|
"@types/dom-speech-recognition": "^0.0.6",
|
|
71
|
-
"@types/react": "^19.1.
|
|
72
|
-
"@types/react-dom": "^19.1.
|
|
71
|
+
"@types/react": "^19.1.3",
|
|
72
|
+
"@types/react-dom": "^19.1.3",
|
|
73
73
|
"@types/web-bluetooth": "^0.0.21",
|
|
74
74
|
"@vitejs/plugin-react": "^4.4.1",
|
|
75
|
-
"core-js": "^3.
|
|
75
|
+
"core-js": "^3.42.0",
|
|
76
76
|
"react": "^19.1.0",
|
|
77
77
|
"react-dom": "^19.1.0",
|
|
78
78
|
"shx": "^0.4.0",
|
|
79
|
-
"vite": "^6.3.
|
|
79
|
+
"vite": "^6.3.5",
|
|
80
80
|
"vite-plugin-dts": "^4.5.3",
|
|
81
|
-
"vitest": "^3.1.
|
|
81
|
+
"vitest": "^3.1.3"
|
|
82
82
|
},
|
|
83
83
|
"lint-staged": {
|
|
84
|
-
"*.js": [
|
|
85
|
-
"eslint --fix",
|
|
86
|
-
"prettier --write"
|
|
87
|
-
],
|
|
88
|
-
"*.ts": [
|
|
84
|
+
"*.{js,ts,tsx}": [
|
|
89
85
|
"eslint --fix",
|
|
90
86
|
"prettier --write"
|
|
91
87
|
]
|