Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TooltipWithBounds] fix : overflowing its parent on small screens #837

Merged
merged 4 commits into from
Oct 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions packages/visx-tooltip/src/tooltips/TooltipWithBounds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,29 @@ function TooltipWithBounds({
let top = initialTop;

if (ownBounds && parentBounds) {
const placeTooltipLeft =
offsetLeft + ownBounds.right > parentBounds.right ||
offsetLeft + ownBounds.right > window.innerWidth;
let placeTooltipLeft = false;
let placeTooltipUp = false;

const placeTooltipUp =
offsetTop + ownBounds.bottom > parentBounds.bottom ||
offsetTop + ownBounds.bottom > window.innerHeight;
if (parentBounds.width) {
const rightPlacementClippedPx = left + offsetLeft + ownBounds.width - parentBounds.width;
const leftPlacementClippedPx = ownBounds.width - left - offsetLeft;
placeTooltipLeft =
rightPlacementClippedPx > 0 && rightPlacementClippedPx > leftPlacementClippedPx;
} else {
const rightPlacementClippedPx = left + offsetLeft + ownBounds.width - window.innerWidth;
const leftPlacementClippedPx = ownBounds.width - left - offsetLeft;
placeTooltipLeft =
rightPlacementClippedPx > 0 && rightPlacementClippedPx > leftPlacementClippedPx;
}

if (parentBounds.height) {
const bottomPlacementClippedPx = top + offsetTop + ownBounds.height - parentBounds.height;
const topPlacementClippedPx = ownBounds.height - top - offsetTop;
placeTooltipUp =
bottomPlacementClippedPx > 0 && bottomPlacementClippedPx > topPlacementClippedPx;
} else {
placeTooltipUp = top + offsetTop + ownBounds.height > window.innerHeight;
}

left = placeTooltipLeft ? left - ownBounds.width - offsetLeft : left + offsetLeft;
top = placeTooltipUp ? top - ownBounds.height - offsetTop : top + offsetTop;
Expand Down