@w3ux/hooks 1.1.0 → 1.1.2-alpha.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/cjs/index.d.ts +2 -0
- package/cjs/index.js +2 -0
- package/cjs/index.js.map +1 -1
- package/cjs/useOutsideAlerter.d.ts +3 -0
- package/cjs/useOutsideAlerter.js +29 -0
- package/cjs/useOutsideAlerter.js.map +1 -0
- package/cjs/useSize.d.ts +5 -0
- package/cjs/useSize.js +32 -0
- package/cjs/useSize.js.map +1 -0
- package/mjs/index.d.ts +2 -0
- package/mjs/index.js +2 -0
- package/mjs/index.js.map +1 -1
- package/mjs/useOutsideAlerter.d.ts +3 -0
- package/mjs/useOutsideAlerter.js +25 -0
- package/mjs/useOutsideAlerter.js.map +1 -0
- package/mjs/useSize.d.ts +5 -0
- package/mjs/useSize.js +25 -0
- package/mjs/useSize.js.map +1 -0
- package/package.json +4 -1
package/cjs/index.d.ts
CHANGED
package/cjs/index.js
CHANGED
|
@@ -15,6 +15,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./useEffectIgnoreInitial"), exports);
|
|
18
|
+
__exportStar(require("./useOutsideAlerter"), exports);
|
|
19
|
+
__exportStar(require("./useSize"), exports);
|
|
18
20
|
|
|
19
21
|
//# sourceMappingURL=index.js.map
|
|
20
22
|
|
package/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAGA,2DAAyC","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nexport * from \"./useEffectIgnoreInitial\";\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAGA,2DAAyC;AACzC,sDAAoC;AACpC,4CAA0B","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nexport * from \"./useEffectIgnoreInitial\";\nexport * from \"./useOutsideAlerter\";\nexport * from \"./useSize\";\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOutsideAlerter = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const useOutsideAlerter = (ref, callback, ignore = []) => {
|
|
6
|
+
(0, react_1.useEffect)(() => {
|
|
7
|
+
const handleClickOutside = (ev) => {
|
|
8
|
+
if (ev) {
|
|
9
|
+
if (ref.current && !ref.current.contains(ev.target)) {
|
|
10
|
+
const target = ev.target;
|
|
11
|
+
const tagName = target.tagName.toLowerCase();
|
|
12
|
+
const ignored = ignore.some((i) => i.toLowerCase() === tagName || target.closest(`.${i}`) !== null);
|
|
13
|
+
if (!ignored) {
|
|
14
|
+
callback();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
document.addEventListener("mousedown", handleClickOutside);
|
|
20
|
+
return () => {
|
|
21
|
+
document.removeEventListener("mousedown", handleClickOutside);
|
|
22
|
+
};
|
|
23
|
+
}, [ref]);
|
|
24
|
+
};
|
|
25
|
+
exports.useOutsideAlerter = useOutsideAlerter;
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=useOutsideAlerter.js.map
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=useOutsideAlerter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useOutsideAlerter.tsx"],"names":[],"mappings":";;;AAGA,iCAAkD;AAI3C,MAAM,iBAAiB,GAAG,CAC/B,GAA2B,EAC3B,QAAqB,EACrB,SAAmB,EAAE,EACrB,EAAE;IACF,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,kBAAkB,GAAG,CAAC,EAAW,EAAE,EAAE;YACzC,IAAI,EAAE,EAAE,CAAC;gBACP,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAc,CAAC,EAAE,CAAC;oBAC5D,MAAM,MAAM,GAAG,EAAE,CAAC,MAAqB,CAAC;oBAGxC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;oBAC7C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CACzB,CAAC,CAAC,EAAE,EAAE,CACJ,CAAC,CAAC,WAAW,EAAE,KAAK,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,IAAI,CAClE,CAAC;oBAEF,IAAI,CAAC,OAAO,EAAE,CAAC;wBACb,QAAQ,EAAE,CAAC;oBACb,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC,CAAC;QACF,QAAQ,CAAC,gBAAgB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAC;QAC3D,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,mBAAmB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAC;QAChE,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACZ,CAAC,CAAC;AA7BW,QAAA,iBAAiB,qBA6B5B","file":"useOutsideAlerter.js","sourcesContent":["// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors\n// SPDX-License-Identifier: GPL-3.0-only\n\nimport { useEffect, type RefObject } from \"react\";\nimport type { AnyFunction, AnyJson } from \"@w3ux/types\";\n\n// A hook that alerts clicks outside of the passed ref.\nexport const useOutsideAlerter = (\n ref: RefObject<HTMLElement>,\n callback: AnyFunction,\n ignore: string[] = []\n) => {\n useEffect(() => {\n const handleClickOutside = (ev: AnyJson) => {\n if (ev) {\n if (ref.current && !ref.current.contains(ev.target as Node)) {\n const target = ev.target as HTMLElement;\n // Ignore tags with a name of `ignore`, or if there is a class of `ignore` in the parent\n // tree.\n const tagName = target.tagName.toLowerCase();\n const ignored = ignore.some(\n (i) =>\n i.toLowerCase() === tagName || target.closest(`.${i}`) !== null\n );\n\n if (!ignored) {\n callback();\n }\n }\n }\n };\n document.addEventListener(\"mousedown\", handleClickOutside);\n return () => {\n document.removeEventListener(\"mousedown\", handleClickOutside);\n };\n }, [ref]);\n};\n"]}
|
package/cjs/useSize.d.ts
ADDED
package/cjs/useSize.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useSize = void 0;
|
|
7
|
+
const lodash_throttle_1 = __importDefault(require("lodash.throttle"));
|
|
8
|
+
const react_1 = require("react");
|
|
9
|
+
const useSize = (element, outerElement) => {
|
|
10
|
+
const getSize = (el = null) => {
|
|
11
|
+
const width = (el === null || el === void 0 ? void 0 : el.offsetWidth) || 0;
|
|
12
|
+
const height = (el === null || el === void 0 ? void 0 : el.offsetHeight) || 0;
|
|
13
|
+
return { width, height };
|
|
14
|
+
};
|
|
15
|
+
const [size, setSize] = (0, react_1.useState)(getSize(element === null || element === void 0 ? void 0 : element.current));
|
|
16
|
+
const resizeThrottle = (0, lodash_throttle_1.default)(() => {
|
|
17
|
+
setSize(getSize(element === null || element === void 0 ? void 0 : element.current));
|
|
18
|
+
}, 100);
|
|
19
|
+
(0, react_1.useEffect)(() => {
|
|
20
|
+
const listenFor = (outerElement === null || outerElement === void 0 ? void 0 : outerElement.current) || window;
|
|
21
|
+
listenFor.addEventListener("resize", resizeThrottle);
|
|
22
|
+
return () => {
|
|
23
|
+
listenFor.removeEventListener("resize", resizeThrottle);
|
|
24
|
+
};
|
|
25
|
+
}, [outerElement]);
|
|
26
|
+
return size;
|
|
27
|
+
};
|
|
28
|
+
exports.useSize = useSize;
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=useSize.js.map
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=useSize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useSize.tsx"],"names":[],"mappings":";;;;;;AAGA,sEAAuC;AAEvC,iCAA4C;AAIrC,MAAM,OAAO,GAAG,CACrB,OAAyD,EACzD,YAA+D,EAC/D,EAAE;IAGF,MAAM,OAAO,GAAG,CAAC,KAAyB,IAAI,EAAE,EAAE;QAChD,MAAM,KAAK,GAAG,CAAA,EAAE,aAAF,EAAE,uBAAF,EAAE,CAAE,WAAW,KAAI,CAAC,CAAC;QACnC,MAAM,MAAM,GAAG,CAAA,EAAE,aAAF,EAAE,uBAAF,EAAE,CAAE,YAAY,KAAI,CAAC,CAAC;QACrC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAC3B,CAAC,CAAC;IAGF,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,IAAA,gBAAQ,EAC9B,OAAO,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC,CAC1B,CAAC;IAGF,MAAM,cAAc,GAAG,IAAA,yBAAQ,EAAC,GAAG,EAAE;QACnC,OAAO,CAAC,OAAO,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC,CAAC,CAAC;IACrC,CAAC,EAAE,GAAG,CAAC,CAAC;IAGR,IAAA,iBAAS,EAAC,GAAG,EAAE;QAGb,MAAM,SAAS,GAAG,CAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,OAAO,KAAI,MAAM,CAAC;QAElD,SAAS,CAAC,gBAAgB,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;QAGrD,OAAO,GAAG,EAAE;YACV,SAAS,CAAC,mBAAmB,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;QAC1D,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC;IAGnB,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAtCW,QAAA,OAAO,WAsClB","file":"useSize.js","sourcesContent":["// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors\n// SPDX-License-Identifier: GPL-3.0-only\n\nimport throttle from \"lodash.throttle\";\nimport type { MutableRefObject } from \"react\";\nimport { useEffect, useState } from \"react\";\n\n// Custom hook to get the width and height of a specified element. Updates the `size` state when the\n// specified \"outer element\" (or the window by default) resizes.\nexport const useSize = (\n element: MutableRefObject<HTMLElement | null | undefined>,\n outerElement?: MutableRefObject<HTMLElement | null | undefined>\n) => {\n // Helper function to retrieve the width and height of an element\n // If no element is found, default dimensions are set to 0.\n const getSize = (el: HTMLElement | null = null) => {\n const width = el?.offsetWidth || 0;\n const height = el?.offsetHeight || 0;\n return { width, height };\n };\n\n // State to store the current width and height of the specified element.\n const [size, setSize] = useState<{ width: number; height: number }>(\n getSize(element?.current)\n );\n\n // Throttle the resize event handler to limit how often size updates occur.\n const resizeThrottle = throttle(() => {\n setSize(getSize(element?.current));\n }, 100);\n\n // Set up the resize event listener on mount and clean it up on unmount.\n useEffect(() => {\n // Determine the target for the resize event listener.\n // If `outerElement` is provided, listen to its resize events; otherwise, listen to the window's.\n const listenFor = outerElement?.current || window;\n\n listenFor.addEventListener(\"resize\", resizeThrottle);\n\n // Clean up event listener when the component unmounts to avoid memory leaks.\n return () => {\n listenFor.removeEventListener(\"resize\", resizeThrottle);\n };\n }, [outerElement]);\n\n // Return the current size of the element.\n return size;\n};\n"]}
|
package/mjs/index.d.ts
CHANGED
package/mjs/index.js
CHANGED
package/mjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,0BAA0B,CAAC","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nexport * from \"./useEffectIgnoreInitial\";\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nexport * from \"./useEffectIgnoreInitial\";\nexport * from \"./useOutsideAlerter\";\nexport * from \"./useSize\";\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
export const useOutsideAlerter = (ref, callback, ignore = []) => {
|
|
3
|
+
useEffect(() => {
|
|
4
|
+
const handleClickOutside = (ev) => {
|
|
5
|
+
if (ev) {
|
|
6
|
+
if (ref.current && !ref.current.contains(ev.target)) {
|
|
7
|
+
const target = ev.target;
|
|
8
|
+
const tagName = target.tagName.toLowerCase();
|
|
9
|
+
const ignored = ignore.some((i) => i.toLowerCase() === tagName || target.closest(`.${i}`) !== null);
|
|
10
|
+
if (!ignored) {
|
|
11
|
+
callback();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
document.addEventListener("mousedown", handleClickOutside);
|
|
17
|
+
return () => {
|
|
18
|
+
document.removeEventListener("mousedown", handleClickOutside);
|
|
19
|
+
};
|
|
20
|
+
}, [ref]);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=useOutsideAlerter.js.map
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=useOutsideAlerter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useOutsideAlerter.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAkB,MAAM,OAAO,CAAC;AAIlD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,GAA2B,EAC3B,QAAqB,EACrB,SAAmB,EAAE,EACrB,EAAE;IACF,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,kBAAkB,GAAG,CAAC,EAAW,EAAE,EAAE;YACzC,IAAI,EAAE,EAAE,CAAC;gBACP,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAc,CAAC,EAAE,CAAC;oBAC5D,MAAM,MAAM,GAAG,EAAE,CAAC,MAAqB,CAAC;oBAGxC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;oBAC7C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CACzB,CAAC,CAAC,EAAE,EAAE,CACJ,CAAC,CAAC,WAAW,EAAE,KAAK,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,IAAI,CAClE,CAAC;oBAEF,IAAI,CAAC,OAAO,EAAE,CAAC;wBACb,QAAQ,EAAE,CAAC;oBACb,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC,CAAC;QACF,QAAQ,CAAC,gBAAgB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAC;QAC3D,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,mBAAmB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAC;QAChE,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACZ,CAAC,CAAC","file":"useOutsideAlerter.js","sourcesContent":["// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors\n// SPDX-License-Identifier: GPL-3.0-only\n\nimport { useEffect, type RefObject } from \"react\";\nimport type { AnyFunction, AnyJson } from \"@w3ux/types\";\n\n// A hook that alerts clicks outside of the passed ref.\nexport const useOutsideAlerter = (\n ref: RefObject<HTMLElement>,\n callback: AnyFunction,\n ignore: string[] = []\n) => {\n useEffect(() => {\n const handleClickOutside = (ev: AnyJson) => {\n if (ev) {\n if (ref.current && !ref.current.contains(ev.target as Node)) {\n const target = ev.target as HTMLElement;\n // Ignore tags with a name of `ignore`, or if there is a class of `ignore` in the parent\n // tree.\n const tagName = target.tagName.toLowerCase();\n const ignored = ignore.some(\n (i) =>\n i.toLowerCase() === tagName || target.closest(`.${i}`) !== null\n );\n\n if (!ignored) {\n callback();\n }\n }\n }\n };\n document.addEventListener(\"mousedown\", handleClickOutside);\n return () => {\n document.removeEventListener(\"mousedown\", handleClickOutside);\n };\n }, [ref]);\n};\n"]}
|
package/mjs/useSize.d.ts
ADDED
package/mjs/useSize.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import throttle from "lodash.throttle";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
export const useSize = (element, outerElement) => {
|
|
4
|
+
const getSize = (el = null) => {
|
|
5
|
+
const width = el?.offsetWidth || 0;
|
|
6
|
+
const height = el?.offsetHeight || 0;
|
|
7
|
+
return { width, height };
|
|
8
|
+
};
|
|
9
|
+
const [size, setSize] = useState(getSize(element?.current));
|
|
10
|
+
const resizeThrottle = throttle(() => {
|
|
11
|
+
setSize(getSize(element?.current));
|
|
12
|
+
}, 100);
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
const listenFor = outerElement?.current || window;
|
|
15
|
+
listenFor.addEventListener("resize", resizeThrottle);
|
|
16
|
+
return () => {
|
|
17
|
+
listenFor.removeEventListener("resize", resizeThrottle);
|
|
18
|
+
};
|
|
19
|
+
}, [outerElement]);
|
|
20
|
+
return size;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=useSize.js.map
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=useSize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useSize.tsx"],"names":[],"mappings":"AAGA,OAAO,QAAQ,MAAM,iBAAiB,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAI5C,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,OAAyD,EACzD,YAA+D,EAC/D,EAAE;IAGF,MAAM,OAAO,GAAG,CAAC,KAAyB,IAAI,EAAE,EAAE;QAChD,MAAM,KAAK,GAAG,EAAE,EAAE,WAAW,IAAI,CAAC,CAAC;QACnC,MAAM,MAAM,GAAG,EAAE,EAAE,YAAY,IAAI,CAAC,CAAC;QACrC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAC3B,CAAC,CAAC;IAGF,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAC9B,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAC1B,CAAC;IAGF,MAAM,cAAc,GAAG,QAAQ,CAAC,GAAG,EAAE;QACnC,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IACrC,CAAC,EAAE,GAAG,CAAC,CAAC;IAGR,SAAS,CAAC,GAAG,EAAE;QAGb,MAAM,SAAS,GAAG,YAAY,EAAE,OAAO,IAAI,MAAM,CAAC;QAElD,SAAS,CAAC,gBAAgB,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;QAGrD,OAAO,GAAG,EAAE;YACV,SAAS,CAAC,mBAAmB,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;QAC1D,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC;IAGnB,OAAO,IAAI,CAAC;AACd,CAAC,CAAC","file":"useSize.js","sourcesContent":["// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors\n// SPDX-License-Identifier: GPL-3.0-only\n\nimport throttle from \"lodash.throttle\";\nimport type { MutableRefObject } from \"react\";\nimport { useEffect, useState } from \"react\";\n\n// Custom hook to get the width and height of a specified element. Updates the `size` state when the\n// specified \"outer element\" (or the window by default) resizes.\nexport const useSize = (\n element: MutableRefObject<HTMLElement | null | undefined>,\n outerElement?: MutableRefObject<HTMLElement | null | undefined>\n) => {\n // Helper function to retrieve the width and height of an element\n // If no element is found, default dimensions are set to 0.\n const getSize = (el: HTMLElement | null = null) => {\n const width = el?.offsetWidth || 0;\n const height = el?.offsetHeight || 0;\n return { width, height };\n };\n\n // State to store the current width and height of the specified element.\n const [size, setSize] = useState<{ width: number; height: number }>(\n getSize(element?.current)\n );\n\n // Throttle the resize event handler to limit how often size updates occur.\n const resizeThrottle = throttle(() => {\n setSize(getSize(element?.current));\n }, 100);\n\n // Set up the resize event listener on mount and clean it up on unmount.\n useEffect(() => {\n // Determine the target for the resize event listener.\n // If `outerElement` is provided, listen to its resize events; otherwise, listen to the window's.\n const listenFor = outerElement?.current || window;\n\n listenFor.addEventListener(\"resize\", resizeThrottle);\n\n // Clean up event listener when the component unmounts to avoid memory leaks.\n return () => {\n listenFor.removeEventListener(\"resize\", resizeThrottle);\n };\n }, [outerElement]);\n\n // Return the current size of the element.\n return size;\n};\n"]}
|
package/package.json
CHANGED