Skip to content

Commit

Permalink
fix: 🐛 getBoundingRect not taking parents into account
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulRill00 committed Jan 23, 2025
1 parent 05a50e0 commit 0910f9f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,17 @@ export const isCommentNode = (el?: Node): el is Comment => el?.nodeType === Node
export const isTaggableNode = (el?: Node) => el && !isTextNode(el) && !isCommentNode(el);

export const getBoundingRect = (el: HTMLElement) => {
const top = el.offsetTop;
const left = el.offsetLeft;
let top = el.offsetTop;
let left = el.offsetLeft;
const width = el.offsetWidth;
const height = el.offsetHeight;

let currentEl = el;
while ((currentEl = currentEl.offsetParent as HTMLElement)) {
top += currentEl.offsetTop;
left += currentEl.offsetLeft;
}

return {
top,
left,
Expand All @@ -168,7 +174,7 @@ export const getBoundingRect = (el: HTMLElement) => {
x: left,
y: top,
};
}
};

/**
* Get DOMRect of the element.
Expand Down

0 comments on commit 0910f9f

Please sign in to comment.