react-hook-toolkit 1.0.15 → 1.0.17
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 +357 -527
- package/dist/hooks/chunk613852.js +1 -1
- package/dist/hooks/chunk940514.d.ts +13 -0
- package/dist/hooks/chunk940514.js +46 -0
- package/dist/skeletons/chunk613555.d.ts +6 -2
- package/dist/skeletons/chunk613555.js +15 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
**react-hook-toolkit** package offers a comprehensive set of hooks to simplify React development. It includes hooks for managing state, handling API requests, optimizing performance, and improving user interactions. Key features include data fetching, form handling, local storage management, debouncing/throttling, window size tracking, event listeners, and more. These hooks are designed to enhance productivity by providing reusable, easy-to-use solutions for common tasks.
|
|
3
3
|
|
|
4
|
-
**Note : This package is written in TypeScript and offers full support for all modern browsers.**
|
|
4
|
+
**Note : This lightweight and type-safe package is written in TypeScript and offers full support for all hooks across all modern browsers.**
|
|
5
5
|
|
|
6
6
|
### Installation
|
|
7
7
|
|
|
@@ -15,20 +15,22 @@ yarn add react-hook-toolkit
|
|
|
15
15
|
pnpm add react-hook-toolkit
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
+
### **Authors**
|
|
19
|
+
|
|
20
|
+
    
