Skip to content

Commit a9edff7

Browse files
committed
fix: improvised positioning of the dropdown
1 parent 7111f88 commit a9edff7

File tree

1 file changed

+59
-16
lines changed

1 file changed

+59
-16
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,33 +1757,76 @@ function RemoteFunctions(config = {}) {
17571757
_getDropdownPosition: function(dropdownWidth, dropdownHeight) {
17581758
const buttonBounds = this.ellipsisButton.getBoundingClientRect();
17591759
const viewportHeight = window.innerHeight;
1760+
const viewportWidth = window.innerWidth;
1761+
1762+
const optionsBox = _nodeMoreOptionsBox._shadow.querySelector(".phoenix-more-options-box");
1763+
const optionsBoxBounds = optionsBox.getBoundingClientRect();
1764+
const targetElementBounds = this.targetElement.getBoundingClientRect();
17601765

17611766
let topPos, leftPos;
17621767

1763-
// Check if there's enough space below the button
1764-
const spaceBelow = viewportHeight - buttonBounds.bottom;
1765-
const spaceAbove = buttonBounds.top;
1768+
const checkOverlap = function (dTop, dLeft, dWidth, dHeight) {
1769+
const dropdownRight = dLeft + dWidth;
1770+
const dropdownBottom = dTop + dHeight;
1771+
const elemRight = targetElementBounds.left + targetElementBounds.width;
1772+
const elemBottom = targetElementBounds.top + targetElementBounds.height;
1773+
1774+
return !(
1775+
dLeft > elemRight ||
1776+
dropdownRight < targetElementBounds.left ||
1777+
dTop > elemBottom ||
1778+
dropdownBottom < targetElementBounds.top
1779+
);
1780+
};
1781+
1782+
const isOptionsBoxAboveElement = optionsBoxBounds.bottom < targetElementBounds.top;
1783+
1784+
if (isOptionsBoxAboveElement) {
1785+
const spaceAbove = optionsBoxBounds.top;
1786+
1787+
if (spaceAbove >= dropdownHeight + 6) {
1788+
topPos = optionsBoxBounds.top + window.pageYOffset - dropdownHeight - 6;
1789+
} else {
1790+
topPos = optionsBoxBounds.bottom + window.pageYOffset + 6;
1791+
1792+
const tempTop = optionsBoxBounds.bottom;
1793+
const tempLeft = buttonBounds.right - dropdownWidth;
1794+
1795+
if (checkOverlap(tempTop, tempLeft, dropdownWidth, dropdownHeight)) {
1796+
let shiftedLeft = targetElementBounds.right + 6;
1797+
if (shiftedLeft + dropdownWidth <= viewportWidth - 6) {
1798+
leftPos = shiftedLeft + window.pageXOffset;
1799+
return { topPos: topPos, leftPos: leftPos };
1800+
}
17661801

1767-
if (spaceBelow >= dropdownHeight + 6) {
1768-
// Show below the ellipsis button
1769-
topPos = buttonBounds.bottom + window.pageYOffset + 6;
1770-
} else if (spaceAbove >= dropdownHeight + 6) {
1771-
// Show above the ellipsis button
1772-
topPos = buttonBounds.top + window.pageYOffset - dropdownHeight - 6;
1802+
shiftedLeft = targetElementBounds.left - dropdownWidth - 6;
1803+
if (shiftedLeft >= 6) {
1804+
leftPos = shiftedLeft + window.pageXOffset;
1805+
return { topPos: topPos, leftPos: leftPos };
1806+
}
1807+
}
1808+
}
17731809
} else {
1774-
// Not enough space either way, default to below
1775-
topPos = buttonBounds.bottom + window.pageYOffset + 6;
1810+
const spaceBelow = viewportHeight - optionsBoxBounds.bottom;
1811+
1812+
if (spaceBelow >= dropdownHeight + 6) {
1813+
topPos = optionsBoxBounds.bottom + window.pageYOffset + 6;
1814+
} else {
1815+
topPos = optionsBoxBounds.top + window.pageYOffset - dropdownHeight - 6;
1816+
}
17761817
}
17771818

1778-
// Align dropdown to the right edge of the button
17791819
leftPos = buttonBounds.right + window.pageXOffset - dropdownWidth;
17801820

1781-
// Make sure dropdown doesn't go off the left edge of viewport
1782-
if (leftPos < 0) {
1783-
leftPos = buttonBounds.left + window.pageXOffset;
1821+
if (leftPos < 6) {
1822+
leftPos = 6;
17841823
}
17851824

1786-
return {topPos: topPos, leftPos: leftPos};
1825+
if (leftPos + dropdownWidth > viewportWidth - 6) {
1826+
leftPos = viewportWidth - dropdownWidth - 6;
1827+
}
1828+
1829+
return { topPos: topPos, leftPos: leftPos };
17871830
},
17881831

17891832
_style: function() {

0 commit comments

Comments
 (0)