Skip to content

Commit bf9a787

Browse files
committed
fix: positioning of the info box
1 parent f691e5a commit bf9a787

File tree

1 file changed

+67
-38
lines changed

1 file changed

+67
-38
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -443,70 +443,99 @@ function RemoteFunctions(config) {
443443
}
444444

445445
NodeInfoBox.prototype = {
446-
create: function() {
447-
// Remove existing info box if any
448-
this.remove();
446+
_style: function() {
447+
this.body = window.document.createElement("div");
448+
449+
// this is shadow DOM.
450+
// we need it because if we add the box directly to the DOM then users style might override it.
451+
// {mode: "closed"} means that users will not be able to access the shadow DOM
452+
const shadow = this.body.attachShadow({ mode: "closed" });
453+
454+
// the element that was clicked
455+
let elemBounds = this.element.getBoundingClientRect();
456+
457+
// the positions where it should be placed
458+
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
459+
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
449460

450461
// this value decides where we need to show the box in the UI
451462
// we are creating this here, because if the element has IDs and Classes then we need to increase the value
452463
// so that the box doesn't obscure the element
453-
let pushBoxUp = 29; // px value
464+
let pushBoxUp = 28; // px value
454465

455466
// get the ID and classes for that element, as we need to display it in the box
456467
const id = this.element.id;
457468
const classes = this.element.className ? this.element.className.split(/\s+/).filter(Boolean) : [];
458469

459470
let content = ""; // this will hold the main content that will be displayed
460-
// add element tag name
461-
content += "<div style='font-weight: bold;'>" + this.element.tagName.toLowerCase() + "</div>";
471+
content += "<div class='tag-name'>" + this.element.tagName.toLowerCase() + "</div>"; // add element tag name
462472

463473
// Add ID if present
464474
if (id) {
465-
content += "<div style='margin-top: 3px;'>#" + id + "</div>";
466-
pushBoxUp += 17;
475+
content += "<div class='id-name'>#" + id + "</div>";
476+
pushBoxUp += 16;
467477
}
468478

469479
// Add classes (limit to 3 with dropdown indicator)
470480
if (classes.length > 0) {
471-
content += "<div style='margin-top: 3px;'>";
481+
content += "<div class='class-name'>";
472482
for (var i = 0; i < Math.min(classes.length, 3); i++) {
473483
content += "." + classes[i] + " ";
474484
}
475485
if (classes.length > 3) {
476-
content += "<span style='opacity: 0.8;'>+" + (classes.length - 3) + " more</span>";
486+
content += "<span class='exceeded-classes'>+" + (classes.length - 3) + " more</span>";
477487
}
478488
content += "</div>";
479-
480-
pushBoxUp += 17;
489+
pushBoxUp += 16;
481490
}
482491

483-
let elemBounds = this.element.getBoundingClientRect();
492+
// Now calculate topPos using the final pushBoxUp value
493+
const leftPos = elemBounds.left + scrollLeft;
494+
const topPos = (elemBounds.top - pushBoxUp < 0
495+
? elemBounds.top + elemBounds.height + 5
496+
: elemBounds.top - pushBoxUp) + scrollTop;
497+
498+
const styles = `
499+
.box {
500+
background-color: #4285F4;
501+
color: white;
502+
border-radius: 3px;
503+
padding: 5px 8px;
504+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
505+
font-size: 12px;
506+
font-family: Arial, sans-serif;
507+
z-index: 2147483647;
508+
position: absolute;
509+
left: ${leftPos}px;
510+
top: ${topPos}px;
511+
max-width: 300px;
512+
box-sizing: border-box;
513+
pointer-events: none;
514+
}
515+
516+
.tag-name {
517+
font-weight: bold;
518+
}
519+
520+
.id-name,
521+
.class-name {
522+
margin-top: 3px;
523+
}
524+
525+
.exceeded-classes {
526+
opacity: 0.8;
527+
}
528+
`;
529+
530+
// add everything to the shadow box
531+
shadow.innerHTML = `<style>${styles}</style><div class="box">${content}</div>`;
532+
this._shadow = shadow;
533+
},
534+
535+
create: function() {
536+
this.remove(); // remove existing box if already present
537+
this._style(); // style the box
484538

485-
// create the container
486-
this.body = window.document.createElement("div");
487-
this.body.style.setProperty("z-index", 2147483647);
488-
this.body.style.setProperty("position", "fixed");
489-
this.body.style.setProperty("left", elemBounds.left + "px");
490-
this.body.style.setProperty(
491-
"top",
492-
// if there's not enough space to show the box above the element,
493-
// we show it below the element
494-
(elemBounds.top - pushBoxUp < 0 ? elemBounds.top + elemBounds.height + 5 : elemBounds.top - pushBoxUp) +
495-
"px"
496-
);
497-
this.body.style.setProperty("font-size", "12px");
498-
this.body.style.setProperty("font-family", "Arial, sans-serif");
499-
500-
// Style the info box with a blue background
501-
this.body.style.setProperty("background", "#4285F4");
502-
this.body.style.setProperty("color", "white");
503-
this.body.style.setProperty("border-radius", "3px");
504-
this.body.style.setProperty("padding", "5px 8px");
505-
this.body.style.setProperty("box-shadow", "0 2px 5px rgba(0,0,0,0.2)");
506-
this.body.style.setProperty("max-width", "300px");
507-
this.body.style.setProperty("pointer-events", "none"); // Make it non-interactive
508-
509-
this.body.innerHTML = content;
510539
window.document.body.appendChild(this.body);
511540
},
512541

0 commit comments

Comments
 (0)