@versini/ui-hooks 4.0.1 → 4.1.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.
- package/dist/hooks/useIsMounted.js +10 -0
- package/dist/hooks/useResizeObserver.js +26 -0
- package/dist/index.d.ts +34 -1
- package/dist/index.js +11 -7
- package/package.json +2 -2
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useRef as i, useState as m, useMemo as a, useEffect as l } from "react";
|
|
2
|
+
import { useIsMounted as d } from "./useIsMounted.js";
|
|
3
|
+
const R = {
|
|
4
|
+
x: 0,
|
|
5
|
+
y: 0,
|
|
6
|
+
width: 0,
|
|
7
|
+
height: 0,
|
|
8
|
+
top: 0,
|
|
9
|
+
left: 0,
|
|
10
|
+
bottom: 0,
|
|
11
|
+
right: 0
|
|
12
|
+
};
|
|
13
|
+
function y(r) {
|
|
14
|
+
const c = d(), t = i(0), n = i(null), [o, s] = m(R), e = a(() => typeof ResizeObserver > "u" ? null : new ResizeObserver((f) => {
|
|
15
|
+
const u = f[0];
|
|
16
|
+
u && (cancelAnimationFrame(t.current), t.current = requestAnimationFrame(() => {
|
|
17
|
+
n.current && c() && s(u.contentRect);
|
|
18
|
+
}));
|
|
19
|
+
}), [c]);
|
|
20
|
+
return l(() => (n.current && (e == null || e.observe(n.current, r)), () => {
|
|
21
|
+
e == null || e.disconnect(), t.current && cancelAnimationFrame(t.current);
|
|
22
|
+
}), [e, r]), [n, o];
|
|
23
|
+
}
|
|
24
|
+
export {
|
|
25
|
+
y as useResizeObserver
|
|
26
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook that returns a function indicating whether the component
|
|
5
|
+
* is mounted or not.
|
|
6
|
+
*
|
|
7
|
+
* @returns A function that returns a boolean value indicating whether
|
|
8
|
+
* the component is mounted.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const isMounted = useIsMounted();
|
|
12
|
+
* console.log(isMounted()); // true
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
declare function useIsMounted(): () => boolean;
|
|
16
|
+
|
|
1
17
|
interface StorageProperties<T> {
|
|
2
18
|
/**
|
|
3
19
|
* Storage key.
|
|
@@ -45,6 +61,23 @@ declare function useLocalStorage<T>({ key, initialValue, }: StorageProperties<T>
|
|
|
45
61
|
*/
|
|
46
62
|
declare function useMergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null>): React.RefCallback<T>;
|
|
47
63
|
|
|
64
|
+
type ObserverRect = Omit<DOMRectReadOnly, "toJSON">;
|
|
65
|
+
/**
|
|
66
|
+
* A custom hook that uses the ResizeObserver API to track the size changes of a DOM element.
|
|
67
|
+
*
|
|
68
|
+
* @template T - The type of the DOM element being observed.
|
|
69
|
+
* @param {ResizeObserverOptions} [options] - The options to configure the ResizeObserver.
|
|
70
|
+
* @returns {[React.RefObject<T>, ObserverRect]} - A tuple containing the ref object and
|
|
71
|
+
* the observed rectangle.
|
|
72
|
+
* @example
|
|
73
|
+
*
|
|
74
|
+
* const [rightElementRef, rect] = useResizeObserver<HTMLDivElement>();
|
|
75
|
+
* <div ref={componentRef}>
|
|
76
|
+
* Observed: <code>{JSON.stringify(rect)}</code>
|
|
77
|
+
* </div>
|
|
78
|
+
*/
|
|
79
|
+
declare function useResizeObserver<T extends HTMLElement = any>(options?: ResizeObserverOptions): readonly [react.RefObject<T>, ObserverRect];
|
|
80
|
+
|
|
48
81
|
/**
|
|
49
82
|
* MIT License
|
|
50
83
|
*
|
|
@@ -117,4 +150,4 @@ type UseUniqueIdOptions = string | number | {
|
|
|
117
150
|
};
|
|
118
151
|
declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
|
|
119
152
|
|
|
120
|
-
export { useLocalStorage, useMergeRefs, useUncontrolled, useUniqueId };
|
|
153
|
+
export { useIsMounted, useLocalStorage, useMergeRefs, useResizeObserver, useUncontrolled, useUniqueId };
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useIsMounted as r } from "./hooks/useIsMounted.js";
|
|
2
|
+
import { useLocalStorage as t } from "./hooks/useLocalStorage.js";
|
|
2
3
|
import { useMergeRefs as f } from "./hooks/useMergeRefs.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
4
|
+
import { useResizeObserver as p } from "./hooks/useResizeObserver.js";
|
|
5
|
+
import { useUncontrolled as n } from "./hooks/useUncontrolled.js";
|
|
6
|
+
import { useUniqueId as l } from "./hooks/useUniqueId.js";
|
|
5
7
|
/*!
|
|
6
|
-
@versini/ui-hooks v4.
|
|
8
|
+
@versini/ui-hooks v4.1.1
|
|
7
9
|
© 2024 gizmette.com
|
|
8
10
|
*/
|
|
9
11
|
export {
|
|
10
|
-
r as
|
|
12
|
+
r as useIsMounted,
|
|
13
|
+
t as useLocalStorage,
|
|
11
14
|
f as useMergeRefs,
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
p as useResizeObserver,
|
|
16
|
+
n as useUncontrolled,
|
|
17
|
+
l as useUniqueId
|
|
14
18
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/ui-hooks",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"react": "18.3.1",
|
|
42
42
|
"react-dom": "18.3.1"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "0efa39da096a4718e375be4a7118f2e48786f401"
|
|
45
45
|
}
|