@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 +259 -246
- package/dist/index.js +826 -30
- package/package.json +9 -9
- package/dist/hooks/useClickOutside.js +0 -26
- package/dist/hooks/useHaptic.js +0 -49
- package/dist/hooks/useHotkeys.js +0 -56
- package/dist/hooks/useInViewport.js +0 -12
- package/dist/hooks/useInterval.js +0 -12
- package/dist/hooks/useIsMounted.js +0 -10
- package/dist/hooks/useLocalStorage.js +0 -42
- package/dist/hooks/useMergeRefs.js +0 -12
- package/dist/hooks/useResizeObserver.js +0 -26
- package/dist/hooks/useUncontrolled.js +0 -23
- package/dist/hooks/useUniqueId.js +0 -15
- package/dist/hooks/useViewportSize.js +0 -21
- package/dist/hooks/useVisualViewportSize.js +0 -22
package/dist/index.d.ts
CHANGED
|
@@ -1,246 +1,259 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
declare function
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* @
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*/
|
|
71
|
-
declare
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
*
|
|
114
|
-
* @
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
*
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
* @
|
|
237
|
-
*
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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 { }
|