@versini/ui-hooks 5.3.2 → 6.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.
Files changed (45) hide show
  1. package/README.md +35 -9
  2. package/dist/__tests__/__mocks__/ResizeObserver.d.ts +21 -0
  3. package/dist/__tests__/useClickOutside.test.d.ts +1 -0
  4. package/dist/__tests__/useHaptic.test.d.ts +1 -0
  5. package/dist/__tests__/useHotkeys.test.d.ts +1 -0
  6. package/dist/__tests__/useInterval.test.d.ts +1 -0
  7. package/dist/__tests__/useIsMounted.test.d.ts +1 -0
  8. package/dist/__tests__/useLocalStorage.test.d.ts +1 -0
  9. package/dist/__tests__/useMergeRefs.test.d.ts +1 -0
  10. package/dist/__tests__/useResizeObserver.test.d.ts +1 -0
  11. package/dist/__tests__/useUncontrolled.test.d.ts +1 -0
  12. package/dist/__tests__/useUniqueId.test.d.ts +1 -0
  13. package/dist/__tests__/useViewportSize.test.d.ts +1 -0
  14. package/dist/__tests__/utilities.test.d.ts +1 -0
  15. package/dist/useClickOutside/useClickOutside.d.ts +17 -0
  16. package/dist/useClickOutside/useClickOutside.js +68 -0
  17. package/dist/useHaptic/useHaptic.d.ts +24 -0
  18. package/dist/useHaptic/useHaptic.js +200 -0
  19. package/dist/useHotkeys/useHotkeys.d.ts +10 -0
  20. package/dist/useHotkeys/useHotkeys.js +65 -0
  21. package/dist/useHotkeys/utilities.d.ts +19 -0
  22. package/dist/useHotkeys/utilities.js +87 -0
  23. package/dist/useInViewport/useInViewport.d.ts +10 -0
  24. package/dist/useInViewport/useInViewport.js +52 -0
  25. package/dist/useInterval/useInterval.d.ts +21 -0
  26. package/dist/useInterval/useInterval.js +75 -0
  27. package/dist/useIsMounted/useIsMounted.d.ts +13 -0
  28. package/dist/useIsMounted/useIsMounted.js +46 -0
  29. package/dist/useLocalStorage/useLocalStorage.d.ts +82 -0
  30. package/dist/useLocalStorage/useLocalStorage.js +236 -0
  31. package/dist/useMergeRefs/useMergeRefs.d.ts +20 -0
  32. package/dist/useMergeRefs/useMergeRefs.js +62 -0
  33. package/dist/useResizeObserver/useResizeObserver.d.ts +17 -0
  34. package/dist/useResizeObserver/useResizeObserver.js +94 -0
  35. package/dist/useUncontrolled/useUncontrolled.d.ts +14 -0
  36. package/dist/useUncontrolled/useUncontrolled.js +76 -0
  37. package/dist/useUniqueId/useUniqueId.d.ts +36 -0
  38. package/dist/useUniqueId/useUniqueId.js +41 -0
  39. package/dist/useViewportSize/useViewportSize.d.ts +13 -0
  40. package/dist/useViewportSize/useViewportSize.js +60 -0
  41. package/dist/useVisualViewportSize/useVisualViewportSize.d.ts +14 -0
  42. package/dist/useVisualViewportSize/useVisualViewportSize.js +70 -0
  43. package/package.json +56 -4
  44. package/dist/index.d.ts +0 -316
  45. package/dist/index.js +0 -958
