@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
41 lines (32 loc) • 1.68 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import { POSITIONS } from "../consts";
var isInside = function isInside(p, canBe) {
if (p === POSITIONS.TOP && canBe[p]) {
return POSITIONS.TOP;
}
if (p === POSITIONS.BOTTOM && canBe[p]) {
return POSITIONS.BOTTOM;
}
return false;
};
var calculateVerticalPosition = function calculateVerticalPosition(desiredPositions, pos) {
var _canBeInWindow, _canBeInDocument;
var canBeInWindow = (_canBeInWindow = {}, _defineProperty(_canBeInWindow, POSITIONS.TOP, pos.containerTop - pos.popoverHeight > pos.windowScrollTop), _defineProperty(_canBeInWindow, POSITIONS.BOTTOM, pos.containerTop - pos.windowScrollTop + pos.containerHeight + pos.popoverHeight < pos.windowHeight), _canBeInWindow);
var canBeInDocument = (_canBeInDocument = {}, _defineProperty(_canBeInDocument, POSITIONS.TOP, pos.containerTop - pos.popoverHeight >= 0), _defineProperty(_canBeInDocument, POSITIONS.BOTTOM, pos.containerTop + pos.containerHeight + pos.popoverHeight < pos.documentHeight), _canBeInDocument);
var possibleWindowPositions = desiredPositions.map(function (p) {
return isInside(p, canBeInWindow);
}).filter(function (p) {
return typeof p === "string";
});
var possibleDocumentPositions = desiredPositions.map(function (p) {
return isInside(p, canBeInDocument);
}).filter(function (p) {
return typeof p === "string";
}); // ordering in POSITIONS const is important
var posPosition = possibleWindowPositions[0] || possibleDocumentPositions[0];
if (posPosition) {
return posPosition;
}
return POSITIONS.BOTTOM;
};
export default calculateVerticalPosition;