Skip to content

Commit 287182c

Browse files
committed
fix: also take the vertical scrollbar width into account when calculating positions
1 parent b88eafa commit 287182c

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,11 +1457,12 @@ function RemoteFunctions(config = {}) {
14571457
const elemBounds = this.element.getBoundingClientRect();
14581458
const offset = _screenOffset(this.element);
14591459
const MARGIN = 6;
1460+
const SCROLLBAR_OFFSET = 8;
14601461

14611462
const viewportLeft = window.scrollX;
1462-
const viewportRight = window.scrollX + window.innerWidth;
1463+
const viewportRight = window.scrollX + document.documentElement.clientWidth - SCROLLBAR_OFFSET;
14631464
const viewportTop = window.scrollY;
1464-
const viewportBottom = window.scrollY + window.innerHeight;
1465+
const viewportBottom = window.scrollY + document.documentElement.clientHeight - SCROLLBAR_OFFSET;
14651466

14661467
// Default: Top of element, left-aligned
14671468
let topPos = offset.top - boxHeight - MARGIN;
@@ -1471,8 +1472,8 @@ function RemoteFunctions(config = {}) {
14711472
const isVeryTall = elemBounds.height >= window.innerHeight - 50;
14721473

14731474
if (isVeryTall) {
1474-
// Place inside element at top
1475-
topPos = offset.top + MARGIN;
1475+
// Place inside element at top, but stick to viewport top if element extends above
1476+
topPos = Math.max(offset.top, viewportTop) + MARGIN;
14761477
leftPos = offset.left + MARGIN;
14771478
return {topPos, leftPos};
14781479
}
@@ -2020,11 +2021,12 @@ function RemoteFunctions(config = {}) {
20202021
const elemBounds = this.element.getBoundingClientRect();
20212022
const offset = _screenOffset(this.element);
20222023
const MARGIN = 6;
2024+
const SCROLLBAR_OFFSET = 8;
20232025

20242026
const viewportLeft = window.scrollX;
2025-
const viewportRight = window.scrollX + window.innerWidth;
2027+
const viewportRight = window.scrollX + document.documentElement.clientWidth - SCROLLBAR_OFFSET;
20262028
const viewportTop = window.scrollY;
2027-
const viewportBottom = window.scrollY + window.innerHeight;
2029+
const viewportBottom = window.scrollY + document.documentElement.clientHeight - SCROLLBAR_OFFSET;
20282030

20292031
// Default: Bottom of element, left-aligned
20302032
let topPos = offset.top + elemBounds.height + MARGIN;
@@ -2034,8 +2036,8 @@ function RemoteFunctions(config = {}) {
20342036
const isVeryTall = elemBounds.height >= window.innerHeight - 50;
20352037

20362038
if (isVeryTall) {
2037-
// Place inside element at bottom
2038-
topPos = offset.top + elemBounds.height - boxDimensions.height - MARGIN;
2039+
// Place inside element at bottom, but stick to viewport bottom if element extends below
2040+
topPos = Math.min(offset.top + elemBounds.height, viewportBottom) - boxDimensions.height - MARGIN;
20392041
leftPos = offset.left + MARGIN;
20402042
return {topPos, leftPos};
20412043
}

0 commit comments

Comments
 (0)