Skip to content

Commit

Permalink
No issue - calculate tooltip position based on root node host
Browse files Browse the repository at this point in the history
  • Loading branch information
Manvel committed Jan 20, 2024
1 parent ed5611b commit 90f485e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/cba-tooltip/cba-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ class List extends HTMLElement
_setDirection()
{
const clientRect = this.getBoundingClientRect();
const viewportWidth = document.documentElement.clientWidth;
const viewportHeight = document.documentElement.clientHeight;
const hostElement = this.getRootNode().host;
const viewportWidth = hostElement ? hostElement.clientWidth : document.documentElement.clientWidth;
const viewportHeight = hostElement ? hostElement.clientHeight : document.documentElement.clientHeight;
const distanceLeft = clientRect.left;
const distanceTop = clientRect.top;
const clientVerticalCenter = clientRect.top + clientRect.height / 2;
const viewoirtVerticalCenter = viewportHeight / 2;
const distanceRight = viewportWidth - clientRect.right;
const distanceBottom = viewportHeight - clientRect.bottom;
const placementX = distanceLeft - distanceRight > 0 ? "left" : "right";
const placementY = distanceTop - distanceBottom > 0 ? "top" : "bottom";
const placementY = viewoirtVerticalCenter > clientVerticalCenter ? "top" : "bottom";
this.tooltipElem.dataset.tooltip = `${placementX}-${placementY}`;
}

Expand Down

0 comments on commit 90f485e

Please sign in to comment.