@addev-be/ui 0.8.8 → 0.8.9
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,14 @@
|
|
|
1
1
|
import * as styles from './styles';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
CSSProperties,
|
|
5
|
+
FC,
|
|
6
|
+
PropsWithChildren,
|
|
7
|
+
useCallback,
|
|
8
|
+
useEffect,
|
|
9
|
+
useMemo,
|
|
10
|
+
useRef,
|
|
11
|
+
} from 'react';
|
|
4
12
|
|
|
5
13
|
import { usePortals } from '../../../providers';
|
|
6
14
|
|
|
@@ -57,23 +65,47 @@ const getDropdownStyle = (dropdown: DropdownProps): CSSProperties => {
|
|
|
57
65
|
export const Dropdown: FC<DropdownProps> = ({
|
|
58
66
|
children,
|
|
59
67
|
onClose,
|
|
60
|
-
$backdropAlpha,
|
|
61
68
|
...props
|
|
62
69
|
}) => {
|
|
63
70
|
const { createPortal } = usePortals();
|
|
64
71
|
|
|
72
|
+
const firstClick = useRef(true);
|
|
73
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
65
74
|
const style = useMemo(() => getDropdownStyle(props), [props]);
|
|
66
75
|
|
|
76
|
+
const onDocumentClick = useCallback(
|
|
77
|
+
(ev: MouseEvent) => {
|
|
78
|
+
if (firstClick.current) {
|
|
79
|
+
firstClick.current = false;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (
|
|
83
|
+
containerRef.current &&
|
|
84
|
+
!containerRef.current.contains(ev.target as Node)
|
|
85
|
+
) {
|
|
86
|
+
return onClose?.();
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
[onClose]
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
if (containerRef.current) {
|
|
94
|
+
document.addEventListener('click', onDocumentClick);
|
|
95
|
+
return () => {
|
|
96
|
+
document.removeEventListener('click', onDocumentClick);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}, [onDocumentClick]);
|
|
100
|
+
|
|
67
101
|
const modalPortal = useMemo(
|
|
68
102
|
() =>
|
|
69
103
|
createPortal(
|
|
70
|
-
<styles.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
</styles.DropdownContainer>
|
|
74
|
-
</styles.DropdownBackdrop>
|
|
104
|
+
<styles.DropdownContainer ref={containerRef} {...props} style={style}>
|
|
105
|
+
{children}
|
|
106
|
+
</styles.DropdownContainer>
|
|
75
107
|
),
|
|
76
|
-
[
|
|
108
|
+
[children, createPortal, props, style]
|
|
77
109
|
);
|
|
78
110
|
|
|
79
111
|
return <>{modalPortal}</>;
|