mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Graph: make ContextMenu potitioning aware of the viewport width (#19699)
This commit is contained in:
parent
0a2d5e16dd
commit
c2749052d7
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user