Skip to content

Commit

Permalink
Fix bug where menu opens outside of screen
Browse files Browse the repository at this point in the history
  • Loading branch information
tentone committed Apr 13, 2023
1 parent e3c2c12 commit 1af7b91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
10 changes: 5 additions & 5 deletions source/editor/components/dropdown/DropdownMenu.js
Expand Up @@ -263,8 +263,8 @@ DropdownMenu.prototype.setExpanded = function(expanded)
this.panel.element.style.top = this.position.y + this.size.y + "px";
this.panel.element.style.left = this.position.x + "px";

var out = DOMUtils.checkBorder(this.panel);

var out = DOMUtils.checkBorder(this.panel.element);
console.log(out);
if (out.y !== 0)
{
this.panel.element.style.top = null;
Expand All @@ -280,7 +280,7 @@ DropdownMenu.prototype.setExpanded = function(expanded)
this.panel.element.style.bottom = this.position.y + this.size.y + "px";
this.panel.element.style.left = this.position.x + "px";

var out = DOMUtils.checkBorder(this.panel);
var out = DOMUtils.checkBorder(this.panel.element);
if (out.y !== 0)
{
this.panel.element.style.bottom = null;
Expand All @@ -296,7 +296,7 @@ DropdownMenu.prototype.setExpanded = function(expanded)
this.panel.element.style.top = this.position.y + "px";
this.panel.element.style.left = this.position.x + this.size.x + "px";

var out = DOMUtils.checkBorder(this.panel);
var out = DOMUtils.checkBorder(this.panel.element);
if (out.x !== 0)
{
this.panel.element.style.left = this.position.x - this.size.x + "px";
Expand All @@ -311,7 +311,7 @@ DropdownMenu.prototype.setExpanded = function(expanded)
this.panel.element.style.top = this.position.y + "px";
this.panel.element.style.left = this.position.x - this.size.x + "px";

var out = DOMUtils.checkBorder(this.panel);
var out = DOMUtils.checkBorder(this.panel.element);
if (out.x !== 0)
{
this.panel.element.style.left = this.position.x + this.size.x + "px";
Expand Down
25 changes: 10 additions & 15 deletions source/editor/utils/DOMUtils.js
Expand Up @@ -47,7 +47,7 @@ DOMUtils.isVisible = function(element)
left += element.offsetLeft;
}

return value = top >= window.pageYOffset && left >= window.pageXOffset && top + height <= window.pageYOffset + window.innerHeight && left + width <= window.pageXOffset + window.innerWidth;
return value = top >= window.scrollY && left >= window.scrollX && top + height <= window.scrollY + window.innerHeight && left + width <= window.scrollX + window.innerWidth;
};

/**
Expand Down Expand Up @@ -80,12 +80,7 @@ DOMUtils.getPosition = function(element)
* @return {Vector2} Distance outside of the viewport.
*/
DOMUtils.checkBorder = function(element)
{
if (element.isElement === true)
{
element = element.element;
}

{
var top = element.offsetTop;
var left = element.offsetLeft;
var width = element.offsetWidth;
Expand All @@ -101,25 +96,25 @@ DOMUtils.checkBorder = function(element)
var result = {x: 0, y: 0};

// Over the top of the window
if (top < window.pageYOffset)
if (top < window.scrollY)
{
result.y = top - window.pageYOffset;
result.y = top - window.scrollY;
}
// Bellow the window
else if (top + height > window.pageYOffset + window.innerHeight)
else if (top + height > window.scrollY + window.innerHeight)
{
result.y = top + height - (window.pageYOffset + window.innerHeight);
result.y = top + height - (window.scrollY + window.innerHeight);
}

// Left to the window
if (left < window.pageXOffset)
if (left < window.scrollX)
{
result.x = left - window.pageXOffset;
result.x = left - window.scrollX;
}
// Right to the window
else if (left + width > window.pageXOffset + window.innerWidth)
else if (left + width > window.scrollX + window.innerWidth)
{
result.x = left + width - (window.pageXOffset + window.innerWidth);
result.x = left + width - (window.scrollX + window.innerWidth);
}

return result;
Expand Down

0 comments on commit 1af7b91

Please sign in to comment.