Skip to content

Commit 544865f

Browse files
committed
fix: don't show UI boxes when element is hidden
1 parent 468f8a7 commit 544865f

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ function RemoteFunctions(config) {
122122
);
123123
}
124124

125+
// Checks if an element is actually visible to the user (not hidden, collapsed, or off-screen)
126+
function isElementVisible(element) {
127+
// Check if element has zero dimensions (indicates it's hidden or collapsed)
128+
const rect = element.getBoundingClientRect();
129+
if (rect.width === 0 && rect.height === 0) {
130+
return false;
131+
}
132+
133+
// Check computed styles for visibility
134+
const computedStyle = window.getComputedStyle(element);
135+
if (computedStyle.display === 'none' ||
136+
computedStyle.visibility === 'hidden' ||
137+
computedStyle.opacity === '0') {
138+
return false;
139+
}
140+
141+
// Check if any parent element is hidden
142+
let parent = element.parentElement;
143+
while (parent && parent !== document.body) {
144+
const parentStyle = window.getComputedStyle(parent);
145+
if (parentStyle.display === 'none' ||
146+
parentStyle.visibility === 'hidden') {
147+
return false;
148+
}
149+
parent = parent.parentElement;
150+
}
151+
152+
return true;
153+
}
154+
125155
// returns the distance from the top of the closest relatively positioned parent element
126156
function getDocumentOffsetTop(element) {
127157
return element.offsetTop + (element.offsetParent ? getDocumentOffsetTop(element.offsetParent) : 0);
@@ -656,11 +686,11 @@ function RemoteFunctions(config) {
656686
return;
657687
}
658688

659-
// this check because when there is no element on the visible viewport, we don't want to show the box
689+
// this check because when there is no element visible to the user, we don't want to show the box
660690
// for ex: when user clicks on a 'x' button and the button is responsible to hide a panel
661691
// then clicking on that button shouldn't show the more options box
662-
const elemBounds = this.element.getBoundingClientRect();
663-
if(elemBounds.height === 0 && elemBounds.width === 0) {
692+
// also covers cases where elements are inside closed/collapsed menus
693+
if(!isElementVisible(this.element)) {
664694
return;
665695
}
666696

@@ -868,11 +898,11 @@ function RemoteFunctions(config) {
868898
return;
869899
}
870900

871-
// this check because when there is no element on the visible viewport, we don't want to show the box
901+
// this check because when there is no element visible to the user, we don't want to show the box
872902
// for ex: when user clicks on a 'x' button and the button is responsible to hide a panel
873903
// then clicking on that button shouldn't show the more options box
874-
const elemBounds = this.element.getBoundingClientRect();
875-
if(elemBounds.height === 0 && elemBounds.width === 0) {
904+
// also covers cases where elements are inside closed/collapsed menus
905+
if(!isElementVisible(this.element)) {
876906
return;
877907
}
878908

@@ -1356,13 +1386,25 @@ function RemoteFunctions(config) {
13561386
}
13571387
}
13581388

1359-
_nodeMoreOptionsBox = new NodeMoreOptionsBox(element);
1389+
// make sure that the element is actually visible to the user
1390+
if (isElementVisible(element)) {
1391+
_nodeMoreOptionsBox = new NodeMoreOptionsBox(element);
13601392

1361-
// show the info box when a DOM element is selected
1362-
if (_nodeInfoBox) {
1363-
_nodeInfoBox.remove();
1393+
// show the info box when a DOM element is selected
1394+
if (_nodeInfoBox) {
1395+
_nodeInfoBox.remove();
1396+
}
1397+
_nodeInfoBox = new NodeInfoBox(element, true); // true means that the element was selected
1398+
} else {
1399+
// Element is hidden, so don't show UI boxes but still apply visual styling
1400+
_nodeMoreOptionsBox = null;
1401+
1402+
// Remove any existing info box since the element is not visible
1403+
if (_nodeInfoBox) {
1404+
_nodeInfoBox.remove();
1405+
_nodeInfoBox = null;
1406+
}
13641407
}
1365-
_nodeInfoBox = new NodeInfoBox(element, true); // true means that the element was selected
13661408

13671409
element._originalOutline = element.style.outline;
13681410
element.style.outline = "1px solid #4285F4";

0 commit comments

Comments
 (0)