Skip to content

Commit dad2745

Browse files
author
Brian Vaughn
committed
Tweaked Tooltip positioning code
1 parent 30c6206 commit dad2745

File tree

1 file changed

+22
-26
lines changed
  • packages/react-devtools-shared/src/devtools/views/Profiler

1 file changed

+22
-26
lines changed

packages/react-devtools-shared/src/devtools/views/Profiler/Tooltip.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,32 @@ export default function Tooltip({children, label}: any) {
4343
);
4444
}
4545

46+
const TOOLTIP_OFFSET = 5;
47+
4648
// Method used to find the position of the tooltip based on current mouse position
4749
function getTooltipPosition(element, mousePosition) {
4850
const {height, mouseX, mouseY, width} = mousePosition;
49-
const TOOLTIP_OFFSET_X = 5;
50-
const TOOLTIP_OFFSET_Y = 15;
5151
let top = 0;
5252
let left = 0;
5353

54-
// Let's check the vertical position.
55-
if (mouseY + TOOLTIP_OFFSET_Y + element.offsetHeight >= height) {
56-
// The tooltip doesn't fit below the mouse cursor (which is our
57-
// default strategy). Therefore we try to position it either above the
58-
// mouse cursor or finally aligned with the window's top edge.
59-
if (mouseY - TOOLTIP_OFFSET_Y - element.offsetHeight > 0) {
60-
// We position the tooltip above the mouse cursor if it fits there.
61-
top = `${mouseY - element.offsetHeight - TOOLTIP_OFFSET_Y}px`;
54+
if (mouseY + TOOLTIP_OFFSET + element.offsetHeight >= height) {
55+
if (mouseY - TOOLTIP_OFFSET - element.offsetHeight > 0) {
56+
top = `${mouseY - element.offsetHeight - TOOLTIP_OFFSET}px`;
6257
} else {
63-
// Otherwise we align the tooltip with the window's top edge.
6458
top = '0px';
6559
}
6660
} else {
67-
top = `${mouseY + TOOLTIP_OFFSET_Y}px`;
61+
top = `${mouseY + TOOLTIP_OFFSET}px`;
6862
}
6963

70-
// Now let's check the horizontal position.
71-
if (mouseX + TOOLTIP_OFFSET_X + element.offsetWidth >= width) {
72-
// The tooltip doesn't fit at the right of the mouse cursor (which is
73-
// our default strategy). Therefore we try to position it either at the
74-
// left of the mouse cursor or finally aligned with the window's left
75-
// edge.
76-
if (mouseX - TOOLTIP_OFFSET_X - element.offsetWidth > 0) {
77-
// We position the tooltip at the left of the mouse cursor if it fits
78-
// there.
79-
left = `${mouseX - element.offsetWidth - TOOLTIP_OFFSET_X}px`;
64+
if (mouseX + TOOLTIP_OFFSET + element.offsetWidth >= width) {
65+
if (mouseX - TOOLTIP_OFFSET - element.offsetWidth > 0) {
66+
left = `${mouseX - element.offsetWidth - TOOLTIP_OFFSET}px`;
8067
} else {
81-
// Otherwise, align the tooltip with the window's left edge.
8268
left = '0px';
8369
}
8470
} else {
85-
left = `${mouseX + TOOLTIP_OFFSET_X * 2}px`;
71+
left = `${mouseX + TOOLTIP_OFFSET * 2}px`;
8672
}
8773

8874
return {left, top};
@@ -94,9 +80,19 @@ function getMousePosition(
9480
mouseEvent: SyntheticMouseEvent<*>,
9581
) {
9682
if (relativeContainer !== null) {
97-
const {height, top, width} = relativeContainer.getBoundingClientRect();
83+
// Positon within the nearest position:relative container.
84+
let targetContainer = relativeContainer;
85+
while (targetContainer.parentElement != null) {
86+
if (targetContainer.style.position === 'relative') {
87+
break;
88+
} else {
89+
targetContainer = targetContainer.parentElement;
90+
}
91+
}
92+
93+
const {height, left, top, width} = targetContainer.getBoundingClientRect();
9894

99-
const mouseX = mouseEvent.clientX;
95+
const mouseX = mouseEvent.clientX - left;
10096
const mouseY = mouseEvent.clientY - top;
10197

10298
return {height, mouseX, mouseY, width};

0 commit comments

Comments
 (0)