| 1 |
/* |
| 2 |
* Copyright (C) 2019 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import React from "react"; |
| 7 |
|
| 8 |
import { Menu, MenuContent } from "@patternfly/react-core/dist/esm/components/Menu"; |
| 9 |
|
| 10 |
import "context-menu.scss"; |
| 11 |
|
| 12 |
/* |
| 13 |
* A context menu component |
| 14 |
* |
| 15 |
* It has two properties: |
| 16 |
* - parentId (required), area in which it listens to left button click |
| 17 |
* - children (optional), a MenuList to be rendered in the context menu |
| 18 |
*/ |
| 19 |
export const ContextMenu = ({ parentId, children } : { |
| 20 |
parentId: string, |
| 21 |
children?: React.ReactNode, |
| 22 |
}) => { |
| 23 |
const [visible, setVisible] = React.useState(false); |
| 24 |
const [event, setEvent] = React.useState<MouseEvent | null>(null); |
| 25 |
const root = React.useRef<HTMLDivElement>(null); |
| 26 |
|
| 27 |
React.useEffect(() => { |
| 28 |
const _handleContextMenu = (event: MouseEvent) => { |
| 29 |
/* In Firefox they explicitly prevent us from interrupting when holding shift while |
| 30 |
* right-clicking for context. Lets make it default for all browsers so they can inspect et. al. |
| 31 |
*/ |
| 32 |
if (event.shiftKey) { |
| 33 |
setVisible(false); |
| 34 |
return; |
| 35 |
} |
| 36 |
event.preventDefault(); |
| 37 |
|
| 38 |
setVisible(true); |
| 39 |
setEvent(event); |
| 40 |
}; |
| 41 |
|
| 42 |
const _handleClick = (event: MouseEvent) => { |
| 43 |
if (event.button === 0) |
| 44 |
setVisible(false); |
| 45 |
}; |
| 46 |
|
| 47 |
const parent = document.getElementById(parentId)!; |
| 48 |
parent.addEventListener('contextmenu', _handleContextMenu); |
| 49 |
document.addEventListener('click', _handleClick); |
| 50 |
|
| 51 |
return () => { |
| 52 |
parent.removeEventListener('contextmenu', _handleContextMenu); |
| 53 |
document.removeEventListener('click', _handleClick); |
| 54 |
}; |
| 55 |
}, [parentId]); |
| 56 |
|
| 57 |
React.useEffect(() => { |
| 58 |
if (!event || !root.current) |
| 59 |
return; |
| 60 |
|
| 61 |
const clickX = event.clientX; |
| 62 |
const clickY = event.clientY; |
| 63 |
const screenW = window.innerWidth; |
| 64 |
const screenH = window.innerHeight; |
| 65 |
const rootW = root.current.offsetWidth; |
| 66 |
const rootH = root.current.offsetHeight; |
| 67 |
|
| 68 |
const right = (screenW - clickX) > rootW; |
| 69 |
const left = !right; |
| 70 |
const top = (screenH - clickY) > rootH; |
| 71 |
const bottom = !top; |
| 72 |
|
| 73 |
if (right) { |
| 74 |
root.current.style.left = `${clickX + 5}px`; |
| 75 |
} |
| 76 |
|
| 77 |
if (left) { |
| 78 |
root.current.style.left = `${clickX - rootW - 5}px`; |
| 79 |
} |
| 80 |
|
| 81 |
if (top) { |
| 82 |
root.current.style.top = `${clickY + 5}px`; |
| 83 |
} |
| 84 |
|
| 85 |
if (bottom) { |
| 86 |
root.current.style.top = `${clickY - rootH - 5}px`; |
| 87 |
} |
| 88 |
}, [event]); |
| 89 |
|
| 90 |
return visible && |
| 91 |
<Menu ref={root} className="contextMenu"> |
| 92 |
<MenuContent ref={root}> |
| 93 |
{children} |
| 94 |
</MenuContent> |
| 95 |
</Menu>; |
| 96 |
}; |