react-hook-toolkit 3.0.5 → 5.0.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.
@@ -0,0 +1,324 @@
1
+ import { useEffect, MutableRefObject } from 'react';
2
+ /**
3
+ * A drop-in replacement for useLayoutEffect that falls back to useEffect on
4
+ * the server (SSR), preventing the "useLayoutEffect does nothing on the server"
5
+ * warning in Next.js / Remix environments.
6
+ */
7
+ export declare const useIsomorphicEffect: typeof useEffect;
8
+ interface UseBooleanReturn {
9
+ value: boolean;
10
+ setTrue: () => void;
11
+ setFalse: () => void;
12
+ toggle: () => void;
13
+ set: (v: boolean) => void;
14
+ }
15
+ /**
16
+ * Manages a boolean state with explicit setTrue, setFalse, toggle, and set helpers.
17
+ */
18
+ export declare function useBoolean(initialValue?: boolean): UseBooleanReturn;
19
+ /**
20
+ * Returns a function that, when called, forces the component to re-render.
21
+ * Useful when external mutable data changes outside of React state.
22
+ */
23
+ export declare function useForceUpdate(): () => void;
24
+ /**
25
+ * Returns a Date that updates every `intervalMs` milliseconds (default 1000).
26
+ * Useful for displaying a live clock.
27
+ */
28
+ export declare function useClock(intervalMs?: number): Date;
29
+ /**
30
+ * Like useState but the exposed value only updates after the setter hasn't
31
+ * been called for `delay` milliseconds.
32
+ * Returns [debouncedValue, setter, immediateValue].
33
+ */
34
+ export declare function useDebouncedState<T>(initialValue: T, delay: number): [T, (value: T) => void, T];
35
+ interface UseAnimationFrameReturn {
36
+ start: () => void;
37
+ stop: () => void;
38
+ isRunning: boolean;
39
+ }
40
+ /**
41
+ * Calls `callback` on every animation frame while running.
42
+ * The callback receives the elapsed time in milliseconds since start.
43
+ * Returns start/stop controls and an isRunning flag.
44
+ */
45
+ export declare function useAnimationFrame(callback: (elapsedMs: number) => void): UseAnimationFrameReturn;
46
+ interface UseAsyncCallbackReturn<T> {
47
+ execute: (...args: any[]) => Promise<void>;
48
+ data: T | null;
49
+ error: Error | null;
50
+ isPending: boolean;
51
+ reset: () => void;
52
+ }
53
+ /**
54
+ * Wraps an async function with loading (isPending), data, and error states.
55
+ * Unlike useAsync, this is imperative — it does NOT run on mount.
56
+ */
57
+ export declare function useAsyncCallback<T>(asyncFn: (...args: any[]) => Promise<T>): UseAsyncCallbackReturn<T>;
58
+ interface UseAudioAnalyserOptions {
59
+ fftSize?: number;
60
+ smoothingTimeConstant?: number;
61
+ }
62
+ interface UseAudioAnalyserReturn {
63
+ analyser: AnalyserNode | null;
64
+ frequencyData: Uint8Array;
65
+ timeDomainData: Uint8Array;
66
+ audioLevel: number;
67
+ isListening: boolean;
68
+ start: () => Promise<void>;
69
+ stop: () => void;
70
+ error: Error | null;
71
+ }
72
+ /**
73
+ * Uses the AnalyserNode API to capture and expose real-time audio frequency
74
+ * and time-domain data from the user's microphone.
75
+ */
76
+ export declare function useAudioAnalyser(options?: UseAudioAnalyserOptions): UseAudioAnalyserReturn;
77
+ interface DragAndDropState {
78
+ isDragging: boolean;
79
+ isOver: boolean;
80
+ }
81
+ interface UseDragAndDropReturn {
82
+ containerProps: {
83
+ onDragOver: (e: React.DragEvent) => void;
84
+ onDragLeave: (e: React.DragEvent) => void;
85
+ onDrop: (e: React.DragEvent) => void;
86
+ };
87
+ draggableProps: (index: number) => {
88
+ draggable: boolean;
89
+ onDragStart: (e: React.DragEvent) => void;
90
+ onDragEnd: (e: React.DragEvent) => void;
91
+ };
92
+ state: DragAndDropState;
93
+ dragIndex: number | null;
94
+ dropIndex: number | null;
95
+ }
96
+ /**
97
+ * Handles drag-and-drop interactions on a container element.
98
+ * Provides props for both the container and each draggable item.
99
+ * Calls onDrop(fromIndex, toIndex) when a drop completes.
100
+ */
101
+ export declare function useDragAndDrop(onDrop?: (fromIndex: number, toIndex: number) => void): UseDragAndDropReturn;
102
+ interface UseFileDropAreaOptions {
103
+ accept?: string[];
104
+ maxSize?: number;
105
+ multiple?: boolean;
106
+ onDrop?: (files: File[]) => void;
107
+ onError?: (error: Error) => void;
108
+ }
109
+ interface UseFileDropAreaReturn {
110
+ dropProps: {
111
+ onDragOver: (e: React.DragEvent) => void;
112
+ onDragLeave: (e: React.DragEvent) => void;
113
+ onDrop: (e: React.DragEvent) => void;
114
+ };
115
+ inputProps: {
116
+ type: 'file';
117
+ accept: string;
118
+ multiple: boolean;
119
+ onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
120
+ style: React.CSSProperties;
121
+ };
122
+ files: File[];
123
+ isOver: boolean;
124
+ clear: () => void;
125
+ }
126
+ /**
127
+ * Handles file drag-drop and file-input selection on a drop area element.
128
+ * Validates file type and size before calling onDrop.
129
+ */
130
+ export declare function useFileDropArea(options?: UseFileDropAreaOptions): UseFileDropAreaReturn;
131
+ interface GamepadState {
132
+ gamepads: (Gamepad | null)[];
133
+ isConnected: boolean;
134
+ }
135
+ /**
136
+ * Handles Gamepad API connections, disconnections, and button/axis state.
137
+ * Polls gamepad state via requestAnimationFrame.
138
+ */
139
+ export declare function useGamepad(): GamepadState;
140
+ interface GridLayoutInfo {
141
+ columns: number;
142
+ rows: number;
143
+ }
144
+ /**
145
+ * Tracks the number of CSS grid columns and rows of a ref'd element.
146
+ * Updates whenever the element resizes.
147
+ */
148
+ export declare function useGridLayout(ref: MutableRefObject<HTMLElement | null>): GridLayoutInfo;
149
+ interface UseInViewOptions extends IntersectionObserverInit {
150
+ once?: boolean;
151
+ }
152
+ interface UseInViewReturn {
153
+ ref: MutableRefObject<Element | null>;
154
+ inView: boolean;
155
+ entry: IntersectionObserverEntry | undefined;
156
+ }
157
+ /**
158
+ * Convenience wrapper over IntersectionObserver that returns a [ref, inView] pair.
159
+ * When once is true, stops observing after the element enters the viewport.
160
+ */
161
+ export declare function useInView(options?: UseInViewOptions): UseInViewReturn;
162
+ type KeyboardHandler = (event: KeyboardEvent) => void;
163
+ type KeyboardBindings = Record<string, KeyboardHandler>;
164
+ /**
165
+ * Binds keyboard hotkeys to handler functions.
166
+ * Key format: 'ctrl+s', 'alt+shift+k', 'Escape', 'ArrowUp', etc.
167
+ */
168
+ export declare function useKeyboard(bindings: KeyboardBindings, element?: MutableRefObject<HTMLElement | null>): void;
169
+ interface MediaDevicesState {
170
+ stream: MediaStream | null;
171
+ devices: MediaDeviceInfo[];
172
+ isLoading: boolean;
173
+ error: Error | null;
174
+ hasCamera: boolean;
175
+ hasMicrophone: boolean;
176
+ }
177
+ interface UseMediaDevicesReturn extends MediaDevicesState {
178
+ requestAccess: (constraints?: MediaStreamConstraints) => Promise<void>;
179
+ stopStream: () => void;
180
+ }
181
+ /**
182
+ * Provides access to the user's camera and microphone via getUserMedia.
183
+ * Enumerates available devices and exposes the active MediaStream.
184
+ */
185
+ export declare function useMediaDevices(): UseMediaDevicesReturn;
186
+ /**
187
+ * Observes DOM mutations on a ref'd element via MutationObserver.
188
+ * Calls the callback whenever the observed mutations are recorded.
189
+ */
190
+ export declare function useMutationObserver(ref: MutableRefObject<Element | null>, callback: MutationCallback, options?: MutationObserverInit): void;
191
+ interface NetworkStateInfo {
192
+ isOnline: boolean;
193
+ effectiveType: '2g' | '3g' | '4g' | 'slow-2g' | null;
194
+ downlink: number | null;
195
+ downlinkMax: number | null;
196
+ rtt: number | null;
197
+ saveData: boolean | null;
198
+ type: string | null;
199
+ since: Date | null;
200
+ }
201
+ /**
202
+ * Full NetworkInformation API integration.
203
+ * Returns online status plus detailed connection metadata when available.
204
+ * Tracks when the online status last changed via `since`.
205
+ */
206
+ export declare function useNetworkState(): NetworkStateInfo;
207
+ interface OrientationState {
208
+ type: OrientationType | null;
209
+ angle: number;
210
+ isPortrait: boolean;
211
+ isLandscape: boolean;
212
+ }
213
+ /**
214
+ * Tracks the device screen orientation type and angle.
215
+ * Uses the ScreenOrientation API with a matchMedia fallback.
216
+ */
217
+ export declare function useOrientation(): OrientationState;
218
+ /**
219
+ * Returns true when the current browser tab is visible to the user.
220
+ * Wraps the Page Visibility API (document.visibilityState).
221
+ */
222
+ export declare function usePageVisibility(): boolean;
223
+ interface PointerInfo {
224
+ pointerId: number;
225
+ x: number;
226
+ y: number;
227
+ pressure: number;
228
+ type: string;
229
+ }
230
+ interface UsePointersReturn {
231
+ pointers: Map<number, PointerInfo>;
232
+ activeCount: number;
233
+ containerProps: {
234
+ onPointerDown: (e: React.PointerEvent) => void;
235
+ onPointerMove: (e: React.PointerEvent) => void;
236
+ onPointerUp: (e: React.PointerEvent) => void;
237
+ onPointerCancel: (e: React.PointerEvent) => void;
238
+ onPointerLeave: (e: React.PointerEvent) => void;
239
+ };
240
+ }
241
+ /**
242
+ * Handles all pointer events at once (mouse, touch, stylus).
243
+ * Tracks every active pointer by its pointerId.
244
+ */
245
+ export declare function usePointers(): UsePointersReturn;
246
+ /**
247
+ * Tracks the full bounding DOMRect of a ref'd element reactively.
248
+ * Re-measures on scroll and resize using a ResizeObserver.
249
+ */
250
+ export declare function useRect<T extends Element = Element>(): [MutableRefObject<T | null>, DOMRect | null];
251
+ interface UseScreenCaptureReturn {
252
+ stream: MediaStream | null;
253
+ isCapturing: boolean;
254
+ error: Error | null;
255
+ start: (options?: DisplayMediaStreamOptions) => Promise<void>;
256
+ stop: () => void;
257
+ }
258
+ /**
259
+ * Uses the Screen Capture API (getDisplayMedia) to capture the screen,
260
+ * a specific window, or the current browser tab.
261
+ */
262
+ export declare function useScreenCapture(): UseScreenCaptureReturn;
263
+ interface UseScrollThresholdOptions {
264
+ threshold?: number;
265
+ element?: MutableRefObject<HTMLElement | null>;
266
+ }
267
+ /**
268
+ * Returns true when the window (or a given element) has been scrolled
269
+ * past the specified pixel threshold.
270
+ */
271
+ export declare function useScrollThreshold(options?: UseScrollThresholdOptions): boolean;
272
+ interface SelectionInfo {
273
+ text: string;
274
+ rect: DOMRect | null;
275
+ isCollapsed: boolean;
276
+ }
277
+ /**
278
+ * Returns the currently selected text along with its bounding DOMRect.
279
+ * Updates whenever the user changes their text selection.
280
+ */
281
+ export declare function useSelection(element?: MutableRefObject<HTMLElement | null>): SelectionInfo;
282
+ type SwipeDirection = 'left' | 'right' | 'up' | 'down' | null;
283
+ interface SwipeState {
284
+ isSwiping: boolean;
285
+ direction: SwipeDirection;
286
+ deltaX: number;
287
+ deltaY: number;
288
+ distance: number;
289
+ }
290
+ interface UseSwipingOptions {
291
+ threshold?: number;
292
+ onSwipeLeft?: () => void;
293
+ onSwipeRight?: () => void;
294
+ onSwipeUp?: () => void;
295
+ onSwipeDown?: () => void;
296
+ }
297
+ interface UseSwipingReturn {
298
+ state: SwipeState;
299
+ handlers: {
300
+ onTouchStart: (e: React.TouchEvent) => void;
301
+ onTouchMove: (e: React.TouchEvent) => void;
302
+ onTouchEnd: (e: React.TouchEvent) => void;
303
+ };
304
+ }
305
+ /**
306
+ * Detects swipe gestures (left/right/up/down) on a touch surface.
307
+ * Fires directional callbacks when the swipe exceeds the pixel threshold.
308
+ */
309
+ export declare function useSwiping(options?: UseSwipingOptions): UseSwipingReturn;
310
+ interface UseWorkerReturn<T> {
311
+ postMessage: (data: any) => void;
312
+ lastMessage: T | null;
313
+ error: ErrorEvent | null;
314
+ isReady: boolean;
315
+ terminate: () => void;
316
+ }
317
+ /**
318
+ * Runs a function in a Web Worker. The workerFn is serialized and executed
319
+ * in a background thread. Pass data via postMessage; receive results via lastMessage.
320
+ *
321
+ * The workerFn receives a MessageEvent and should call postMessage() to send results back.
322
+ */
323
+ export declare function useWorker<T = any>(workerFn: (event: MessageEvent) => void): UseWorkerReturn<T>;
324
+ export {};