diff --git a/platform/ui/src/components/ContextMenu/ContextMenu.tsx b/platform/ui/src/components/ContextMenu/ContextMenu.tsx index 3cd66a635..c2bd83889 100644 --- a/platform/ui/src/components/ContextMenu/ContextMenu.tsx +++ b/platform/ui/src/components/ContextMenu/ContextMenu.tsx @@ -1,14 +1,33 @@ -import React from 'react'; +import React, { useEffect, useRef } from 'react'; import PropTypes from 'prop-types'; import Typography from '../Typography'; import { Icons } from '@ohif/ui-next'; const ContextMenu = ({ items, ...props }) => { + const contextMenuRef = useRef(null); + useEffect(() => { + if(!contextMenuRef?.current) { + return; + } + + const contextMenu = contextMenuRef.current; + + const boundingClientRect = contextMenu.getBoundingClientRect(); + if (boundingClientRect.bottom + boundingClientRect.height > window.innerHeight) { + props.defaultPosition.y = props.defaultPosition.y - boundingClientRect.height; + } + if (boundingClientRect.right + boundingClientRect.width > window.innerWidth) { + props.defaultPosition.x = props.defaultPosition.x - boundingClientRect.width; + } + }, [props.defaultPosition]); + if (!items) { return null; } + return (
e.preventDefault()} @@ -35,6 +54,10 @@ const ContextMenu = ({ items, ...props }) => { }; ContextMenu.propTypes = { + defaultPosition: PropTypes.shape({ + x: PropTypes.number, + y: PropTypes.number, + }), items: PropTypes.arrayOf( PropTypes.shape({ label: PropTypes.string.isRequired,