@w3ux/hooks 1.2.0 → 1.2.1-beta.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/index.d.ts +1 -0
- package/cjs/index.js +1 -0
- package/cjs/index.js.map +1 -1
- package/cjs/useOnResize.d.ts +7 -0
- package/cjs/useOnResize.js +28 -0
- package/cjs/useOnResize.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/useOnResize.d.ts +7 -0
- package/mjs/useOnResize.js +24 -0
- package/mjs/useOnResize.js.map +1 -0
- package/package.json +1 -1
package/cjs/index.d.ts
CHANGED
package/cjs/index.js
CHANGED
|
@@ -15,6 +15,7 @@ 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("./useOnResize"), exports);
|
|
18
19
|
__exportStar(require("./useOutsideAlerter"), exports);
|
|
19
20
|
__exportStar(require("./useSize"), exports);
|
|
20
21
|
|
package/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAGA,2DAAyC;AACzC,gDAA8B;AAC9B,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 \"./useOnResize\";\nexport * from \"./useOutsideAlerter\";\nexport * from \"./useSize\";\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { MutableRefObject } from "react";
|
|
2
|
+
interface UseOnResizeOptions {
|
|
3
|
+
outerElement?: MutableRefObject<HTMLElement | null | undefined>;
|
|
4
|
+
throttle?: number;
|
|
5
|
+
}
|
|
6
|
+
export declare const useOnResize: (callback: () => void, options?: UseOnResizeOptions) => void;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOnResize = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const useOnResize = (callback, options = {}) => {
|
|
6
|
+
const { outerElement, throttle: throttleDuration = 100 } = options;
|
|
7
|
+
const lastExecutedRef = (0, react_1.useRef)(0);
|
|
8
|
+
const handleResize = () => {
|
|
9
|
+
const now = Date.now();
|
|
10
|
+
if (now - lastExecutedRef.current < throttleDuration) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
lastExecutedRef.current = now;
|
|
14
|
+
callback();
|
|
15
|
+
};
|
|
16
|
+
(0, react_1.useEffect)(() => {
|
|
17
|
+
const listenFor = (outerElement === null || outerElement === void 0 ? void 0 : outerElement.current) || window;
|
|
18
|
+
listenFor.addEventListener("resize", handleResize);
|
|
19
|
+
return () => {
|
|
20
|
+
listenFor.removeEventListener("resize", handleResize);
|
|
21
|
+
};
|
|
22
|
+
}, [throttleDuration, callback]);
|
|
23
|
+
};
|
|
24
|
+
exports.useOnResize = useOnResize;
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=useOnResize.js.map
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=useOnResize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useOnResize.tsx"],"names":[],"mappings":";;;AAAA,iCAA4D;AAQrD,MAAM,WAAW,GAAG,CACzB,QAAoB,EACpB,UAA8B,EAAE,EAChC,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;IACnE,MAAM,eAAe,GAAG,IAAA,cAAM,EAAS,CAAC,CAAC,CAAC;IAG1C,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;QAC9B,QAAQ,EAAE,CAAC;IACb,CAAC,CAAC;IAEF,IAAA,iBAAS,EAAC,GAAG,EAAE;QAGb,MAAM,SAAS,GAAG,CAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,OAAO,KAAI,MAAM,CAAC;QAGlD,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,gBAAgB,EAAE,QAAQ,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;AA/BW,QAAA,WAAW,eA+BtB","file":"useOnResize.js","sourcesContent":["import { MutableRefObject, useEffect, useRef } from \"react\";\n\ninterface UseOnResizeOptions {\n outerElement?: MutableRefObject<HTMLElement | null | undefined>;\n throttle?: number;\n}\n\n// Hook to execute a callback function on window resize, with optional throttling.\nexport const useOnResize = (\n callback: () => void,\n options: UseOnResizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options;\n const lastExecutedRef = useRef<number>(0);\n\n // Throttled resize handler\n const handleResize = () => {\n const now = Date.now();\n if (now - lastExecutedRef.current < throttleDuration) {\n return;\n }\n\n lastExecutedRef.current = now;\n callback();\n };\n\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 // Add event listener for resize on mount.\n listenFor.addEventListener(\"resize\", handleResize);\n\n // Clean up event listener on unmount.\n return () => {\n listenFor.removeEventListener(\"resize\", handleResize);\n };\n }, [throttleDuration, callback]);\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;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"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,0BAA0B,CAAC;AACzC,cAAc,eAAe,CAAC;AAC9B,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 \"./useOnResize\";\nexport * from \"./useOutsideAlerter\";\nexport * from \"./useSize\";\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { MutableRefObject } from "react";
|
|
2
|
+
interface UseOnResizeOptions {
|
|
3
|
+
outerElement?: MutableRefObject<HTMLElement | null | undefined>;
|
|
4
|
+
throttle?: number;
|
|
5
|
+
}
|
|
6
|
+
export declare const useOnResize: (callback: () => void, options?: UseOnResizeOptions) => void;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
export const useOnResize = (callback, options = {}) => {
|
|
3
|
+
const { outerElement, throttle: throttleDuration = 100 } = options;
|
|
4
|
+
const lastExecutedRef = useRef(0);
|
|
5
|
+
const handleResize = () => {
|
|
6
|
+
const now = Date.now();
|
|
7
|
+
if (now - lastExecutedRef.current < throttleDuration) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
lastExecutedRef.current = now;
|
|
11
|
+
callback();
|
|
12
|
+
};
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
const listenFor = outerElement?.current || window;
|
|
15
|
+
listenFor.addEventListener("resize", handleResize);
|
|
16
|
+
return () => {
|
|
17
|
+
listenFor.removeEventListener("resize", handleResize);
|
|
18
|
+
};
|
|
19
|
+
}, [throttleDuration, callback]);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=useOnResize.js.map
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=useOnResize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useOnResize.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAoB,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAQ5D,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,QAAoB,EACpB,UAA8B,EAAE,EAChC,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;IACnE,MAAM,eAAe,GAAG,MAAM,CAAS,CAAC,CAAC,CAAC;IAG1C,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;QAC9B,QAAQ,EAAE,CAAC;IACb,CAAC,CAAC;IAEF,SAAS,CAAC,GAAG,EAAE;QAGb,MAAM,SAAS,GAAG,YAAY,EAAE,OAAO,IAAI,MAAM,CAAC;QAGlD,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,gBAAgB,EAAE,QAAQ,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC","file":"useOnResize.js","sourcesContent":["import { MutableRefObject, useEffect, useRef } from \"react\";\n\ninterface UseOnResizeOptions {\n outerElement?: MutableRefObject<HTMLElement | null | undefined>;\n throttle?: number;\n}\n\n// Hook to execute a callback function on window resize, with optional throttling.\nexport const useOnResize = (\n callback: () => void,\n options: UseOnResizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options;\n const lastExecutedRef = useRef<number>(0);\n\n // Throttled resize handler\n const handleResize = () => {\n const now = Date.now();\n if (now - lastExecutedRef.current < throttleDuration) {\n return;\n }\n\n lastExecutedRef.current = now;\n callback();\n };\n\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 // Add event listener for resize on mount.\n listenFor.addEventListener(\"resize\", handleResize);\n\n // Clean up event listener on unmount.\n return () => {\n listenFor.removeEventListener(\"resize\", handleResize);\n };\n }, [throttleDuration, callback]);\n};\n"]}
|