@yakad/ui 1.2.1 → 1.2.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/clickAwayListener/clickAwayListener.tsx +27 -0
- package/index.tsx +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
interface ClickAwayListenerProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
4
|
+
onClickAway: React.MouseEventHandler<HTMLDivElement>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function ClickAwayListener(props: ClickAwayListenerProps) {
|
|
8
|
+
const ref = useRef(null);
|
|
9
|
+
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
const handleOutSideClick = (event: MouseEvent) => {
|
|
12
|
+
if (ref.current && !(ref.current as any).contains(event.target)) {
|
|
13
|
+
props.onClickAway && props.onClickAway(event as any);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
document.addEventListener("click", handleOutSideClick, true);
|
|
18
|
+
|
|
19
|
+
// CleanUp When element unmount
|
|
20
|
+
return () =>
|
|
21
|
+
document.removeEventListener("click", handleOutSideClick, true);
|
|
22
|
+
}, [props.onClickAway]);
|
|
23
|
+
|
|
24
|
+
return <div ref={ref}>{props.children}</div>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default ClickAwayListener;
|
package/index.tsx
CHANGED
|
@@ -16,6 +16,7 @@ import Row from "./row/row";
|
|
|
16
16
|
import Spacer from "./spacer/spacer";
|
|
17
17
|
import Stack from "./stack/stack";
|
|
18
18
|
import SvgIcon from "./svgIcon/svgIcon";
|
|
19
|
+
import ClickAwayListener from "./clickAwayListener/clickAwayListener";
|
|
19
20
|
|
|
20
21
|
export {
|
|
21
22
|
Button,
|
|
@@ -36,4 +37,5 @@ export {
|
|
|
36
37
|
Stack,
|
|
37
38
|
Spacer,
|
|
38
39
|
SvgIcon,
|
|
40
|
+
ClickAwayListener,
|
|
39
41
|
};
|