Skip to content

Commit bca6ddb

Browse files
committed
fix: boxes disappear when scrolling too slow
1 parent 260ba80 commit bca6ddb

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,32 +2095,57 @@ function RemoteFunctions(config) {
20952095
}
20962096

20972097
window.addEventListener("resize", redrawEverything);
2098-
// Add a capture-phase scroll listener to update highlights when
2099-
// any element scrolls.
2100-
21012098

21022099
// Helper function to dismiss boxes only for elements that don't move with scroll
2100+
// this is needed for fixed positioned elements because otherwise the boxes will move along with scroll,
2101+
// but the element stays at position which will lead to drift between the element & boxes
21032102
function _dismissBoxesForFixedElements() {
2103+
// first we try more options box, because its position is generally fixed even in overlapping cases
21042104
if (_nodeMoreOptionsBox && _nodeMoreOptionsBox.element) {
2105-
// Store the element's position before scroll
2106-
if (!_nodeMoreOptionsBox._lastPosition) {
2107-
_nodeMoreOptionsBox._lastPosition = _nodeMoreOptionsBox.element.getBoundingClientRect();
2108-
return; // First time, just store position
2109-
}
2105+
const moreOptionsBoxElement = _nodeMoreOptionsBox._shadow.querySelector('.phoenix-more-options-box');
2106+
if(moreOptionsBoxElement) {
21102107

2111-
const currentPosition = _nodeMoreOptionsBox.element.getBoundingClientRect();
2112-
const lastPosition = _nodeMoreOptionsBox._lastPosition;
2108+
// get the position of both the moreOptionsBox as well as the element
2109+
const moreOptionsBoxBounds = moreOptionsBoxElement.getBoundingClientRect();
2110+
const elementBounds = _nodeMoreOptionsBox.element.getBoundingClientRect();
21132111

2114-
// If element position hasn't changed despite scrolling, it's likely fixed/sticky
2115-
const positionUnchanged =
2116-
Math.abs(currentPosition.top - lastPosition.top) < 1 &&
2117-
Math.abs(currentPosition.left - lastPosition.left) < 1;
2112+
// this is to store the prev value, so that we can compare it the second time
2113+
if(!_nodeMoreOptionsBox._possDifference) {
2114+
_nodeMoreOptionsBox._possDifference = moreOptionsBoxBounds.top - elementBounds.top;
2115+
} else {
2116+
const calcNewDifference = moreOptionsBoxBounds.top - elementBounds.top;
2117+
const prevDifference = _nodeMoreOptionsBox._possDifference;
21182118

2119-
if (positionUnchanged) {
2120-
dismissMoreOptionsBox();
2121-
} else {
2122-
// Update stored position for next scroll event
2123-
_nodeMoreOptionsBox._lastPosition = currentPosition;
2119+
// 4 is just for pixelated differences
2120+
if (Math.abs(calcNewDifference - prevDifference) > 4) {
2121+
dismissMoreOptionsBox();
2122+
}
2123+
}
2124+
}
2125+
} else if (_nodeInfoBox && _nodeInfoBox.element) {
2126+
// if more options box didn't exist, we check with info box (logic is same)
2127+
const infoBoxElement = _nodeInfoBox._shadow.querySelector('.phoenix-node-info-box');
2128+
if (infoBoxElement) {
2129+
// here just we make sure that the element is same
2130+
if(!_nodeInfoBox._prevElement) {
2131+
_nodeInfoBox._prevElement = _nodeInfoBox.element;
2132+
} else if(_nodeInfoBox._prevElement !== _nodeInfoBox.element) {
2133+
return;
2134+
} else {
2135+
const infoBoxBounds = infoBoxElement.getBoundingClientRect();
2136+
const elementBounds = _nodeInfoBox.element.getBoundingClientRect();
2137+
2138+
if(!_nodeInfoBox._possDifference) {
2139+
_nodeInfoBox._possDifference = infoBoxBounds.top - elementBounds.top;
2140+
} else {
2141+
const calcNewDifference = infoBoxBounds.top - elementBounds.top;
2142+
const prevDifference = _nodeInfoBox._possDifference;
2143+
2144+
if (Math.abs(calcNewDifference - prevDifference) > 4) {
2145+
dismissMoreOptionsBox();
2146+
}
2147+
}
2148+
}
21242149
}
21252150
}
21262151
}

0 commit comments

Comments
 (0)