@w3ux/hooks 1.1.1 → 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 CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./useEffectIgnoreInitial";
2
2
  export * from "./useOutsideAlerter";
3
+ export * from "./useSize";
package/cjs/index.js CHANGED
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./useEffectIgnoreInitial"), exports);
18
18
  __exportStar(require("./useOutsideAlerter"), exports);
19
+ __exportStar(require("./useSize"), exports);
19
20
 
20
21
  //# sourceMappingURL=index.js.map
21
22
 
package/cjs/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAGA,2DAAyC;AACzC,sDAAoC","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nexport * from \"./useEffectIgnoreInitial\";\nexport * from \"./useOutsideAlerter\";\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,5 @@
1
+ import type { MutableRefObject } from "react";
2
+ export declare const useSize: (element: MutableRefObject<HTMLElement | null | undefined>, outerElement?: MutableRefObject<HTMLElement | null | undefined>) => {
3
+ width: number;
4
+ height: number;
5
+ };
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
@@ -1,2 +1,3 @@
1
1
  export * from "./useEffectIgnoreInitial";
2
2
  export * from "./useOutsideAlerter";
3
+ export * from "./useSize";
package/mjs/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./useEffectIgnoreInitial";
2
2
  export * from "./useOutsideAlerter";
3
+ export * from "./useSize";
3
4
 
4
5
  //# sourceMappingURL=index.js.map
5
6
 
package/mjs/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,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\";\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,5 @@
1
+ import type { MutableRefObject } from "react";
2
+ export declare const useSize: (element: MutableRefObject<HTMLElement | null | undefined>, outerElement?: MutableRefObject<HTMLElement | null | undefined>) => {
3
+ width: number;
4
+ height: number;
5
+ };
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
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@w3ux/hooks",
3
- "version": "1.1.1",
3
+ "version": "1.1.2-alpha.1",
4
4
  "license": "GPL-3.0-only",
5
+ "dependencies": {
6
+ "lodash.throttle": "^4.1.1"
7
+ },
5
8
  "main": "cjs/index.js",
6
9
  "module": "mjs/index.js",
7
10
  "exports": {