(children: React.ReactNode[], {
event,
offset = [0, 0],
}: {
event: MouseEvent | React.MouseEvent;
offset?: [number, number];
})
| 193 | } |
| 194 | |
| 195 | export function createContextMenu(children: React.ReactNode[], { |
| 196 | event, |
| 197 | offset = [0, 0], |
| 198 | }: { |
| 199 | event: MouseEvent | React.MouseEvent; |
| 200 | offset?: [number, number]; |
| 201 | }) { |
| 202 | event.preventDefault(); |
| 203 | event.stopPropagation(); |
| 204 | |
| 205 | const viewportWidth = window.innerWidth; |
| 206 | const viewportHeight = window.innerHeight; |
| 207 | const dividerCount = React.Children.count(children.filter(child => React.isValidElement(child) && child.type === Divider)); |
| 208 | const popupItemCount = React.Children.count(children.filter(child => React.isValidElement(child) && (child.type === PopupItem || child.type === Item))); |
| 209 | const menuHeight = popupItemCount * parseInt(getMenuItemHeight(), 10) + dividerCount * 8 + 16; |
| 210 | const menuWidthLimit = 200; |
| 211 | let x = event.clientX + offset[0]; |
| 212 | let y = event.clientY + offset[1]; |
| 213 | if (x + menuWidthLimit > viewportWidth) { |
| 214 | x = x - menuWidthLimit; |
| 215 | } |
| 216 | if (y + menuHeight > viewportHeight) { |
| 217 | y = y - menuHeight; |
| 218 | } |
| 219 | |
| 220 | const menuInstance = Menu.create({ |
| 221 | target: document.body, |
| 222 | offset: [x, y], |
| 223 | children, |
| 224 | className: 'engine-context-menu', |
| 225 | }); |
| 226 | |
| 227 | destroyFn = (menuInstance as any).destroy; |
| 228 | |
| 229 | return destroyFn; |
| 230 | } |
no test coverage detected
searching dependent graphs…