Graph: make ContextMenu potitioning aware of the viewport width (#19699)

(cherry picked from commit c2749052d7)
This commit is contained in:
Miguel Carvajal
2019-10-16 12:32:09 +03:00
committed by Sofia Papagiannaki
parent e40dd982c6
commit fed38ea617
@@ -162,15 +162,7 @@ export const ContextMenu: React.FC<ContextMenuProps> = React.memo(({ x, y, onClo
return (
<Portal>
<div
ref={menuRef}
style={{
position: 'fixed',
left: x - 5,
top: y + 5,
}}
className={styles.wrapper}
>
<div ref={menuRef} style={getStyle(menuRef.current)} className={styles.wrapper}>
{renderHeader && <div className={styles.header}>{renderHeader()}</div>}
<List
items={items || []}
@@ -185,6 +177,25 @@ export const ContextMenu: React.FC<ContextMenuProps> = React.memo(({ x, y, onClo
</div>
</Portal>
);
function getStyle(menuNode: HTMLDivElement | null) {
const haventMeasuredMenuYet = !menuNode;
if (haventMeasuredMenuYet) {
return { visibility: 'hidden' as const };
}
const rect = menuNode!.getBoundingClientRect();
const OFFSET = 5;
const collisions = {
right: window.innerWidth < x + rect.width,
bottom: window.innerHeight < rect.bottom + rect.height + OFFSET,
};
return {
position: 'fixed' as const,
left: collisions.right ? x - rect.width - OFFSET : x - OFFSET,
top: collisions.bottom ? y - rect.height - OFFSET : y + OFFSET,
};
}
});
interface ContextMenuItemProps {