sse-hooks 1.0.0 → 1.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/README.md +40 -0
- package/dist/index.cjs +162 -48
- package/dist/index.d.cts +841 -13
- package/dist/index.d.mts +841 -13
- package/dist/index.d.ts +841 -13
- package/dist/index.mjs +161 -49
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useEffect } from 'react';
|
|
1
|
+
import React, { Dispatch, SetStateAction, RefObject, useEffect } from 'react';
|
|
2
2
|
|
|
3
3
|
/** The useBoolean return type. */
|
|
4
4
|
type UseBooleanReturn = {
|
|
@@ -26,6 +26,92 @@ type UseBooleanReturn = {
|
|
|
26
26
|
*/
|
|
27
27
|
declare function useBoolean(defaultValue?: boolean): UseBooleanReturn;
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Custom hook that handles click events anywhere on the document.
|
|
31
|
+
* @param {Function} handler - The function to be called when a click event is detected anywhere on the document.
|
|
32
|
+
* @public
|
|
33
|
+
* @see [Documentation](/docs/use-click-any-where)
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* const handleClick = (event) => {
|
|
37
|
+
* console.log('Document clicked!', event);
|
|
38
|
+
* };
|
|
39
|
+
*
|
|
40
|
+
* // Attach click event handler to document
|
|
41
|
+
* useClickAnywhere(handleClick);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
declare function useClickAnyWhere(handler: (event: MouseEvent) => void): void;
|
|
45
|
+
|
|
46
|
+
declare global {
|
|
47
|
+
interface WindowEventMap {
|
|
48
|
+
"cookie-change": CustomEvent<{
|
|
49
|
+
key: string;
|
|
50
|
+
}>;
|
|
51
|
+
visibilitychange: Event;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Options for customizing the behavior of the useCookie hook.
|
|
56
|
+
* @template T - The type of the state to be stored in the cookie.
|
|
57
|
+
*/
|
|
58
|
+
type UseCookieOptions<T> = {
|
|
59
|
+
/** A function to serialize the value before storing it. */
|
|
60
|
+
serializer?: (value: T) => string;
|
|
61
|
+
/** A function to deserialize the stored value. */
|
|
62
|
+
deserializer?: (value: string) => T;
|
|
63
|
+
/**
|
|
64
|
+
* If `true` (default), the hook will initialize reading the cookie.
|
|
65
|
+
* @default true
|
|
66
|
+
*/
|
|
67
|
+
initializeWithValue?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* A prefix to be prepended to the key (e.g., "myApp_").
|
|
70
|
+
* Useful for namespacing or complying with cookie prefixes like `__Secure-` or `__Host-`.
|
|
71
|
+
*/
|
|
72
|
+
prefix?: string;
|
|
73
|
+
/**
|
|
74
|
+
* The path within the site for which the cookie is valid.
|
|
75
|
+
* @default "/"
|
|
76
|
+
*/
|
|
77
|
+
path?: string;
|
|
78
|
+
/** The domain for which the cookie is valid. */
|
|
79
|
+
domain?: string;
|
|
80
|
+
/** The expiration date of the cookie. */
|
|
81
|
+
expires?: Date;
|
|
82
|
+
/** The maximum age of the cookie in seconds. */
|
|
83
|
+
maxAge?: number;
|
|
84
|
+
/** If `true`, the cookie is only transmitted over secure protocols (HTTPS). */
|
|
85
|
+
secure?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Controls whether the cookie is sent with cross-site requests.
|
|
88
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
|
|
89
|
+
*/
|
|
90
|
+
sameSite?: "lax" | "strict" | "none";
|
|
91
|
+
/** The default value to return if the cookie is not found. */
|
|
92
|
+
defaultValue?: T;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Custom hook that manages state synchronized with a browser [`cookie`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies).
|
|
96
|
+
* It handles serialization, prefixes, updates across tabs, and custom event synchronization.
|
|
97
|
+
* @template T - The type of the state to be stored in the cookie.
|
|
98
|
+
* @param {string} key - The base name of the cookie.
|
|
99
|
+
* @param {T | (() => T)} initialValue - The initial value of the state.
|
|
100
|
+
* @param {UseCookieOptions<T>} [options] - Options for customization.
|
|
101
|
+
* @returns {[T, Dispatch<SetStateAction<T>>, () => void]} A tuple containing the stored value, a setter, and a remover.
|
|
102
|
+
* @public
|
|
103
|
+
* @see [Documentation](/docs/use-cookie)
|
|
104
|
+
* @example
|
|
105
|
+
* ```tsx
|
|
106
|
+
* // Creates a cookie named "__Secure-token"
|
|
107
|
+
* const [token, setToken] = useCookie('token', '', {
|
|
108
|
+
* prefix: '__Secure-',
|
|
109
|
+
* secure: true,
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
declare function useCookie<T>(key: string, initialValue: T | (() => T), options?: UseCookieOptions<T>): [T, Dispatch<SetStateAction<T>>, () => void];
|
|
114
|
+
|
|
29
115
|
/**
|
|
30
116
|
* The copied text as `string` or `null` if nothing has been copied yet.
|
|
31
117
|
*/
|
|
@@ -58,123 +144,478 @@ type CopyFn = (text: string) => Promise<boolean>;
|
|
|
58
144
|
*/
|
|
59
145
|
declare function useCopyToClipboard(): [CopiedValue, CopyFn];
|
|
60
146
|
|
|
147
|
+
/** The countdown's options. */
|
|
61
148
|
type CountdownOptions = {
|
|
149
|
+
/** The countdown's starting number, initial value of the returned number. */
|
|
62
150
|
countStart: number;
|
|
151
|
+
/**
|
|
152
|
+
* The countdown's interval, in milliseconds.
|
|
153
|
+
* @default 1000
|
|
154
|
+
*/
|
|
63
155
|
intervalMs?: number;
|
|
156
|
+
/**
|
|
157
|
+
* True if the countdown is increment.
|
|
158
|
+
* @default false
|
|
159
|
+
*/
|
|
64
160
|
isIncrement?: boolean;
|
|
161
|
+
/**
|
|
162
|
+
* The countdown's stopping number. Pass `-Infinity` to decrease forever.
|
|
163
|
+
* @default 0
|
|
164
|
+
*/
|
|
65
165
|
countStop?: number;
|
|
66
166
|
};
|
|
167
|
+
/** The countdown's controllers. */
|
|
67
168
|
type CountdownControllers = {
|
|
169
|
+
/** Start the countdown. */
|
|
68
170
|
startCountdown: () => void;
|
|
171
|
+
/** Stop the countdown. */
|
|
69
172
|
stopCountdown: () => void;
|
|
173
|
+
/** Reset the countdown. */
|
|
70
174
|
resetCountdown: () => void;
|
|
71
175
|
};
|
|
176
|
+
/**
|
|
177
|
+
* Custom hook that manages countdown.
|
|
178
|
+
* @param {CountdownOptions} countdownOptions - The countdown's options.
|
|
179
|
+
* @returns {[number, CountdownControllers]} An array containing the countdown's count and its controllers.
|
|
180
|
+
* @public
|
|
181
|
+
* @see [Documentation](/docs/use-countdown)
|
|
182
|
+
* @example
|
|
183
|
+
* ```tsx
|
|
184
|
+
* const [counter, { start, stop, reset }] = useCountdown({
|
|
185
|
+
* countStart: 10,
|
|
186
|
+
* intervalMs: 1000,
|
|
187
|
+
* isIncrement: false,
|
|
188
|
+
* });
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
72
191
|
declare function useCountdown({ countStart, countStop, intervalMs, isIncrement, }: CountdownOptions): [number, CountdownControllers];
|
|
73
192
|
|
|
193
|
+
/** The hook return type. */
|
|
74
194
|
type UseCounterReturn = {
|
|
195
|
+
/** The current count value. */
|
|
75
196
|
count: number;
|
|
197
|
+
/** Function to increment the counter by 1. */
|
|
76
198
|
increment: () => void;
|
|
199
|
+
/** Function to decrement the counter by 1. */
|
|
77
200
|
decrement: () => void;
|
|
201
|
+
/** Function to reset the counter to its initial value. */
|
|
78
202
|
reset: () => void;
|
|
203
|
+
/** Function to set a specific value to the counter. */
|
|
79
204
|
setCount: React.Dispatch<React.SetStateAction<number>>;
|
|
80
205
|
};
|
|
206
|
+
/**
|
|
207
|
+
* Custom hook that manages a counter with increment, decrement, reset, and setCount functionalities.
|
|
208
|
+
* @param {number} [initialValue] - The initial value for the counter.
|
|
209
|
+
* @returns {UseCounterReturn} An object containing the current count and functions to interact with the counter.
|
|
210
|
+
* @public
|
|
211
|
+
* @see [Documentation](/docs/use-counter)
|
|
212
|
+
* @example
|
|
213
|
+
* ```tsx
|
|
214
|
+
* const { count, increment, decrement, reset, setCount } = useCounter(5);
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
81
217
|
declare function useCounter(initialValue?: number): UseCounterReturn;
|
|
82
218
|
|
|
219
|
+
/** The hook options. */
|
|
83
220
|
type DarkModeOptions = {
|
|
221
|
+
/**
|
|
222
|
+
* The initial value of the dark mode.
|
|
223
|
+
* @default false
|
|
224
|
+
*/
|
|
84
225
|
defaultValue?: boolean;
|
|
226
|
+
/**
|
|
227
|
+
* The key to use in the local storage.
|
|
228
|
+
* @default 'sse-hooks-dark-mode'
|
|
229
|
+
*/
|
|
85
230
|
localStorageKey?: string;
|
|
231
|
+
/**
|
|
232
|
+
* If `true` (default), the hook will initialize reading `localStorage`.
|
|
233
|
+
* In SSR, you should set it to `false`, returning the `defaultValue` or `false` initially.
|
|
234
|
+
* @default true
|
|
235
|
+
*/
|
|
86
236
|
initializeWithValue?: boolean;
|
|
87
237
|
};
|
|
238
|
+
/** The hook return type. */
|
|
88
239
|
type DarkModeReturn = {
|
|
240
|
+
/** The current state of the dark mode. */
|
|
89
241
|
isDarkMode: boolean;
|
|
242
|
+
/** Function to toggle the dark mode. */
|
|
90
243
|
toggle: () => void;
|
|
244
|
+
/** Function to enable the dark mode. */
|
|
91
245
|
enable: () => void;
|
|
246
|
+
/** Function to disable the dark mode. */
|
|
92
247
|
disable: () => void;
|
|
248
|
+
/** Function to set a specific value to the dark mode. */
|
|
93
249
|
set: (value: boolean) => void;
|
|
94
250
|
};
|
|
251
|
+
/**
|
|
252
|
+
* Custom hook that returns the current state of the dark mode.
|
|
253
|
+
* @param {?DarkModeOptions} [options] - The initial value of the dark mode, default `false`.
|
|
254
|
+
* @returns {DarkModeReturn} An object containing the dark mode's state and its controllers.
|
|
255
|
+
* @public
|
|
256
|
+
* @see [Documentation](/docs/use-dark-mode)
|
|
257
|
+
* @example
|
|
258
|
+
* ```tsx
|
|
259
|
+
* const { isDarkMode, toggle, enable, disable, set } = useDarkMode({ defaultValue: true });
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
95
262
|
declare function useDarkMode(options?: DarkModeOptions): DarkModeReturn;
|
|
96
263
|
|
|
264
|
+
/** Configuration options for controlling the behavior of the debounced function. */
|
|
97
265
|
type DebounceOptions = {
|
|
266
|
+
/**
|
|
267
|
+
* Determines whether the function should be invoked on the leading edge of the timeout.
|
|
268
|
+
* @default false
|
|
269
|
+
*/
|
|
98
270
|
leading?: boolean;
|
|
271
|
+
/**
|
|
272
|
+
* Determines whether the function should be invoked on the trailing edge of the timeout.
|
|
273
|
+
* @default false
|
|
274
|
+
*/
|
|
99
275
|
trailing?: boolean;
|
|
276
|
+
/**
|
|
277
|
+
* The maximum time the specified function is allowed to be delayed before it is invoked.
|
|
278
|
+
*/
|
|
100
279
|
maxWait?: number;
|
|
101
280
|
};
|
|
281
|
+
/** Functions to manage a debounced callback. */
|
|
102
282
|
type ControlFunctions = {
|
|
283
|
+
/** Cancels pending function invocations. */
|
|
103
284
|
cancel: () => void;
|
|
285
|
+
/** Immediately invokes pending function invocations. */
|
|
104
286
|
flush: () => void;
|
|
287
|
+
/**
|
|
288
|
+
* Checks if there are any pending function invocations.
|
|
289
|
+
* @returns `true` if there are pending invocations, otherwise `false`.
|
|
290
|
+
*/
|
|
105
291
|
isPending: () => boolean;
|
|
106
292
|
};
|
|
293
|
+
/**
|
|
294
|
+
* Represents the state and control functions of a debounced callback.
|
|
295
|
+
* Subsequent calls to the debounced function return the result of the last invocation.
|
|
296
|
+
* Note: If there are no previous invocations, the result will be undefined.
|
|
297
|
+
* Ensure proper handling in your code.
|
|
298
|
+
*/
|
|
107
299
|
type DebouncedState<T extends (...args: any) => ReturnType<T>> = ((...args: Parameters<T>) => ReturnType<T> | undefined) & ControlFunctions;
|
|
300
|
+
/**
|
|
301
|
+
* Custom hook that creates a debounced version of a callback function.
|
|
302
|
+
* @template T - Type of the original callback function.
|
|
303
|
+
* @param {T} func - The callback function to be debounced.
|
|
304
|
+
* @param {number} delay - The delay in milliseconds before the callback is invoked (default is `500` milliseconds).
|
|
305
|
+
* @param {DebounceOptions} [options] - Options to control the behavior of the debounced function.
|
|
306
|
+
* @returns {DebouncedState<T>} A debounced version of the original callback along with control functions.
|
|
307
|
+
* @public
|
|
308
|
+
* @see [Documentation](/docs/use-debounce-callback)
|
|
309
|
+
* @example
|
|
310
|
+
* ```tsx
|
|
311
|
+
* const debouncedCallback = useDebounceCallback(
|
|
312
|
+
* (searchTerm) => {
|
|
313
|
+
* // Perform search after user stops typing for 500 milliseconds
|
|
314
|
+
* searchApi(searchTerm);
|
|
315
|
+
* },
|
|
316
|
+
* 500
|
|
317
|
+
* );
|
|
318
|
+
*
|
|
319
|
+
* // Later in the component
|
|
320
|
+
* debouncedCallback('react hooks'); // Will invoke the callback after 500 milliseconds of inactivity.
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
108
323
|
declare function useDebounceCallback<T extends (...args: any) => ReturnType<T>>(func: T, delay?: number, options?: DebounceOptions): DebouncedState<T>;
|
|
109
324
|
|
|
325
|
+
/**
|
|
326
|
+
* Hook options.
|
|
327
|
+
* @template T - The type of the value.
|
|
328
|
+
*/
|
|
110
329
|
type UseDebounceValueOptions<T> = {
|
|
330
|
+
/**
|
|
331
|
+
* Determines whether the function should be invoked on the leading edge of the timeout.
|
|
332
|
+
* @default false
|
|
333
|
+
*/
|
|
111
334
|
leading?: boolean;
|
|
335
|
+
/**
|
|
336
|
+
* Determines whether the function should be invoked on the trailing edge of the timeout.
|
|
337
|
+
* @default false
|
|
338
|
+
*/
|
|
112
339
|
trailing?: boolean;
|
|
340
|
+
/**
|
|
341
|
+
* The maximum time the specified function is allowed to be delayed before it is invoked.
|
|
342
|
+
*/
|
|
113
343
|
maxWait?: number;
|
|
344
|
+
/** A function to determine if the value has changed. Defaults to a function that checks if the value is strictly equal to the previous value. */
|
|
114
345
|
equalityFn?: (left: T, right: T) => boolean;
|
|
115
346
|
};
|
|
347
|
+
/**
|
|
348
|
+
* Custom hook that returns a debounced version of the provided value, along with a function to update it.
|
|
349
|
+
* @template T - The type of the value.
|
|
350
|
+
* @param {T | (() => T)} initialValue - The value to be debounced.
|
|
351
|
+
* @param {number} delay - The delay in milliseconds before the value is updated (default is 500ms).
|
|
352
|
+
* @param {object} [options] - Optional configurations for the debouncing behavior.
|
|
353
|
+
* @returns {[T, DebouncedState<(value: T) => void>]} An array containing the debounced value and the function to update it.
|
|
354
|
+
* @public
|
|
355
|
+
* @see [Documentation](/docs/use-debounce-value)
|
|
356
|
+
* @example
|
|
357
|
+
* ```tsx
|
|
358
|
+
* const [debouncedValue, updateDebouncedValue] = useDebounceValue(inputValue, 500, { leading: true });
|
|
359
|
+
* ```
|
|
360
|
+
*/
|
|
116
361
|
declare function useDebounceValue<T>(initialValue: T | (() => T), delay: number, options?: UseDebounceValueOptions<T>): [T, DebouncedState<(value: T) => void>];
|
|
117
362
|
|
|
363
|
+
/** Hook options. */
|
|
118
364
|
type UseDocumentTitleOptions = {
|
|
365
|
+
/** Whether to keep the title after unmounting the component (default is `true`). */
|
|
119
366
|
preserveTitleOnUnmount?: boolean;
|
|
120
367
|
};
|
|
368
|
+
/**
|
|
369
|
+
* Custom hook that sets the document title.
|
|
370
|
+
* @param {string} title - The title to set.
|
|
371
|
+
* @param {?UseDocumentTitleOptions} [options] - The options.
|
|
372
|
+
* @public
|
|
373
|
+
* @see [Documentation](/docs/use-document-title)
|
|
374
|
+
* @example
|
|
375
|
+
* ```tsx
|
|
376
|
+
* useDocumentTitle('My new title');
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
121
379
|
declare function useDocumentTitle(title: string, options?: UseDocumentTitleOptions): void;
|
|
122
380
|
|
|
381
|
+
/**
|
|
382
|
+
* Custom hook that creates a memoized event callback.
|
|
383
|
+
* @template Args - An array of argument types for the event callback.
|
|
384
|
+
* @template R - The return type of the event callback.
|
|
385
|
+
* @param {(...args: Args) => R} fn - The callback function.
|
|
386
|
+
* @returns {(...args: Args) => R} A memoized event callback function.
|
|
387
|
+
* @public
|
|
388
|
+
* @see [Documentation](/docs/use-event-callback)
|
|
389
|
+
* @example
|
|
390
|
+
* ```tsx
|
|
391
|
+
* const handleClick = useEventCallback((event) => {
|
|
392
|
+
* // Handle the event here
|
|
393
|
+
* });
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
123
396
|
declare function useEventCallback<Args extends unknown[], R>(fn: (...args: Args) => R): (...args: Args) => R;
|
|
124
397
|
declare function useEventCallback<Args extends unknown[], R>(fn: ((...args: Args) => R) | undefined): ((...args: Args) => R) | undefined;
|
|
125
398
|
|
|
126
|
-
declare function useEventListener<K extends keyof MediaQueryListEventMap>(eventName: K, handler: (event: MediaQueryListEventMap[K]) => void, element:
|
|
399
|
+
declare function useEventListener<K extends keyof MediaQueryListEventMap>(eventName: K, handler: (event: MediaQueryListEventMap[K]) => void, element: RefObject<MediaQueryList>, options?: boolean | AddEventListenerOptions): void;
|
|
127
400
|
declare function useEventListener<K extends keyof WindowEventMap>(eventName: K, handler: (event: WindowEventMap[K]) => void, element?: undefined, options?: boolean | AddEventListenerOptions): void;
|
|
128
|
-
declare function useEventListener<K extends keyof HTMLElementEventMap & keyof SVGElementEventMap, T extends Element = K extends keyof HTMLElementEventMap ? HTMLDivElement : SVGElement>(eventName: K, handler: ((event: HTMLElementEventMap[K]) => void) | ((event: SVGElementEventMap[K]) => void), element:
|
|
129
|
-
declare function useEventListener<K extends keyof DocumentEventMap>(eventName: K, handler: (event: DocumentEventMap[K]) => void, element:
|
|
401
|
+
declare function useEventListener<K extends keyof HTMLElementEventMap & keyof SVGElementEventMap, T extends Element = K extends keyof HTMLElementEventMap ? HTMLDivElement : SVGElement>(eventName: K, handler: ((event: HTMLElementEventMap[K]) => void) | ((event: SVGElementEventMap[K]) => void), element: RefObject<T>, options?: boolean | AddEventListenerOptions): void;
|
|
402
|
+
declare function useEventListener<K extends keyof DocumentEventMap>(eventName: K, handler: (event: DocumentEventMap[K]) => void, element: RefObject<Document>, options?: boolean | AddEventListenerOptions): void;
|
|
130
403
|
|
|
404
|
+
/**
|
|
405
|
+
* Options for customizing the behavior of the useFetch hook.
|
|
406
|
+
* Extends the standard [`RequestInit`](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit) interface.
|
|
407
|
+
*/
|
|
131
408
|
interface UseFetchOptions extends RequestInit {
|
|
409
|
+
/**
|
|
410
|
+
* If `true`, the fetch request will be executed immediately upon mounting or when the URL changes.
|
|
411
|
+
* @default false
|
|
412
|
+
*/
|
|
132
413
|
immediate?: boolean;
|
|
414
|
+
/**
|
|
415
|
+
* Callback function invoked when the request completes successfully.
|
|
416
|
+
* @param data - The parsed response data.
|
|
417
|
+
*/
|
|
133
418
|
onSuccess?: (data: any) => void;
|
|
419
|
+
/**
|
|
420
|
+
* Callback function invoked when the request fails.
|
|
421
|
+
* @param error - The error object.
|
|
422
|
+
*/
|
|
134
423
|
onError?: (error: Error) => void;
|
|
135
424
|
}
|
|
425
|
+
/**
|
|
426
|
+
* The state representation of the fetch request.
|
|
427
|
+
* @template T - The type of the data.
|
|
428
|
+
*/
|
|
136
429
|
interface UseFetchState<T> {
|
|
430
|
+
/** The data received from the response, or null if not yet received. */
|
|
137
431
|
data: T | null;
|
|
432
|
+
/** Indicates if the request is currently in progress. */
|
|
138
433
|
loading: boolean;
|
|
434
|
+
/** The error object if the request failed, or null if successful. */
|
|
139
435
|
error: Error | null;
|
|
140
436
|
}
|
|
437
|
+
/**
|
|
438
|
+
* The return value of the useFetch hook, including state and control methods.
|
|
439
|
+
* @template T - The type of the data.
|
|
440
|
+
*/
|
|
141
441
|
interface UseFetchReturn<T> extends UseFetchState<T> {
|
|
442
|
+
/**
|
|
443
|
+
* Manually triggers the fetch request.
|
|
444
|
+
* @param {string} [url] - Optional override URL.
|
|
445
|
+
* @param {RequestInit} [options] - Optional override options.
|
|
446
|
+
* @returns {Promise<T | null>} A promise that resolves to the data or null.
|
|
447
|
+
*/
|
|
142
448
|
execute: (url?: string, options?: RequestInit) => Promise<T | null>;
|
|
449
|
+
/** Aborts the current request if it is pending. */
|
|
143
450
|
abort: () => void;
|
|
451
|
+
/** Resets the state to its initial values (data: null, loading: false, error: null). */
|
|
144
452
|
reset: () => void;
|
|
145
453
|
}
|
|
454
|
+
/**
|
|
455
|
+
* Custom hook that provides a wrapper around the native [`fetch API`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to handle HTTP requests with state management, abort capability, and TypeScript support.
|
|
456
|
+
* @template T - The type of the data expected from the response.
|
|
457
|
+
* @param {string} [url] - The URL to fetch.
|
|
458
|
+
* @param {UseFetchOptions} [options] - Options for customizing the request and hook behavior (optional).
|
|
459
|
+
* @returns {UseFetchReturn<T>} An object containing the fetched data, loading status, error, and methods to control the request.
|
|
460
|
+
* @public
|
|
461
|
+
* @example
|
|
462
|
+
* ```tsx
|
|
463
|
+
* interface User {
|
|
464
|
+
* id: number;
|
|
465
|
+
* name: string;
|
|
466
|
+
* }
|
|
467
|
+
*
|
|
468
|
+
* const { data, loading, error, execute } = useFetch<User>('https://api.example.com/user/1', {
|
|
469
|
+
* immediate: true,
|
|
470
|
+
* onSuccess: (data) => console.log('User loaded:', data),
|
|
471
|
+
* });
|
|
472
|
+
* ```
|
|
473
|
+
*/
|
|
146
474
|
declare function useFetch<T = any>(url?: string, options?: UseFetchOptions): UseFetchReturn<T>;
|
|
147
475
|
declare function useGet<T = any>(url?: string, options?: UseFetchOptions): UseFetchReturn<T>;
|
|
148
476
|
declare function usePost<T = any>(url?: string, options?: UseFetchOptions): UseFetchReturn<T>;
|
|
149
477
|
declare function usePut<T = any>(url?: string, options?: UseFetchOptions): UseFetchReturn<T>;
|
|
150
478
|
declare function useDelete<T = any>(url?: string, options?: UseFetchOptions): UseFetchReturn<T>;
|
|
151
479
|
|
|
152
|
-
|
|
480
|
+
/**
|
|
481
|
+
* Custom hook that tracks whether a DOM element is being hovered over.
|
|
482
|
+
* @template T - The type of the DOM element. Defaults to `HTMLElement`.
|
|
483
|
+
* @param {RefObject<T>} elementRef - The ref object for the DOM element to track.
|
|
484
|
+
* @returns {boolean} A boolean value indicating whether the element is being hovered over.
|
|
485
|
+
* @public
|
|
486
|
+
* @see [Documentation](/docs/use-hover)
|
|
487
|
+
* @example
|
|
488
|
+
* ```tsx
|
|
489
|
+
* const buttonRef = useRef<HTMLButtonElement>(null);
|
|
490
|
+
* const isHovered = useHover(buttonRef);
|
|
491
|
+
* // Access the isHovered variable to determine if the button is being hovered over.
|
|
492
|
+
* ```
|
|
493
|
+
*/
|
|
494
|
+
declare function useHover<T extends HTMLElement = HTMLElement>(elementRef: RefObject<T>): boolean;
|
|
153
495
|
|
|
496
|
+
/**
|
|
497
|
+
* Options for configuring the IndexedDB connection and behavior.
|
|
498
|
+
*/
|
|
154
499
|
interface UseIndexedDBOptions {
|
|
500
|
+
/**
|
|
501
|
+
* The version of the database. Changing this triggers an upgrade.
|
|
502
|
+
* @default 1
|
|
503
|
+
*/
|
|
155
504
|
version?: number;
|
|
505
|
+
/**
|
|
506
|
+
* A callback function executed when the database version changes or is created.
|
|
507
|
+
* Use this to create or modify object stores.
|
|
508
|
+
* @param db - The IDBDatabase instance.
|
|
509
|
+
* @param oldVersion - The previous version of the database.
|
|
510
|
+
* @param newVersion - The new version of the database.
|
|
511
|
+
*/
|
|
156
512
|
onUpgradeNeeded?: (db: IDBDatabase, oldVersion: number, newVersion: number) => void;
|
|
157
513
|
}
|
|
514
|
+
/**
|
|
515
|
+
* The return value of the useIndexedDB hook, containing state and methods for interacting with the database.
|
|
516
|
+
* @template T - The type of the data stored.
|
|
517
|
+
*/
|
|
158
518
|
interface UseIndexedDBReturn<T> {
|
|
519
|
+
/** The most recently accessed or modified data item. */
|
|
159
520
|
data: T | null;
|
|
521
|
+
/** The error message if an operation failed, or null if successful. */
|
|
160
522
|
error: string | null;
|
|
523
|
+
/** Indicates if the database is initializing or an operation is in progress. */
|
|
161
524
|
loading: boolean;
|
|
525
|
+
/**
|
|
526
|
+
* Stores a value in the database under the specified key.
|
|
527
|
+
* @param key - The unique key for the item.
|
|
528
|
+
* @param value - The value to store.
|
|
529
|
+
*/
|
|
162
530
|
setItem: (key: string, value: T) => Promise<void>;
|
|
531
|
+
/**
|
|
532
|
+
* Retrieves a value from the database by its key.
|
|
533
|
+
* @param key - The key of the item to retrieve.
|
|
534
|
+
* @returns A promise that resolves to the item or null if not found.
|
|
535
|
+
*/
|
|
163
536
|
getItem: (key: string) => Promise<T | null>;
|
|
537
|
+
/**
|
|
538
|
+
* Removes an item from the database by its key.
|
|
539
|
+
* @param key - The key of the item to remove.
|
|
540
|
+
*/
|
|
164
541
|
removeItem: (key: string) => Promise<void>;
|
|
542
|
+
/**
|
|
543
|
+
* Removes all items from the current object store.
|
|
544
|
+
*/
|
|
165
545
|
clear: () => Promise<void>;
|
|
546
|
+
/**
|
|
547
|
+
* Retrieves all keys currently stored in the object store.
|
|
548
|
+
* @returns A promise that resolves to an array of keys.
|
|
549
|
+
*/
|
|
166
550
|
getAllKeys: () => Promise<string[]>;
|
|
167
551
|
}
|
|
552
|
+
/**
|
|
553
|
+
* Custom hook that provides an interface to the [`IndexedDB API`](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) for client-side storage of significant amounts of structured data.
|
|
554
|
+
* @template T - The type of the data to be stored.
|
|
555
|
+
* @param {string} databaseName - The name of the IndexedDB database.
|
|
556
|
+
* @param {string} storeName - The name of the object store within the database.
|
|
557
|
+
* @param {UseIndexedDBOptions} [options] - Configuration options for the database connection (optional).
|
|
558
|
+
* @returns {UseIndexedDBReturn<T>} An object containing the current data state, error state, loading state, and methods to interact with the database.
|
|
559
|
+
* @public
|
|
560
|
+
* @example
|
|
561
|
+
* ```tsx
|
|
562
|
+
* interface UserProfile {
|
|
563
|
+
* name: string;
|
|
564
|
+
* age: number;
|
|
565
|
+
* }
|
|
566
|
+
*
|
|
567
|
+
* const { setItem, getItem, data } = useIndexedDB<UserProfile>('myAppDB', 'profiles');
|
|
568
|
+
*
|
|
569
|
+
* const saveProfile = async () => {
|
|
570
|
+
* await setItem('user_1', { name: 'Alice', age: 30 });
|
|
571
|
+
* };
|
|
572
|
+
* ```
|
|
573
|
+
*/
|
|
168
574
|
declare function useIndexedDB<T>(databaseName: string, storeName: string, options?: UseIndexedDBOptions): UseIndexedDBReturn<T>;
|
|
169
575
|
|
|
576
|
+
/** Represents the options for configuring the Intersection Observer. */
|
|
170
577
|
type UseIntersectionObserverOptions = {
|
|
578
|
+
/**
|
|
579
|
+
* The element that is used as the viewport for checking visibility of the target.
|
|
580
|
+
* @default null
|
|
581
|
+
*/
|
|
171
582
|
root?: Element | Document | null;
|
|
583
|
+
/**
|
|
584
|
+
* A margin around the root.
|
|
585
|
+
* @default '0%'
|
|
586
|
+
*/
|
|
172
587
|
rootMargin?: string;
|
|
588
|
+
/**
|
|
589
|
+
* A threshold indicating the percentage of the target's visibility needed to trigger the callback.
|
|
590
|
+
* @default 0
|
|
591
|
+
*/
|
|
173
592
|
threshold?: number | number[];
|
|
593
|
+
/**
|
|
594
|
+
* If true, freezes the intersection state once the element becomes visible.
|
|
595
|
+
* @default false
|
|
596
|
+
*/
|
|
174
597
|
freezeOnceVisible?: boolean;
|
|
598
|
+
/**
|
|
599
|
+
* A callback function to be invoked when the intersection state changes.
|
|
600
|
+
* @param {boolean} isIntersecting - A boolean indicating if the element is intersecting.
|
|
601
|
+
* @param {IntersectionObserverEntry} entry - The intersection observer Entry.
|
|
602
|
+
* @default undefined
|
|
603
|
+
*/
|
|
175
604
|
onChange?: (isIntersecting: boolean, entry: IntersectionObserverEntry) => void;
|
|
605
|
+
/**
|
|
606
|
+
* The initial state of the intersection.
|
|
607
|
+
* @default false
|
|
608
|
+
*/
|
|
176
609
|
initialIsIntersecting?: boolean;
|
|
177
610
|
};
|
|
611
|
+
/**
|
|
612
|
+
* The return type of the useIntersectionObserver hook.
|
|
613
|
+
*
|
|
614
|
+
* Supports both tuple and object destructing.
|
|
615
|
+
* @param {(node: Element | null) => void} ref - The ref callback function.
|
|
616
|
+
* @param {boolean} isIntersecting - A boolean indicating if the element is intersecting.
|
|
617
|
+
* @param {IntersectionObserverEntry | undefined} entry - The intersection observer Entry.
|
|
618
|
+
*/
|
|
178
619
|
type IntersectionReturn = [
|
|
179
620
|
(node?: Element | null) => void,
|
|
180
621
|
boolean,
|
|
@@ -184,14 +625,80 @@ type IntersectionReturn = [
|
|
|
184
625
|
isIntersecting: boolean;
|
|
185
626
|
entry?: IntersectionObserverEntry;
|
|
186
627
|
};
|
|
628
|
+
/**
|
|
629
|
+
* Custom hook that tracks the intersection of a DOM element with its containing element or the viewport using the [`Intersection Observer API`](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
|
|
630
|
+
* @param {UseIntersectionObserverOptions} options - The options for the Intersection Observer.
|
|
631
|
+
* @returns {IntersectionReturn} The ref callback, a boolean indicating if the element is intersecting, and the intersection observer entry.
|
|
632
|
+
* @public
|
|
633
|
+
* @see [Documentation](/docs/use-intersection-observer)
|
|
634
|
+
* @example
|
|
635
|
+
* ```tsx
|
|
636
|
+
* // Example 1
|
|
637
|
+
* const [ref, isIntersecting, entry] = useIntersectionObserver({ threshold: 0.5 });
|
|
638
|
+
* ```
|
|
639
|
+
*
|
|
640
|
+
* ```tsx
|
|
641
|
+
* // Example 2
|
|
642
|
+
* const { ref, isIntersecting, entry } = useIntersectionObserver({ threshold: 0.5 });
|
|
643
|
+
* ```
|
|
644
|
+
*/
|
|
187
645
|
declare function useIntersectionObserver({ threshold, root, rootMargin, freezeOnceVisible, initialIsIntersecting, onChange, }?: UseIntersectionObserverOptions): IntersectionReturn;
|
|
188
646
|
|
|
647
|
+
/**
|
|
648
|
+
* Custom hook that creates an interval that invokes a callback function at a specified delay using the [`setInterval API`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval).
|
|
649
|
+
* @param {() => void} callback - The function to be invoked at each interval.
|
|
650
|
+
* @param {number | null} delay - The time, in milliseconds, between each invocation of the callback. Use `null` to clear the interval.
|
|
651
|
+
* @public
|
|
652
|
+
* @see [Documentation](/docs/use-interval)
|
|
653
|
+
* @example
|
|
654
|
+
* ```tsx
|
|
655
|
+
* const handleInterval = () => {
|
|
656
|
+
* // Code to be executed at each interval
|
|
657
|
+
* };
|
|
658
|
+
* useInterval(handleInterval, 1000);
|
|
659
|
+
* ```
|
|
660
|
+
*/
|
|
189
661
|
declare function useInterval(callback: () => void, delay: number | null): void;
|
|
190
662
|
|
|
663
|
+
/**
|
|
664
|
+
* Custom hook that determines if the code is running on the client side (in the browser).
|
|
665
|
+
* @returns {boolean} A boolean value indicating whether the code is running on the client side.
|
|
666
|
+
* @public
|
|
667
|
+
* @see [Documentation](/docs/use-is-client)
|
|
668
|
+
* @example
|
|
669
|
+
* ```tsx
|
|
670
|
+
* const isClient = useIsClient();
|
|
671
|
+
* // Use isClient to conditionally render or execute code specific to the client side.
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
191
674
|
declare function useIsClient(): boolean;
|
|
192
675
|
|
|
676
|
+
/**
|
|
677
|
+
* Custom hook that determines if the component is currently mounted.
|
|
678
|
+
* @returns {() => boolean} A function that returns a boolean value indicating whether the component is mounted.
|
|
679
|
+
* @public
|
|
680
|
+
* @see [Documentation](/docs/use-is-mounted)
|
|
681
|
+
* @example
|
|
682
|
+
* ```tsx
|
|
683
|
+
* const isComponentMounted = useIsMounted();
|
|
684
|
+
* // Use isComponentMounted() to check if the component is currently mounted before performing certain actions.
|
|
685
|
+
* ```
|
|
686
|
+
*/
|
|
193
687
|
declare function useIsMounted(): () => boolean;
|
|
194
688
|
|
|
689
|
+
/**
|
|
690
|
+
* Custom hook that uses either `useLayoutEffect` or `useEffect` based on the environment (client-side or server-side).
|
|
691
|
+
* @param {Function} effect - The effect function to be executed.
|
|
692
|
+
* @param {Array<any>} [dependencies] - An array of dependencies for the effect (optional).
|
|
693
|
+
* @public
|
|
694
|
+
* @see [Documentation](/docs/use-isomorphic-layout-effect)
|
|
695
|
+
* @example
|
|
696
|
+
* ```tsx
|
|
697
|
+
* useIsomorphicLayoutEffect(() => {
|
|
698
|
+
* // Code to be executed during the layout phase on the client side
|
|
699
|
+
* }, [dependency1, dependency2]);
|
|
700
|
+
* ```
|
|
701
|
+
*/
|
|
195
702
|
declare const useIsomorphicLayoutEffect: typeof useEffect;
|
|
196
703
|
|
|
197
704
|
declare global {
|
|
@@ -199,75 +706,261 @@ declare global {
|
|
|
199
706
|
"local-storage": CustomEvent;
|
|
200
707
|
}
|
|
201
708
|
}
|
|
709
|
+
/**
|
|
710
|
+
* Options for customizing the behavior of serialization and deserialization.
|
|
711
|
+
* @template T - The type of the state to be stored in local storage.
|
|
712
|
+
*/
|
|
202
713
|
type UseLocalStorageOptions<T> = {
|
|
714
|
+
/** A function to serialize the value before storing it. */
|
|
203
715
|
serializer?: (value: T) => string;
|
|
716
|
+
/** A function to deserialize the stored value. */
|
|
204
717
|
deserializer?: (value: string) => T;
|
|
718
|
+
/**
|
|
719
|
+
* If `true` (default), the hook will initialize reading the local storage. In SSR, you should set it to `false`, returning the initial value initially.
|
|
720
|
+
* @default true
|
|
721
|
+
*/
|
|
205
722
|
initializeWithValue?: boolean;
|
|
206
723
|
};
|
|
207
|
-
|
|
724
|
+
/**
|
|
725
|
+
* Custom hook that uses the [`localStorage API`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) to persist state across page reloads.
|
|
726
|
+
* @template T - The type of the state to be stored in local storage.
|
|
727
|
+
* @param {string} key - The key under which the value will be stored in local storage.
|
|
728
|
+
* @param {T | (() => T)} initialValue - The initial value of the state or a function that returns the initial value.
|
|
729
|
+
* @param {UseLocalStorageOptions<T>} [options] - Options for customizing the behavior of serialization and deserialization (optional).
|
|
730
|
+
* @returns {[T, Dispatch<SetStateAction<T>>, () => void]} A tuple containing the stored value, a function to set the value and a function to remove the key from storage.
|
|
731
|
+
* @public
|
|
732
|
+
* @see [Documentation](/docs/use-local-storage)
|
|
733
|
+
* @example
|
|
734
|
+
* ```tsx
|
|
735
|
+
* const [count, setCount, removeCount] = useLocalStorage('count', 0);
|
|
736
|
+
* // Access the `count` value, the `setCount` function to update it and `removeCount` function to remove the key from storage.
|
|
737
|
+
* ```
|
|
738
|
+
*/
|
|
739
|
+
declare function useLocalStorage<T>(key: string, initialValue: T | (() => T), options?: UseLocalStorageOptions<T>): [T, Dispatch<SetStateAction<T>>, () => void];
|
|
208
740
|
|
|
741
|
+
/**
|
|
742
|
+
* Represents the type for either a Map or an array of key-value pairs.
|
|
743
|
+
* @template K - The type of keys in the map.
|
|
744
|
+
* @template V - The type of values in the map.
|
|
745
|
+
*/
|
|
209
746
|
type MapOrEntries<K, V> = Map<K, V> | [K, V][];
|
|
747
|
+
/**
|
|
748
|
+
* Represents the actions available to interact with the map state.
|
|
749
|
+
* @template K - The type of keys in the map.
|
|
750
|
+
* @template V - The type of values in the map.
|
|
751
|
+
*/
|
|
210
752
|
type UseMapActions<K, V> = {
|
|
753
|
+
/** Set a key-value pair in the map. */
|
|
211
754
|
set: (key: K, value: V) => void;
|
|
755
|
+
/** Set all key-value pairs in the map. */
|
|
212
756
|
setAll: (entries: MapOrEntries<K, V>) => void;
|
|
757
|
+
/** Remove a key-value pair from the map. */
|
|
213
758
|
remove: (key: K) => void;
|
|
759
|
+
/** Reset the map to an empty state. */
|
|
214
760
|
reset: Map<K, V>["clear"];
|
|
215
761
|
};
|
|
762
|
+
/**
|
|
763
|
+
* Represents the return type of the `useMap` hook.
|
|
764
|
+
* We hide some setters from the returned map to disable autocompletion.
|
|
765
|
+
* @template K - The type of keys in the map.
|
|
766
|
+
* @template V - The type of values in the map.
|
|
767
|
+
*/
|
|
216
768
|
type UseMapReturn<K, V> = [
|
|
217
769
|
Omit<Map<K, V>, "set" | "clear" | "delete">,
|
|
218
770
|
UseMapActions<K, V>
|
|
219
771
|
];
|
|
772
|
+
/**
|
|
773
|
+
* Custom hook that manages a key-value [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) state with setter actions.
|
|
774
|
+
* @template K - The type of keys in the map.
|
|
775
|
+
* @template V - The type of values in the map.
|
|
776
|
+
* @param {MapOrEntries<K, V>} [initialState] - The initial state of the map as a Map or an array of key-value pairs (optional).
|
|
777
|
+
* @returns {UseMapReturn<K, V>} A tuple containing the map state and actions to interact with the map.
|
|
778
|
+
* @public
|
|
779
|
+
* @see [Documentation](/docs/use-map)
|
|
780
|
+
* @example
|
|
781
|
+
* ```tsx
|
|
782
|
+
* const [map, mapActions] = useMap();
|
|
783
|
+
* // Access the `map` state and use `mapActions` to set, remove, or reset entries.
|
|
784
|
+
* ```
|
|
785
|
+
*/
|
|
220
786
|
declare function useMap<K, V>(initialState?: MapOrEntries<K, V>): UseMapReturn<K, V>;
|
|
221
787
|
|
|
788
|
+
/** Hook options. */
|
|
222
789
|
type UseMediaQueryOptions = {
|
|
790
|
+
/**
|
|
791
|
+
* The default value to return if the hook is being run on the server.
|
|
792
|
+
* @default false
|
|
793
|
+
*/
|
|
223
794
|
defaultValue?: boolean;
|
|
795
|
+
/**
|
|
796
|
+
* If `true` (default), the hook will initialize reading the media query. In SSR, you should set it to `false`, returning `options.defaultValue` or `false` initially.
|
|
797
|
+
* @default true
|
|
798
|
+
*/
|
|
224
799
|
initializeWithValue?: boolean;
|
|
225
800
|
};
|
|
801
|
+
/**
|
|
802
|
+
* Custom hook that tracks the state of a media query using the [`Match Media API`](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia).
|
|
803
|
+
* @param {string} query - The media query to track.
|
|
804
|
+
* @param {?UseMediaQueryOptions} [options] - The options for customizing the behavior of the hook (optional).
|
|
805
|
+
* @returns {boolean} The current state of the media query (true if the query matches, false otherwise).
|
|
806
|
+
* @public
|
|
807
|
+
* @see [Documentation](/docs/use-media-query)
|
|
808
|
+
* @example
|
|
809
|
+
* ```tsx
|
|
810
|
+
* const isSmallScreen = useMediaQuery('(max-width: 600px)');
|
|
811
|
+
* // Use `isSmallScreen` to conditionally apply styles or logic based on the screen size.
|
|
812
|
+
* ```
|
|
813
|
+
*/
|
|
226
814
|
declare function useMediaQuery(query: string, { defaultValue, initializeWithValue, }?: UseMediaQueryOptions): boolean;
|
|
227
815
|
|
|
816
|
+
/**
|
|
817
|
+
* Represents the type for the options available when reading from local storage.
|
|
818
|
+
* @template T - The type of the stored value.
|
|
819
|
+
*/
|
|
228
820
|
type Options<T, InitializeWithValue extends boolean | undefined> = {
|
|
821
|
+
/** Custom deserializer function to convert the stored string value to the desired type (optional). */
|
|
229
822
|
deserializer?: (value: string) => T;
|
|
823
|
+
/** If `true` (default), the hook will initialize reading the local storage. In SSR, you should set it to `false`, returning `undefined` initially. */
|
|
230
824
|
initializeWithValue: InitializeWithValue;
|
|
231
825
|
};
|
|
232
826
|
declare function useReadLocalStorage<T>(key: string, options: Options<T, false>): T | null | undefined;
|
|
233
827
|
declare function useReadLocalStorage<T>(key: string, options?: Partial<Options<T, true>>): T | null;
|
|
234
828
|
|
|
829
|
+
/** The size of the observed element. */
|
|
235
830
|
type Size = {
|
|
831
|
+
/** The width of the observed element. */
|
|
236
832
|
width: number | undefined;
|
|
833
|
+
/** The height of the observed element. */
|
|
237
834
|
height: number | undefined;
|
|
238
835
|
};
|
|
836
|
+
/** The options for the ResizeObserver. */
|
|
239
837
|
type UseResizeObserverOptions<T extends HTMLElement = HTMLElement> = {
|
|
240
|
-
ref
|
|
838
|
+
/** The ref of the element to observe. */
|
|
839
|
+
ref: RefObject<T>;
|
|
840
|
+
/**
|
|
841
|
+
* When using `onResize`, the hook doesn't re-render on element size changes; it delegates handling to the provided callback.
|
|
842
|
+
* @default undefined
|
|
843
|
+
*/
|
|
241
844
|
onResize?: (size: Size) => void;
|
|
845
|
+
/**
|
|
846
|
+
* The box model to use for the ResizeObserver.
|
|
847
|
+
* @default 'content-box'
|
|
848
|
+
*/
|
|
242
849
|
box?: "border-box" | "content-box" | "device-pixel-content-box";
|
|
243
850
|
};
|
|
851
|
+
/**
|
|
852
|
+
* Custom hook that observes the size of an element using the [`ResizeObserver API`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver).
|
|
853
|
+
* @template T - The type of the element to observe.
|
|
854
|
+
* @param {UseResizeObserverOptions<T>} options - The options for the ResizeObserver.
|
|
855
|
+
* @returns {Size} - The size of the observed element.
|
|
856
|
+
* @public
|
|
857
|
+
* @see [Documentation](/docs/use-resize-observer)
|
|
858
|
+
* @example
|
|
859
|
+
* ```tsx
|
|
860
|
+
* const myRef = useRef(null);
|
|
861
|
+
* const { width = 0, height = 0 } = useResizeObserver({
|
|
862
|
+
* ref: myRef,
|
|
863
|
+
* box: 'content-box',
|
|
864
|
+
* });
|
|
865
|
+
*
|
|
866
|
+
* <div ref={myRef}>Hello, world!</div>
|
|
867
|
+
* ```
|
|
868
|
+
*/
|
|
244
869
|
declare function useResizeObserver<T extends HTMLElement = HTMLElement>(options: UseResizeObserverOptions<T>): Size;
|
|
245
870
|
|
|
871
|
+
/**
|
|
872
|
+
* The hooks options.
|
|
873
|
+
* @template InitializeWithValue - If `true` (default), the hook will initialize reading the screen dimensions. In SSR, you should set it to `false`, returning `undefined` initially.
|
|
874
|
+
*/
|
|
246
875
|
type UseScreenOptions<InitializeWithValue extends boolean | undefined> = {
|
|
876
|
+
/**
|
|
877
|
+
* If `true` (default), the hook will initialize reading the screen dimensions. In SSR, you should set it to `false`, returning `undefined` initially.
|
|
878
|
+
* @default true
|
|
879
|
+
*/
|
|
247
880
|
initializeWithValue: InitializeWithValue;
|
|
881
|
+
/**
|
|
882
|
+
* The delay in milliseconds before the state is updated (disabled by default for retro-compatibility).
|
|
883
|
+
* @default undefined
|
|
884
|
+
*/
|
|
248
885
|
debounceDelay?: number;
|
|
249
886
|
};
|
|
250
887
|
declare function useScreen(options: UseScreenOptions<false>): Screen | undefined;
|
|
251
888
|
declare function useScreen(options?: Partial<UseScreenOptions<true>>): Screen;
|
|
252
889
|
|
|
890
|
+
/** Script loading status. */
|
|
253
891
|
type UseScriptStatus = "idle" | "loading" | "ready" | "error";
|
|
892
|
+
/** Hook options. */
|
|
254
893
|
type UseScriptOptions = {
|
|
894
|
+
/** If `true`, prevents the script from being loaded (optional). */
|
|
255
895
|
shouldPreventLoad?: boolean;
|
|
896
|
+
/** If `true`, removes the script from the DOM when the component unmounts (optional). */
|
|
256
897
|
removeOnUnmount?: boolean;
|
|
898
|
+
/** Script's `id` (optional). */
|
|
257
899
|
id?: string;
|
|
258
900
|
};
|
|
901
|
+
/**
|
|
902
|
+
* Custom hook that dynamically loads scripts and tracking their loading status.
|
|
903
|
+
* @param {string | null} src - The source URL of the script to load. Set to `null` or omit to prevent loading (optional).
|
|
904
|
+
* @param {UseScriptOptions} [options] - Additional options for controlling script loading (optional).
|
|
905
|
+
* @returns {UseScriptStatus} The status of the script loading, which can be one of 'idle', 'loading', 'ready', or 'error'.
|
|
906
|
+
* @see [Documentation](/docs/use-script)
|
|
907
|
+
* @example
|
|
908
|
+
* const scriptStatus = useScript('https://example.com/script.js', { removeOnUnmount: true });
|
|
909
|
+
* // Access the status of the script loading (e.g., 'loading', 'ready', 'error').
|
|
910
|
+
*/
|
|
259
911
|
declare function useScript(src: string | null, options?: UseScriptOptions): UseScriptStatus;
|
|
260
912
|
|
|
913
|
+
/** Hook options. */
|
|
261
914
|
type UseScrollLockOptions = {
|
|
915
|
+
/**
|
|
916
|
+
* Whether to lock the scroll initially.
|
|
917
|
+
* @default true
|
|
918
|
+
*/
|
|
262
919
|
autoLock?: boolean;
|
|
920
|
+
/**
|
|
921
|
+
* The target element to lock the scroll (default is the body element).
|
|
922
|
+
* @default document.body
|
|
923
|
+
*/
|
|
263
924
|
lockTarget?: HTMLElement | string;
|
|
925
|
+
/**
|
|
926
|
+
* Whether to prevent width reflow when locking the scroll.
|
|
927
|
+
* @default true
|
|
928
|
+
*/
|
|
264
929
|
widthReflow?: boolean;
|
|
265
930
|
};
|
|
931
|
+
/** Hook return type. */
|
|
266
932
|
type UseScrollLockReturn = {
|
|
933
|
+
/** Whether the scroll is locked. */
|
|
267
934
|
isLocked: boolean;
|
|
935
|
+
/** Lock the scroll. */
|
|
268
936
|
lock: () => void;
|
|
937
|
+
/** Unlock the scroll. */
|
|
269
938
|
unlock: () => void;
|
|
270
939
|
};
|
|
940
|
+
/**
|
|
941
|
+
* A custom hook that locks and unlocks scroll.
|
|
942
|
+
* @param {UseScrollLockOptions} [options] - Options to configure the hook, by default it will lock the scroll automatically.
|
|
943
|
+
* @returns {UseScrollLockReturn} - An object containing the lock and unlock functions.
|
|
944
|
+
* @public
|
|
945
|
+
* @see [Documentation](/docs/use-scroll-lock)
|
|
946
|
+
* @example
|
|
947
|
+
* ```tsx
|
|
948
|
+
* // Lock the scroll when the modal is mounted, and unlock it when it's unmounted
|
|
949
|
+
* useScrollLock()
|
|
950
|
+
* ```
|
|
951
|
+
* @example
|
|
952
|
+
* ```tsx
|
|
953
|
+
* // Manually lock and unlock the scroll
|
|
954
|
+
* const { lock, unlock } = useScrollLock({ autoLock: false })
|
|
955
|
+
*
|
|
956
|
+
* return (
|
|
957
|
+
* <div>
|
|
958
|
+
* <button onClick={lock}>Lock</button>
|
|
959
|
+
* <button onClick={unlock}>Unlock</button>
|
|
960
|
+
* </div>
|
|
961
|
+
* )
|
|
962
|
+
* ```
|
|
963
|
+
*/
|
|
271
964
|
declare function useScrollLock(options?: UseScrollLockOptions): UseScrollLockReturn;
|
|
272
965
|
|
|
273
966
|
declare global {
|
|
@@ -275,53 +968,188 @@ declare global {
|
|
|
275
968
|
"session-storage": CustomEvent;
|
|
276
969
|
}
|
|
277
970
|
}
|
|
971
|
+
/**
|
|
972
|
+
* Represents the options for customizing the behavior of serialization and deserialization.
|
|
973
|
+
* @template T - The type of the state to be stored in session storage.
|
|
974
|
+
*/
|
|
278
975
|
type UseSessionStorageOptions<T> = {
|
|
976
|
+
/** A function to serialize the value before storing it. */
|
|
279
977
|
serializer?: (value: T) => string;
|
|
978
|
+
/** A function to deserialize the stored value. */
|
|
280
979
|
deserializer?: (value: string) => T;
|
|
980
|
+
/**
|
|
981
|
+
* If `true` (default), the hook will initialize reading the session storage. In SSR, you should set it to `false`, returning the initial value initially.
|
|
982
|
+
* @default true
|
|
983
|
+
*/
|
|
281
984
|
initializeWithValue?: boolean;
|
|
282
985
|
};
|
|
283
|
-
|
|
986
|
+
/**
|
|
987
|
+
* Custom hook that uses the [`sessionStorage API`](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage) to persist state across page reloads.
|
|
988
|
+
* @template T - The type of the state to be stored in session storage.
|
|
989
|
+
* @param {string} key - The key under which the value will be stored in session storage.
|
|
990
|
+
* @param {T | (() => T)} initialValue - The initial value of the state or a function that returns the initial value.
|
|
991
|
+
* @param {?UseSessionStorageOptions<T>} [options] - Options for customizing the behavior of serialization and deserialization (optional).
|
|
992
|
+
* @returns {[T, Dispatch<SetStateAction<T>>, () => void]} A tuple containing the stored value, a function to set the value and a function to remove the key from storage.
|
|
993
|
+
* @public
|
|
994
|
+
* @see [Documentation](/docs/use-session-storage)
|
|
995
|
+
* @example
|
|
996
|
+
* ```tsx
|
|
997
|
+
* const [count, setCount, removeCount] = useSessionStorage('count', 0);
|
|
998
|
+
* // Access the `count` value, the `setCount` function to update it and `removeCount` function to remove the key from storage.
|
|
999
|
+
* ```
|
|
1000
|
+
*/
|
|
1001
|
+
declare function useSessionStorage<T>(key: string, initialValue: T | (() => T), options?: UseSessionStorageOptions<T>): [T, Dispatch<SetStateAction<T>>, () => void];
|
|
284
1002
|
|
|
1003
|
+
/** Represents the second element of the output of the `useStep` hook. */
|
|
285
1004
|
type UseStepActions = {
|
|
1005
|
+
/** Go to the next step in the process. */
|
|
286
1006
|
goToNextStep: () => void;
|
|
1007
|
+
/** Go to the previous step in the process. */
|
|
287
1008
|
goToPrevStep: () => void;
|
|
1009
|
+
/** Reset the step to the initial step. */
|
|
288
1010
|
reset: () => void;
|
|
1011
|
+
/** Check if the next step is available. */
|
|
289
1012
|
canGoToNextStep: boolean;
|
|
1013
|
+
/** Check if the previous step is available. */
|
|
290
1014
|
canGoToPrevStep: boolean;
|
|
291
|
-
|
|
1015
|
+
/** Set the current step to a specific value. */
|
|
1016
|
+
setStep: Dispatch<SetStateAction<number>>;
|
|
292
1017
|
};
|
|
1018
|
+
/**
|
|
1019
|
+
* Custom hook that manages and navigates between steps in a multi-step process.
|
|
1020
|
+
* @param {number} maxStep - The maximum step in the process.
|
|
1021
|
+
* @returns {[number, UseStepActions]} An tuple containing the current step and helper functions for navigating steps.
|
|
1022
|
+
* @public
|
|
1023
|
+
* @see [Documentation](/docs/use-step)
|
|
1024
|
+
* @example
|
|
1025
|
+
* ```tsx
|
|
1026
|
+
* const [currentStep, { goToNextStep, goToPrevStep, reset, canGoToNextStep, canGoToPrevStep, setStep }] = useStep(3);
|
|
1027
|
+
* // Access and use the current step and provided helper functions.
|
|
1028
|
+
* ```
|
|
1029
|
+
*/
|
|
293
1030
|
declare function useStep(maxStep: number): [number, UseStepActions];
|
|
294
1031
|
|
|
1032
|
+
/** Ternary dark mode options. */
|
|
295
1033
|
type TernaryDarkMode = "system" | "dark" | "light";
|
|
1034
|
+
/** Options for the `useTernaryDarkMode` hook. */
|
|
296
1035
|
type TernaryDarkModeOptions = {
|
|
1036
|
+
/**
|
|
1037
|
+
* The default value for the dark mode.
|
|
1038
|
+
* @default 'system'
|
|
1039
|
+
*/
|
|
297
1040
|
defaultValue?: TernaryDarkMode;
|
|
1041
|
+
/**
|
|
1042
|
+
* The key for storing dark mode preference in local storage.
|
|
1043
|
+
* @default 'sse-hooks-ternary-dark-mode'
|
|
1044
|
+
*/
|
|
298
1045
|
localStorageKey?: string;
|
|
1046
|
+
/**
|
|
1047
|
+
* If `true` (default), the hook will initialize reading `localStorage`. In SSR, you should set it to `false`, returning default values initially.
|
|
1048
|
+
* @default true
|
|
1049
|
+
*/
|
|
299
1050
|
initializeWithValue?: boolean;
|
|
300
1051
|
};
|
|
1052
|
+
/** Represents the return type of the `useTernaryDarkMode` hook. */
|
|
301
1053
|
type TernaryDarkModeReturn = {
|
|
1054
|
+
/** The current state of the dark mode. */
|
|
302
1055
|
isDarkMode: boolean;
|
|
1056
|
+
/** The current state of the dark mode. */
|
|
303
1057
|
ternaryDarkMode: TernaryDarkMode;
|
|
304
|
-
|
|
1058
|
+
/** A function to set the dark mode state. */
|
|
1059
|
+
setTernaryDarkMode: Dispatch<SetStateAction<TernaryDarkMode>>;
|
|
1060
|
+
/** A function to toggle the dark mode state. */
|
|
305
1061
|
toggleTernaryDarkMode: () => void;
|
|
306
1062
|
};
|
|
1063
|
+
/**
|
|
1064
|
+
* Custom hook that manages ternary (system, dark, light) dark mode with local storage support.
|
|
1065
|
+
* @param {?TernaryDarkModeOptions | string} [options] - Options or the local storage key for the hook.
|
|
1066
|
+
* @returns {TernaryDarkModeReturn} An object containing the dark mode state and helper functions.
|
|
1067
|
+
* @public
|
|
1068
|
+
* @see [Documentation](/docs/use-ternary-dark-mode)
|
|
1069
|
+
* @example
|
|
1070
|
+
* ```tsx
|
|
1071
|
+
* const { isDarkMode, ternaryDarkMode, setTernaryDarkMode, toggleTernaryDarkMode } = useTernaryDarkMode({ defaultValue: 'dark' });
|
|
1072
|
+
* // Access and use the dark mode state and provided helper functions.
|
|
1073
|
+
* ```
|
|
1074
|
+
*/
|
|
307
1075
|
declare function useTernaryDarkMode({ defaultValue, localStorageKey, initializeWithValue, }?: TernaryDarkModeOptions): TernaryDarkModeReturn;
|
|
308
1076
|
|
|
1077
|
+
/**
|
|
1078
|
+
* Custom hook that handles timeouts in React components using the [`setTimeout API`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout).
|
|
1079
|
+
* @param {() => void} callback - The function to be executed when the timeout elapses.
|
|
1080
|
+
* @param {number | null} delay - The duration (in milliseconds) for the timeout. Set to `null` to clear the timeout.
|
|
1081
|
+
* @returns {void} This hook does not return anything.
|
|
1082
|
+
* @public
|
|
1083
|
+
* @see [Documentation](/docs/use-timeout)
|
|
1084
|
+
* @example
|
|
1085
|
+
* ```tsx
|
|
1086
|
+
* // Usage of useTimeout hook
|
|
1087
|
+
* useTimeout(() => {
|
|
1088
|
+
* // Code to be executed after the specified delay
|
|
1089
|
+
* }, 1000); // Set a timeout of 1000 milliseconds (1 second)
|
|
1090
|
+
* ```
|
|
1091
|
+
*/
|
|
309
1092
|
declare function useTimeout(callback: () => void, delay: number | null): void;
|
|
310
1093
|
|
|
311
|
-
|
|
1094
|
+
/**
|
|
1095
|
+
* Custom hook that manages a boolean toggle state in React components.
|
|
1096
|
+
* @param {boolean} [defaultValue] - The initial value for the toggle state.
|
|
1097
|
+
* @returns {[boolean, () => void, Dispatch<SetStateAction<boolean>>]} A tuple containing the current state,
|
|
1098
|
+
* a function to toggle the state, and a function to set the state explicitly.
|
|
1099
|
+
* @public
|
|
1100
|
+
* @see [Documentation](/docs/use-toggle)
|
|
1101
|
+
* @example
|
|
1102
|
+
* ```tsx
|
|
1103
|
+
* const [isToggled, toggle, setToggle] = useToggle(); // Initial value is false
|
|
1104
|
+
* // OR
|
|
1105
|
+
* const [isToggled, toggle, setToggle] = useToggle(true); // Initial value is true
|
|
1106
|
+
* // Use isToggled in your component, toggle to switch the state, setToggle to set the state explicitly.
|
|
1107
|
+
* ```
|
|
1108
|
+
*/
|
|
1109
|
+
declare function useToggle(defaultValue?: boolean): [boolean, () => void, Dispatch<SetStateAction<boolean>>];
|
|
312
1110
|
|
|
1111
|
+
/**
|
|
1112
|
+
* Custom hook that runs a cleanup function when the component is unmounted.
|
|
1113
|
+
* @param {() => void} func - The cleanup function to be executed on unmount.
|
|
1114
|
+
* @public
|
|
1115
|
+
* @see [Documentation](/docs/use-unmount)
|
|
1116
|
+
* @example
|
|
1117
|
+
* ```tsx
|
|
1118
|
+
* useUnmount(() => {
|
|
1119
|
+
* // Cleanup logic here
|
|
1120
|
+
* });
|
|
1121
|
+
* ```
|
|
1122
|
+
*/
|
|
313
1123
|
declare function useUnmount(func: () => void): void;
|
|
314
1124
|
|
|
1125
|
+
/**
|
|
1126
|
+
* Represent the dimension of the window.
|
|
1127
|
+
* @template T - The type of the dimension (number or undefined).
|
|
1128
|
+
*/
|
|
315
1129
|
type WindowSize<T extends number | undefined = number | undefined> = {
|
|
1130
|
+
/** The width of the window. */
|
|
316
1131
|
width: T;
|
|
1132
|
+
/** The height of the window. */
|
|
317
1133
|
height: T;
|
|
318
1134
|
};
|
|
1135
|
+
/**
|
|
1136
|
+
* Hook options.
|
|
1137
|
+
* @template InitializeWithValue - If `true` (default), the hook will initialize reading the window size. In SSR, you should set it to `false`, returning `undefined` initially.
|
|
1138
|
+
*/
|
|
319
1139
|
type UseWindowSizeOptions<InitializeWithValue extends boolean | undefined> = {
|
|
1140
|
+
/**
|
|
1141
|
+
* If `true` (default), the hook will initialize reading the window size. In SSR, you should set it to `false`, returning `undefined` initially.
|
|
1142
|
+
* @default true
|
|
1143
|
+
*/
|
|
320
1144
|
initializeWithValue: InitializeWithValue;
|
|
1145
|
+
/**
|
|
1146
|
+
* The delay in milliseconds before the state is updated (disabled by default for retro-compatibility).
|
|
1147
|
+
* @default undefined
|
|
1148
|
+
*/
|
|
321
1149
|
debounceDelay?: number;
|
|
322
1150
|
};
|
|
323
1151
|
declare function useWindowSize(options: UseWindowSizeOptions<false>): WindowSize;
|
|
324
1152
|
declare function useWindowSize(options?: Partial<UseWindowSizeOptions<true>>): WindowSize<number>;
|
|
325
1153
|
|
|
326
|
-
export { useBoolean, useCopyToClipboard, useCountdown, useCounter, useDarkMode, useDebounceCallback, useDebounceValue, useDelete, useDocumentTitle, useEventCallback, useEventListener, useFetch, useGet, useHover, useIndexedDB, useIntersectionObserver, useInterval, useIsClient, useIsMounted, useIsomorphicLayoutEffect, useLocalStorage, useMap, useMediaQuery, usePost, usePut, useReadLocalStorage, useResizeObserver, useScreen, useScript, useScrollLock, useSessionStorage, useStep, useTernaryDarkMode, useTimeout, useToggle, useUnmount, useWindowSize };
|
|
327
|
-
export type { DebouncedState, TernaryDarkMode, TernaryDarkModeOptions, TernaryDarkModeReturn, UseFetchOptions, UseFetchReturn, UseFetchState };
|
|
1154
|
+
export { useBoolean, useClickAnyWhere, useCookie, useCopyToClipboard, useCountdown, useCounter, useDarkMode, useDebounceCallback, useDebounceValue, useDelete, useDocumentTitle, useEventCallback, useEventListener, useFetch, useGet, useHover, useIndexedDB, useIntersectionObserver, useInterval, useIsClient, useIsMounted, useIsomorphicLayoutEffect, useLocalStorage, useMap, useMediaQuery, usePost, usePut, useReadLocalStorage, useResizeObserver, useScreen, useScript, useScrollLock, useSessionStorage, useStep, useTernaryDarkMode, useTimeout, useToggle, useUnmount, useWindowSize };
|
|
1155
|
+
export type { DebouncedState, TernaryDarkMode, TernaryDarkModeOptions, TernaryDarkModeReturn, UseCookieOptions, UseFetchOptions, UseFetchReturn, UseFetchState };
|