Skip to content

Commit 3e47d63

Browse files
committed
fix: positioning of the info box
1 parent 910b8af commit 3e47d63

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,13 @@ function RemoteFunctions(config) {
505505
// we need this as the value is not consistent, it depends on the number of options we show in the box
506506
_getBoxPosition: function(boxWidth) {
507507
const elemBounds = this.element.getBoundingClientRect();
508+
const offset = _screenOffset(this.element);
508509

509-
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
510-
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
510+
let topPos = offset.top - 30;
511+
let leftPos = offset.left + elemBounds.width - boxWidth;
511512

512-
let topPos = elemBounds.top - 30 + scrollTop;
513-
let leftPos = elemBounds.right - boxWidth + scrollLeft;
514-
515-
if (elemBounds.top - 30 < 0) {
516-
topPos = elemBounds.top + elemBounds.height + 5 + scrollTop;
513+
if (offset.top - 30 < 0) {
514+
topPos = offset.top + elemBounds.height + 5;
517515
}
518516

519517
return {topPos: topPos, leftPos: leftPos};
@@ -700,33 +698,15 @@ function RemoteFunctions(config) {
700698
}
701699

702700
NodeInfoBox.prototype = {
703-
_calcHeight: function() {
704-
const element = this.element;
705-
706-
let baseHeight = 26.75;
707-
if(element.id) {
708-
baseHeight += 17.25;
709-
}
710-
if(element.className.length !== 0) {
711-
baseHeight += 17.25;
712-
}
713-
714-
return baseHeight;
715-
},
716-
717-
_getBoxPosition: function() {
701+
_getBoxPosition: function(boxHeight) {
718702
const elemBounds = this.element.getBoundingClientRect();
703+
const offset = _screenOffset(this.element);
719704

720-
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
721-
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
722-
723-
const boxHeight = this._calcHeight();
724-
725-
let topPos = elemBounds.top - boxHeight + scrollTop;
726-
let leftPos = elemBounds.left + scrollLeft;
705+
let topPos = offset.top - boxHeight - 6; // 6 for just some little space to breathes
706+
let leftPos = offset.left;
727707

728-
if (elemBounds.top - boxHeight < 0) {
729-
topPos = elemBounds.top + elemBounds.height + 5 + scrollTop;
708+
if (offset.top - boxHeight < 0) {
709+
topPos = offset.top + elemBounds.height + 6;
730710
}
731711

732712
return {topPos: topPos, leftPos: leftPos};
@@ -737,8 +717,8 @@ function RemoteFunctions(config) {
737717

738718
// this is shadow DOM.
739719
// we need it because if we add the box directly to the DOM then users style might override it.
740-
// {mode: "closed"} means that users will not be able to access the shadow DOM
741-
const shadow = this.body.attachShadow({ mode: "closed" });
720+
// {mode: "open"} allows us to access the shadow DOM to get actual height/position of the boxes
721+
const shadow = this.body.attachShadow({ mode: "open" });
742722

743723
// get the ID and classes for that element, as we need to display it in the box
744724
const id = this.element.id;
@@ -764,10 +744,14 @@ function RemoteFunctions(config) {
764744
content += "</div>";
765745
}
766746

767-
const boxPos = this._getBoxPosition();
747+
// initially, we place our info box -1000px to the top but at the right left pos. this is done so that
748+
// we can take the text-wrapping inside the info box in account when calculating the height
749+
// after calculating the height of the box, we place it at the exact position above the element
750+
const offset = _screenOffset(this.element);
751+
const leftPos = offset.left;
768752

769753
const styles = `
770-
.box {
754+
.phoenix-node-info-box {
771755
background-color: #4285F4;
772756
color: white;
773757
border-radius: 3px;
@@ -777,9 +761,9 @@ function RemoteFunctions(config) {
777761
font-family: Arial, sans-serif;
778762
z-index: 2147483647;
779763
position: absolute;
780-
left: ${boxPos.leftPos}px;
781-
top: ${boxPos.topPos}px;
782-
max-width: fit-content;
764+
left: ${leftPos}px;
765+
top: -1000px;
766+
max-width: 300px;
783767
box-sizing: border-box;
784768
pointer-events: none;
785769
}
@@ -799,7 +783,7 @@ function RemoteFunctions(config) {
799783
`;
800784

801785
// add everything to the shadow box
802-
shadow.innerHTML = `<style>${styles}</style><div class="box">${content}</div>`;
786+
shadow.innerHTML = `<style>${styles}</style><div class="phoenix-node-info-box">${content}</div>`;
803787
this._shadow = shadow;
804788
},
805789

@@ -808,6 +792,16 @@ function RemoteFunctions(config) {
808792
this._style(); // style the box
809793

810794
window.document.body.appendChild(this.body);
795+
796+
// get the actual rendered height of the box and then we reposition it to the actual place
797+
const boxElement = this._shadow.querySelector('.phoenix-node-info-box');
798+
if (boxElement) {
799+
const nodeInfoBoxHeight = boxElement.getBoundingClientRect().height;
800+
const pos = this._getBoxPosition(nodeInfoBoxHeight);
801+
802+
boxElement.style.left = pos.leftPos + 'px';
803+
boxElement.style.top = pos.topPos + 'px';
804+
}
811805
},
812806

813807
remove: function() {

0 commit comments

Comments
 (0)