Skip to content

Commit

Permalink
clamp tooltip position to the visible area of the viewport. closes #1041
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelcobain committed Jun 14, 2019
1 parent f03272b commit c9022e9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion addon/utils/calculate-tooltip-position.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import clamp from './clamp';

export default function calculateTooltipPosition(tooltip, target, position) {
let panelBounds = tooltip.getBoundingClientRect();
let panelWidth = panelBounds.width;
Expand Down Expand Up @@ -41,5 +43,17 @@ export default function calculateTooltipPosition(tooltip, target, position) {
break;
}

// clamp position to the visible area of the viewport

let tooltipBounds = tooltip.getBoundingClientRect();

// account for negative margins
let { marginTop: tooltipMarginTop, marginLeft: tooltipMarginLeft } = window.getComputedStyle(tooltip);
tooltipMarginTop = parseInt(tooltipMarginTop);
tooltipMarginLeft = parseInt(tooltipMarginLeft);

positionStyle.top = clamp(positionStyle.top, 0 - tooltipMarginTop, window.innerHeight - tooltipBounds.height - tooltipMarginTop);
positionStyle.left = clamp(positionStyle.left, 0 - tooltipMarginLeft, window.innerWidth - tooltipBounds.width - tooltipMarginLeft);

return positionStyle;
}
}

0 comments on commit c9022e9

Please sign in to comment.