-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_popover.tsx
161 lines (150 loc) · 3.67 KB
/
_popover.tsx
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// deno-lint-ignore-file no-explicit-any no-undef
import { React, ReactDOM } from "./_deps.tsx";
import { classnames } from "./_utils.tsx";
interface PopoverProps {
content: React.ReactNode;
placement?:
| "top-start"
| "top"
| "top-end"
| "bottom-start"
| "bottom"
| "bottom-end";
className?: string;
style?: React.CSSProperties;
onClick?: React.MouseEventHandler;
onMouseEnter?: React.MouseEventHandler;
onMouseLeave?: React.MouseEventHandler;
}
const hoverMap: Record<string, boolean> = {};
export const Popover: React.FC<PopoverProps> = ({
content,
placement = "top",
className,
style,
children,
onClick,
}) => {
const [hover, setHover] = React.useState(false);
const popoverRootId = "popover-root-" +
React.useMemo(() => Math.random().toString().slice(2), []);
const [topLeft, setTopLeft] = React.useState({
top: 0,
left: 0,
});
// https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node
const measuredRef = React.useCallback(
(node) => {
if (node !== null) {
const { top, left, right, bottom } = node.getBoundingClientRect();
setTopLeft({
top: (window as any).pageYOffset +
(placement.startsWith("top") ? top : bottom),
left: (window as any).pageXOffset +
(placement.endsWith("start")
? left
: placement.endsWith("end")
? right
: (left + right) / 2),
});
}
},
[hover],
);
const onMouseEnter = React.useCallback(() => {
hoverMap[popoverRootId] = true;
setHover(true);
}, []);
const onMouseLeave = React.useCallback(() => {
hoverMap[popoverRootId] = false;
setTimeout(() => {
if (hoverMap[popoverRootId]) {
return;
}
hoverMap[popoverRootId] = false;
setHover(false);
}, 300);
}, []);
let validChildren;
const props = {
ref: measuredRef,
onMouseEnter,
onMouseLeave,
onClick,
};
if (React.isValidElement(children)) {
validChildren = React.cloneElement(children, props);
} else {
validChildren = <span {...props}>{children}</span>;
}
return (
<>
{content && (
<PopoverPortal
popoverRootId={popoverRootId}
content={content}
placement={placement}
style={{
display: hover ? "block" : "none",
top: topLeft.top,
left: topLeft.left,
...style,
}}
className={className}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
/>
)}
{validChildren}
</>
);
};
const PopoverPortal: React.FC<
PopoverProps & {
popoverRootId: string;
}
> = (
{
popoverRootId,
content,
placement = "top",
className,
style,
onMouseEnter,
onMouseLeave,
},
) => {
if (window.Deno) {
return null;
}
// @ts-ignore need for parse
let popoverRoot = document.getElementById(popoverRootId);
if (!popoverRoot) {
// @ts-ignore need for parse
popoverRoot = document.createElement("div");
popoverRoot.id = popoverRootId;
// @ts-ignore need for parse
document.body.appendChild(popoverRoot);
}
return ReactDOM.createPortal(
<div
className={classnames(className, "popover")}
style={{
transform: `translate(${
placement.endsWith("start")
? "0"
: placement.endsWith("end")
? "-100%"
: "-50%"
}, ${placement.startsWith("top") ? "-100%" : "0%"})`,
...style,
}}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
{content}
</div>,
popoverRoot,
);
};
export default Popover;