Skip to content

Commit ac6215b

Browse files
committed
fix: consider scroll
1 parent faf6f85 commit ac6215b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/app/ContentScript/SelectTranslator/SelectTranslator.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ export const getSelectedTextOfInput = (elm: HTMLInputElement | HTMLTextAreaEleme
7676
return elm.value.slice(selectionStart, selectionEnd);
7777
};
7878

79+
export const getAbsolutePositionOfElement = (element: HTMLElement) => {
80+
const bounds = element.getBoundingClientRect();
81+
const { scrollX = 0, scrollY = 0 } = window;
82+
return {
83+
x: bounds.x + scrollX,
84+
y: bounds.y + scrollY,
85+
};
86+
};
87+
7988
/**
8089
* This wrapper on component need to allow convenient manage state
8190
*/
@@ -343,10 +352,10 @@ export class SelectTranslator {
343352
// the element will be rendered too far of requested coordinates,
344353
// because element renders related root node
345354
// See issue #529
346-
const bounds = rootNode.getBoundingClientRect();
355+
const rootPosition = getAbsolutePositionOfElement(rootNode);
347356
const fixedPosition = {
348-
x: x - bounds.x,
349-
y: y - bounds.y,
357+
x: x - rootPosition.x,
358+
y: y - rootPosition.y,
350359
};
351360

352361
this.shadowRoot.mountComponent(

0 commit comments

Comments
 (0)