@versini/ui-hooks 5.1.0 → 5.2.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/dist/index.d.ts CHANGED
@@ -1,246 +1,259 @@
1
- import * as react from 'react';
2
-
3
- /**
4
- * Custom hooks that triggers a callback when a click is detected
5
- * outside the target element.
6
- *
7
- * @param handler - Function to be called when clicked outside
8
- * @param events - Array of events to listen to
9
- * @param nodes - Array of nodes to check against
10
- * @returns Ref to be attached to the target element
11
- *
12
- * @example
13
- * const ref = useClickOutside(() => {
14
- * console.log('Clicked outside!');
15
- * });
16
- * <div ref={ref}>Click me!</div>
17
- *
18
- */
19
- declare function useClickOutside<T extends HTMLElement = any>(handler: () => void, events?: string[] | null, nodes?: (HTMLElement | null)[]): react.RefObject<T | null>;
20
-
21
- /**
22
- * Custom hook providing imperative haptic feedback for mobile devices. Uses
23
- * navigator.vibrate when available, falls back to iOS switch element trick
24
- * for Safari on iOS devices that don't support the Vibration API.
25
- *
26
- * @example
27
- * ```tsx
28
- * const { haptic } = useHaptic();
29
- *
30
- * // Trigger a single haptic pulse
31
- * haptic(1);
32
- *
33
- * // Trigger two rapid haptic pulses
34
- * haptic(2);
35
- * ```
36
- */
37
- declare const useHaptic: () => {
38
- haptic: (count?: number) => void;
39
- };
40
-
41
- interface HotkeyItemOptions {
42
- preventDefault?: boolean;
43
- }
44
- type HotkeyItem$1 = [string, (event: any) => void, HotkeyItemOptions?];
45
- declare function getHotkeyHandler(hotkeys: HotkeyItem$1[]): (event: React.KeyboardEvent<HTMLElement> | KeyboardEvent) => void;
46
-
47
- type HotkeyItem = [
48
- string,
49
- (event: KeyboardEvent) => void,
50
- HotkeyItemOptions?
51
- ];
52
- declare function shouldFireEvent(event: KeyboardEvent, tagsToIgnore: string[], triggerOnContentEditable?: boolean): boolean;
53
- declare function useHotkeys(hotkeys: HotkeyItem[], tagsToIgnore?: string[], triggerOnContentEditable?: boolean): void;
54
-
55
- /**
56
- * Custom hook to call a function within a given interval.
57
- *
58
- * @param fn Callback function to be executed at each interval
59
- * @param interval Interval time in milliseconds
60
- * @returns An object containing start, stop, and active state
61
- *
62
- * @example
63
- * const { start, stop, active } = useInterval(() => {
64
- * console.log("Interval executed");
65
- * }, 1000);
66
- * start(); // To start the interval
67
- * stop(); // To stop the interval
68
- * console.log(active); // To check if the interval is active
69
- *
70
- */
71
- declare function useInterval(fn: () => void, interval: number): {
72
- start: () => void;
73
- stop: () => void;
74
- active: boolean;
75
- };
76
-
77
- /**
78
- * Hook that checks if an element is visible in the viewport.
79
- * @returns
80
- * ref: React ref object to attach to the element you want to monitor.
81
- * inViewport: Boolean indicating if the element is in the viewport.
82
- */
83
- declare function useInViewport<T extends HTMLElement = any>(): {
84
- ref: (node: T | null) => void;
85
- inViewport: boolean;
86
- };
87
-
88
- /**
89
- * Custom hook that returns a function indicating whether the component
90
- * is mounted or not.
91
- *
92
- * @returns A function that returns a boolean value indicating whether
93
- * the component is mounted.
94
- *
95
- * @example
96
- * const isMounted = useIsMounted();
97
- * console.log(isMounted()); // true
98
- *
99
- */
100
- declare function useIsMounted(): () => boolean;
101
-
102
- interface StorageProperties<T> {
103
- /**
104
- * Storage key.
105
- */
106
- key: string;
107
- /**
108
- * Default value that will be set if value is not found in storage.
109
- */
110
- initialValue?: T;
111
- }
112
- /**
113
- *
114
- * @example
115
- * import { useLocalStorage } from '@versini/ui-hooks';
116
- * const [value, setValue, resetValue, removeValue] = useLocalStorage({
117
- * key: 'gpt-model',
118
- * initialValue: 'gpt-3',
119
- * });
120
- *
121
- * setValue('gpt-4'); ==> "gpt-4"
122
- * setValue((current) => (current === 'gpt-3' ? 'gpt-4' : 'gpt-3'));
123
- * resetValue(); ==> "gpt-3"
124
- * removeValue(); ==> null
125
- */
126
- declare function useLocalStorage<T>({ key, initialValue, }: StorageProperties<T>): any[];
127
-
128
- /**
129
- * React utility to merge refs.
130
- *
131
- * When developing low level UI components, it is common to have to use a local
132
- * ref but also support an external one using React.forwardRef. Natively, React
133
- * does not offer a way to set two refs inside the ref property.
134
- *
135
- * @param Array of refs (object, function, etc.)
136
- *
137
- * @example
138
- *
139
- * const Example = React.forwardRef(function Example(props, ref) {
140
- * const localRef = React.useRef();
141
- * const mergedRefs = useMergeRefs([localRef, ref]);
142
- *
143
- * return <div ref={mergedRefs} />;
144
- * });
145
- *
146
- */
147
- declare function useMergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null>): React.RefCallback<T>;
148
-
149
- type ObserverRect = Omit<DOMRectReadOnly, "toJSON">;
150
- /**
151
- * A custom hook that uses the ResizeObserver API to track the size changes of a DOM element.
152
- *
153
- * @template T - The type of the DOM element being observed.
154
- * @param {ResizeObserverOptions} [options] - The options to configure the ResizeObserver.
155
- * @returns {[React.RefObject<T>, ObserverRect]} - A tuple containing the ref object and
156
- * the observed rectangle.
157
- * @example
158
- *
159
- * const [rightElementRef, rect] = useResizeObserver<HTMLDivElement>();
160
- * <div ref={componentRef}>
161
- * Observed: <code>{JSON.stringify(rect)}</code>
162
- * </div>
163
- */
164
- declare function useResizeObserver<T extends HTMLElement = any>(options?: ResizeObserverOptions): readonly [react.RefObject<T | null>, ObserverRect];
165
-
166
- interface UseUncontrolledInput<T> {
167
- /** Value for controlled state */
168
- value?: T;
169
- /** Initial value for uncontrolled state */
170
- defaultValue?: T;
171
- /** Final value for uncontrolled state when value and defaultValue are not provided */
172
- finalValue?: T;
173
- /** Controlled state onChange handler */
174
- onChange?: (value: T) => void;
175
- /** Initial delay for controlled state */
176
- initialControlledDelay?: number;
177
- }
178
- declare function useUncontrolled<T>({ value, defaultValue, finalValue, onChange, initialControlledDelay, }: UseUncontrolledInput<T>): [T, (value: T) => void, boolean];
179
-
180
- /**
181
- * Hook that generates a unique id that will retain its value
182
- * during the lifecycle of the calling functional component.
183
- *
184
- * The parameters are either
185
- * - nothing: a unique id will simply be generated with no prefix.
186
- * - a string or a number: a prefix to prepend to the generated id.
187
- * - an object with the following props:
188
- * - id: if this is a number or a string, it will be returned as is
189
- * - prefix: prefix to prepend to the generated id.
190
- *
191
- * @param {string | number | object} options
192
- * @returns a unique id
193
- *
194
- * @examples
195
- *
196
- * const someId = useUniqueId();
197
- * // -> someId = "1j3h4f5"
198
- *
199
- * const errorId = useUniqueId("av-text-input-error-");
200
- * // -> errorId = "av-text-input-error-1j3h4f5"
201
- *
202
- * const inputId = useUniqueId({ id: 42, prefix: "av-text-input-" });
203
- * // -> inputId = "av-text-input-42"
204
- *
205
- * const inputHintId = useUniqueId({
206
- * prefix: "av-text-input-hint-",
207
- * });
208
- * // -> inputHintId = "av-text-input-hint-1j3h4f5"
209
- *
210
- */
211
- type UseUniqueIdOptions = string | number | {
212
- id?: string | number;
213
- prefix?: string;
214
- };
215
- declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
216
-
217
- /**
218
- * Custom hook that returns the current viewport size. It will update
219
- * when the window is resized or the orientation changes.
220
- *
221
- * @returns The current viewport size
222
- *
223
- * @example
224
- * const { width, height } = useViewportSize();
225
- */
226
- declare function useViewportSize(): {
227
- width: number;
228
- height: number;
229
- };
230
-
231
- /**
232
- * Custom hook that returns the current visual viewport size. It will update
233
- * when the window is resized (zoom, virtual keyboard displayed, etc.) or
234
- * the orientation changes.
235
- *
236
- * @returns The current visual viewport size
237
- *
238
- * @example
239
- * const { width, height } = useVisualViewportSize();
240
- */
241
- declare function useVisualViewportSize(): {
242
- width: number;
243
- height: number;
244
- };
245
-
246
- export { type HotkeyItem, type HotkeyItemOptions, type StorageProperties, type UseUniqueIdOptions, getHotkeyHandler, shouldFireEvent, useClickOutside, useHaptic, useHotkeys, useInViewport, useInterval, useIsMounted, useLocalStorage, useMergeRefs, useResizeObserver, useUncontrolled, useUniqueId, useViewportSize, useVisualViewportSize };
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
+ export declare interface StorageProperties<T> {
22
+ /**
23
+ * Storage key.
24
+ */
25
+ key: string;
26
+ /**
27
+ * Default value that will be set if value is not found in storage.
28
+ */
29
+ initialValue?: T;
30
+ }
31
+
32
+ /**
33
+ * Custom hooks that triggers a callback when a click is detected
34
+ * outside the target element.
35
+ *
36
+ * @param handler - Function to be called when clicked outside
37
+ * @param events - Array of events to listen to
38
+ * @param nodes - Array of nodes to check against
39
+ * @returns Ref to be attached to the target element
40
+ *
41
+ * @example
42
+ * const ref = useClickOutside(() => {
43
+ * console.log('Clicked outside!');
44
+ * });
45
+ * <div ref={ref}>Click me!</div>
46
+ *
47
+ */
48
+ export declare function useClickOutside<T extends HTMLElement = any>(handler: () => void, events?: string[] | null, nodes?: (HTMLElement | null)[]): RefObject<T | null>;
49
+
50
+ /**
51
+ * Custom hook providing imperative haptic feedback for mobile devices. Uses
52
+ * navigator.vibrate when available, falls back to iOS switch element trick for
53
+ * Safari on iOS devices that don't support the Vibration API.
54
+ *
55
+ * This hook uses a singleton pattern - only one haptic element is created in
56
+ * the DOM regardless of how many components use this hook. The element is
57
+ * automatically cleaned up when the last component unmounts.
58
+ *
59
+ * @example
60
+ * ```tsx
61
+ * const { haptic } = useHaptic();
62
+ *
63
+ * // Trigger a single haptic pulse
64
+ * haptic(1);
65
+ *
66
+ * // Trigger two rapid haptic pulses
67
+ * haptic(2);
68
+ * ```
69
+ *
70
+ */
71
+ export declare const useHaptic: () => {
72
+ haptic: (count?: number) => void;
73
+ };
74
+
75
+ export declare function useHotkeys(hotkeys: HotkeyItem[], tagsToIgnore?: string[], triggerOnContentEditable?: boolean): void;
76
+
77
+ /**
78
+ * Custom hook to call a function within a given interval.
79
+ *
80
+ * @param fn Callback function to be executed at each interval
81
+ * @param interval Interval time in milliseconds
82
+ * @returns An object containing start, stop, and active state
83
+ *
84
+ * @example
85
+ * const { start, stop, active } = useInterval(() => {
86
+ * console.log("Interval executed");
87
+ * }, 1000);
88
+ * start(); // To start the interval
89
+ * stop(); // To stop the interval
90
+ * console.log(active); // To check if the interval is active
91
+ *
92
+ */
93
+ export declare function useInterval(fn: () => void, interval: number): {
94
+ start: () => void;
95
+ stop: () => void;
96
+ active: boolean;
97
+ };
98
+
99
+ /**
100
+ * Hook that checks if an element is visible in the viewport.
101
+ * @returns
102
+ * ref: React ref object to attach to the element you want to monitor.
103
+ * inViewport: Boolean indicating if the element is in the viewport.
104
+ */
105
+ export declare function useInViewport<T extends HTMLElement = any>(): {
106
+ ref: (node: T | null) => void;
107
+ inViewport: boolean;
108
+ };
109
+
110
+ /**
111
+ * Custom hook that returns a function indicating whether the component
112
+ * is mounted or not.
113
+ *
114
+ * @returns A function that returns a boolean value indicating whether
115
+ * the component is mounted.
116
+ *
117
+ * @example
118
+ * const isMounted = useIsMounted();
119
+ * console.log(isMounted()); // true
120
+ *
121
+ */
122
+ export declare function useIsMounted(): () => boolean;
123
+
124
+ /**
125
+ *
126
+ * @example
127
+ * import { useLocalStorage } from '@versini/ui-hooks';
128
+ * const [value, setValue, resetValue, removeValue] = useLocalStorage({
129
+ * key: 'gpt-model',
130
+ * initialValue: 'gpt-3',
131
+ * });
132
+ *
133
+ * setValue('gpt-4'); ==> "gpt-4"
134
+ * setValue((current) => (current === 'gpt-3' ? 'gpt-4' : 'gpt-3'));
135
+ * resetValue(); ==> "gpt-3"
136
+ * removeValue(); ==> null
137
+ */
138
+ export declare function useLocalStorage<T>({ key, initialValue, }: StorageProperties<T>): any[];
139
+
140
+ /**
141
+ * React utility to merge refs.
142
+ *
143
+ * When developing low level UI components, it is common to have to use a local
144
+ * ref but also support an external one using React.forwardRef. Natively, React
145
+ * does not offer a way to set two refs inside the ref property.
146
+ *
147
+ * @param Array of refs (object, function, etc.)
148
+ *
149
+ * @example
150
+ *
151
+ * const Example = React.forwardRef(function Example(props, ref) {
152
+ * const localRef = React.useRef();
153
+ * const mergedRefs = useMergeRefs([localRef, ref]);
154
+ *
155
+ * return <div ref={mergedRefs} />;
156
+ * });
157
+ *
158
+ */
159
+ export declare function useMergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null>): React.RefCallback<T>;
160
+
161
+ /**
162
+ * A custom hook that uses the ResizeObserver API to track the size changes of a DOM element.
163
+ *
164
+ * @template T - The type of the DOM element being observed.
165
+ * @param {ResizeObserverOptions} [options] - The options to configure the ResizeObserver.
166
+ * @returns {[React.RefObject<T>, ObserverRect]} - A tuple containing the ref object and
167
+ * the observed rectangle.
168
+ * @example
169
+ *
170
+ * const [rightElementRef, rect] = useResizeObserver<HTMLDivElement>();
171
+ * <div ref={componentRef}>
172
+ * Observed: <code>{JSON.stringify(rect)}</code>
173
+ * </div>
174
+ */
175
+ export declare function useResizeObserver<T extends HTMLElement = any>(options?: ResizeObserverOptions): readonly [RefObject<T | null>, ObserverRect];
176
+
177
+ export declare function useUncontrolled<T>({ value, defaultValue, finalValue, onChange, initialControlledDelay, }: UseUncontrolledInput<T>): [T, (value: T) => void, boolean];
178
+
179
+ declare interface UseUncontrolledInput<T> {
180
+ /** Value for controlled state */
181
+ value?: T;
182
+ /** Initial value for uncontrolled state */
183
+ defaultValue?: T;
184
+ /** Final value for uncontrolled state when value and defaultValue are not provided */
185
+ finalValue?: T;
186
+ /** Controlled state onChange handler */
187
+ onChange?: (value: T) => void;
188
+ /** Initial delay for controlled state */
189
+ initialControlledDelay?: number;
190
+ }
191
+
192
+ export declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
193
+
194
+ /**
195
+ * Hook that generates a unique id that will retain its value
196
+ * during the lifecycle of the calling functional component.
197
+ *
198
+ * The parameters are either
199
+ * - nothing: a unique id will simply be generated with no prefix.
200
+ * - a string or a number: a prefix to prepend to the generated id.
201
+ * - an object with the following props:
202
+ * - id: if this is a number or a string, it will be returned as is
203
+ * - prefix: prefix to prepend to the generated id.
204
+ *
205
+ * @param {string | number | object} options
206
+ * @returns a unique id
207
+ *
208
+ * @examples
209
+ *
210
+ * const someId = useUniqueId();
211
+ * // -> someId = "1j3h4f5"
212
+ *
213
+ * const errorId = useUniqueId("av-text-input-error-");
214
+ * // -> errorId = "av-text-input-error-1j3h4f5"
215
+ *
216
+ * const inputId = useUniqueId({ id: 42, prefix: "av-text-input-" });
217
+ * // -> inputId = "av-text-input-42"
218
+ *
219
+ * const inputHintId = useUniqueId({
220
+ * prefix: "av-text-input-hint-",
221
+ * });
222
+ * // -> inputHintId = "av-text-input-hint-1j3h4f5"
223
+ *
224
+ */
225
+ export declare type UseUniqueIdOptions = string | number | {
226
+ id?: string | number;
227
+ prefix?: string;
228
+ };
229
+
230
+ /**
231
+ * Custom hook that returns the current viewport size. It will update
232
+ * when the window is resized or the orientation changes.
233
+ *
234
+ * @returns The current viewport size
235
+ *
236
+ * @example
237
+ * const { width, height } = useViewportSize();
238
+ */
239
+ export declare function useViewportSize(): {
240
+ width: number;
241
+ height: number;
242
+ };
243
+
244
+ /**
245
+ * Custom hook that returns the current visual viewport size. It will update
246
+ * when the window is resized (zoom, virtual keyboard displayed, etc.) or
247
+ * the orientation changes.
248
+ *
249
+ * @returns The current visual viewport size
250
+ *
251
+ * @example
252
+ * const { width, height } = useVisualViewportSize();
253
+ */
254
+ export declare function useVisualViewportSize(): {
255
+ width: number;
256
+ height: number;
257
+ };
258
+
259
+ export { }