@alessiofrittoli/react-hooks 1.2.0 → 2.0.1
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 +387 -73
- package/dist/eslint.js +1 -1
- package/dist/eslint.mjs +1 -1
- package/dist/index.d.mts +940 -11
- package/dist/index.d.ts +940 -11
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +26 -34
- package/dist/browser-api/index.d.mts +0 -6
- package/dist/browser-api/index.d.ts +0 -6
- package/dist/browser-api/index.js +0 -1
- package/dist/browser-api/index.mjs +0 -1
- package/dist/browser-api/storage/index.d.mts +0 -4
- package/dist/browser-api/storage/index.d.ts +0 -4
- package/dist/browser-api/storage/index.js +0 -1
- package/dist/browser-api/storage/index.mjs +0 -1
- package/dist/browser-api/storage/useLocalStorage.d.mts +0 -11
- package/dist/browser-api/storage/useLocalStorage.d.ts +0 -11
- package/dist/browser-api/storage/useLocalStorage.js +0 -1
- package/dist/browser-api/storage/useLocalStorage.mjs +0 -1
- package/dist/browser-api/storage/useSessionStorage.d.mts +0 -11
- package/dist/browser-api/storage/useSessionStorage.d.ts +0 -11
- package/dist/browser-api/storage/useSessionStorage.js +0 -1
- package/dist/browser-api/storage/useSessionStorage.mjs +0 -1
- package/dist/browser-api/storage/useStorage.d.mts +0 -12
- package/dist/browser-api/storage/useStorage.d.ts +0 -12
- package/dist/browser-api/storage/useStorage.js +0 -1
- package/dist/browser-api/storage/useStorage.mjs +0 -1
- package/dist/browser-api/useIsPortrait.d.mts +0 -10
- package/dist/browser-api/useIsPortrait.d.ts +0 -10
- package/dist/browser-api/useIsPortrait.js +0 -1
- package/dist/browser-api/useIsPortrait.mjs +0 -1
- package/dist/browser-api/useMediaQuery.d.mts +0 -11
- package/dist/browser-api/useMediaQuery.d.ts +0 -11
- package/dist/browser-api/useMediaQuery.js +0 -1
- package/dist/browser-api/useMediaQuery.mjs +0 -1
- package/dist/chunk-26XSIIUK.mjs +0 -1
- package/dist/chunk-2CZNCZFR.mjs +0 -0
- package/dist/chunk-43RVMR6S.mjs +0 -1
- package/dist/chunk-4BGMCO3F.js +0 -1
- package/dist/chunk-65SF33P4.mjs +0 -1
- package/dist/chunk-6BSD56XC.js +0 -1
- package/dist/chunk-6IRIG5HU.mjs +0 -1
- package/dist/chunk-7ILYMOVB.js +0 -1
- package/dist/chunk-AJKPRTOJ.mjs +0 -1
- package/dist/chunk-EFFJB2GR.js +0 -1
- package/dist/chunk-FUCUVFLW.js +0 -1
- package/dist/chunk-G55NP3QG.js +0 -1
- package/dist/chunk-HO57SPST.js +0 -1
- package/dist/chunk-HZLWSH3W.mjs +0 -1
- package/dist/chunk-IDNLFWFO.js +0 -1
- package/dist/chunk-RENUP2VU.mjs +0 -1
- package/dist/chunk-SNMJ53QB.js +0 -1
- package/dist/chunk-SW7BCMZF.js +0 -1
- package/dist/chunk-TA6PCHH4.js +0 -1
- package/dist/chunk-UAJXVBNR.mjs +0 -1
- package/dist/chunk-UXNKH2MC.mjs +0 -1
- package/dist/chunk-VLSP3MAN.js +0 -1
- package/dist/chunk-WLIFJNZA.mjs +0 -0
- package/dist/chunk-WOVEE75M.js +0 -1
- package/dist/chunk-YAZXPL6I.mjs +0 -1
- package/dist/chunk-YLUTFORB.js +0 -1
- package/dist/chunk-YQO25XKN.mjs +0 -0
- package/dist/chunk-ZNAB4TYW.mjs +0 -0
- package/dist/dom-api/index.d.mts +0 -2
- package/dist/dom-api/index.d.ts +0 -2
- package/dist/dom-api/index.js +0 -1
- package/dist/dom-api/index.mjs +0 -1
- package/dist/dom-api/useFocusTrap.d.mts +0 -15
- package/dist/dom-api/useFocusTrap.d.ts +0 -15
- package/dist/dom-api/useFocusTrap.js +0 -1
- package/dist/dom-api/useFocusTrap.mjs +0 -1
- package/dist/dom-api/useScrollBlock.d.mts +0 -8
- package/dist/dom-api/useScrollBlock.d.ts +0 -8
- package/dist/dom-api/useScrollBlock.js +0 -1
- package/dist/dom-api/useScrollBlock.mjs +0 -1
- package/dist/misc/index.d.mts +0 -3
- package/dist/misc/index.d.ts +0 -3
- package/dist/misc/index.js +0 -1
- package/dist/misc/index.mjs +0 -1
- package/dist/misc/useIsClient.d.mts +0 -8
- package/dist/misc/useIsClient.d.ts +0 -8
- package/dist/misc/useIsClient.js +0 -1
- package/dist/misc/useIsClient.mjs +0 -1
- package/dist/misc/useIsFirstRender.d.mts +0 -9
- package/dist/misc/useIsFirstRender.d.ts +0 -9
- package/dist/misc/useIsFirstRender.js +0 -1
- package/dist/misc/useIsFirstRender.mjs +0 -1
- package/dist/misc/usePagination.d.mts +0 -12
- package/dist/misc/usePagination.d.ts +0 -12
- package/dist/misc/usePagination.js +0 -1
- package/dist/misc/usePagination.mjs +0 -1
- package/dist/misc/useUpdateEffect.d.mts +0 -9
- package/dist/misc/useUpdateEffect.d.ts +0 -9
- package/dist/misc/useUpdateEffect.js +0 -1
- package/dist/misc/useUpdateEffect.mjs +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,11 +1,940 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type Value<T> = T | undefined | null;
|
|
4
|
+
type SetValue<T> = React.Dispatch<React.SetStateAction<T>>;
|
|
5
|
+
/**
|
|
6
|
+
* Easly handle Local or Session Storage State.
|
|
7
|
+
*
|
|
8
|
+
* @param key The storage item key.
|
|
9
|
+
* @param initial The storage item initial value.
|
|
10
|
+
* @param type ( Optional ) The storage API to use. Default: `local`.
|
|
11
|
+
*/
|
|
12
|
+
declare const useStorage: <T = string>(key: string, initial?: T, type?: "local" | "session") => [Value<T>, SetValue<Value<T>>];
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* useLocalStorage hook.
|
|
16
|
+
*
|
|
17
|
+
* @param key The local storage item key.
|
|
18
|
+
* @param initial The local storage item initial value.
|
|
19
|
+
*/
|
|
20
|
+
declare const useLocalStorage: <T = string>(key: string, initial?: T) => [T | null | undefined, (value: react.SetStateAction<T | null | undefined>) => void];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* useSessionStorage hook.
|
|
24
|
+
*
|
|
25
|
+
* @param key The session item key.
|
|
26
|
+
* @param initial The session item initial value.
|
|
27
|
+
*/
|
|
28
|
+
declare const useSessionStorage: <T = string>(key: string, initial?: T) => [T | null | undefined, (value: react.SetStateAction<T | null | undefined>) => void];
|
|
29
|
+
|
|
30
|
+
interface UseDarkModeOutput {
|
|
31
|
+
/**
|
|
32
|
+
* Indicates whether dark mode is enabled.
|
|
33
|
+
*
|
|
34
|
+
*/
|
|
35
|
+
isDarkMode: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Indicates whether the current device prefers dark theme.
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
isDarkOS: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Toggle dark mode.
|
|
43
|
+
*
|
|
44
|
+
*/
|
|
45
|
+
toggleDarkMode: () => void;
|
|
46
|
+
/**
|
|
47
|
+
* Enable dark mode.
|
|
48
|
+
*
|
|
49
|
+
*/
|
|
50
|
+
enableDarkMode: () => void;
|
|
51
|
+
/**
|
|
52
|
+
* Disable dark mode.
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
disableDarkMode: () => void;
|
|
56
|
+
}
|
|
57
|
+
interface UseDarkModeOptions {
|
|
58
|
+
/**
|
|
59
|
+
* The fallback value to use if no preference is saved in `localStorage`.
|
|
60
|
+
*
|
|
61
|
+
* @default
|
|
62
|
+
*
|
|
63
|
+
* true // if device prefers dark color scheme.
|
|
64
|
+
* false // if device prefers light color scheme.
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
initial?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Array of class names to toggle on the `<html>` element, e.g. `['dark', 'light']`.
|
|
70
|
+
*
|
|
71
|
+
*/
|
|
72
|
+
docClassNames?: [dark: string, light: string];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Easily manage dark mode with full respect for user device preferences.
|
|
76
|
+
*
|
|
77
|
+
* This hook is user-oriented and built to honor system-level color scheme settings:
|
|
78
|
+
*
|
|
79
|
+
* - If the device prefers a dark color scheme, dark mode is automatically enabled on first load.
|
|
80
|
+
* - If the user enables/disables dark mode via a web widget, the preference is stored in `localStorage` under the key `dark-mode`.
|
|
81
|
+
* - If the device color scheme preference changes (e.g. via OS settings), that change takes precedence and is stored for future visits.
|
|
82
|
+
*
|
|
83
|
+
* @param options Configuration object for the hook. See {@link UseDarkModeOptions} for more info.
|
|
84
|
+
*
|
|
85
|
+
* @returns An object containing utilities for managing dark mode.
|
|
86
|
+
*/
|
|
87
|
+
declare const useDarkMode: (options?: UseDarkModeOptions) => UseDarkModeOutput;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Specifies characteristics about the event listener.
|
|
91
|
+
*
|
|
92
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#options)
|
|
93
|
+
*/
|
|
94
|
+
interface AddEventListenerOptions {
|
|
95
|
+
/**
|
|
96
|
+
* A boolean value indicating that events of this type will be dispatched to the registered
|
|
97
|
+
* listener before being dispatched to any `EventTarget`beneath it in the DOM tree.
|
|
98
|
+
*
|
|
99
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture)
|
|
100
|
+
*
|
|
101
|
+
* @default false
|
|
102
|
+
*/
|
|
103
|
+
capture?: boolean;
|
|
104
|
+
/**
|
|
105
|
+
* A boolean value indicating that the listener should be invoked at most once after being added.
|
|
106
|
+
* If `true`, the listener would be automatically removed when invoked.
|
|
107
|
+
*
|
|
108
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#once)
|
|
109
|
+
*
|
|
110
|
+
* @default false
|
|
111
|
+
*/
|
|
112
|
+
once?: boolean;
|
|
113
|
+
/**
|
|
114
|
+
* A boolean value that, if `true`, indicates that the function specified by `listener`
|
|
115
|
+
* will never call [`preventDefault()`](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault).
|
|
116
|
+
* If a passive listener calls `preventDefault()`, nothing will happen and a console warning may be generated.
|
|
117
|
+
*
|
|
118
|
+
* If this option is not specified it defaults to `false` – except that in browsers other than Safari,
|
|
119
|
+
* it defaults to `true` for [`wheel`](https://developer.mozilla.org/en-US/docs/Web/API/Element/wheel_event),
|
|
120
|
+
* [`mousewheel`](https://developer.mozilla.org/en-US/docs/Web/API/Element/mousewheel_event),
|
|
121
|
+
* [`touchstart`](https://developer.mozilla.org/en-US/docs/Web/API/Element/touchstart_event) and
|
|
122
|
+
* [`touchmove`](https://developer.mozilla.org/en-US/docs/Web/API/Element/touchmove_event) events.
|
|
123
|
+
*
|
|
124
|
+
* See [Using passive listeners](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#using_passive_listeners) to learn more.
|
|
125
|
+
*
|
|
126
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#passive)
|
|
127
|
+
*/
|
|
128
|
+
passive?: boolean;
|
|
129
|
+
/**
|
|
130
|
+
* An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal).
|
|
131
|
+
* The listener will be removed when the
|
|
132
|
+
* [`abort()`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort) method
|
|
133
|
+
* of the [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) which
|
|
134
|
+
* owns the `AbortSignal` is called. If not specified, no `AbortSignal` is associated with the listener.
|
|
135
|
+
*
|
|
136
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#signal)
|
|
137
|
+
*/
|
|
138
|
+
signal?: AbortSignal;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The `addEventListener` options.
|
|
142
|
+
*
|
|
143
|
+
* - If `boolean` is given, indicates whether events of this type will be dispatched to the registered
|
|
144
|
+
* listener before being dispatched to any EventTarget beneath it in the DOM tree. See [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture).
|
|
145
|
+
* - If an object is given, it specifies characteristics about the event listener. See [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#options).
|
|
146
|
+
*/
|
|
147
|
+
type ListenerOptions = boolean | AddEventListenerOptions;
|
|
148
|
+
/**
|
|
149
|
+
* The Window Event listener.
|
|
150
|
+
*
|
|
151
|
+
* @param event The event object that implements the [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event) interface.
|
|
152
|
+
*/
|
|
153
|
+
type WindowEventListener<K extends keyof WindowEventMap> = (event: WindowEventMap[K]) => void;
|
|
154
|
+
/**
|
|
155
|
+
* The Document Event listener.
|
|
156
|
+
*
|
|
157
|
+
* @param event The event object that implements the [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event) interface.
|
|
158
|
+
*/
|
|
159
|
+
type DocumentEventListener<K extends keyof DocumentEventMap> = (event: DocumentEventMap[K]) => void;
|
|
160
|
+
/**
|
|
161
|
+
* The MediaQueryList Event listener.
|
|
162
|
+
*
|
|
163
|
+
* @param event The [`MediaQueryListEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryListEvent) object.
|
|
164
|
+
*/
|
|
165
|
+
type MediaQueryEventListener<K extends keyof MediaQueryListEventMap> = (event: MediaQueryListEventMap[K]) => void;
|
|
166
|
+
/**
|
|
167
|
+
* The MediaQueryList "change" Event listener.
|
|
168
|
+
*
|
|
169
|
+
* @param event The [`MediaQueryListEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryListEvent) object.
|
|
170
|
+
*/
|
|
171
|
+
type MediaQueryChangeListener = (event: MediaQueryListEventMap['change']) => void;
|
|
172
|
+
/**
|
|
173
|
+
* The HTMLElement Event listener.
|
|
174
|
+
*
|
|
175
|
+
* @param event The event object that implements the [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event) interface.
|
|
176
|
+
*/
|
|
177
|
+
type ElementEventListener<K extends keyof HTMLElementEventMap> = (event: HTMLElementEventMap[K]) => void;
|
|
178
|
+
interface CommonListenerOptions {
|
|
179
|
+
/**
|
|
180
|
+
* A custom callback executed before event listener get attached.
|
|
181
|
+
*
|
|
182
|
+
*/
|
|
183
|
+
onLoad?: () => void;
|
|
184
|
+
/**
|
|
185
|
+
* A custom callback executed after event listener get removed.
|
|
186
|
+
*
|
|
187
|
+
*/
|
|
188
|
+
onCleanUp?: () => void;
|
|
189
|
+
/**
|
|
190
|
+
* Specifies characteristics about the event listener.
|
|
191
|
+
*
|
|
192
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#options)
|
|
193
|
+
*/
|
|
194
|
+
options?: ListenerOptions;
|
|
195
|
+
}
|
|
196
|
+
interface WindowListenerOptions<K extends keyof WindowEventMap> extends CommonListenerOptions {
|
|
197
|
+
/**
|
|
198
|
+
* The Window Event listener.
|
|
199
|
+
*
|
|
200
|
+
* @param event The event object that implements the [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event) interface.
|
|
201
|
+
*/
|
|
202
|
+
listener: WindowEventListener<K>;
|
|
203
|
+
}
|
|
204
|
+
interface DocumentListenerOptions<K extends keyof DocumentEventMap> extends CommonListenerOptions {
|
|
205
|
+
/**
|
|
206
|
+
* The `Document` reference or a React RefObject of the `Document`.
|
|
207
|
+
*
|
|
208
|
+
*/
|
|
209
|
+
target: Document | null | React.RefObject<Document | null>;
|
|
210
|
+
/**
|
|
211
|
+
* The Document Event listener.
|
|
212
|
+
*
|
|
213
|
+
* @param event The event object that implements the [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event) interface.
|
|
214
|
+
*/
|
|
215
|
+
listener: DocumentEventListener<K>;
|
|
216
|
+
}
|
|
217
|
+
interface MediaQueryListenerOptions extends CommonListenerOptions {
|
|
218
|
+
/**
|
|
219
|
+
* The Media Query string to check.
|
|
220
|
+
*
|
|
221
|
+
*/
|
|
222
|
+
query: string;
|
|
223
|
+
/**
|
|
224
|
+
* The MediaQueryList Event listener.
|
|
225
|
+
*
|
|
226
|
+
* @param event The [`MediaQueryListEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryListEvent) object.
|
|
227
|
+
*/
|
|
228
|
+
listener: MediaQueryChangeListener;
|
|
229
|
+
}
|
|
230
|
+
interface ElementListenerOptions<T extends HTMLElement, K extends keyof HTMLElementEventMap> extends CommonListenerOptions {
|
|
231
|
+
/**
|
|
232
|
+
* The React RefObject of the target where the listener get attached to.
|
|
233
|
+
*
|
|
234
|
+
*/
|
|
235
|
+
target: T | React.RefObject<T | null>;
|
|
236
|
+
/**
|
|
237
|
+
* The HTMLElement Event listener.
|
|
238
|
+
*
|
|
239
|
+
* @param event The event object that implements the [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event) interface.
|
|
240
|
+
*/
|
|
241
|
+
listener: ElementEventListener<K>;
|
|
242
|
+
}
|
|
243
|
+
interface CustomEventListenerOptions<T extends Record<string, Event>, K extends keyof T = keyof T> extends CommonListenerOptions {
|
|
244
|
+
/**
|
|
245
|
+
* The target where the listener get attached to.
|
|
246
|
+
*
|
|
247
|
+
* If not set, the listener will get attached to the `Window` object.
|
|
248
|
+
*/
|
|
249
|
+
target?: Document | HTMLElement | null | React.RefObject<Document | HTMLElement | null>;
|
|
250
|
+
/**
|
|
251
|
+
* The Event listener.
|
|
252
|
+
*
|
|
253
|
+
* @param event The event object that implements the [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event) interface.
|
|
254
|
+
*/
|
|
255
|
+
listener: (event: T[K]) => void;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Attach a new Event listener to the `Window` object.
|
|
259
|
+
*
|
|
260
|
+
* @param type The `Window` event name or an array of event names.
|
|
261
|
+
* @param options An object defining init options. See {@link WindowListenerOptions} for more info.
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
*
|
|
265
|
+
* #### Add a new `Window` event listener
|
|
266
|
+
*
|
|
267
|
+
* ```ts
|
|
268
|
+
* useEventListener( 'scroll', {
|
|
269
|
+
* listener: useCallback( () => {
|
|
270
|
+
* console.log( window.scrollY )
|
|
271
|
+
* }, [] )
|
|
272
|
+
* } )
|
|
273
|
+
* ```
|
|
274
|
+
*
|
|
275
|
+
* ---
|
|
276
|
+
*
|
|
277
|
+
* #### Add multiple `Window` event listeners
|
|
278
|
+
*
|
|
279
|
+
* ```ts
|
|
280
|
+
* useEventListener( [ 'scroll', 'resize' ], {
|
|
281
|
+
* listener: useCallback( event => {
|
|
282
|
+
* console.log( event )
|
|
283
|
+
* }, [] )
|
|
284
|
+
* } )
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
declare function useEventListener<K extends keyof WindowEventMap>(type: K | K[], options: WindowListenerOptions<K>): void;
|
|
288
|
+
/**
|
|
289
|
+
* Attach a new Event listener to the `Document` object.
|
|
290
|
+
*
|
|
291
|
+
* @param type The `Document` event name or an array of event names.
|
|
292
|
+
* @param options An object defining init options. See {@link DocumentListenerOptions} for more info.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
*
|
|
296
|
+
* #### Add a new `Document` event listener
|
|
297
|
+
*
|
|
298
|
+
* ```ts
|
|
299
|
+
* // A React.RefObject<Document | null> can be used too.
|
|
300
|
+
* const documentRef = useRef( typeof document !== 'undefined' ? document : null )
|
|
301
|
+
*
|
|
302
|
+
* useEventListener( 'visibilitychange', {
|
|
303
|
+
* target : typeof document !== 'undefined' ? document : null, // or `documentRef`
|
|
304
|
+
* listener: useCallback( () => {
|
|
305
|
+
* console.log( document.visibilityState )
|
|
306
|
+
* }, [] )
|
|
307
|
+
* } )
|
|
308
|
+
* ```
|
|
309
|
+
*
|
|
310
|
+
* ---
|
|
311
|
+
*
|
|
312
|
+
* #### Add multiple `Document` event listeners
|
|
313
|
+
*
|
|
314
|
+
* ```ts
|
|
315
|
+
* useEventListener( [ 'visibilitychange', 'dblclick' ], {
|
|
316
|
+
* target : typeof document !== 'undefined' ? document : null,
|
|
317
|
+
* listener: useCallback( ( event ) => {
|
|
318
|
+
* console.log( event )
|
|
319
|
+
* }, [] )
|
|
320
|
+
* } )
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
declare function useEventListener<K extends keyof DocumentEventMap>(type: K | K[], options: DocumentListenerOptions<K>): void;
|
|
324
|
+
/**
|
|
325
|
+
* Attach a new Event listener to a `HTMLElement` object.
|
|
326
|
+
*
|
|
327
|
+
* @param type The `HTMLElement` event name or an array of event names.
|
|
328
|
+
* @param options An object defining init options. See {@link ElementListenerOptions} for more info.
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
*
|
|
332
|
+
* #### Add a new `HTMLElement` event listener
|
|
333
|
+
*
|
|
334
|
+
* ```tsx
|
|
335
|
+
* 'use client'
|
|
336
|
+
*
|
|
337
|
+
* const Component: React.FC = () => {
|
|
338
|
+
*
|
|
339
|
+
* const elementRef = useRef<HTMLButtonElement>( null )
|
|
340
|
+
*
|
|
341
|
+
* useEventListener( 'click', {
|
|
342
|
+
* target : elementRef,
|
|
343
|
+
* listener: useCallback( event => {
|
|
344
|
+
* console.log( event )
|
|
345
|
+
* }, [] )
|
|
346
|
+
* } )
|
|
347
|
+
*
|
|
348
|
+
* return (
|
|
349
|
+
* <button ref={ elementRef }>Click me</button>
|
|
350
|
+
* )
|
|
351
|
+
*
|
|
352
|
+
* }
|
|
353
|
+
* ```
|
|
354
|
+
*
|
|
355
|
+
* ---
|
|
356
|
+
*
|
|
357
|
+
* #### Add multiple `HTMLElement` event listeners
|
|
358
|
+
*
|
|
359
|
+
* ```tsx
|
|
360
|
+
* 'use client'
|
|
361
|
+
*
|
|
362
|
+
* const Component: React.FC = () => {
|
|
363
|
+
*
|
|
364
|
+
* const elementRef = useRef<HTMLButtonElement>( null )
|
|
365
|
+
*
|
|
366
|
+
* useEventListener( [ 'click', 'touchmove' ], {
|
|
367
|
+
* target : elementRef,
|
|
368
|
+
* listener: useCallback( event => {
|
|
369
|
+
* console.log( event )
|
|
370
|
+
* }, [] )
|
|
371
|
+
* } )
|
|
372
|
+
*
|
|
373
|
+
* return (
|
|
374
|
+
* <button ref={ elementRef }>Click me</button>
|
|
375
|
+
* )
|
|
376
|
+
*
|
|
377
|
+
* }
|
|
378
|
+
* ```
|
|
379
|
+
*/
|
|
380
|
+
declare function useEventListener<T extends HTMLElement, K extends keyof HTMLElementEventMap>(type: K | K[], options: ElementListenerOptions<T, K>): void;
|
|
381
|
+
/**
|
|
382
|
+
* Listen MediaQuery changes.
|
|
383
|
+
*
|
|
384
|
+
* @param type The `MediaQueryList` event name or an array of event names.
|
|
385
|
+
* @param options An object defining init options. See {@link MediaQueryListenerOptions} for more info.
|
|
386
|
+
*
|
|
387
|
+
* @example
|
|
388
|
+
*
|
|
389
|
+
* #### Listen for MediaQuery changes.
|
|
390
|
+
*
|
|
391
|
+
* ```ts
|
|
392
|
+
* useEventListener( 'change', {
|
|
393
|
+
* query : '(max-width: 768px)',
|
|
394
|
+
* listener: useCallback( event => {
|
|
395
|
+
* console.error( event.matches )
|
|
396
|
+
* }, [] ),
|
|
397
|
+
* } )
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
declare function useEventListener(type: 'change', options: MediaQueryListenerOptions): void;
|
|
401
|
+
/**
|
|
402
|
+
* Attach a new custom Event listener to the given target.
|
|
403
|
+
*
|
|
404
|
+
* @param type The custom event name or an array of event names.
|
|
405
|
+
* @param options An object defining init options. See {@link CustomEventListenerOptions} for more info.
|
|
406
|
+
*
|
|
407
|
+
* @example
|
|
408
|
+
*
|
|
409
|
+
* #### Add a new custom event listener
|
|
410
|
+
*
|
|
411
|
+
* ```tsx
|
|
412
|
+
* 'use client'
|
|
413
|
+
*
|
|
414
|
+
* const Component: React.FC = () => {
|
|
415
|
+
*
|
|
416
|
+
* const elementRef = useRef<HTMLButtonElement>( null )
|
|
417
|
+
*
|
|
418
|
+
* useEventListener( 'customevent', {
|
|
419
|
+
* target : elementRef, // could be document. window will be used if omitted.
|
|
420
|
+
* listener: useCallback( event => {
|
|
421
|
+
* console.log( event )
|
|
422
|
+
* }, [] )
|
|
423
|
+
* } )
|
|
424
|
+
*
|
|
425
|
+
* useEffect( () => {
|
|
426
|
+
* elementRef.current?.dispatchEvent( new Event( 'customevent' ) )
|
|
427
|
+
* }, [] )
|
|
428
|
+
*
|
|
429
|
+
* return (
|
|
430
|
+
* <button ref={ elementRef }>Button</button>
|
|
431
|
+
* )
|
|
432
|
+
*
|
|
433
|
+
* }
|
|
434
|
+
* ```
|
|
435
|
+
*
|
|
436
|
+
* ---
|
|
437
|
+
*
|
|
438
|
+
* #### Adding types for custom events
|
|
439
|
+
*
|
|
440
|
+
* ```tsx
|
|
441
|
+
* interface Details
|
|
442
|
+
* {
|
|
443
|
+
* property?: string
|
|
444
|
+
* }
|
|
445
|
+
*
|
|
446
|
+
* type CustomEventMap = {
|
|
447
|
+
* customevent : CustomEvent<Details>
|
|
448
|
+
* anothercustomEvent : MouseEvent
|
|
449
|
+
* }
|
|
450
|
+
*
|
|
451
|
+
* type CustomEventType = keyof CustomEventMap
|
|
452
|
+
* const elementRef = useRef<HTMLButtonElement>( null )
|
|
453
|
+
* const eventType: CustomEventType = 'anothercustomEvent'
|
|
454
|
+
*
|
|
455
|
+
* useEventListener<CustomEventMap, typeof eventType>( eventType, {
|
|
456
|
+
* target : elementRef,
|
|
457
|
+
* listener: useCallback( event => {
|
|
458
|
+
* console.log( event )
|
|
459
|
+
* }, [] )
|
|
460
|
+
* } )
|
|
461
|
+
*
|
|
462
|
+
* useEffect( () => {
|
|
463
|
+
*
|
|
464
|
+
* elementRef.current?.dispatchEvent( new CustomEvent<Details>( 'customevent', {
|
|
465
|
+
* detail: {
|
|
466
|
+
* property: 'value'
|
|
467
|
+
* }
|
|
468
|
+
* } ) )
|
|
469
|
+
*
|
|
470
|
+
* }, [] )
|
|
471
|
+
*```
|
|
472
|
+
*/
|
|
473
|
+
declare function useEventListener<T extends Record<string, Event>, K extends keyof T = keyof T>(type: K | K[], options: CustomEventListenerOptions<T, K>): void;
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Check if device is portrait oriented.
|
|
477
|
+
*
|
|
478
|
+
* State get updated when device orientation changes.
|
|
479
|
+
*
|
|
480
|
+
* @returns `true` if the device is portrait oriented, `false` otherwise.
|
|
481
|
+
*/
|
|
482
|
+
declare const useIsPortrait: () => boolean;
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Get Document Media matches and listen for changes.
|
|
486
|
+
*
|
|
487
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia)
|
|
488
|
+
*
|
|
489
|
+
* @param query A string specifying the media query to parse into a `MediaQueryList`.
|
|
490
|
+
* @returns A boolean value that returns `true` if the document currently matches the media query list, or `false` if not.
|
|
491
|
+
*/
|
|
492
|
+
declare const useMediaQuery: (query: string) => boolean;
|
|
493
|
+
|
|
494
|
+
type SetFocusTrap = (target?: HTMLElement) => void;
|
|
495
|
+
type RestoreFocusTrap = () => void;
|
|
496
|
+
/**
|
|
497
|
+
* Trap focus inside the given HTML Element.
|
|
498
|
+
*
|
|
499
|
+
* @param target (Optional) The target HTMLElement React RefObject to trap focus within.
|
|
500
|
+
* If no target is given, you must provide the target HTMLElement when calling `setFocusTrap`.
|
|
501
|
+
*
|
|
502
|
+
* @returns A tuple containing:
|
|
503
|
+
* - `setFocusTrap`: A function to enable the focus trap. Optionally accept an HTMLElement as target.
|
|
504
|
+
* - `restoreFocusTrap`: A function to restore the previous focus state.
|
|
505
|
+
*/
|
|
506
|
+
declare const useFocusTrap: (target?: React.RefObject<HTMLElement | null>) => readonly [SetFocusTrap, RestoreFocusTrap];
|
|
507
|
+
|
|
508
|
+
type MarginValue = `${number}${'px' | '%'}`;
|
|
509
|
+
type MarginType = (MarginValue | `${MarginValue} ${MarginValue}` | `${MarginValue} ${MarginValue} ${MarginValue}` | `${MarginValue} ${MarginValue} ${MarginValue} ${MarginValue}`);
|
|
510
|
+
/**
|
|
511
|
+
* A custom callback executed when target element's visibility has crossed one or more thresholds.
|
|
512
|
+
*
|
|
513
|
+
* This callback is awaited before any state update.
|
|
514
|
+
*
|
|
515
|
+
* If an error is thrown the React State update won't be fired.
|
|
516
|
+
*
|
|
517
|
+
* ⚠️ Wrap your callback with `useCallback` to avoid unnecessary `IntersectionObserver` recreation.
|
|
518
|
+
*
|
|
519
|
+
* @param entry The intersecting {@link IntersectionObserverEntry}.
|
|
520
|
+
* @param observer The current {@link IntersectionObserver} instance.
|
|
521
|
+
*/
|
|
522
|
+
type OnStartHandler = (entry: IntersectionObserverEntry, observer: IntersectionObserver) => void | Promise<void>;
|
|
523
|
+
interface UseInViewOptions {
|
|
524
|
+
/**
|
|
525
|
+
* Identifies the {@link Element} or {@link Document} whose bounds are treated as the bounding box of the viewport for the Element which is the observer's target.
|
|
526
|
+
*
|
|
527
|
+
* If the `root` is `null`, then the bounds of the actual document viewport are used.
|
|
528
|
+
*
|
|
529
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/root)
|
|
530
|
+
*/
|
|
531
|
+
root?: Element | Document | false | null;
|
|
532
|
+
/**
|
|
533
|
+
* A string, formatted similarly to the CSS margin property's value, which contains offsets for one or more sides of the root's bounding box.
|
|
534
|
+
*
|
|
535
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin)
|
|
536
|
+
*/
|
|
537
|
+
margin?: MarginType;
|
|
538
|
+
/**
|
|
539
|
+
* The intersecting target thresholds.
|
|
540
|
+
*
|
|
541
|
+
* Threshold can be set to:
|
|
542
|
+
* - `all` - `1` will be used.
|
|
543
|
+
* - `some` - `0.5` will be used.
|
|
544
|
+
* - `number`
|
|
545
|
+
* - `number[]`
|
|
546
|
+
*
|
|
547
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/thresholds)
|
|
548
|
+
*/
|
|
549
|
+
amount?: 'all' | 'some' | number | number[];
|
|
550
|
+
/**
|
|
551
|
+
* By setting this to `true` the observer will be disconnected after the target Element enters the viewport.
|
|
552
|
+
*
|
|
553
|
+
* Consider using a placeholder to avoid layout shifts while mounting/unmounting components when `once` is set to `false`.
|
|
554
|
+
*/
|
|
555
|
+
once?: boolean;
|
|
556
|
+
/**
|
|
557
|
+
* Initial value.
|
|
558
|
+
*
|
|
559
|
+
* @default true
|
|
560
|
+
*/
|
|
561
|
+
initial?: boolean;
|
|
562
|
+
/**
|
|
563
|
+
* Defines the initial observation activity. Use {@link UseInViewReturnType.setEnabled} to update this state.
|
|
564
|
+
*
|
|
565
|
+
* @default true
|
|
566
|
+
*/
|
|
567
|
+
enable?: boolean;
|
|
568
|
+
/**
|
|
569
|
+
* A custom callback executed when target element's visibility has crossed one or more thresholds.
|
|
570
|
+
*
|
|
571
|
+
* This callback is awaited before any state update.
|
|
572
|
+
*
|
|
573
|
+
* If an error is thrown the React State update won't be fired.
|
|
574
|
+
*
|
|
575
|
+
* ⚠️ Wrap your callback with `useCallback` to avoid unnecessary `IntersectionObserver` recreation.
|
|
576
|
+
*
|
|
577
|
+
* @param entry The intersecting {@link IntersectionObserverEntry}.
|
|
578
|
+
* @param observer The current {@link IntersectionObserver} instance.
|
|
579
|
+
*/
|
|
580
|
+
onStart?: OnStartHandler;
|
|
581
|
+
}
|
|
582
|
+
interface UseInViewReturnType {
|
|
583
|
+
/**
|
|
584
|
+
* Indicates whether the target Element is in viewport or not.
|
|
585
|
+
*
|
|
586
|
+
*/
|
|
587
|
+
inView: boolean;
|
|
588
|
+
/**
|
|
589
|
+
* Indicates whether the target Element is being observed or not.
|
|
590
|
+
*
|
|
591
|
+
*/
|
|
592
|
+
enabled: boolean;
|
|
593
|
+
/**
|
|
594
|
+
* The {@link IntersectionObserver} instance.
|
|
595
|
+
*
|
|
596
|
+
* It could be `undefined` if `IntersectionObserver` is not available or observation is not enabled.
|
|
597
|
+
*
|
|
598
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver)
|
|
599
|
+
*/
|
|
600
|
+
observer?: IntersectionObserver;
|
|
601
|
+
/**
|
|
602
|
+
* A React Dispatch SetState action that allows custom state updates.
|
|
603
|
+
*
|
|
604
|
+
*/
|
|
605
|
+
setInView: React.Dispatch<React.SetStateAction<boolean>>;
|
|
606
|
+
/**
|
|
607
|
+
* A React Dispatch SetState action that allows to enable/disable observation when needed.
|
|
608
|
+
*
|
|
609
|
+
*/
|
|
610
|
+
setEnabled: React.Dispatch<React.SetStateAction<boolean>>;
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Check if the given target Element is intersecting with an ancestor Element or with a top-level document's viewport.
|
|
614
|
+
*
|
|
615
|
+
* @param target The React.RefObject of the target Element to observe.
|
|
616
|
+
* @param options ( Optional ) An object defining custom {@link IntersectionObserver} options. See {@link UseInViewOptions} for more info.
|
|
617
|
+
*
|
|
618
|
+
* @returns An object defining intersection data. See {@link UseInViewReturnType} for more info.
|
|
619
|
+
*/
|
|
620
|
+
declare const useInView: (target: React.RefObject<Element | null>, options?: UseInViewOptions) => UseInViewReturnType;
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* Prevent Element overflow.
|
|
624
|
+
*
|
|
625
|
+
* @param target (Optional) The React RefObject target HTMLElement. Default: `Document.documentElement`.
|
|
626
|
+
*/
|
|
627
|
+
declare const useScrollBlock: (target?: React.RefObject<HTMLElement | null>) => readonly [() => void, () => void];
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Check if the React Hook or Component where this hook is executed is running in a browser environment.
|
|
631
|
+
*
|
|
632
|
+
* @returns `true` if the React Hook or Component is running in a browser environment, `false` otherwise.
|
|
633
|
+
*/
|
|
634
|
+
declare const useIsClient: () => boolean;
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Check if is first React Hook/Component render.
|
|
638
|
+
*
|
|
639
|
+
* @returns `true` at the mount time, then always `false`.
|
|
640
|
+
* - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.
|
|
641
|
+
*/
|
|
642
|
+
declare const useIsFirstRender: () => boolean;
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Modified version of `useEffect` that skips the first render.
|
|
646
|
+
*
|
|
647
|
+
* @param effect Imperative function that can return a cleanup function.
|
|
648
|
+
* @param deps If present, effect will only activate if the values in the list change.
|
|
649
|
+
*/
|
|
650
|
+
declare const useUpdateEffect: (effect: React.EffectCallback, deps?: React.DependencyList) => void;
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* The function called when the timer elapses.
|
|
654
|
+
*
|
|
655
|
+
*/
|
|
656
|
+
type TimerHandler<T extends readonly unknown[]> = ((...args: T) => void) | (() => void);
|
|
657
|
+
/**
|
|
658
|
+
* The Timer Id return by `setTimeout` or `setInterval` functions
|
|
659
|
+
*
|
|
660
|
+
*/
|
|
661
|
+
type TimerId = number | NodeJS.Timeout;
|
|
662
|
+
/**
|
|
663
|
+
* Start the timer.
|
|
664
|
+
*
|
|
665
|
+
* @returns The {@link TimerId}.
|
|
666
|
+
*/
|
|
667
|
+
type StartTimer = () => TimerId;
|
|
668
|
+
/**
|
|
669
|
+
* Stop the timer.
|
|
670
|
+
*
|
|
671
|
+
*/
|
|
672
|
+
type StopTimer = () => void;
|
|
673
|
+
interface BasicTimerOptions<T extends readonly unknown[]> {
|
|
674
|
+
/**
|
|
675
|
+
* The number of milliseconds to wait before calling the `callback`.
|
|
676
|
+
*
|
|
677
|
+
* @default 1
|
|
678
|
+
*/
|
|
679
|
+
delay?: number;
|
|
680
|
+
/**
|
|
681
|
+
* Optional arguments to pass when the `callback` is called.
|
|
682
|
+
*
|
|
683
|
+
* Make sure to memoize its value to avoid timer restarts when a state update happen in your Component.
|
|
684
|
+
*/
|
|
685
|
+
args?: T;
|
|
686
|
+
}
|
|
687
|
+
interface TimerOptions<T extends readonly unknown[]> extends BasicTimerOptions<T> {
|
|
688
|
+
/**
|
|
689
|
+
* Indicates whether auto start the timer.
|
|
690
|
+
*
|
|
691
|
+
* @default true
|
|
692
|
+
*/
|
|
693
|
+
autoplay?: boolean;
|
|
694
|
+
/**
|
|
695
|
+
* Whether to update React state about Timer running status.
|
|
696
|
+
*
|
|
697
|
+
* @default false
|
|
698
|
+
*/
|
|
699
|
+
updateState?: boolean;
|
|
700
|
+
/**
|
|
701
|
+
* Indicates whether to execute the callback when timer starts.
|
|
702
|
+
*
|
|
703
|
+
* @default false
|
|
704
|
+
*/
|
|
705
|
+
runOnStart?: boolean;
|
|
706
|
+
}
|
|
707
|
+
interface StateTimerOptions<T extends readonly unknown[]> extends TimerOptions<T> {
|
|
708
|
+
/**
|
|
709
|
+
* Whether to update React state about Timer running status.
|
|
710
|
+
*
|
|
711
|
+
* @default false
|
|
712
|
+
*/
|
|
713
|
+
updateState: true;
|
|
714
|
+
}
|
|
715
|
+
interface TimerReturnType {
|
|
716
|
+
/**
|
|
717
|
+
* Manually start the timer.
|
|
718
|
+
*
|
|
719
|
+
* @returns The Timer ID.
|
|
720
|
+
*/
|
|
721
|
+
start: StartTimer;
|
|
722
|
+
/**
|
|
723
|
+
* Manually stop the timer.
|
|
724
|
+
*
|
|
725
|
+
*/
|
|
726
|
+
stop: StopTimer;
|
|
727
|
+
}
|
|
728
|
+
interface StateTimerReturnType extends TimerReturnType {
|
|
729
|
+
/**
|
|
730
|
+
* Indicates whether the timer is active.
|
|
731
|
+
*
|
|
732
|
+
*/
|
|
733
|
+
isActive: boolean;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* Debounce a value by a specified delay.
|
|
738
|
+
*
|
|
739
|
+
* This hook returns a debounced version of the input value, which only updates
|
|
740
|
+
* after the specified delay has passed without any changes to the input value.
|
|
741
|
+
* It is useful for scenarios like search input fields or other cases where
|
|
742
|
+
* frequent updates should be minimized.
|
|
743
|
+
*
|
|
744
|
+
* The `Timeout` automatically restarts when the given `value` changes.
|
|
745
|
+
*
|
|
746
|
+
* @template T The type of the `value`.
|
|
747
|
+
*
|
|
748
|
+
* @param value The value to debounce. This can be of any type.
|
|
749
|
+
* @param delay The debounce delay in milliseconds. Default `500`.
|
|
750
|
+
*
|
|
751
|
+
* @returns The debounced value, which updates only after the delay has passed.
|
|
752
|
+
*/
|
|
753
|
+
declare const useDebounce: <T>(value: T, delay?: number) => T;
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* Schedules repeated execution of `callback` every `delay` milliseconds.
|
|
757
|
+
*
|
|
758
|
+
* A React State update will occur when the timer expires or get cancelled by `stop()` method.
|
|
759
|
+
*
|
|
760
|
+
* When `delay` is larger than `2147483647` or less than `1` or `NaN`, the `delay`
|
|
761
|
+
* will be set to `1`. Non-integer delays are truncated to an integer.
|
|
762
|
+
*
|
|
763
|
+
* If `callback` is not a function, a `TypeError` will be thrown.
|
|
764
|
+
*
|
|
765
|
+
* The `Timeout` is automatically cancelled on unmount.
|
|
766
|
+
*
|
|
767
|
+
* @template T An Array defining optional arguments passed to the `callback`.
|
|
768
|
+
*
|
|
769
|
+
* @param callback The function to call when the timer elapses.
|
|
770
|
+
* @param options (Optional) An object defining custom timer options. See {@link StateTimerOptions} for more info.
|
|
771
|
+
*
|
|
772
|
+
* @returns An object with timer utilities. See {@link StateTimerReturnType} for more info.
|
|
773
|
+
*/
|
|
774
|
+
declare function useInterval<T extends readonly unknown[]>(callback: TimerHandler<T>, options?: StateTimerOptions<T>): StateTimerReturnType;
|
|
775
|
+
/**
|
|
776
|
+
* Schedules repeated execution of `callback` every `delay` milliseconds.
|
|
777
|
+
*
|
|
778
|
+
* When `delay` is larger than `2147483647` or less than `1` or `NaN`, the `delay`
|
|
779
|
+
* will be set to `1`. Non-integer delays are truncated to an integer.
|
|
780
|
+
*
|
|
781
|
+
* If `callback` is not a function, a `TypeError` will be thrown.
|
|
782
|
+
*
|
|
783
|
+
* The `Timeout` is automatically cancelled on unmount.
|
|
784
|
+
*
|
|
785
|
+
* @template T An Array defining optional arguments passed to the `callback`.
|
|
786
|
+
*
|
|
787
|
+
* @param callback The function to call when the timer elapses.
|
|
788
|
+
* @param options (Optional) An object defining custom timer options. See {@link TimerOptions} for more info.
|
|
789
|
+
*
|
|
790
|
+
* @returns An object with timer utilities. See {@link TimerReturnType} for more info.
|
|
791
|
+
*/
|
|
792
|
+
declare function useInterval<T extends readonly unknown[]>(callback: TimerHandler<T>, options?: TimerOptions<T>): TimerReturnType;
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* Schedules repeated execution of `callback` every `delay` milliseconds.
|
|
796
|
+
*
|
|
797
|
+
* This is a lighter version of [`useInterval`](./useInterval.ts), suggested to use when
|
|
798
|
+
* a basic functionality is enough (no manual start/stop or state updates).
|
|
799
|
+
*
|
|
800
|
+
* When `delay` is larger than `2147483647` or less than `1` or `NaN`, the `delay`
|
|
801
|
+
* will be set to `1`. Non-integer delays are truncated to an integer.
|
|
802
|
+
*
|
|
803
|
+
* If `callback` is not a function, a `TypeError` will be thrown.
|
|
804
|
+
*
|
|
805
|
+
* The `Timeout` is automatically cancelled on unmount.
|
|
806
|
+
*
|
|
807
|
+
* @template T An Array defining optional arguments passed to the `callback`.
|
|
808
|
+
*
|
|
809
|
+
* @param callback The function to call when the timer elapses.
|
|
810
|
+
* @param options (Optional) An object defining custom timer options. See {@link BasicTimerOptions} for more info.
|
|
811
|
+
*/
|
|
812
|
+
declare const useLightInterval: <T extends readonly unknown[]>(callback: TimerHandler<T>, options?: BasicTimerOptions<T>) => void;
|
|
813
|
+
|
|
814
|
+
interface UseIntervalWhenVisibleReturnType extends Omit<TimerReturnType, 'start'> {
|
|
815
|
+
/**
|
|
816
|
+
* Manually start the timer.
|
|
817
|
+
*
|
|
818
|
+
* @returns The Timer ID if Document is visible.
|
|
819
|
+
*/
|
|
820
|
+
start: () => TimerId | undefined;
|
|
821
|
+
}
|
|
822
|
+
interface UseIntervalWhenVisibleStateReturnType extends Omit<StateTimerReturnType, 'start'> {
|
|
823
|
+
/**
|
|
824
|
+
* Manually start the timer.
|
|
825
|
+
*
|
|
826
|
+
* @returns The Timer ID if Document is visible.
|
|
827
|
+
*/
|
|
828
|
+
start: () => TimerId | undefined;
|
|
829
|
+
/**
|
|
830
|
+
* Indicates that the timer is active, thus the Document is visible.
|
|
831
|
+
*
|
|
832
|
+
*/
|
|
833
|
+
isActive: boolean;
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Schedules repeated execution of `callback` every `delay` milliseconds when `Document` is visible.
|
|
837
|
+
*
|
|
838
|
+
* This hook automatically starts and stops the interval based on the `Document` visibility.
|
|
839
|
+
*
|
|
840
|
+
* The `Timeout` is automatically cancelled on unmount.
|
|
841
|
+
*
|
|
842
|
+
* @template T An Array defining optional arguments passed to the `callback`.
|
|
843
|
+
*
|
|
844
|
+
* @param callback The function to call when the timer elapses.
|
|
845
|
+
* @param options (Optional) An object defining custom timer options. See {@link StateTimerOptions} for more info.
|
|
846
|
+
*
|
|
847
|
+
* @returns An object with timer utilities. See {@link UseIntervalWhenVisibleStateReturnType} for more info.
|
|
848
|
+
*/
|
|
849
|
+
declare function useIntervalWhenVisible<T extends readonly unknown[]>(callback: TimerHandler<T>, options?: StateTimerOptions<T>): UseIntervalWhenVisibleStateReturnType;
|
|
850
|
+
/**
|
|
851
|
+
* Schedules repeated execution of `callback` every `delay` milliseconds when `Document` is visible.
|
|
852
|
+
*
|
|
853
|
+
* This hook automatically starts and stops the interval based on the `Document` visibility.
|
|
854
|
+
*
|
|
855
|
+
* The `Timeout` is automatically cancelled on unmount.
|
|
856
|
+
*
|
|
857
|
+
* @template T An Array defining optional arguments passed to the `callback`.
|
|
858
|
+
*
|
|
859
|
+
* @param callback The function to call when the timer elapses.
|
|
860
|
+
* @param options (Optional) An object defining custom timer options. See {@link TimerOptions} for more info.
|
|
861
|
+
*
|
|
862
|
+
* @returns An object with timer utilities. See {@link UseIntervalWhenVisibleReturnType} for more info.
|
|
863
|
+
*/
|
|
864
|
+
declare function useIntervalWhenVisible<T extends readonly unknown[]>(callback: TimerHandler<T>, options?: TimerOptions<T>): UseIntervalWhenVisibleReturnType;
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* Schedules execution of a one-time `callback` after `delay` milliseconds.
|
|
868
|
+
*
|
|
869
|
+
* A React State update will occur when the timer expires or get cancelled by `stop()` method.
|
|
870
|
+
*
|
|
871
|
+
* The `callback` will likely not be invoked in precisely `delay` milliseconds.
|
|
872
|
+
* Node.js makes no guarantees about the exact timing of when callbacks will fire,
|
|
873
|
+
* nor of their ordering. The callback will be called as close as possible to the
|
|
874
|
+
* time specified.
|
|
875
|
+
*
|
|
876
|
+
* When `delay` is larger than `2147483647` or less than `1` or `NaN`, the `delay`
|
|
877
|
+
* will be set to `1`. Non-integer delays are truncated to an integer.
|
|
878
|
+
*
|
|
879
|
+
* If `callback` is not a function, a `TypeError` will be thrown.
|
|
880
|
+
*
|
|
881
|
+
* The `Timeout` is automatically cancelled on unmount.
|
|
882
|
+
*
|
|
883
|
+
* @template T An Array defining optional arguments passed to the `callback`.
|
|
884
|
+
*
|
|
885
|
+
* @param callback The function to call when the timer elapses.
|
|
886
|
+
* @param options (Optional) An object defining custom timer options. See {@link StateTimerOptions} for more info.
|
|
887
|
+
*
|
|
888
|
+
* @returns An object with timer utilities. See {@link StateTimerReturnType} for more info.
|
|
889
|
+
*/
|
|
890
|
+
declare function useTimeout<T extends readonly unknown[]>(callback: TimerHandler<T>, options?: StateTimerOptions<T>): StateTimerReturnType;
|
|
891
|
+
/**
|
|
892
|
+
* Schedules execution of a one-time `callback` after `delay` milliseconds.
|
|
893
|
+
*
|
|
894
|
+
* The `callback` will likely not be invoked in precisely `delay` milliseconds.
|
|
895
|
+
* Node.js makes no guarantees about the exact timing of when callbacks will fire,
|
|
896
|
+
* nor of their ordering. The callback will be called as close as possible to the
|
|
897
|
+
* time specified.
|
|
898
|
+
*
|
|
899
|
+
* When `delay` is larger than `2147483647` or less than `1` or `NaN`, the `delay`
|
|
900
|
+
* will be set to `1`. Non-integer delays are truncated to an integer.
|
|
901
|
+
*
|
|
902
|
+
* If `callback` is not a function, a `TypeError` will be thrown.
|
|
903
|
+
*
|
|
904
|
+
* The `Timeout` is automatically cancelled on unmount.
|
|
905
|
+
*
|
|
906
|
+
* @template T An Array defining optional arguments passed to the `callback`.
|
|
907
|
+
*
|
|
908
|
+
* @param callback The function to call when the timer elapses.
|
|
909
|
+
* @param options (Optional) An object defining custom timer options. See {@link TimerOptions} for more info.
|
|
910
|
+
*
|
|
911
|
+
* @returns An object with timer utilities. See {@link TimerReturnType} for more info.
|
|
912
|
+
*/
|
|
913
|
+
declare function useTimeout<T extends readonly unknown[]>(callback: TimerHandler<T>, options?: TimerOptions<T>): TimerReturnType;
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* Schedules execution of a one-time `callback` after `delay` milliseconds.
|
|
917
|
+
*
|
|
918
|
+
* This is a lighter version of [`useTimeout`](./useTimeout.ts), suggested to use when
|
|
919
|
+
* a basic functionality is enough (no manual start/stop or state updates).
|
|
920
|
+
*
|
|
921
|
+
* The `callback` will likely not be invoked in precisely `delay` milliseconds.
|
|
922
|
+
* Node.js makes no guarantees about the exact timing of when callbacks will fire,
|
|
923
|
+
* nor of their ordering. The callback will be called as close as possible to the
|
|
924
|
+
* time specified.
|
|
925
|
+
*
|
|
926
|
+
* When `delay` is larger than `2147483647` or less than `1` or `NaN`, the `delay`
|
|
927
|
+
* will be set to `1`. Non-integer delays are truncated to an integer.
|
|
928
|
+
*
|
|
929
|
+
* If `callback` is not a function, a `TypeError` will be thrown.
|
|
930
|
+
*
|
|
931
|
+
* The `Timeout` is automatically cancelled on unmount.
|
|
932
|
+
*
|
|
933
|
+
* @template T An Array defining optional arguments passed to the `callback`.
|
|
934
|
+
*
|
|
935
|
+
* @param callback The function to call when the timer elapses.
|
|
936
|
+
* @param options (Optional) An object defining custom timer options. See {@link BasicTimerOptions} for more info.
|
|
937
|
+
*/
|
|
938
|
+
declare const useLightTimeout: <T extends readonly unknown[]>(callback: TimerHandler<T>, options?: BasicTimerOptions<T>) => void;
|
|
939
|
+
|
|
940
|
+
export { type AddEventListenerOptions, type BasicTimerOptions, type CommonListenerOptions, type CustomEventListenerOptions, type DocumentEventListener, type DocumentListenerOptions, type ElementEventListener, type ElementListenerOptions, type ListenerOptions, type MarginType, type MarginValue, type MediaQueryChangeListener, type MediaQueryEventListener, type MediaQueryListenerOptions, type OnStartHandler, type StartTimer, type StateTimerOptions, type StateTimerReturnType, type StopTimer, type TimerHandler, type TimerId, type TimerOptions, type TimerReturnType, type UseDarkModeOptions, type UseDarkModeOutput, type UseInViewOptions, type UseInViewReturnType, type UseIntervalWhenVisibleReturnType, type UseIntervalWhenVisibleStateReturnType, type WindowEventListener, type WindowListenerOptions, useDarkMode, useDebounce, useEventListener, useFocusTrap, useInView, useInterval, useIntervalWhenVisible, useIsClient, useIsFirstRender, useIsPortrait, useLightInterval, useLightTimeout, useLocalStorage, useMediaQuery, useScrollBlock, useSessionStorage, useStorage, useTimeout, useUpdateEffect };
|