@w3ux/hooks 1.1.0 → 1.1.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 +1 -0
- package/cjs/index.js +1 -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/mjs/index.d.ts +1 -0
- package/mjs/index.js +1 -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/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("./useOutsideAlerter"), exports);
|
|
18
19
|
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
|
20
21
|
|
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","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"]}
|
|
@@ -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/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","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"]}
|
|
@@ -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"]}
|