Commit c2749052 by Miguel Carvajal Committed by Torkel Ödegaard

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

parent 0a2d5e16
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment