@mwakidenis/logx-react 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/LICENSE +23 -0
- package/README.md +151 -3
- package/dist/components/index.d.mts +71 -0
- package/dist/components/index.d.ts +71 -0
- package/dist/components/index.js +268 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/index.mjs +262 -0
- package/dist/components/index.mjs.map +1 -0
- package/dist/hooks/index.d.mts +316 -0
- package/dist/hooks/index.d.ts +316 -0
- package/dist/hooks/index.js +638 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +615 -0
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/hooks/storage/index.d.mts +40 -0
- package/dist/hooks/storage/index.d.ts +40 -0
- package/dist/hooks/storage/index.js +119 -0
- package/dist/hooks/storage/index.js.map +1 -0
- package/dist/hooks/storage/index.mjs +116 -0
- package/dist/hooks/storage/index.mjs.map +1 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +1066 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +974 -0
- package/dist/index.mjs.map +1 -0
- package/dist/providers/LogxProvider.d.mts +134 -0
- package/dist/providers/LogxProvider.d.ts +134 -0
- package/dist/providers/LogxProvider.js +117 -0
- package/dist/providers/LogxProvider.js.map +1 -0
- package/dist/providers/LogxProvider.mjs +114 -0
- package/dist/providers/LogxProvider.mjs.map +1 -0
- package/dist/types/index.d.mts +87 -0
- package/dist/types/index.d.ts +87 -0
- package/dist/types/index.js +4 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +3 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/utilities/index.d.mts +140 -0
- package/dist/utilities/index.d.ts +140 -0
- package/dist/utilities/index.js +167 -0
- package/dist/utilities/index.js.map +1 -0
- package/dist/utilities/index.mjs +104 -0
- package/dist/utilities/index.mjs.map +1 -0
- package/package.json +127 -6
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { DebouncedValueOptions, AsyncState } from '../types/index.js';
|
|
2
|
+
export { useLocalStorage, useSessionStorage } from './storage/index.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A boolean toggle hook.
|
|
6
|
+
*
|
|
7
|
+
* Returns the current boolean value and a stable set of actions to mutate it.
|
|
8
|
+
* All callbacks are stable (no unnecessary re-renders of consumers).
|
|
9
|
+
*
|
|
10
|
+
* @param initialValue - Initial boolean value (defaults to false)
|
|
11
|
+
*/
|
|
12
|
+
declare function useToggle(initialValue?: boolean): [
|
|
13
|
+
boolean,
|
|
14
|
+
{
|
|
15
|
+
toggle: () => void;
|
|
16
|
+
set: (next?: boolean) => void;
|
|
17
|
+
setTrue: () => void;
|
|
18
|
+
setFalse: () => void;
|
|
19
|
+
}
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A boolean state hook with explicit setTrue/setFalse/toggle actions.
|
|
24
|
+
*
|
|
25
|
+
* Alias of useToggle with a more descriptive name. Provides stable callbacks.
|
|
26
|
+
*
|
|
27
|
+
* @param initialValue - Initial boolean value (defaults to false)
|
|
28
|
+
*/
|
|
29
|
+
declare function useBoolean(initialValue?: boolean): [
|
|
30
|
+
boolean,
|
|
31
|
+
{
|
|
32
|
+
setTrue: () => void;
|
|
33
|
+
setFalse: () => void;
|
|
34
|
+
toggle: () => void;
|
|
35
|
+
set: (next?: boolean) => void;
|
|
36
|
+
}
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A numeric counter hook.
|
|
41
|
+
*
|
|
42
|
+
* @param initialValue - Starting value (defaults to 0)
|
|
43
|
+
*/
|
|
44
|
+
declare function useCounter(initialValue?: number): {
|
|
45
|
+
count: number;
|
|
46
|
+
increment: (step?: number) => void;
|
|
47
|
+
decrement: (step?: number) => void;
|
|
48
|
+
reset: () => void;
|
|
49
|
+
set: (value: number) => void;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Returns the previous value of a tracked value.
|
|
54
|
+
*
|
|
55
|
+
* On first render returns undefined (there is no previous value yet).
|
|
56
|
+
* The returned ref updates after each render so the value is always the
|
|
57
|
+
* value from the previous committed render.
|
|
58
|
+
*
|
|
59
|
+
* @param value - The value to track
|
|
60
|
+
*/
|
|
61
|
+
declare function usePrevious<T>(value: T): T | undefined;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns a ref whose `.current` always holds the latest value without
|
|
65
|
+
* triggering re-renders. Useful for accessing the most recent value inside
|
|
66
|
+
* timers, callbacks or event listeners without stale closures.
|
|
67
|
+
*
|
|
68
|
+
* @param value - The value to track
|
|
69
|
+
*/
|
|
70
|
+
declare function useLatest<T>(value: T): {
|
|
71
|
+
current: T;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Returns whether the component is mounted.
|
|
76
|
+
*
|
|
77
|
+
* The value starts false (SSR-safe) and becomes true after mount. It returns
|
|
78
|
+
* to false on unmount. Use sparingly; prefer event-driven logic where possible.
|
|
79
|
+
*
|
|
80
|
+
* NOTE: Reading this during render can cause a flash of unmounted content on
|
|
81
|
+
* the client. Prefer using it inside effects or event handlers.
|
|
82
|
+
*/
|
|
83
|
+
declare function useMounted(): boolean;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Array state with immutable helpers.
|
|
87
|
+
*
|
|
88
|
+
* Several helpers delegate to @mwakidenis/logx array utilities (unique,
|
|
89
|
+
* compact, first, last, take, drop, shuffle, partition, difference,
|
|
90
|
+
* intersection, union, flatten). React-specific state management is kept in
|
|
91
|
+
* this hook; LOGX provides the pure array transformations.
|
|
92
|
+
*
|
|
93
|
+
* @param initialValue - Initial array (defaults to [])
|
|
94
|
+
*/
|
|
95
|
+
declare function useArray<T>(initialValue?: T[]): {
|
|
96
|
+
array: T[];
|
|
97
|
+
set: (value: T[] | ((prev: T[]) => T[])) => void;
|
|
98
|
+
push: (...items: T[]) => void;
|
|
99
|
+
unshift: (...items: T[]) => void;
|
|
100
|
+
remove: (index: number) => void;
|
|
101
|
+
removeBy: (predicate: (value: T, index: number) => unknown) => void;
|
|
102
|
+
update: (index: number, value: T) => void;
|
|
103
|
+
clear: () => void;
|
|
104
|
+
reset: () => void;
|
|
105
|
+
unique: () => void;
|
|
106
|
+
compact: () => void;
|
|
107
|
+
shuffle: () => void;
|
|
108
|
+
first: () => T | undefined;
|
|
109
|
+
last: () => T | undefined;
|
|
110
|
+
take: (count: number) => T[];
|
|
111
|
+
drop: (count: number) => T[];
|
|
112
|
+
difference: (...others: T[][]) => T[];
|
|
113
|
+
intersection: (...others: T[][]) => T[];
|
|
114
|
+
union: (...others: T[][]) => T[];
|
|
115
|
+
partition: (predicate: (value: T, index: number) => unknown) => [T[], T[]];
|
|
116
|
+
flatten: () => unknown[];
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Object state with immutable helpers.
|
|
121
|
+
*
|
|
122
|
+
* Several helpers delegate to @mwakidenis/logx object utilities (get, set,
|
|
123
|
+
* pick, omit, merge, keys, values, entries, clone, deepClone, mapValues).
|
|
124
|
+
* React-specific state management is kept in this hook; LOGX provides the pure
|
|
125
|
+
* object transformations.
|
|
126
|
+
*
|
|
127
|
+
* @param initialValue - Initial object (defaults to {})
|
|
128
|
+
*/
|
|
129
|
+
declare function useObject<T extends Record<string, unknown>>(initialValue?: T): {
|
|
130
|
+
object: T;
|
|
131
|
+
set: (value: T | ((prev: T) => T)) => void;
|
|
132
|
+
setKey: (key: string, value: unknown) => void;
|
|
133
|
+
removeKey: (key: string) => void;
|
|
134
|
+
clear: () => void;
|
|
135
|
+
reset: () => void;
|
|
136
|
+
pick: (keys: readonly string[]) => Partial<T>;
|
|
137
|
+
omit: (keys: readonly string[]) => Partial<T>;
|
|
138
|
+
merge: (sources: Partial<T>[]) => T;
|
|
139
|
+
get: (path: string | Array<string | number>) => unknown;
|
|
140
|
+
keys: () => string[];
|
|
141
|
+
values: () => unknown[];
|
|
142
|
+
entries: () => [string, unknown][];
|
|
143
|
+
clone: () => T;
|
|
144
|
+
deepClone: () => T;
|
|
145
|
+
mapValues: <U>(iteratee: (value: unknown, key: string) => U) => Record<string, U>;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Debounce a value.
|
|
150
|
+
*
|
|
151
|
+
* Returns the value after it has stopped changing for `delay` milliseconds.
|
|
152
|
+
* Uses reference equality to detect changes; for objects/arrays pass a new
|
|
153
|
+
* reference only when you want a debounce to restart.
|
|
154
|
+
*
|
|
155
|
+
* LOGX provides function debouncing (@mwakidenis/logx/function). This hook
|
|
156
|
+
* implements value debouncing (a React-specific behaviour) on top of timers;
|
|
157
|
+
* it does not map to an existing LOGX function.
|
|
158
|
+
*
|
|
159
|
+
* @param value - The value to debounce
|
|
160
|
+
* @param options - delay (ms, default 300) and leading (default false)
|
|
161
|
+
*/
|
|
162
|
+
declare function useDebouncedValue<T>(value: T, options?: DebouncedValueOptions): T;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Debounce a callback using LOGX's `debounce` (@mwakidenis/logx/function).
|
|
166
|
+
*
|
|
167
|
+
* LOGX debounce is trailing-edge only: the function is invoked after `delay`
|
|
168
|
+
* milliseconds of inactivity. It exposes `cancel`, `flush` and `pending`.
|
|
169
|
+
*
|
|
170
|
+
* The latest callback reference is captured internally so the debounced
|
|
171
|
+
* function never holds a stale closure, even if `callback` changes every render.
|
|
172
|
+
*
|
|
173
|
+
* @param callback - Function to debounce
|
|
174
|
+
* @param delay - Delay in milliseconds
|
|
175
|
+
*/
|
|
176
|
+
declare function useDebouncedCallback<F extends (...args: unknown[]) => unknown>(callback: F, delay: number): {
|
|
177
|
+
(...args: Parameters<F>): void;
|
|
178
|
+
cancel: () => void;
|
|
179
|
+
flush: () => void;
|
|
180
|
+
pending: () => boolean;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Throttle a callback using LOGX's `throttle` (@mwakidenis/logx/function).
|
|
185
|
+
*
|
|
186
|
+
* LOGX throttle invokes on the leading edge and flushes the trailing edge at
|
|
187
|
+
* the end of the wait period. It exposes `cancel`, `flush` and `pending`.
|
|
188
|
+
*
|
|
189
|
+
* @param callback - Function to throttle
|
|
190
|
+
* @param wait - Wait period in milliseconds
|
|
191
|
+
*/
|
|
192
|
+
declare function useThrottle<F extends (...args: unknown[]) => unknown>(callback: F, wait: number): {
|
|
193
|
+
(...args: Parameters<F>): void;
|
|
194
|
+
cancel: () => void;
|
|
195
|
+
flush: () => void;
|
|
196
|
+
pending: () => boolean;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Track whether a media query matches.
|
|
201
|
+
*
|
|
202
|
+
* SSR-safe: returns `false` on the server and before mount, then reflects the
|
|
203
|
+
* real match status after mount. Listens for `change` events and cleans up on
|
|
204
|
+
* unmount.
|
|
205
|
+
*
|
|
206
|
+
* @param query - A CSS media query string
|
|
207
|
+
*/
|
|
208
|
+
declare function useMediaQuery(query: string): boolean;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Track the window size (innerWidth / innerHeight).
|
|
212
|
+
*
|
|
213
|
+
* SSR-safe: returns { width: 0, height: 0 } on the server and first client
|
|
214
|
+
* render, then updates on resize. Cleans up the listener on unmount.
|
|
215
|
+
*/
|
|
216
|
+
declare function useWindowSize(): {
|
|
217
|
+
width: number;
|
|
218
|
+
height: number;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Fire a handler when a click occurs outside a ref'd element.
|
|
223
|
+
*
|
|
224
|
+
* Uses useLatest internally so the handler is always current without
|
|
225
|
+
* re-subscribing the document listeners every render.
|
|
226
|
+
*
|
|
227
|
+
* @param ref - A ref to the element to ignore clicks for
|
|
228
|
+
* @param handler - Callback fired on outside click
|
|
229
|
+
*/
|
|
230
|
+
declare function useClickOutside(ref: {
|
|
231
|
+
current: HTMLElement | null | undefined;
|
|
232
|
+
}, handler: (event: MouseEvent | TouchEvent) => void): void;
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Attach an event listener with automatic cleanup on unmount.
|
|
236
|
+
*
|
|
237
|
+
* Supports window, document, element, or Document/Window targets. The listener
|
|
238
|
+
* is captured via a ref so it stays current without re-attaching.
|
|
239
|
+
*
|
|
240
|
+
* @param target - EventTarget (window, document, element) or null/undefined
|
|
241
|
+
* @param type - Event type
|
|
242
|
+
* @param listener - Event listener
|
|
243
|
+
* @param options - AddEventListenerOptions or capture boolean
|
|
244
|
+
*/
|
|
245
|
+
declare function useEventListener(target: EventTarget | null | undefined, type: string, listener: EventListener, options?: boolean | AddEventListenerOptions): void;
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Copy text to the clipboard.
|
|
249
|
+
*
|
|
250
|
+
* Uses the Clipboard API when available, with a textarea fallback for older
|
|
251
|
+
* browsers. Never throws; returns false on failure.
|
|
252
|
+
*/
|
|
253
|
+
declare function useClipboard(): {
|
|
254
|
+
copy: (text: string) => Promise<boolean>;
|
|
255
|
+
isSupported: boolean;
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Track online/offline status.
|
|
260
|
+
*
|
|
261
|
+
* SSR-safe: assumes `true` on the server, then reads `navigator.onLine` after
|
|
262
|
+
* mount and listens for `online`/`offline` events.
|
|
263
|
+
*/
|
|
264
|
+
declare function useOnlineStatus(): boolean;
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* A controllable timeout hook.
|
|
268
|
+
*
|
|
269
|
+
* Provides start/stop/reset and clears the timer on unmount. The callback is
|
|
270
|
+
* captured via useLatest so it never goes stale. `active` reflects whether a
|
|
271
|
+
* timeout is currently scheduled.
|
|
272
|
+
*
|
|
273
|
+
* @param callback - Function to invoke after the delay
|
|
274
|
+
* @param delay - Delay in milliseconds, or null to disable
|
|
275
|
+
*/
|
|
276
|
+
declare function useTimeout(callback: () => void, delay: number | null): {
|
|
277
|
+
start: () => void;
|
|
278
|
+
stop: () => void;
|
|
279
|
+
reset: () => void;
|
|
280
|
+
active: boolean;
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* A controllable interval hook.
|
|
285
|
+
*
|
|
286
|
+
* Provides start/stop and clears the interval on unmount. The callback is
|
|
287
|
+
* captured via useLatest so it never goes stale. `active` reflects whether an
|
|
288
|
+
* interval is currently scheduled.
|
|
289
|
+
*
|
|
290
|
+
* @param callback - Function to invoke every `delay` milliseconds
|
|
291
|
+
* @param delay - Delay in milliseconds, or null to disable
|
|
292
|
+
*/
|
|
293
|
+
declare function useInterval(callback: () => void, delay: number | null): {
|
|
294
|
+
start: () => void;
|
|
295
|
+
stop: () => void;
|
|
296
|
+
active: boolean;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Manage an asynchronous operation with loading, success and error states.
|
|
301
|
+
*
|
|
302
|
+
* Supports cancellation via AbortController, race-condition protection (only
|
|
303
|
+
* the latest invocation updates state), and manual execute / reset.
|
|
304
|
+
*
|
|
305
|
+
* @param asyncFn - Function returning a Promise
|
|
306
|
+
* @param immediate - Whether to invoke immediately on mount (default true)
|
|
307
|
+
*/
|
|
308
|
+
declare function useAsync<T>(asyncFn: () => Promise<T>, immediate?: boolean): AsyncState<T> & {
|
|
309
|
+
execute: () => Promise<void>;
|
|
310
|
+
setData: (data: T) => void;
|
|
311
|
+
setError: (error: Error) => void;
|
|
312
|
+
reset: () => void;
|
|
313
|
+
cancel: () => void;
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
export { useArray, useAsync, useBoolean, useClickOutside, useClipboard, useCounter, useDebouncedCallback, useDebouncedValue, useEventListener, useInterval, useLatest, useMediaQuery, useMounted, useObject, useOnlineStatus, usePrevious, useThrottle, useTimeout, useToggle, useWindowSize };
|