@hrnec06/react_utils 1.7.4 → 1.8.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hrnec06/react_utils",
3
- "version": "1.7.4",
3
+ "version": "1.8.0",
4
4
  "description": "React utilities",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -0,0 +1,17 @@
1
+ import { Nullable } from "@hrnec06/util";
2
+ import useEvent from "./useEvent";
3
+ import useListener from "./useListener";
4
+
5
+ export default function useOutsideClick(
6
+ element: Nullable<HTMLElement>,
7
+ callback: (element: MouseEvent) => void
8
+ )
9
+ {
10
+ const cb = useEvent(callback);
11
+
12
+ useListener(document, 'mousedown', (event) => {
13
+ if (!element || !(event.target instanceof Node) || element.contains(event.target)) return;
14
+
15
+ cb(event);
16
+ }, [cb, element]);
17
+ }
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@ import useEfficientRef from "./hooks/useEfficientRef";
8
8
  import useUUID from "./hooks/useUUID";
9
9
  import useFlags from "./hooks/useFlags";
10
10
  import useNamespacedId from "./hooks/useNamespacedId";
11
+ import useOutsideClick from "./hooks/useOutsideClick";
11
12
  import useTransition, { transitionDataAttributes } from "./hooks/useTransition";
12
13
  import useLocalStorage from "./hooks/useLocalStorage";
13
14
  import useDebounce from "./hooks/useDebounce";
@@ -28,7 +29,6 @@ import ContextMenu from "./components/ContextMenu/ContextMenu";
28
29
  import * as util from './lib/utils';
29
30
  import ContextError from "./lib/errors/ContextError";
30
31
 
31
-
32
32
  export {
33
33
  // Hooks
34
34
  useDebounce,
@@ -42,6 +42,7 @@ export {
42
42
  useListener,
43
43
  useLocalStorage,
44
44
  useNamespacedId,
45
+ useOutsideClick,
45
46
  useSyncRef,
46
47
  useSyncRefAuto,
47
48
  useTransition,