|
|
21
|
+
|
|
18
22
|
### **Browser Support**
|
|
19
23
|
|
|
20
24
|
|  |  |  |  | 
|
|
21
25
|
| ------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
|
|
22
26
|
| Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ |
|
|
23
27
|
|
|
24
|
-
### **Authors**
|
|
25
|
-
|
|
26
|
-
_&_Shyamal_(JS_Expert)-red)    
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
### **Features**
|
|
29
30
|
Here's the properly structured documentation with clear purpose explanations:
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
------------------------------------------ **Custom Hooks** ------------------------------------------------
|
|
33
|
+
|
|
32
34
|
|
|
33
35
|
📌 **useAxios**
|
|
34
36
|
A custom hook for making API requests using Axios. It manages request states (`loading`, `error`, `data`) and provides a function (`makeRequest`) to initiate a request.
|
|
@@ -39,67 +41,30 @@ useEffect(() => {
|
|
|
39
41
|
makeRequest('/users', 'GET');
|
|
40
42
|
}, []);
|
|
41
43
|
```
|
|
42
|
-
|
|
43
|
-
📌 **useDrawer**
|
|
44
|
-
A custom hook for managing application drawer state. It provides control over drawer visibility (`drawerOpen`) and active menu selection (`currentMainMenu`), with methods to toggle these states.
|
|
45
|
-
|
|
46
|
-
```tsx
|
|
47
|
-
const {
|
|
48
|
-
drawerOpen,
|
|
49
|
-
currentMainMenu,
|
|
50
|
-
openDrawer,
|
|
51
|
-
closeDrawer,
|
|
52
|
-
setCurrentMainMenu
|
|
53
|
-
} = useDrawer();
|
|
54
|
-
|
|
55
|
-
// Example usage:
|
|
56
|
-
<button onClick={openDrawer}>Open Menu</button>
|
|
57
|
-
<button onClick={() => setCurrentMainMenu('settings')}>
|
|
58
|
-
Show Settings
|
|
59
|
-
</button>
|
|
60
|
-
```
|
|
61
|
-
|
|
62
44
|
---
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
A custom reducer function that takes an `initialState` and an object of actions, returning a new state based on the action type.
|
|
45
|
+
📌 **useFetch**
|
|
46
|
+
Provides an easy way to fetch data with hooks, managing loading, data, and error states.
|
|
66
47
|
|
|
67
48
|
```ts
|
|
68
|
-
const
|
|
69
|
-
increment: (state) => ({ count: state.count + 1 }),
|
|
70
|
-
});
|
|
71
|
-
const [state, dispatch] = useReducer(reducer, initialState);
|
|
49
|
+
const { data, loading, error } = useFetch('/api/data');
|
|
72
50
|
```
|
|
73
|
-
|
|
74
51
|
---
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
Fetches data from a given URL and manages the loading, error, and data states.
|
|
52
|
+
📌 **useFetchRetry**
|
|
53
|
+
Performs an HTTP request with retry logic, retrying a specified number of times upon failure before returning an error.
|
|
78
54
|
|
|
79
55
|
```ts
|
|
80
|
-
const { data, loading, error } =
|
|
56
|
+
const { data, loading, error } = useFetchRetry('/api/user', { method: 'GET' }, 3);
|
|
81
57
|
```
|
|
82
|
-
|
|
83
58
|
---
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
A hook to persist state in `localStorage`. It initializes state from `localStorage` and updates it whenever the value changes.
|
|
59
|
+
📌 **useAsync**
|
|
60
|
+
Handles the state of an asynchronous function, including loading, success, and error states, with the ability to trigger the execution of the async operation.
|
|
87
61
|
|
|
88
62
|
```ts
|
|
89
|
-
const
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
---
|
|
93
|
-
|
|
94
|
-
📌 **useToggle**
|
|
95
|
-
A simple boolean toggle hook that returns a state and a function to toggle its value.
|
|
63
|
+
const { execute, status, value, error } = useAsync(fetchUser);
|
|
96
64
|
|
|
97
|
-
|
|
98
|
-
const [isOpen, toggle] = useToggle(false);
|
|
65
|
+
execute(); // ➝ runs the async function
|
|
99
66
|
```
|
|
100
|
-
|
|
101
67
|
---
|
|
102
|
-
|
|
103
68
|
📌 **useDebounce**
|
|
104
69
|
Delays updating the state until after a specified delay, useful for preventing frequent updates (e.g., handling user input).
|
|
105
70
|
|
|
@@ -108,44 +73,44 @@ const debouncedSearch = useDebounce(searchText, 500);
|
|
|
108
73
|
```
|
|
109
74
|
|
|
110
75
|
---
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
Limits how often a value updates within a given time interval, useful for performance optimization.
|
|
76
|
+
📌 **useDebouncedCallback**
|
|
77
|
+
Delays the execution of a callback function until after a specified delay, resetting the delay on each call to prevent frequent execution.
|
|
114
78
|
|
|
115
79
|
```ts
|
|
116
|
-
const
|
|
117
|
-
```
|
|
80
|
+
const debounced = useDebouncedCallback(() => search(input), 300);
|
|
118
81
|
|
|
82
|
+
input.onChange = debounced;
|
|
83
|
+
```
|
|
119
84
|
---
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
Returns the previous value of a state or prop.
|
|
85
|
+
📌 **useDebouncedValue**
|
|
86
|
+
Returns a debounced version of a value.
|
|
123
87
|
|
|
124
88
|
```ts
|
|
125
|
-
const
|
|
89
|
+
const debouncedValue = useDebouncedValue(value, 300);
|
|
126
90
|
```
|
|
127
|
-
|
|
128
91
|
---
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
Checks if a media query matches the current viewport size and updates the state accordingly.
|
|
92
|
+
📌 **useThrottle**
|
|
93
|
+
Limits how often a value updates within a given time interval, useful for performance optimization.
|
|
132
94
|
|
|
133
95
|
```ts
|
|
134
|
-
const
|
|
96
|
+
const throttledScroll = useThrottle(scrollValue, 300);
|
|
135
97
|
```
|
|
136
98
|
|
|
137
99
|
---
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
Copies text to the clipboard and provides a state to indicate if copying was successful.
|
|
100
|
+
📌 **useDelay**
|
|
101
|
+
Delays the update of a value for a specified amount of time before reflecting it in the state.
|
|
141
102
|
|
|
142
103
|
```ts
|
|
143
|
-
const
|
|
144
|
-
copyToClipboard('Text to copy');
|
|
104
|
+
const delayedValue = useDelay(value, 500);
|
|
145
105
|
```
|
|
146
|
-
|
|
147
106
|
---
|
|
107
|
+
📌 **useTimer**
|
|
108
|
+
Starts a countdown timer that updates the time every second, returning the current time and any errors.
|
|
148
109
|
|
|
110
|
+
```ts
|
|
111
|
+
const { time, error } = useTimer(60); // starts from 60 and counts down
|
|
112
|
+
```
|
|
113
|
+
---
|
|
149
114
|
📌 **useInterval**
|
|
150
115
|
Runs a callback function at a specified time interval and clears it when the delay is null or the component unmounts.
|
|
151
116
|
|
|
@@ -154,45 +119,7 @@ useInterval(() => {
|
|
|
154
119
|
console.log('Tick');
|
|
155
120
|
}, 1000);
|
|
156
121
|
```
|
|
157
|
-
|
|
158
|
-
---
|
|
159
|
-
|
|
160
|
-
📌 **useWindowSize**
|
|
161
|
-
Tracks the width and height of the browser window and updates when resized.
|
|
162
|
-
|
|
163
|
-
```ts
|
|
164
|
-
const { width, height } = useWindowSize();
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
---
|
|
168
|
-
|
|
169
|
-
📌 **useKeyPress**
|
|
170
|
-
Detects whether a specific key is being pressed or released.
|
|
171
|
-
|
|
172
|
-
```ts
|
|
173
|
-
const isEnterPressed = useKeyPress('Enter');
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
---
|
|
177
|
-
|
|
178
|
-
📌 **useOnlineStatus**
|
|
179
|
-
Monitors the user's network connection status (online/offline).
|
|
180
|
-
|
|
181
|
-
```ts
|
|
182
|
-
const isOnline = useOnlineStatus();
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
---
|
|
186
|
-
|
|
187
|
-
📌 **useScrollPosition**
|
|
188
|
-
Tracks the horizontal (x) and vertical (y) scroll positions of the window.
|
|
189
|
-
|
|
190
|
-
```ts
|
|
191
|
-
const { x, y } = useScrollPosition();
|
|
192
|
-
```
|
|
193
|
-
|
|
194
122
|
---
|
|
195
|
-
|
|
196
123
|
📌 **useTimeout**
|
|
197
124
|
Executes a function after a specified delay and cleans up automatically when dependencies change.
|
|
198
125
|
|
|
@@ -202,15 +129,6 @@ useTimeout(() => {
|
|
|
202
129
|
}, 2000);
|
|
203
130
|
```
|
|
204
131
|
|
|
205
|
-
---
|
|
206
|
-
|
|
207
|
-
📌 **useDarkMode**
|
|
208
|
-
Detects and toggles dark mode based on the user's system preference.
|
|
209
|
-
|
|
210
|
-
```ts
|
|
211
|
-
const [isDarkMode, toggleDarkMode] = useDarkMode();
|
|
212
|
-
```
|
|
213
|
-
|
|
214
132
|
---
|
|
215
133
|
|
|
216
134
|
📌 **useForm**
|
|
@@ -226,631 +144,531 @@ validate({
|
|
|
226
144
|
email: (val) => (!val.includes('@') ? 'Invalid email' : null),
|
|
227
145
|
});
|
|
228
146
|
```
|
|
229
|
-
|
|
230
147
|
---
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
Provides an array state with helper functions to manipulate it, including setting, adding, removing by index, and clearing elements.
|
|
148
|
+
📌 **useFormSubmit**
|
|
149
|
+
Handles form submission with loading/error states and success callbacks.
|
|
234
150
|
|
|
235
151
|
```ts
|
|
236
|
-
const {
|
|
152
|
+
const { submit, isLoading, error } = useFormSubmit(apiCall, {
|
|
153
|
+
onSuccess: (data) => console.log('Saved!', data)
|
|
154
|
+
});
|
|
237
155
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
clear(); // ➝ []
|
|
156
|
+
// Example
|
|
157
|
+
submit(formData);
|
|
241
158
|
```
|
|
242
|
-
|
|
243
159
|
---
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
Manages step-based navigation by keeping track of the current step index and providing functions to move forward, backward, and reset.
|
|
160
|
+
📌 **useSmartForm**
|
|
161
|
+
Complete form management with validation, submission, and state handling.
|
|
247
162
|
|
|
248
163
|
```ts
|
|
249
|
-
const {
|
|
164
|
+
const { register, handleSubmit, errors } = useSmartForm({
|
|
165
|
+
initialValues,
|
|
166
|
+
validationSchema,
|
|
167
|
+
onSubmit: (data) => console.log(data)
|
|
168
|
+
});
|
|
250
169
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
170
|
+
// Example
|
|
171
|
+
<input {...register('email') />
|
|
172
|
+
<button onClick={handleSubmit}>Save</button>
|
|
254
173
|
```
|
|
255
|
-
|
|
256
174
|
---
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
Executes a callback function after a specified delay and provides functions to reset or clear the timeout.
|
|
175
|
+
📌 **useCrossFieldValidation**
|
|
176
|
+
Handles complex validation rules that depend on multiple fields.
|
|
260
177
|
|
|
261
178
|
```ts
|
|
262
|
-
const {
|
|
179
|
+
const { errors, validate } = useCrossFieldValidation({
|
|
180
|
+
rules: {
|
|
181
|
+
password: (val, { confirmPassword }) => val === confirmPassword,
|
|
182
|
+
},
|
|
183
|
+
initialValues
|
|
184
|
+
});
|
|
263
185
|
|
|
264
|
-
|
|
265
|
-
|
|
186
|
+
// Example
|
|
187
|
+
validate('password', 'secret123', { confirmPassword: 'secret123' });
|
|
266
188
|
```
|
|
267
189
|
|
|
268
190
|
---
|
|
269
191
|
|
|
270
|
-
|
|
271
|
-
|
|
192
|
+
📌 **useFieldArray**
|
|
193
|
+
Manages dynamic arrays of form fields with add/remove/reorder operations.
|
|
272
194
|
|
|
273
195
|
```ts
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
input.onChange = debounced;
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
---
|
|
280
|
-
|
|
281
|
-
📌 **useScrollLock**
|
|
282
|
-
Locks or unlocks scrolling on the webpage when the `lock` parameter changes.
|
|
196
|
+
const { fields, append, remove, move } = useFieldArray(initialItems);
|
|
283
197
|
|
|
284
|
-
|
|
285
|
-
|
|
198
|
+
// Example
|
|
199
|
+
append({ name: '', email: '' });
|
|
200
|
+
remove(0); // Remove first item
|
|
201
|
+
move(1, 0); // Move second item to first position
|
|
286
202
|
```
|
|
287
|
-
|
|
288
203
|
---
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
Observes and returns the dimensions of an HTML element using the `ResizeObserver` API.
|
|
204
|
+
📌 **useFormWizard**
|
|
205
|
+
Manages multi-step forms with navigation between steps.
|
|
292
206
|
|
|
293
207
|
```ts
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
// size = { width: 200, height: 100 }
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
---
|
|
300
|
-
|
|
301
|
-
📌 **useMousePosition**
|
|
302
|
-
Tracks the user's mouse position in real-time and returns the coordinates.
|
|
208
|
+
const { currentStep, next, prev, goto } = useFormWizard(steps.length);
|
|
303
209
|
|
|
304
|
-
|
|
305
|
-
|
|
210
|
+
// Example
|
|
211
|
+
next(); // Move to next step
|
|
212
|
+
goto(3); // Jump to step 4
|
|
306
213
|
```
|
|
307
214
|
|
|
308
215
|
---
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
Determines whether the user is scrolling up or down based on window scroll position changes.
|
|
216
|
+
📌 **usePersistedForm**
|
|
217
|
+
Automatically persists form state to localStorage/sessionStorage and restores it on page reload.
|
|
312
218
|
|
|
313
219
|
```ts
|
|
314
|
-
const
|
|
315
|
-
```
|
|
316
|
-
|
|
317
|
-
---
|
|
318
|
-
|
|
319
|
-
📌 **useImageLoader**
|
|
320
|
-
Loads an image and provides `loaded` and `error` states to track its loading status.
|
|
220
|
+
const { values, setField, reset } = usePersistedForm('form-key', initialValues);
|
|
321
221
|
|
|
322
|
-
|
|
323
|
-
|
|
222
|
+
// Example
|
|
223
|
+
setField('username', 'john_doe'); // Auto-saves to storage
|
|
224
|
+
reset(); // Clears storage
|
|
324
225
|
```
|
|
325
226
|
|
|
326
|
-
---
|
|
327
|
-
|
|
328
|
-
📌 **usePersistedState**
|
|
329
|
-
Stores and retrieves state from `localStorage` to persist data across page reloads.
|
|
330
|
-
|
|
331
|
-
```ts
|
|
332
|
-
const [theme, setTheme] = usePersistedState('theme', 'light');
|
|
333
|
-
```
|
|
334
227
|
|
|
335
228
|
---
|
|
336
229
|
|
|
337
|
-
📌 **
|
|
338
|
-
|
|
230
|
+
📌 **useEventListener**
|
|
231
|
+
Listens for a specified event on an element or window, and triggers the handler function when the event is fired. It ensures proper cleanup when the component is unmounted.
|
|
339
232
|
|
|
340
233
|
```ts
|
|
341
|
-
|
|
234
|
+
useEventListener('resize', (e) => {
|
|
235
|
+
console.log('Window resized');
|
|
236
|
+
});
|
|
342
237
|
```
|
|
343
|
-
|
|
344
238
|
---
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
Manages cookies by providing functions to get, set, and delete a specific cookie by its key.
|
|
239
|
+
📌 **useEventListeners**
|
|
240
|
+
Simplifies adding/removing event listeners.
|
|
348
241
|
|
|
349
242
|
```ts
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
```
|
|
355
|
-
|
|
356
|
-
---
|
|
357
|
-
|
|
358
|
-
📌 **useFetchRetry**
|
|
359
|
-
Performs an HTTP request with retry logic, retrying a specified number of times upon failure before returning an error.
|
|
243
|
+
useEventListeners({
|
|
244
|
+
click: handleClick,
|
|
245
|
+
resize: handleResize
|
|
246
|
+
});
|
|
360
247
|
|
|
361
|
-
|
|
362
|
-
|
|
248
|
+
// Example
|
|
249
|
+
useEventListeners({
|
|
250
|
+
keydown: (e) => console.log(e.key)
|
|
251
|
+
});
|
|
363
252
|
```
|
|
364
|
-
|
|
365
253
|
---
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
Delays the update of a value for a specified amount of time before reflecting it in the state.
|
|
254
|
+
📌 **useKeyPress**
|
|
255
|
+
Detects whether a specific key is being pressed or released.
|
|
369
256
|
|
|
370
257
|
```ts
|
|
371
|
-
const
|
|
258
|
+
const isEnterPressed = useKeyPress('Enter');
|
|
372
259
|
```
|
|
373
|
-
|
|
374
260
|
---
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
Tracks the visibility state of the document and returns whether the page is currently visible or hidden.
|
|
261
|
+
📌 **useLongPress**
|
|
262
|
+
Detects long press gestures.
|
|
378
263
|
|
|
379
264
|
```ts
|
|
380
|
-
const
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
---
|
|
384
|
-
|
|
385
|
-
📌 **useDebouncedValue**
|
|
386
|
-
Debounces a value to avoid immediate changes, returning the delayed value after a specified delay.
|
|
265
|
+
const handlers = useLongPress(() => console.log('Long pressed!'), {
|
|
266
|
+
threshold: 1000
|
|
267
|
+
});
|
|
387
268
|
|
|
388
|
-
|
|
389
|
-
|
|
269
|
+
// Example
|
|
270
|
+
<button {...handlers}>Hold me</button>
|
|
390
271
|
```
|
|
391
|
-
|
|
392
272
|
---
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
Handles the state of an asynchronous function, including loading, success, and error states, with the ability to trigger the execution of the async operation.
|
|
273
|
+
📌 **useTouch**
|
|
274
|
+
Tracks touch events on a given DOM element, returning the touch positions for `touchstart`, `touchmove`, and `touchend`.
|
|
396
275
|
|
|
397
276
|
```ts
|
|
398
|
-
const
|
|
277
|
+
const ref = useRef(null);
|
|
278
|
+
const {
|
|
279
|
+
touchStart,
|
|
280
|
+
touchMove,
|
|
281
|
+
touchEnd
|
|
282
|
+
} = useTouch(ref);
|
|
399
283
|
|
|
400
|
-
|
|
284
|
+
<div ref={ref}>Swipe here</div>
|
|
401
285
|
```
|
|
402
|
-
|
|
403
286
|
---
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
Dynamically loads an external script, managing its loading, ready, and error states, and optionally removes the script on unmount.
|
|
287
|
+
📌 **useIdle**
|
|
288
|
+
Detects when user becomes inactive.
|
|
407
289
|
|
|
408
290
|
```ts
|
|
409
|
-
const
|
|
291
|
+
const isIdle = useIdle(30000); // 30s timeout
|
|
410
292
|
|
|
411
|
-
//
|
|
293
|
+
// Example
|
|
294
|
+
{isIdle && <ScreenSaver />}
|
|
412
295
|
```
|
|
413
|
-
|
|
414
296
|
---
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
Fetches data from an IndexedDB store, handling errors and updating the state with the fetched data.
|
|
297
|
+
📌 **useDragReorder**
|
|
298
|
+
Enables drag-and-drop reordering of lists.
|
|
418
299
|
|
|
419
300
|
```ts
|
|
420
|
-
const {
|
|
421
|
-
```
|
|
422
|
-
|
|
423
|
-
---
|
|
424
|
-
|
|
425
|
-
📌 **useGeoLocation**
|
|
426
|
-
Retrieves the user's current geographic location and returns it along with any error encountered.
|
|
301
|
+
const { items, dragStart, dragOver, drop } = useDragReorder(initialItems);
|
|
427
302
|
|
|
428
|
-
|
|
429
|
-
|
|
303
|
+
// Example
|
|
304
|
+
<div
|
|
305
|
+
draggable
|
|
306
|
+
onDragStart={() => dragStart(index)}
|
|
307
|
+
onDragOver={() => dragOver(index)}
|
|
308
|
+
onDrop={drop}
|
|
309
|
+
/>
|
|
430
310
|
```
|
|
431
311
|
|
|
432
312
|
---
|
|
433
313
|
|
|
434
|
-
📌 **
|
|
435
|
-
|
|
314
|
+
📌 **useToggle**
|
|
315
|
+
A simple boolean toggle hook that returns a state and a function to toggle its value.
|
|
436
316
|
|
|
437
317
|
```ts
|
|
438
|
-
const
|
|
318
|
+
const [isOpen, toggle] = useToggle(false);
|
|
439
319
|
```
|
|
440
|
-
|
|
441
320
|
---
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
Tracks whether the component is mounted or unmounted, returning a boolean value indicating the component's mount status.
|
|
321
|
+
📌 **useArray**
|
|
322
|
+
Provides an array state with helper functions to manipulate it, including setting, adding, removing by index, and clearing elements.
|
|
445
323
|
|
|
446
324
|
```ts
|
|
447
|
-
const
|
|
448
|
-
```
|
|
449
|
-
|
|
450
|
-
---
|
|
451
|
-
|
|
452
|
-
📌 **useCss**
|
|
453
|
-
Dynamically applies custom CSS to the document and removes it when the component unmounts, handling any errors during the process.
|
|
325
|
+
const { array, set, push, removeByIndex, clear } = useArray([1, 2, 3]);
|
|
454
326
|
|
|
455
|
-
|
|
456
|
-
|
|
327
|
+
push(4); // ➝ [1, 2, 3, 4]
|
|
328
|
+
removeByIndex(1); // ➝ [1, 3, 4]
|
|
329
|
+
clear(); // ➝ []
|
|
457
330
|
```
|
|
458
|
-
|
|
459
331
|
---
|
|
332
|
+
📌 **useStepper**
|
|
333
|
+
Manages step-based navigation by keeping track of the current step index and providing functions to move forward, backward, and reset.
|
|
460
334
|
|
|
461
|
-
📌 **useSpeak**
|
|
462
|
-
Converts text to speech using the Web Speech API, allowing the text to be spoken and handling errors.
|
|
463
335
|
```ts
|
|
464
|
-
const {
|
|
465
|
-
speak();
|
|
466
|
-
```
|
|
467
|
-
|
|
468
|
-
---
|
|
469
|
-
|
|
470
|
-
📌 **useCountUp**
|
|
471
|
-
Animates a counter that counts up from 0 to a target value within a specified duration, returning the current count and any errors.
|
|
336
|
+
const { activeStep, handleNext, handleBack, handleReset, isFirstStep, isLastStep } = useStepper(5);
|
|
472
337
|
|
|
473
|
-
|
|
474
|
-
|
|
338
|
+
handleNext(); // ➝ advances step
|
|
339
|
+
handleBack(); // ➝ goes back
|
|
340
|
+
handleReset(); // ➝ step 0
|
|
475
341
|
```
|
|
476
342
|
|
|
477
343
|
---
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
Counts down from a specified start value to zero, updating every second and returning the current count and any errors.
|
|
344
|
+
📌 **useUndo**
|
|
345
|
+
Provides undo/redo functionality for state changes.
|
|
481
346
|
|
|
482
347
|
```ts
|
|
483
|
-
const {
|
|
484
|
-
```
|
|
485
|
-
|
|
486
|
-
---
|
|
487
|
-
|
|
488
|
-
📌 **useBattery**
|
|
489
|
-
Monitors the battery status of the device, including level, charging status, and remaining charging or discharging time.
|
|
348
|
+
const { state, setState, undo, redo } = useUndo(initialState);
|
|
490
349
|
|
|
491
|
-
|
|
492
|
-
|
|
350
|
+
// Example
|
|
351
|
+
setState({ items: [...] });
|
|
352
|
+
undo(); // Reverts to previous state
|
|
493
353
|
```
|
|
494
|
-
|
|
495
354
|
---
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
Listens for a specified event on an element or window, and triggers the handler function when the event is fired. It ensures proper cleanup when the component is unmounted.
|
|
355
|
+
📌 **useAdvReducer**
|
|
356
|
+
A custom reducer function that takes an `initialState` and an object of actions, returning a new state based on the action type.
|
|
499
357
|
|
|
500
358
|
```ts
|
|
501
|
-
|
|
502
|
-
|
|
359
|
+
const reducer = useAdvReducer(initialState, {
|
|
360
|
+
increment: (state) => ({ count: state.count + 1 }),
|
|
503
361
|
});
|
|
362
|
+
const [state, dispatch] = useReducer(reducer, initialState);
|
|
504
363
|
```
|
|
505
|
-
|
|
506
|
-
---
|
|
507
|
-
|
|
508
|
-
📌 **useHistory**
|
|
509
|
-
Provides methods to navigate the browser history, including pushing and replacing history states, as well as moving back and forward in the history stack.
|
|
510
|
-
|
|
511
|
-
```ts
|
|
512
|
-
const {
|
|
513
|
-
history,
|
|
514
|
-
state,
|
|
515
|
-
push,
|
|
516
|
-
replace,
|
|
517
|
-
goBack,
|
|
518
|
-
goForward
|
|
519
|
-
} = useHistory();
|
|
520
|
-
|
|
521
|
-
push('/about', { from: 'home' });
|
|
522
|
-
replace('/profile');
|
|
523
|
-
goBack();
|
|
524
|
-
goForward();
|
|
525
|
-
```
|
|
526
|
-
|
|
527
|
-
---
|
|
528
|
-
|
|
529
|
-
📌 **usePreferredLanguage**
|
|
530
|
-
Retrieves the user's preferred language and languages from the browser, and checks if the browser supports the language APIs.
|
|
531
|
-
|
|
532
|
-
```ts
|
|
533
|
-
const {
|
|
534
|
-
language,
|
|
535
|
-
languages,
|
|
536
|
-
isSupported
|
|
537
|
-
} = usePreferredLanguage();
|
|
538
|
-
|
|
539
|
-
console.log(language); // e.g., "en-US"
|
|
540
|
-
```
|
|
541
|
-
|
|
542
364
|
---
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
Stores and retrieves data from the `sessionStorage` API, persisting the data during the session. It provides a getter and setter for session-based data.
|
|
365
|
+
📌 **useIsMounted**
|
|
366
|
+
Tracks whether the component is mounted or unmounted, returning a boolean value indicating the component's mount status.
|
|
546
367
|
|
|
547
368
|
```ts
|
|
548
|
-
const
|
|
549
|
-
|
|
550
|
-
setValue("newValue");
|
|
551
|
-
console.log(value);
|
|
369
|
+
const isMounted = useIsMounted();
|
|
552
370
|
```
|
|
553
|
-
|
|
554
371
|
---
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
Controls the playback of an audio file, including play, pause, and stop functionalities, while also managing the audio volume and handling any errors.
|
|
372
|
+
📌 **useIsFirstRender**
|
|
373
|
+
Returns true only on component's first render.
|
|
558
374
|
|
|
559
375
|
```ts
|
|
560
|
-
const
|
|
561
|
-
play,
|
|
562
|
-
pause,
|
|
563
|
-
stop,
|
|
564
|
-
setVolume,
|
|
565
|
-
isPlaying,
|
|
566
|
-
error
|
|
567
|
-
} = useSound("/sound.mp3");
|
|
376
|
+
const isFirst = useIsFirstRender();
|
|
568
377
|
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
378
|
+
// Example
|
|
379
|
+
useEffect(() => {
|
|
380
|
+
if (!isFirst) console.log('Updated!');
|
|
381
|
+
}, [deps]);
|
|
573
382
|
```
|
|
574
|
-
|
|
575
383
|
---
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
Tracks touch events on a given DOM element, returning the touch positions for `touchstart`, `touchmove`, and `touchend`.
|
|
384
|
+
📌 **usePrevious**
|
|
385
|
+
Tracks the previous value of a state or prop.
|
|
579
386
|
|
|
580
387
|
```ts
|
|
581
|
-
const
|
|
582
|
-
const {
|
|
583
|
-
touchStart,
|
|
584
|
-
touchMove,
|
|
585
|
-
touchEnd
|
|
586
|
-
} = useTouch(ref);
|
|
587
|
-
|
|
588
|
-
<div ref={ref}>Swipe here</div>
|
|
388
|
+
const previousValue = usePrevious(currentValue);
|
|
589
389
|
```
|
|
590
|
-
|
|
591
390
|
---
|
|
592
|
-
|
|
593
391
|
📌 **useUpdateEffect**
|
|
594
392
|
Executes a side effect on updates after the initial render, skipping the effect during the first render to avoid unnecessary executions.
|
|
595
393
|
|
|
596
394
|
```ts
|
|
597
395
|
useUpdateEffect(() => {
|
|
598
|
-
console.log("This runs only on updates.");
|
|
599
|
-
}, [value]);
|
|
600
|
-
```
|
|
601
|
-
|
|
602
|
-
---
|
|
603
|
-
|
|
604
|
-
📌 **usePersistedForm**
|
|
605
|
-
Automatically persists form state to localStorage/sessionStorage and restores it on page reload.
|
|
606
|
-
|
|
607
|
-
```ts
|
|
608
|
-
const { values, setField, reset } = usePersistedForm('form-key', initialValues);
|
|
609
|
-
|
|
610
|
-
// Example
|
|
611
|
-
setField('username', 'john_doe'); // Auto-saves to storage
|
|
612
|
-
reset(); // Clears storage
|
|
613
|
-
```
|
|
614
|
-
|
|
615
|
-
---
|
|
616
|
-
|
|
617
|
-
📌 **useCrossFieldValidation**
|
|
618
|
-
Handles complex validation rules that depend on multiple fields.
|
|
619
|
-
|
|
620
|
-
```ts
|
|
621
|
-
const { errors, validate } = useCrossFieldValidation({
|
|
622
|
-
rules: {
|
|
623
|
-
password: (val, { confirmPassword }) => val === confirmPassword,
|
|
624
|
-
},
|
|
625
|
-
initialValues
|
|
626
|
-
});
|
|
627
|
-
|
|
628
|
-
// Example
|
|
629
|
-
validate('password', 'secret123', { confirmPassword: 'secret123' });
|
|
396
|
+
console.log("This runs only on updates.");
|
|
397
|
+
}, [value]);
|
|
630
398
|
```
|
|
631
|
-
|
|
632
399
|
---
|
|
633
400
|
|
|
634
|
-
📌 **
|
|
635
|
-
Manages
|
|
401
|
+
📌 **useList**
|
|
402
|
+
Manages array state with utility methods.
|
|
636
403
|
|
|
637
404
|
```ts
|
|
638
|
-
const {
|
|
405
|
+
const { list, push, remove, update } = useList(initialItems);
|
|
639
406
|
|
|
640
407
|
// Example
|
|
641
|
-
|
|
408
|
+
push(newItem);
|
|
642
409
|
remove(0); // Remove first item
|
|
643
|
-
move(1, 0); // Move second item to first position
|
|
644
410
|
```
|
|
645
411
|
|
|
646
412
|
---
|
|
647
413
|
|
|
648
|
-
📌 **
|
|
649
|
-
|
|
414
|
+
📌 **useLocalStorage**
|
|
415
|
+
A hook to persist state in `localStorage`. It initializes state from `localStorage` and updates it whenever the value changes.
|
|
650
416
|
|
|
651
417
|
```ts
|
|
652
|
-
const
|
|
653
|
-
onSuccess: (data) => console.log('Saved!', data)
|
|
654
|
-
});
|
|
655
|
-
|
|
656
|
-
// Example
|
|
657
|
-
submit(formData);
|
|
418
|
+
const [token, setToken] = useLocalStorage('authToken', '');
|
|
658
419
|
```
|
|
659
|
-
|
|
660
420
|
---
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
Complete form management with validation, submission, and state handling.
|
|
421
|
+
📌 **useSessionStorage**
|
|
422
|
+
Stores and retrieves data from the `sessionStorage` API, persisting the data during the session. It provides a getter and setter for session-based data.
|
|
664
423
|
|
|
665
424
|
```ts
|
|
666
|
-
const
|
|
667
|
-
initialValues,
|
|
668
|
-
validationSchema,
|
|
669
|
-
onSubmit: (data) => console.log(data)
|
|
670
|
-
});
|
|
425
|
+
const [value, setValue] = useSessionStorage("myKey", "default");
|
|
671
426
|
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
<button onClick={handleSubmit}>Save</button>
|
|
427
|
+
setValue("newValue");
|
|
428
|
+
console.log(value);
|
|
675
429
|
```
|
|
676
430
|
|
|
677
431
|
---
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
Provides undo/redo functionality for state changes.
|
|
432
|
+
📌 **useCookie**
|
|
433
|
+
Manages cookies by providing functions to get, set, and delete a specific cookie by its key.
|
|
681
434
|
|
|
682
435
|
```ts
|
|
683
|
-
const
|
|
436
|
+
const [token, setToken, deleteToken] = useCookie('token');
|
|
684
437
|
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
undo(); // Reverts to previous state
|
|
438
|
+
setToken('abc123');
|
|
439
|
+
deleteToken();
|
|
688
440
|
```
|
|
689
|
-
|
|
690
441
|
---
|
|
442
|
+
📌 **usePersistedState**
|
|
443
|
+
Stores and retrieves state from `localStorage` to persist data across page reloads.
|
|
691
444
|
|
|
692
|
-
|
|
693
|
-
|
|
445
|
+
```ts
|
|
446
|
+
const [theme, setTheme] = usePersistedState('theme', 'light');
|
|
447
|
+
```
|
|
448
|
+
---
|
|
449
|
+
📌 **useHistory**
|
|
450
|
+
Provides methods to navigate the browser history, including pushing and replacing history states, as well as moving back and forward in the history stack.
|
|
694
451
|
|
|
695
452
|
```ts
|
|
696
|
-
const {
|
|
453
|
+
const {
|
|
454
|
+
history,
|
|
455
|
+
state,
|
|
456
|
+
push,
|
|
457
|
+
replace,
|
|
458
|
+
goBack,
|
|
459
|
+
goForward
|
|
460
|
+
} = useHistory();
|
|
697
461
|
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
462
|
+
push('/about', { from: 'home' });
|
|
463
|
+
replace('/profile');
|
|
464
|
+
goBack();
|
|
465
|
+
goForward();
|
|
701
466
|
```
|
|
702
467
|
|
|
703
468
|
---
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
Manages WebSocket connections with auto-reconnect.
|
|
469
|
+
📌 **useHistoryState**
|
|
470
|
+
Manages state history with undo/redo capabilities.
|
|
707
471
|
|
|
708
472
|
```ts
|
|
709
|
-
const {
|
|
473
|
+
const { state, setState, undo, redo } = useHistoryState(initialState);
|
|
710
474
|
|
|
711
475
|
// Example
|
|
712
|
-
|
|
476
|
+
setState({...});
|
|
477
|
+
undo(); // Reverts change
|
|
713
478
|
```
|
|
714
|
-
|
|
715
479
|
---
|
|
480
|
+
📌 **useVisibilityChange**
|
|
481
|
+
Tracks the visibility state of the document and returns whether the page is currently visible or hidden.
|
|
716
482
|
|
|
717
|
-
|
|
718
|
-
|
|
483
|
+
```ts
|
|
484
|
+
const isVisible = useVisibilityChange(); // ➝ true | false
|
|
485
|
+
```
|
|
486
|
+
---
|
|
487
|
+
📌 **useDarkMode**
|
|
488
|
+
Detects and manages dark mode preferences in the browser.
|
|
719
489
|
|
|
720
490
|
```ts
|
|
721
|
-
const
|
|
491
|
+
const [isDarkMode, toggleDarkMode] = useDarkMode();
|
|
492
|
+
```
|
|
493
|
+
---
|
|
494
|
+
📌 **usePreferredLanguage**
|
|
495
|
+
Detects the preferred language set in the browser.
|
|
722
496
|
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
draggable
|
|
726
|
-
onDragStart={() => dragStart(index)}
|
|
727
|
-
onDragOver={() => dragOver(index)}
|
|
728
|
-
onDrop={drop}
|
|
729
|
-
/>
|
|
497
|
+
```ts
|
|
498
|
+
const language = usePreferredLanguage();
|
|
730
499
|
```
|
|
500
|
+
---
|
|
501
|
+
📌 **useIndexedDB**
|
|
502
|
+
Provides a hook to interact with IndexedDB in the browser, enabling persistent client-side storage.
|
|
731
503
|
|
|
504
|
+
```ts
|
|
505
|
+
const { data, setData } = useIndexedDB('dbName');
|
|
506
|
+
```
|
|
732
507
|
---
|
|
733
508
|
|
|
734
|
-
📌 **
|
|
735
|
-
|
|
509
|
+
📌 **useMediaQuery**
|
|
510
|
+
Checks if a media query condition matches the current viewport, useful for responsive design.
|
|
736
511
|
|
|
737
512
|
```ts
|
|
738
|
-
const
|
|
739
|
-
|
|
740
|
-
// Example
|
|
741
|
-
<div onScroll={loadMore}>...</div>
|
|
513
|
+
const isMobile = useMediaQuery('(max-width: 768px)');
|
|
742
514
|
```
|
|
743
|
-
|
|
744
515
|
---
|
|
516
|
+
📌 **useWindowSize**
|
|
517
|
+
Returns the current window size (width and height).
|
|
745
518
|
|
|
746
|
-
|
|
747
|
-
|
|
519
|
+
```ts
|
|
520
|
+
const { width, height } = useWindowSize();
|
|
521
|
+
```
|
|
522
|
+
---
|
|
523
|
+
📌 **useResizeObserver**
|
|
524
|
+
Observes and returns the dimensions of an HTML element using the `ResizeObserver` API.
|
|
748
525
|
|
|
749
526
|
```ts
|
|
750
|
-
|
|
751
|
-
click: handleClick,
|
|
752
|
-
resize: handleResize
|
|
753
|
-
});
|
|
527
|
+
const size = useResizeObserver(ref);
|
|
754
528
|
|
|
755
|
-
//
|
|
756
|
-
useEventListeners({
|
|
757
|
-
keydown: (e) => console.log(e.key)
|
|
758
|
-
});
|
|
529
|
+
// size = { width: 200, height: 100 }
|
|
759
530
|
```
|
|
760
|
-
|
|
761
531
|
---
|
|
532
|
+
📌 **useScrollPosition**
|
|
533
|
+
Tracks the horizontal (x) and vertical (y) scroll positions of the window.
|
|
762
534
|
|
|
763
|
-
|
|
764
|
-
|
|
535
|
+
```ts
|
|
536
|
+
const { x, y } = useScrollPosition();
|
|
537
|
+
```
|
|
538
|
+
---
|
|
539
|
+
📌 **useScrollDirection**
|
|
540
|
+
Determines whether the user is scrolling up or down based on window scroll position changes.
|
|
765
541
|
|
|
766
542
|
```ts
|
|
767
|
-
const
|
|
543
|
+
const direction = useScrollDirection(); // ➝ 'up' | 'down'
|
|
544
|
+
```
|
|
768
545
|
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
546
|
+
📌 **useScrollLock**
|
|
547
|
+
Locks or unlocks scrolling on the page.
|
|
548
|
+
|
|
549
|
+
```ts
|
|
550
|
+
useScrollLock(true); // Lock scrolling
|
|
551
|
+
useScrollLock(false); // Unlock scrolling
|
|
772
552
|
```
|
|
553
|
+
---
|
|
554
|
+
📌 **useLockBodyScroll**
|
|
555
|
+
Prevents scrolling on the body element, useful for modals or off-canvas menus.
|
|
773
556
|
|
|
557
|
+
```ts
|
|
558
|
+
useLockBodyScroll(true);
|
|
559
|
+
```
|
|
774
560
|
---
|
|
561
|
+
📌 **useCss**
|
|
562
|
+
Allows injecting dynamic CSS rules into the document.
|
|
775
563
|
|
|
776
|
-
|
|
777
|
-
|
|
564
|
+
```ts
|
|
565
|
+
useCss('.class { color: red; }');
|
|
566
|
+
```
|
|
567
|
+
---
|
|
568
|
+
📌 **useReducedMotion**
|
|
569
|
+
Detects whether the user has reduced motion settings in their OS preferences.
|
|
778
570
|
|
|
779
571
|
```ts
|
|
780
|
-
const
|
|
572
|
+
const prefersReducedMotion = useReducedMotion();
|
|
573
|
+
```
|
|
574
|
+
---
|
|
575
|
+
📌 **useImageLoader**
|
|
576
|
+
Provides a way to load and display images in a way that improves performance and loading behavior.
|
|
781
577
|
|
|
782
|
-
|
|
783
|
-
{
|
|
578
|
+
```ts
|
|
579
|
+
const { isLoading, image } = useImageLoader('imageURL');
|
|
784
580
|
```
|
|
785
581
|
|
|
786
582
|
---
|
|
787
583
|
|
|
788
|
-
📌 **
|
|
789
|
-
|
|
584
|
+
📌 **useBattery**
|
|
585
|
+
Monitors the battery status of the device, including level, charging status, and remaining charging or discharging time.
|
|
790
586
|
|
|
791
587
|
```ts
|
|
792
|
-
const
|
|
793
|
-
|
|
794
|
-
// Example
|
|
795
|
-
useEffect(() => {
|
|
796
|
-
if (!isFirst) console.log('Updated!');
|
|
797
|
-
}, [deps]);
|
|
588
|
+
const { level, charging, supported, loading, chargingTime, dischargingTime } = useBattery();
|
|
798
589
|
```
|
|
799
|
-
|
|
800
590
|
---
|
|
591
|
+
📌 **useGeoLocation**
|
|
592
|
+
Retrieves the user's current geographic location and returns it along with any error encountered.
|
|
801
593
|
|
|
802
|
-
|
|
803
|
-
|
|
594
|
+
```ts
|
|
595
|
+
const { position, error } = useGeoLocation();
|
|
596
|
+
```
|
|
597
|
+
---
|
|
598
|
+
📌 **useMousePosition**
|
|
599
|
+
Tracks the user's mouse position in real-time and returns the coordinates.
|
|
804
600
|
|
|
805
601
|
```ts
|
|
806
|
-
const {
|
|
602
|
+
const { x, y } = useMousePosition();
|
|
603
|
+
```
|
|
807
604
|
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
605
|
+
---
|
|
606
|
+
📌 **useOnlineStatus**
|
|
607
|
+
Monitors the user's network connection status (online/offline).
|
|
608
|
+
|
|
609
|
+
```ts
|
|
610
|
+
const isOnline = useOnlineStatus();
|
|
811
611
|
```
|
|
812
612
|
|
|
813
613
|
---
|
|
814
614
|
|
|
815
|
-
📌 **
|
|
816
|
-
|
|
615
|
+
📌 **useSound**
|
|
616
|
+
Controls the playback of an audio file, including play, pause, and stop functionalities, while also managing the audio volume and handling any errors.
|
|
817
617
|
|
|
818
618
|
```ts
|
|
819
|
-
|
|
619
|
+
const {
|
|
620
|
+
play,
|
|
621
|
+
pause,
|
|
622
|
+
stop,
|
|
623
|
+
setVolume,
|
|
624
|
+
isPlaying,
|
|
625
|
+
error
|
|
626
|
+
} = useSound("/sound.mp3");
|
|
820
627
|
|
|
821
|
-
|
|
822
|
-
|
|
628
|
+
play();
|
|
629
|
+
pause();
|
|
630
|
+
stop();
|
|
631
|
+
setVolume(0.5);
|
|
823
632
|
```
|
|
824
|
-
|
|
825
633
|
---
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
634
|
+
📌 **useSpeak**
|
|
635
|
+
Converts text to speech using the Web Speech API, allowing the text to be spoken and handling errors.
|
|
636
|
+
```ts
|
|
637
|
+
const { speak, error } = useSpeak('Hello world!');
|
|
638
|
+
speak();
|
|
639
|
+
```
|
|
640
|
+
---
|
|
641
|
+
📌 **useCountUp**
|
|
642
|
+
Animates a counter that counts up from 0 to a target value within a specified duration, returning the current count and any errors.
|
|
829
643
|
|
|
830
644
|
```ts
|
|
831
|
-
const
|
|
832
|
-
|
|
833
|
-
});
|
|
645
|
+
const { count, error } = useCountUp(100, 5000); // counts up to 100 in 5s
|
|
646
|
+
```
|
|
834
647
|
|
|
835
|
-
|
|
836
|
-
|
|
648
|
+
---
|
|
649
|
+
📌 **useCountDown**
|
|
650
|
+
Counts down from a specified start value to zero, updating every second and returning the current count and any errors.
|
|
651
|
+
|
|
652
|
+
```ts
|
|
653
|
+
const { count, error } = useCountDown(10); // counts down from 10
|
|
837
654
|
```
|
|
838
655
|
|
|
839
656
|
---
|
|
840
657
|
|
|
841
|
-
📌 **
|
|
842
|
-
|
|
658
|
+
📌 **useWebSocket**
|
|
659
|
+
Manages WebSocket connections with auto-reconnect.
|
|
843
660
|
|
|
844
661
|
```ts
|
|
845
|
-
const {
|
|
662
|
+
const { send, message, status } = useWebSocket('ws://api.example.com');
|
|
846
663
|
|
|
847
664
|
// Example
|
|
848
|
-
|
|
665
|
+
send(JSON.stringify({ action: 'ping' }));
|
|
849
666
|
```
|
|
850
|
-
|
|
851
667
|
---
|
|
852
668
|
|
|
853
|
-
|
|
669
|
+
Let me know if you'd like more information or changes!
|
|
670
|
+
|
|
671
|
+
------------------------------------- **Custom Components** --------------------------------------------
|
|
854
672
|
|
|
855
673
|
✅ **DynamicLoader**
|
|
856
674
|
A Higher-Order Component (HOC) that dynamically loads a React component using `React.lazy()` with `Suspense`, while wrapping it in an `ErrorBoundary` to handle potential errors gracefully. It ensures that the component is loaded only when needed, reducing the initial bundle size and improving performance.
|
|
@@ -950,6 +768,18 @@ A functional React component that displays dynamic alerts using the enqueueSnack
|
|
|
950
768
|
```jsx
|
|
951
769
|
<AlertMessage type="success" msg="Data saved successfully!" />
|
|
952
770
|
```
|
|
771
|
+
---
|
|
772
|
+
✅ **FieldSkeleton**
|
|
773
|
+
Simulates form field placeholders with optional label skeletons, useful for loading states in forms or input sections.
|
|
774
|
+
|
|
775
|
+
```jsx
|
|
776
|
+
<FieldSkeleton
|
|
777
|
+
length={3}
|
|
778
|
+
spacing={2}
|
|
779
|
+
isLabel={true}
|
|
780
|
+
responsive={{ xs: 12, sm: 6, md: 4 }}
|
|
781
|
+
/>
|
|
782
|
+
```
|
|
953
783
|
---
|
|
954
784
|
|
|
955
785
|
✅ **FileSkeleton**
|
|
@@ -1006,4 +836,4 @@ Displays a skeleton placeholder for a pie chart inside a card layout. Includes a
|
|
|
1006
836
|
|
|
1007
837
|
**License**
|
|
1008
838
|
|
|
1009
|
-
[MIT](https://
|
|
839
|
+
[MIT](https://docs.npmjs.com/policies/npm-license) © (2022-2024)
|
|
@@ -126,7 +126,7 @@ export var FilePreview = function (_a) {
|
|
|
126
126
|
overflow: 'hidden',
|
|
127
127
|
textOverflow: 'ellipsis',
|
|
128
128
|
maxWidth: width - 120,
|
|
129
|
-
}, children: filename })), _jsx(Typography, { variant: "caption", color: "text.secondary", children: size })] }), _jsx(IconButton, { disabled: onDownload === undefined, "aria-label": "download", onClick: function () { return onDownload(primaryKey); }, sx: { color: '#dfdfdf', '&:hover': { color: 'primary.main' } }, children: isDownloading ? (_jsx(CircularProgress, { color: "success", size: 20 })) : (_jsx(Download, { fontSize: "small", color:
|
|
129
|
+
}, children: filename })), _jsx(Typography, { variant: "caption", color: "text.secondary", children: size })] }), _jsx(IconButton, { disabled: onDownload === undefined, "aria-label": "download", onClick: function () { return onDownload(primaryKey); }, sx: { color: '#dfdfdf', '&:hover': { color: 'primary.main' } }, children: isDownloading ? (_jsx(CircularProgress, { color: "success", size: 20 })) : (_jsx(Download, { fontSize: "small", color: onDownload ? 'primary' : 'inherit' })) })] }) }));
|
|
130
130
|
};
|
|
131
131
|
var VisuallyHiddenInput = forwardRef(function (props, ref) { return (_jsx("input", __assign({ ref: ref, type: "file", style: {
|
|
132
132
|
position: 'absolute',
|
|
@@ -69,6 +69,19 @@ export declare const useStepper: (totalSteps: number) => {
|
|
|
69
69
|
isLastStep: boolean;
|
|
70
70
|
isProccesing: boolean;
|
|
71
71
|
setIsProccesing: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
72
|
+
goto: (step: number) => void;
|
|
73
|
+
};
|
|
74
|
+
export declare const useSteps: (totalSteps: number) => {
|
|
75
|
+
activeStep: number;
|
|
76
|
+
handleNext: () => void;
|
|
77
|
+
handleBack: () => void;
|
|
78
|
+
handleReset: () => void;
|
|
79
|
+
goto: (step: number) => void;
|
|
80
|
+
totalSteps: number;
|
|
81
|
+
isFirstStep: boolean;
|
|
82
|
+
isLastStep: boolean;
|
|
83
|
+
isProcessing: boolean;
|
|
84
|
+
setIsProcessing: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
72
85
|
};
|
|
73
86
|
export declare function useTimeoutFn(callback: () => void, delay: number | null): {
|
|
74
87
|
reset: () => void;
|
|
@@ -431,6 +431,14 @@ export var useStepper = function (totalSteps) {
|
|
|
431
431
|
var handleReset = function () {
|
|
432
432
|
setActiveStep(0);
|
|
433
433
|
};
|
|
434
|
+
var goto = function (step) {
|
|
435
|
+
if (step >= 0 && step < totalSteps) {
|
|
436
|
+
setActiveStep(step);
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
console.warn("Step ".concat(step, " is out of bounds (0-").concat(totalSteps - 1, ")"));
|
|
440
|
+
}
|
|
441
|
+
};
|
|
434
442
|
return {
|
|
435
443
|
activeStep: activeStep,
|
|
436
444
|
handleNext: handleNext,
|
|
@@ -441,6 +449,44 @@ export var useStepper = function (totalSteps) {
|
|
|
441
449
|
isLastStep: activeStep === totalSteps - 1,
|
|
442
450
|
isProccesing: isProccesing,
|
|
443
451
|
setIsProccesing: setIsProccesing,
|
|
452
|
+
goto: goto
|
|
453
|
+
};
|
|
454
|
+
};
|
|
455
|
+
export var useSteps = function (totalSteps) {
|
|
456
|
+
var _a = useState(0), activeStep = _a[0], setActiveStep = _a[1];
|
|
457
|
+
var _b = useState(false), isProcessing = _b[0], setIsProcessing = _b[1];
|
|
458
|
+
var handleNext = function () {
|
|
459
|
+
if (activeStep < totalSteps - 1) {
|
|
460
|
+
setActiveStep(function (prev) { return prev + 1; });
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
var handleBack = function () {
|
|
464
|
+
if (activeStep > 0) {
|
|
465
|
+
setActiveStep(function (prev) { return prev - 1; });
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
var handleReset = function () {
|
|
469
|
+
setActiveStep(0);
|
|
470
|
+
};
|
|
471
|
+
var goto = function (step) {
|
|
472
|
+
if (step >= 0 && step < totalSteps) {
|
|
473
|
+
setActiveStep(step);
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
console.warn("Step ".concat(step, " is out of bounds (0-").concat(totalSteps - 1, ")"));
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
return {
|
|
480
|
+
activeStep: activeStep,
|
|
481
|
+
handleNext: handleNext,
|
|
482
|
+
handleBack: handleBack,
|
|
483
|
+
handleReset: handleReset,
|
|
484
|
+
goto: goto,
|
|
485
|
+
totalSteps: totalSteps,
|
|
486
|
+
isFirstStep: activeStep === 0,
|
|
487
|
+
isLastStep: activeStep === totalSteps - 1,
|
|
488
|
+
isProcessing: isProcessing,
|
|
489
|
+
setIsProcessing: setIsProcessing,
|
|
444
490
|
};
|
|
445
491
|
};
|
|
446
492
|
export function useTimeoutFn(callback, delay) {
|
|
@@ -10,10 +10,14 @@ export declare const TableSkeleton: ({ rows, columns }: {
|
|
|
10
10
|
export declare const LineChartSkeleton: () => import("react/jsx-runtime").JSX.Element;
|
|
11
11
|
export declare const CardSkeleton: () => import("react/jsx-runtime").JSX.Element;
|
|
12
12
|
export declare const PieChartSkeleton: React.FC;
|
|
13
|
-
export declare const FieldSkeleton: ({ length, width, spacing, isLabel,
|
|
13
|
+
export declare const FieldSkeleton: ({ length, width, spacing, isLabel, responsive, }: {
|
|
14
14
|
length?: number | undefined;
|
|
15
15
|
width?: string | undefined;
|
|
16
16
|
spacing?: number | undefined;
|
|
17
17
|
isLabel?: boolean | undefined;
|
|
18
|
-
|
|
18
|
+
responsive?: {
|
|
19
|
+
xs: number;
|
|
20
|
+
sm: number;
|
|
21
|
+
md: number;
|
|
22
|
+
} | undefined;
|
|
19
23
|
}) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
import { createElement as _createElement } from "react";
|
|
1
13
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
14
|
import { Box, Card, CardContent, CardHeader, Grid, IconButton, Skeleton, Table, TableBody, TableCell, TableHead, TableRow, Toolbar, Tooltip, } from "@mui/material";
|
|
3
15
|
import { Download } from "@mui/icons-material";
|
|
@@ -88,7 +100,7 @@ export var PieChartSkeleton = function () {
|
|
|
88
100
|
}, children: _jsx(Skeleton, { variant: "circular", width: 200, height: 200, sx: { marginBottom: '16px' } }) })] }) })] }));
|
|
89
101
|
};
|
|
90
102
|
export var FieldSkeleton = function (_a) {
|
|
91
|
-
var _b = _a.length, length = _b === void 0 ? 1 : _b, _c = _a.width, width = _c === void 0 ? '100%' : _c, _d = _a.spacing, spacing = _d === void 0 ? 2 : _d, _e = _a.isLabel, isLabel = _e === void 0 ? false : _e, _f = _a.
|
|
92
|
-
|
|
93
|
-
|
|
103
|
+
var _b = _a.length, length = _b === void 0 ? 1 : _b, _c = _a.width, width = _c === void 0 ? '100%' : _c, _d = _a.spacing, spacing = _d === void 0 ? 2 : _d, _e = _a.isLabel, isLabel = _e === void 0 ? false : _e, _f = _a.responsive, responsive = _f === void 0 ? { xs: 12, sm: 6, md: 4 } : _f;
|
|
104
|
+
return (_jsx(Grid, { container: true, spacing: spacing, children: Array.from({ length: length }).map(function (_, index) { return (_createElement(Grid, __assign({ item: true }, responsive, { key: index.toString() }),
|
|
105
|
+
_jsxs(Box, { sx: { width: '100%', margin: 2 }, children: [isLabel && _jsx(Skeleton, { variant: "text", width: "40%", height: 20, sx: { marginBottom: 1 } }), _jsx(Skeleton, { variant: "rectangular", width: width, height: 40, sx: { borderRadius: 1 } })] }))); }) }));
|
|
94
106
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-hook-toolkit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
4
4
|
"description": "Ultimate package for React developers, offering a powerful collection of hooks and components to enhance their development experience.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|