UNPKG

@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.

52 lines (39 loc) 2.05 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _consts = require("../consts"); var isInside = function isInside(p, canBe) { if (p === _consts.POSITIONS.TOP && canBe[p]) { return _consts.POSITIONS.TOP; } if (p === _consts.POSITIONS.BOTTOM && canBe[p]) { return _consts.POSITIONS.BOTTOM; } return false; }; var calculateVerticalPosition = function calculateVerticalPosition(desiredPositions, pos) { var _canBeInWindow, _canBeInDocument; var canBeInWindow = (_canBeInWindow = {}, (0, _defineProperty2.default)(_canBeInWindow, _consts.POSITIONS.TOP, pos.containerTop - pos.popoverHeight > pos.windowScrollTop), (0, _defineProperty2.default)(_canBeInWindow, _consts.POSITIONS.BOTTOM, pos.containerTop - pos.windowScrollTop + pos.containerHeight + pos.popoverHeight < pos.windowHeight), _canBeInWindow); var canBeInDocument = (_canBeInDocument = {}, (0, _defineProperty2.default)(_canBeInDocument, _consts.POSITIONS.TOP, pos.containerTop - pos.popoverHeight >= 0), (0, _defineProperty2.default)(_canBeInDocument, _consts.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 _consts.POSITIONS.BOTTOM; }; var _default = calculateVerticalPosition; exports.default = _default;