@w3ux/hooks 1.1.2-alpha.1 → 1.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/cjs/useSize.d.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import type { MutableRefObject } from "react";
2
- export declare const useSize: (element: MutableRefObject<HTMLElement | null | undefined>, outerElement?: MutableRefObject<HTMLElement | null | undefined>) => {
2
+ interface UseSizeOptions {
3
+ outerElement?: MutableRefObject<HTMLElement | null | undefined>;
4
+ throttle?: number;
5
+ }
6
+ export declare const useSize: (element: MutableRefObject<HTMLElement | null | undefined>, options?: UseSizeOptions) => {
3
7
  width: number;
4
8
  height: number;
5
9
  };
10
+ export {};
package/cjs/useSize.js CHANGED
@@ -1,28 +1,31 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.useSize = void 0;
7
- const lodash_throttle_1 = __importDefault(require("lodash.throttle"));
8
4
  const react_1 = require("react");
9
- const useSize = (element, outerElement) => {
5
+ const useSize = (element, options = {}) => {
6
+ const { outerElement, throttle: throttleDuration = 100 } = options;
10
7
  const getSize = (el = null) => {
11
8
  const width = (el === null || el === void 0 ? void 0 : el.offsetWidth) || 0;
12
9
  const height = (el === null || el === void 0 ? void 0 : el.offsetHeight) || 0;
13
10
  return { width, height };
14
11
  };
12
+ const lastExecutedRef = (0, react_1.useRef)(0);
15
13
  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)(() => {
14
+ const handleResize = () => {
15
+ const now = Date.now();
16
+ if (now - lastExecutedRef.current < throttleDuration) {
17
+ return;
18
+ }
19
+ lastExecutedRef.current = now;
17
20
  setSize(getSize(element === null || element === void 0 ? void 0 : element.current));
18
- }, 100);
21
+ };
19
22
  (0, react_1.useEffect)(() => {
20
23
  const listenFor = (outerElement === null || outerElement === void 0 ? void 0 : outerElement.current) || window;
21
- listenFor.addEventListener("resize", resizeThrottle);
24
+ listenFor.addEventListener("resize", handleResize);
22
25
  return () => {
23
- listenFor.removeEventListener("resize", resizeThrottle);
26
+ listenFor.removeEventListener("resize", handleResize);
24
27
  };
25
- }, [outerElement]);
28
+ }, [outerElement === null || outerElement === void 0 ? void 0 : outerElement.current]);
26
29
  return size;
27
30
  };
28
31
  exports.useSize = useSize;
@@ -1 +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"]}
1
+ {"version":3,"sources":["../src/useSize.tsx"],"names":[],"mappings":";;;AAI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;YACV,SAAS,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QACxD,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,OAAO,CAAC,CAAC,CAAC;IAG5B,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAlDW,QAAA,OAAO,WAkDlB","file":"useSize.js","sourcesContent":["// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors\n// SPDX-License-Identifier: GPL-3.0-only\n\nimport type { MutableRefObject } from \"react\";\nimport { useEffect, useRef, useState } from \"react\";\n\n// Define the type for the options parameter.\ninterface UseSizeOptions {\n outerElement?: MutableRefObject<HTMLElement | null | undefined>;\n throttle?: number;\n}\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 options: UseSizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options;\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 // Ref to store the last execution time of the `resizeThrottle` handler.\n const lastExecutedRef = useRef<number>(0);\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 handleResize = () => {\n const now = Date.now();\n if (now - lastExecutedRef.current < throttleDuration) {\n return;\n } // Exit if `throttleDuration` has not passed.\n\n lastExecutedRef.current = now; // Update last execution time.\n\n setSize(getSize(element?.current));\n };\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\", handleResize);\n\n // Clean up event listener when the component unmounts to avoid memory leaks.\n return () => {\n listenFor.removeEventListener(\"resize\", handleResize);\n };\n }, [outerElement?.current]);\n\n // Return the current size of the element.\n return size;\n};\n"]}
package/mjs/useSize.d.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import type { MutableRefObject } from "react";
2
- export declare const useSize: (element: MutableRefObject<HTMLElement | null | undefined>, outerElement?: MutableRefObject<HTMLElement | null | undefined>) => {
2
+ interface UseSizeOptions {
3
+ outerElement?: MutableRefObject<HTMLElement | null | undefined>;
4
+ throttle?: number;
5
+ }
6
+ export declare const useSize: (element: MutableRefObject<HTMLElement | null | undefined>, options?: UseSizeOptions) => {
3
7
  width: number;
4
8
  height: number;
5
9
  };
10
+ export {};
package/mjs/useSize.js CHANGED
@@ -1,22 +1,28 @@
1
- import throttle from "lodash.throttle";
2
- import { useEffect, useState } from "react";
3
- export const useSize = (element, outerElement) => {
1
+ import { useEffect, useRef, useState } from "react";
2
+ export const useSize = (element, options = {}) => {
3
+ const { outerElement, throttle: throttleDuration = 100 } = options;
4
4
  const getSize = (el = null) => {
5
5
  const width = el?.offsetWidth || 0;
6
6
  const height = el?.offsetHeight || 0;
7
7
  return { width, height };
8
8
  };
9
+ const lastExecutedRef = useRef(0);
9
10
  const [size, setSize] = useState(getSize(element?.current));
10
- const resizeThrottle = throttle(() => {
11
+ const handleResize = () => {
12
+ const now = Date.now();
13
+ if (now - lastExecutedRef.current < throttleDuration) {
14
+ return;
15
+ }
16
+ lastExecutedRef.current = now;
11
17
  setSize(getSize(element?.current));
12
- }, 100);
18
+ };
13
19
  useEffect(() => {
14
20
  const listenFor = outerElement?.current || window;
15
- listenFor.addEventListener("resize", resizeThrottle);
21
+ listenFor.addEventListener("resize", handleResize);
16
22
  return () => {
17
- listenFor.removeEventListener("resize", resizeThrottle);
23
+ listenFor.removeEventListener("resize", handleResize);
18
24
  };
19
- }, [outerElement]);
25
+ }, [outerElement?.current]);
20
26
  return size;
21
27
  };
