Skip to content

Commit

Permalink
Fix: tooltip position inside shadow root
Browse files Browse the repository at this point in the history
Tooltip now positions correctly when it is inside the shadowroot of a
custom element and that custom element is inside a fixed, absolute or
relatively positioned ancestor.
  • Loading branch information
mitchthorson committed Dec 31, 2024
1 parent 8313afa commit c31595d
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/lib/Tooltip/Tooltip.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,27 @@
if (!el) return;
// traverse the tree upwards to see if there are any absolutely, fixed or relatively positioned ancestors
let ancestor = el.parentNode;
while (ancestor && ancestor !== document.documentElement && ancestor instanceof Element) {
const position = window.getComputedStyle(ancestor).position;
if (position === "relative" || position === "absolute" || position === "fixed") {
return ancestor;
// loop through all ancestors until we reach the document element
while (ancestor && ancestor !== document.documentElement) {
// check if ancestor is a shadow root with a host element
if (ancestor instanceof ShadowRoot) {
// if it is, set ancestor to the host element instead
ancestor = ancestor.host;
}
// make sure ancestor is instance of element
if (ancestor instanceof Element) {
// if it is, check if it is relative, absolute or fixed position
const position = window.getComputedStyle(ancestor).position;
// return it if so
if (position === "relative" || position === "absolute" || position === "fixed") {
return ancestor;
}
// check next ancestor
ancestor = ancestor.parentNode;
} else {
// if ancestor is not an element, break the loop
break;
}
ancestor = ancestor.parentNode;
}
return null;
}
Expand Down

0 comments on commit c31595d

Please sign in to comment.