smarter popup placement

This commit is contained in:
Alex Yatskov 2017-03-11 17:01:47 -08:00
parent a8c6fc01b8
commit 9ebcc8e2c1

View File

@ -45,16 +45,32 @@ class Popup {
let x = elementRect.left; let x = elementRect.left;
let width = containerWidth; let width = containerWidth;
if (x + width >= window.innerWidth) { const overflowX = Math.max(x + width - document.body.clientWidth, 0);
width = Math.min(width, x); if (overflowX > 0) {
x = window.innerWidth - width; if (x >= overflowX) {
x -= overflowX;
} else {
width = document.body.clientWidth;
x = 0;
}
} }
let y = elementRect.bottom + this.offset; let y = 0;
let height = containerHeight; let height = containerHeight;
if (y + height >= window.innerHeight) { const yBelow = elementRect.bottom + this.offset;
height = Math.min(height, y); const yAbove = elementRect.top - this.offset;
y = elementRect.top - height - this.offset; const overflowBelow = Math.max(yBelow + height - document.body.clientHeight, 0);
const overflowAbove = Math.max(height - yAbove, 0);
if (overflowBelow > 0 || overflowAbove > 0) {
if (overflowBelow < overflowAbove) {
height = Math.max(height - overflowBelow, 0);
y = yBelow;
} else {
height = Math.max(height - overflowAbove, 0);
y = Math.max(yAbove - height, 0);
}
} else {
y = yBelow;
} }
this.showAt({x, y, width, height}); this.showAt({x, y, width, height});