-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPortalDrawer.js
78 lines (69 loc) · 1.71 KB
/
PortalDrawer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { useMemo, useCallback } from "react";
import { createPortal } from "react-dom";
const PortalDrawer = ({
children,
overlayColor,
placement = "Left",
onOutsideClick,
zIndex = 100,
}) => {
const drawerStyle = useMemo(() => {
const style = {};
style.zIndex = zIndex;
if (overlayColor) {
style.backgroundColor = overlayColor;
}
switch (placement) {
case "Left":
style.alignItems = "flex-start";
break;
case "Right":
style.alignItems = "flex-end";
break;
case "Top":
style.alignItems = "center";
break;
case "Bottom":
style.alignItems = "center";
style.justifyContent = "flex-end";
break;
}
return style;
}, [placement, overlayColor, zIndex]);
const onOverlayClick = useCallback(
(e) => {
if (onOutsideClick && e.target.classList.contains("portalPopupOverlay")) {
onOutsideClick();
}
e.stopPropagation();
},
[onOutsideClick]
);
return (
<DrawerContainer>
<div
className="portalPopupOverlay"
style={{
...drawerStyle,
display: "flex",
flexDirection: "column",
position: "fixed",
inset: 0,
}}
onClick={onOverlayClick}
>
{children}
</div>
</DrawerContainer>
);
};
export const DrawerContainer = ({ children, containerId = "portals" }) => {
let portalsDiv = document.getElementById(containerId);
if (!portalsDiv) {
portalsDiv = document.createElement("div");
portalsDiv.setAttribute("id", containerId);
document.body.appendChild(portalsDiv);
}
return createPortal(children, portalsDiv);
};
export default PortalDrawer;