@w3ux/hooks 1.1.1 → 1.1.2-alpha.2
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 +1 -0
- package/cjs/index.js +1 -0
- package/cjs/index.js.map +1 -1
- package/cjs/useSize.d.ts +10 -0
- package/cjs/useSize.js +35 -0
- package/cjs/useSize.js.map +1 -0
- package/mjs/index.d.ts +1 -0
- package/mjs/index.js +1 -0
- package/mjs/index.js.map +1 -1
- package/mjs/useSize.d.ts +10 -0
- package/mjs/useSize.js +31 -0
- package/mjs/useSize.js.map +1 -0
- package/package.json +1 -1
package/cjs/index.d.ts
CHANGED
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"]}
|
package/cjs/useSize.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MutableRefObject } from "react";
|
|
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) => {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
};
|
|
10
|
+
export {};
|
package/cjs/useSize.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSize = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const useSize = (element, options = {}) => {
|
|
6
|
+
const { outerElement, throttle: throttleDuration = 100 } = options;
|
|
7
|
+
const getSize = (el = null) => {
|
|
8
|
+
const width = (el === null || el === void 0 ? void 0 : el.offsetWidth) || 0;
|
|
9
|
+
const height = (el === null || el === void 0 ? void 0 : el.offsetHeight) || 0;
|
|
10
|
+
return { width, height };
|
|
11
|
+
};
|
|
12
|
+
const lastExecutedRef = (0, react_1.useRef)(0);
|
|
13
|
+
const [size, setSize] = (0, react_1.useState)(getSize(element === null || element === void 0 ? void 0 : element.current));
|
|
14
|
+
const handleResize = () => {
|
|
15
|
+
const now = Date.now();
|
|
16
|
+
if (now - lastExecutedRef.current < throttleDuration) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
lastExecutedRef.current = now;
|
|
20
|
+
setSize(getSize(element === null || element === void 0 ? void 0 : element.current));
|
|
21
|
+
};
|
|
22
|
+
(0, react_1.useEffect)(() => {
|
|
23
|
+
const listenFor = (outerElement === null || outerElement === void 0 ? void 0 : outerElement.current) || window;
|
|
24
|
+
listenFor.addEventListener("resize", handleResize);
|
|
25
|
+
return () => {
|
|
26
|
+
listenFor.removeEventListener("resize", handleResize);
|
|
27
|
+
};
|
|
28
|
+
}, [outerElement === null || outerElement === void 0 ? void 0 : outerElement.current]);
|
|
29
|
+
return size;
|
|
30
|
+
};
|
|
31
|
+
exports.useSize = useSize;
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=useSize.js.map
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=useSize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useSize.tsx"],"names":[],"mappings":";;;AAIA,iCAAoD;AAU7C,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,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,eAAe,GAAG,IAAA,cAAM,EAAS,CAAC,CAAC,CAAC;IAG1C,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,IAAA,gBAAQ,EAC9B,OAAO,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,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,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC,CAAC,CAAC;IACrC,CAAC,CAAC;IAGF,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,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,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/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;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"]}
|
package/mjs/useSize.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MutableRefObject } from "react";
|
|
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) => {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
};
|
|
10
|
+
export {};
|
package/mjs/useSize.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
export const useSize = (element, options = {}) => {
|
|
3
|
+
const { outerElement, throttle: throttleDuration = 100 } = options;
|
|
4
|
+
const getSize = (el = null) => {
|
|
5
|
+
const width = el?.offsetWidth || 0;
|
|
6
|
+
const height = el?.offsetHeight || 0;
|
|
7
|
+
return { width, height };
|
|
8
|
+
};
|
|
9
|
+
const lastExecutedRef = useRef(0);
|
|
10
|
+
const [size, setSize] = useState(getSize(element?.current));
|
|
11
|
+
const handleResize = () => {
|
|
12
|
+
const now = Date.now();
|
|
13
|
+
if (now - lastExecutedRef.current < throttleDuration) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
lastExecutedRef.current = now;
|
|
17
|
+
setSize(getSize(element?.current));
|
|
18
|
+
};
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
const listenFor = outerElement?.current || window;
|
|
21
|
+
listenFor.addEventListener("resize", handleResize);
|
|
22
|
+
return () => {
|
|
23
|
+
listenFor.removeEventListener("resize", handleResize);
|
|
24
|
+
};
|
|
25
|
+
}, [outerElement?.current]);
|
|
26
|
+
return size;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=useSize.js.map
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=useSize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,E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ot-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"]}
|