package/dist/index.d.ts DELETED
@@ -1,316 +0,0 @@
1
- import { RefObject } from 'react';
2
-
3
- export declare function getHotkeyHandler(hotkeys: HotkeyItem_2[]): (event: React.KeyboardEvent<HTMLElement> | KeyboardEvent) => void;
4
-
5
- export declare type HotkeyItem = [
6
- string,
7
- (event: KeyboardEvent) => void,
8
- HotkeyItemOptions?
9
- ];
10
-
11
- declare type HotkeyItem_2 = [string, (event: any) => void, HotkeyItemOptions?];
12
-
13
- export declare interface HotkeyItemOptions {
14
- preventDefault?: boolean;
15
- }
16
-
17
- declare type ObserverRect = Omit<DOMRectReadOnly, "toJSON">;
18
-
19
- export declare function shouldFireEvent(event: KeyboardEvent, tagsToIgnore: string[], triggerOnContentEditable?: boolean): boolean;
20
-
21
- /**
22
- * Configuration properties for the useLocalStorage hook.
23
- *
24
- * @template T - The type of the value stored in localStorage
25
- *
26
- */
27
- export declare interface StorageProperties<T> {
28
- /**
29
- * The localStorage key under which the value will be stored. Must be unique
30
- * within your application to avoid conflicts.
31
- */
32
- key: string;
33
- /**
34
- * Optional default value that will be set in localStorage if no value exists.
35
- * This value will be used on initial mount and when resetValue() is called.
36
- */
37
- initialValue?: T;
38
- }
39
-
40
- /**
41
- * Custom hooks that triggers a callback when a click is detected
42
- * outside the target element.
43
- *
44
- * @param handler - Function to be called when clicked outside
45
- * @param events - Array of events to listen to
46
- * @param nodes - Array of nodes to check against
47
- * @returns Ref to be attached to the target element
48
- *
49
- * @example
50
- * const ref = useClickOutside(() => {
51
- * console.log('Clicked outside!');
52
- * });
53
- * <div ref={ref}>Click me!</div>
54
- *
55
- */
56
- export declare function useClickOutside<T extends HTMLElement = any>(handler: () => void, events?: string[] | null, nodes?: (HTMLElement | null)[]): RefObject<T | null>;
57
-
58
- /**
59
- * Custom hook providing imperative haptic feedback for mobile devices. Uses
60
- * navigator.vibrate when available, falls back to iOS switch element trick for
61
- * Safari on iOS devices that don't support the Vibration API.
62
- *
63
- * This hook uses a singleton pattern - only one haptic element is created in
64
- * the DOM regardless of how many components use this hook. The element is
65
- * automatically cleaned up when the last component unmounts.
66
- *
67
- * @example
68
- * ```tsx
69
- * const { haptic } = useHaptic();
70
- *
71
- * // Trigger a single haptic pulse
72
- * haptic(1);
73
- *
74
- * // Trigger two rapid haptic pulses
75
- * haptic(2);
76
- * ```
77
- *
78
- */
79
- export declare const useHaptic: () => {
80
- haptic: (count?: number) => void;
81
- };
82
-
83
- export declare function useHotkeys(hotkeys: HotkeyItem[], tagsToIgnore?: string[], triggerOnContentEditable?: boolean): void;
84
-
85
- /**
86
- * Custom hook to call a function within a given interval.
87
- *
88
- * @param fn Callback function to be executed at each interval
89
- * @param interval Interval time in milliseconds
90
- * @returns An object containing start, stop, and active state
91
- *
92
- * @example
93
- * const { start, stop, active } = useInterval(() => {
94
- * console.log("Interval executed");
95
- * }, 1000);
96
- * start(); // To start the interval
97
- * stop(); // To stop the interval
98
- * console.log(active); // To check if the interval is active
99
- *
100
- */
101
- export declare function useInterval(fn: () => void, interval: number): {
102
- start: () => void;
103
- stop: () => void;
104
- active: boolean;
105
- };
106
-
107
- /**
108
- * Hook that checks if an element is visible in the viewport.
109
- * @returns
110
- * ref: React ref object to attach to the element you want to monitor.
111
- * inViewport: Boolean indicating if the element is in the viewport.
112
- */
113
- export declare function useInViewport<T extends HTMLElement = any>(): {
114
- ref: (node: T | null) => void;
115
- inViewport: boolean;
116
- };
117
-
118
- /**
119
- * Custom hook that returns a function indicating whether the component
120
- * is mounted or not.
121
- *
122
- * @returns A function that returns a boolean value indicating whether
123
- * the component is mounted.
124
- *
125
- * @example
126
- * const isMounted = useIsMounted();
127
- * console.log(isMounted()); // true
128
- *
129
- */
130
- export declare function useIsMounted(): () => boolean;
131
-
132
- /**
133
- * A React hook for managing state synchronized with localStorage. Uses React
134
- * 19's useSyncExternalStore for optimal concurrent rendering support and
135
- * automatic synchronization across components. Changes are automatically
136
- * persisted to localStorage and synchronized across all components using the
137
- * same key, including across browser tabs.
138
- *
139
- * Features:
140
- * - Automatic serialization/deserialization with JSON
141
- * - Type-safe with TypeScript generics
142
- * - Supports functional updates (similar to setState)
143
- * - Synchronized across components and browser tabs
144
- * - Handles edge cases (null, undefined, errors)
145
- * - Compatible with React 19+ concurrent features
146
- *
147
- * @template T - The type of the value stored in localStorage
148
- * @param {StorageProperties<T>} config - Configuration object with key and optional initialValue
149
- * @returns {[T | null, (value: T | ((current: T) => T)) => void, () => void, () => void]} A tuple containing:
150
- * - [0] current value from localStorage (or null if not set)
151
- * - [1] setValue function to update the stored value (supports direct value or function updater)
152
- * - [2] resetValue function to restore the initialValue
153
- * - [3] removeValue function to remove the value from localStorage
154
- *
155
- * @example
156
- * ```js
157
- * // Basic usage with a string value
158
- * import { useLocalStorage } from '@versini/ui-hooks';
159
- * const [model, setModel, resetModel, removeModel] = useLocalStorage({
160
- * key: 'gpt-model',
161
- * initialValue: 'gpt-3',
162
- * });
163
- *
164
- * // Direct update
165
- * setModel('gpt-4'); // Stores "gpt-4"
166
- *
167
- * // Functional update (receives current value)
168
- * setModel((current) => (current === 'gpt-3' ? 'gpt-4' : 'gpt-3'));
169
- *
170
- * // Reset to initial value
171
- * resetModel(); // Restores "gpt-3"
172
- *
173
- * // Remove from localStorage
174
- * removeModel(); // Sets value to null and removes from storage
175
- * ```
176
- *
177
- * @example
178
- * ```js
179
- * // Usage with complex objects
180
- * interface UserPreferences {
181
- * theme: 'light' | 'dark';
182
- * fontSize: number;
183
- * }
184
- *
185
- * const [prefs, setPrefs] = useLocalStorage<UserPreferences>({
186
- * key: 'user-preferences',
187
- * initialValue: { theme: 'light', fontSize: 14 }
188
- * });
189
- *
190
- * // Update specific property
191
- * setPrefs(current => ({ ...current, theme: 'dark' }));
192
- * ```
193
- *
194
- */
195
- export declare function useLocalStorage<T>({ key, initialValue, }: StorageProperties<T>): any[];
196
-
197
- /**
198
- * React utility to merge refs.
199
- *
200
- * When developing low level UI components, it is common to have to use a local
201
- * ref but also support an external one using React.forwardRef. Natively, React
202
- * does not offer a way to set two refs inside the ref property.
203
- *
204
- * @param Array of refs (object, function, etc.)
205
- *
206
- * @example
207
- *
208
- * const Example = React.forwardRef(function Example(props, ref) {
209
- * const localRef = React.useRef();
210
- * const mergedRefs = useMergeRefs([localRef, ref]);
211
- *
212
- * return <div ref={mergedRefs} />;
213
- * });
214
- *
215
- */
216
- export declare function useMergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null>): React.RefCallback<T>;
217
-
218
- /**
219
- * A custom hook that uses the ResizeObserver API to track the size changes of a DOM element.
220
- *
221
- * @template T - The type of the DOM element being observed.
222
- * @param {ResizeObserverOptions} [options] - The options to configure the ResizeObserver.
223
- * @returns {[React.RefObject<T>, ObserverRect]} - A tuple containing the ref object and
224
- * the observed rectangle.
225
- * @example
226
- *
227
- * const [rightElementRef, rect] = useResizeObserver<HTMLDivElement>();
228
- * <div ref={componentRef}>
229
- * Observed: <code>{JSON.stringify(rect)}</code>
230
- * </div>
231
- */
232
- export declare function useResizeObserver<T extends HTMLElement = any>(options?: ResizeObserverOptions): readonly [RefObject<T | null>, ObserverRect];
233
-
234
- export declare function useUncontrolled<T>({ value, defaultValue, finalValue, onChange, initialControlledDelay, }: UseUncontrolledInput<T>): [T, (value: T) => void, boolean];
235
-
236
- declare interface UseUncontrolledInput<T> {
237
- /** Value for controlled state */
238
- value?: T;
239
- /** Initial value for uncontrolled state */
240
- defaultValue?: T;
241
- /** Final value for uncontrolled state when value and defaultValue are not provided */
242
- finalValue?: T;
243
- /** Controlled state onChange handler */
244
- onChange?: (value: T) => void;
245
- /** Initial delay for controlled state */
246
- initialControlledDelay?: number;
247
- }
248
-
249
- export declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
250
-
251
- /**
252
- * Hook that generates a unique id that will retain its value
253
- * during the lifecycle of the calling functional component.
254
- *
255
- * The parameters are either
256
- * - nothing: a unique id will simply be generated with no prefix.
257
- * - a string or a number: a prefix to prepend to the generated id.
258
- * - an object with the following props:
259
- * - id: if this is a number or a string, it will be returned as is
260
- * - prefix: prefix to prepend to the generated id.
261
- *
262
- * @param {string | number | object} options
263
- * @returns a unique id
264
- *
265
- * @examples
266
- *
267
- * const someId = useUniqueId();
268
- * // -> someId = "1j3h4f5"
269
- *
270
- * const errorId = useUniqueId("av-text-input-error-");
271
- * // -> errorId = "av-text-input-error-1j3h4f5"
272
- *
273
- * const inputId = useUniqueId({ id: 42, prefix: "av-text-input-" });
274
- * // -> inputId = "av-text-input-42"
275
- *
276
- * const inputHintId = useUniqueId({
277
- * prefix: "av-text-input-hint-",
278
- * });
279
- * // -> inputHintId = "av-text-input-hint-1j3h4f5"
280
- *
281
- */
282
- export declare type UseUniqueIdOptions = string | number | {
283
- id?: string | number;
284
- prefix?: string;
285
- };
286
-
287
- /**
288
- * Custom hook that returns the current viewport size. It will update
289
- * when the window is resized or the orientation changes.
290
- *
291
- * @returns The current viewport size
292
- *
293
- * @example
294
- * const { width, height } = useViewportSize();
295
- */
296
- export declare function useViewportSize(): {
297
- width: number;
298
- height: number;
299
- };
300
-
301
- /**
302
- * Custom hook that returns the current visual viewport size. It will update
303
- * when the window is resized (zoom, virtual keyboard displayed, etc.) or
304
- * the orientation changes.
305
- *
306
- * @returns The current visual viewport size
307
- *
308
- * @example
309
- * const { width, height } = useVisualViewportSize();
310
- */
311
- export declare function useVisualViewportSize(): {
312
- width: number;
313
- height: number;
314
- };
315
-
316
- export { }