22
28
 
@@ -1 +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"]}
1
+ {"version":3,"sources":["../src/useSize.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAUpD,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,OAAyD,EACzD,UAA0B,EAAE,EAC5B,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;IAInE,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,eAAe,GAAG,MAAM,CAAS,CAAC,CAAC,CAAC;IAG1C,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAC9B,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAC1B,CAAC;IAGF,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,IAAI,GAAG,GAAG,eAAe,CAAC,OAAO,GAAG,gBAAgB,EAAE,CAAC;YACrD,OAAO;QACT,CAAC;QAED,eAAe,CAAC,OAAO,GAAG,GAAG,CAAC;QAE9B,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IACrC,CAAC,CAAC;IAGF,SAAS,CAAC,GAAG,EAAE;QAGb,MAAM,SAAS,GAAG,YAAY,EAAE,OAAO,IAAI,MAAM,CAAC;QAElD,SAAS,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QAGnD,OAAO,GAAG,EAAE;YACV,SAAS,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QACxD,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC;IAG5B,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 type { MutableRefObject } from \"react\";\nimport { useEffect, useRef, useState } from \"react\";\n\n// Define the type for the options parameter.\ninterface UseSizeOptions {\n outerElement?: MutableRefObject<HTMLElement | null | undefined>;\n throttle?: number;\n}\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 options: UseSizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options;\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 // Ref to store the last execution time of the `resizeThrottle` handler.\n const lastExecutedRef = useRef<number>(0);\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 handleResize = () => {\n const now = Date.now();\n if (now - lastExecutedRef.current < throttleDuration) {\n return;\n } // Exit if `throttleDuration` has not passed.\n\n lastExecutedRef.current = now; // Update last execution time.\n\n setSize(getSize(element?.current));\n };\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\", handleResize);\n\n // Clean up event listener when the component unmounts to avoid memory leaks.\n return () => {\n listenFor.removeEventListener(\"resize\", handleResize);\n };\n }, [outerElement?.current]);\n\n // Return the current size of the element.\n return size;\n};\n"]}
package/package.json CHANGED
@@ -1,10 +1,7 @@
1
1
  {
2
2
  "name": "@w3ux/hooks",
3
- "version": "1.1.2-alpha.1",
3
+ "version": "1.2.0",
4
4
  "license": "GPL-3.0-only",
5
- "dependencies": {
6
- "lodash.throttle": "^4.1.1"
7
- },
8
5
  "main": "cjs/index.js",
9
6
  "module": "mjs/index.js",
10
7
  "exports": {