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.

66 lines (49 loc) 3.4 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 _isPosition = require("./isPosition"); var _consts = require("../consts"); var getPositionDirection = function getPositionDirection(position) { if ((0, _isPosition.isVertical)(position)) { return _consts.POSITION_DIRECTIONS.VERTICAL; } if ((0, _isPosition.isHorizontal)(position)) { return _consts.POSITION_DIRECTIONS.HORIZONTAL; } return null; }; var getPossibleAligns = function getPossibleAligns(_ref) { var _POSITION_DIRECTIONS$, _POSITION_DIRECTIONS$2, _ref2; var containerHeight = _ref.containerHeight, containerWidth = _ref.containerWidth, tooltipWidth = _ref.tooltipWidth, tooltipHeight = _ref.tooltipHeight, windowWidth = _ref.windowWidth, windowHeight = _ref.windowHeight, containerLeftPure = _ref.containerLeftPure, containerTopPure = _ref.containerTopPure; return _ref2 = {}, (0, _defineProperty2.default)(_ref2, _consts.POSITION_DIRECTIONS.VERTICAL, (_POSITION_DIRECTIONS$ = {}, (0, _defineProperty2.default)(_POSITION_DIRECTIONS$, _consts.ALIGNS.START, containerLeftPure + containerWidth / 2 - _consts.TOOLTIP_TOTAL_PADDING > 0 && containerLeftPure + containerWidth / 2 - _consts.TOOLTIP_TOTAL_PADDING + tooltipWidth < windowWidth), (0, _defineProperty2.default)(_POSITION_DIRECTIONS$, _consts.ALIGNS.CENTER, containerLeftPure + containerWidth / 2 - tooltipWidth / 2 > 0 && containerLeftPure + containerWidth / 2 + tooltipWidth / 2 < windowWidth), (0, _defineProperty2.default)(_POSITION_DIRECTIONS$, _consts.ALIGNS.END, containerLeftPure + containerWidth + _consts.TOOLTIP_TOTAL_PADDING < windowWidth && containerLeftPure + containerWidth / 2 + _consts.TOOLTIP_TOTAL_PADDING - tooltipWidth > 0), _POSITION_DIRECTIONS$)), (0, _defineProperty2.default)(_ref2, _consts.POSITION_DIRECTIONS.HORIZONTAL, (_POSITION_DIRECTIONS$2 = {}, (0, _defineProperty2.default)(_POSITION_DIRECTIONS$2, _consts.ALIGNS.START, containerTopPure + containerHeight / 2 - _consts.TOOLTIP_TOTAL_PADDING > 0 && containerTopPure + containerHeight / 2 + (tooltipHeight - _consts.TOOLTIP_TOTAL_PADDING) < windowHeight), (0, _defineProperty2.default)(_POSITION_DIRECTIONS$2, _consts.ALIGNS.CENTER, containerTopPure + containerHeight / 2 - tooltipHeight / 2 > 0 && containerTopPure + containerHeight / 2 + tooltipHeight / 2 < windowHeight), (0, _defineProperty2.default)(_POSITION_DIRECTIONS$2, _consts.ALIGNS.END, containerTopPure + containerHeight + _consts.TOOLTIP_TOTAL_PADDING - tooltipHeight > 0 && containerTopPure + containerHeight / 2 + _consts.TOOLTIP_TOTAL_PADDING < windowHeight), _POSITION_DIRECTIONS$2)), _ref2; }; var calculateTooltipAlign = function calculateTooltipAlign(position, aligns, dimensions) { if (!position) { return null; } var direction = getPositionDirection(position); if (direction) { var possibleAligns = getPossibleAligns(dimensions); var align = aligns.find(function (a) { return possibleAligns[direction][a]; }); if (typeof align === "string") { return align; } return null; } return null; }; var _default = calculateTooltipAlign; exports.default = _default;