Skip to content

Commit

Permalink
fix: ensure tooltip is in view
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeduboc committed Mar 2, 2024
1 parent dec8398 commit 0b1b95f
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/flamegraph/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ export const Tooltip: FunctionalComponent<TooltipProps> = ({

if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}

if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}

setStyle(pos);
Expand Down
4 changes: 2 additions & 2 deletions src/network/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export const Tooltip: FunctionalComponent<TooltipProps> = ({ node, visible }) =>

if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}

if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}

setStyle(pos);
Expand Down
4 changes: 1 addition & 3 deletions src/style/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ main {

padding: 5px;

white-space: nowrap;

font-size: 0.875rem;

background-color: var(--background-color);
Expand Down Expand Up @@ -139,4 +137,4 @@ main {

.node {
cursor: pointer;
}
}
4 changes: 2 additions & 2 deletions src/treemap/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ export const Tooltip: FunctionalComponent<TooltipProps> = ({

if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}

if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}

setStyle(pos);
Expand Down
32 changes: 12 additions & 20 deletions test/__snapshots__/e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -2962,11 +2961,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down Expand Up @@ -4666,7 +4665,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -9582,11 +9580,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down Expand Up @@ -10111,7 +10109,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -15008,7 +15005,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -18291,11 +18287,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down Expand Up @@ -19854,7 +19850,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -22747,11 +22742,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down Expand Up @@ -24457,7 +24452,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -29373,11 +29367,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down Expand Up @@ -29902,7 +29896,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -34799,7 +34792,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -38082,11 +38074,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down
32 changes: 12 additions & 20 deletions test/__snapshots__/gh59.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -2962,11 +2961,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down Expand Up @@ -4669,7 +4668,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -9585,11 +9583,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down Expand Up @@ -10121,7 +10119,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -15018,7 +15015,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -18301,11 +18297,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down Expand Up @@ -19864,7 +19860,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -22757,11 +22752,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down Expand Up @@ -24472,7 +24467,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -29388,11 +29382,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down Expand Up @@ -29928,7 +29922,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -34825,7 +34818,6 @@ main {
border: 2px solid;
border-radius: 5px;
padding: 5px;
white-space: nowrap;
font-size: 0.875rem;
background-color: var(--background-color);
color: var(--text-color);
Expand Down Expand Up @@ -38108,11 +38100,11 @@ var drawChart = (function (exports) {
const boundingRect = ref.current.getBoundingClientRect();
if (pos.left + boundingRect.width > window.innerWidth) {
// Shifting horizontally
pos.left = window.innerWidth - boundingRect.width;
pos.left = Math.max(0, window.innerWidth - boundingRect.width);
}
if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = Math.max(0, mouseCoords.y - Tooltip_marginY - boundingRect.height);
}
setStyle(pos);
};
Expand Down
Loading

0 comments on commit 0b1b95f

Please sign in to comment.