@carbonorm/carbonreact 4.0.24 → 4.0.25
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/dist/components/OutsideClickHandler/OutsideClickHandler.d.ts +9 -0
- package/dist/index.cjs.js +24 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +25 -3
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -2
- package/src/components/Errors/BackendThrowable.tsx +1 -1
- package/src/components/OutsideClickHandler/OutsideClickHandler.tsx +33 -0
- package/src/components/Popup/Popup.tsx +1 -1
- package/src/index.ts +2 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ReactNode, useRef, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
interface OutsideClickHandlerProps {
|
|
4
|
+
onOutsideClick: () => any;
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
useCapture?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default function OutsideClickHandler({ onOutsideClick, children, disabled, useCapture }: OutsideClickHandlerProps) {
|
|
11
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (disabled) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
function handle(event: MouseEvent | TouchEvent) {
|
|
18
|
+
const element = containerRef.current;
|
|
19
|
+
if (!element || element.contains(event.target as Node)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
onOutsideClick();
|
|
23
|
+
}
|
|
24
|
+
document.addEventListener('mousedown', handle, useCapture);
|
|
25
|
+
document.addEventListener('touchstart', handle, useCapture);
|
|
26
|
+
return () => {
|
|
27
|
+
document.removeEventListener('mousedown', handle, useCapture);
|
|
28
|
+
document.removeEventListener('touchstart', handle, useCapture);
|
|
29
|
+
};
|
|
30
|
+
}, [onOutsideClick, disabled, useCapture]);
|
|
31
|
+
|
|
32
|
+
return <div ref={containerRef}>{children}</div>;
|
|
33
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import classNames from "classnames";
|
|
2
|
-
import OutsideClickHandler from '
|
|
2
|
+
import OutsideClickHandler from '../OutsideClickHandler/OutsideClickHandler';
|
|
3
3
|
import getStyles from "hoc/getStyles";
|
|
4
4
|
import {PropsWithChildren, ReactElement} from "react";
|
|
5
5
|
|
package/src/index.ts
CHANGED
|
@@ -23,6 +23,8 @@ export { default as Loading } from "./components/Loading/Loading";
|
|
|
23
23
|
export * from "./components/Loading/Loading";
|
|
24
24
|
export { default as Nest } from "./components/Nest/Nest";
|
|
25
25
|
export * from "./components/Nest/Nest";
|
|
26
|
+
export { default as OutsideClickHandler } from "./components/OutsideClickHandler/OutsideClickHandler";
|
|
27
|
+
export * from "./components/OutsideClickHandler/OutsideClickHandler";
|
|
26
28
|
export { default as Popup } from "./components/Popup/Popup";
|
|
27
29
|
export * from "./components/Popup/Popup";
|
|
28
30
|
export { default as CarbonWebSocket } from "./components/WebSocket/CarbonWebSocket";
|