Skip to content

Commit 0803b28

Browse files
committed
Avoid displaying popup outside viewport
1 parent 5fc921c commit 0803b28

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Lib/profiling/sampling/flamegraph.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,20 @@ function main() {
114114
const mouseX = event.pageX || event.clientX;
115115
const mouseY = event.pageY || event.clientY;
116116

117-
// Calculate tooltip width (default to 320px if not rendered yet)
117+
// Calculate tooltip dimensions (default to 320px width if not rendered yet)
118118
let tooltipWidth = 320;
119+
let tooltipHeight = 200;
119120
if (this._tooltip && this._tooltip.node()) {
120121
const node = this._tooltip
121122
.style("opacity", 0)
122123
.style("display", "block")
123124
.node();
124125
tooltipWidth = node.offsetWidth || 320;
126+
tooltipHeight = node.offsetHeight || 200;
125127
this._tooltip.style("display", null);
126128
}
127129

128-
// Calculate position: if overflow, show to the left of cursor
130+
// Calculate horizontal position: if overflow, show to the left of cursor
129131
const padding = 10;
130132
const rightEdge = mouseX + padding + tooltipWidth;
131133
const viewportWidth = window.innerWidth;
@@ -137,10 +139,21 @@ function main() {
137139
left = mouseX + padding;
138140
}
139141

142+
// Calculate vertical position: if overflow, show above cursor
143+
const bottomEdge = mouseY + padding + tooltipHeight;
144+
const viewportHeight = window.innerHeight;
145+
let top;
146+
if (bottomEdge > viewportHeight) {
147+
top = mouseY - tooltipHeight - padding;
148+
if (top < 0) top = padding; // prevent off top edge
149+
} else {
150+
top = mouseY + padding;
151+
}
152+
140153
this._tooltip
141154
.html(tooltipHTML)
142155
.style("left", left + "px")
143-
.style("top", mouseY - 10 + "px")
156+
.style("top", top + "px")
144157
.transition()
145158
.duration(200)
146159
.style("opacity", 1);

0 commit comments

Comments
 (0)