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.

64 lines (48 loc) 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _isPosition = require("./isPosition"); var _consts = require("../consts"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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 = {}, _defineProperty(_ref2, _consts.POSITION_DIRECTIONS.VERTICAL, (_POSITION_DIRECTIONS$ = {}, _defineProperty(_POSITION_DIRECTIONS$, _consts.ALIGNS.START, containerLeftPure + containerWidth / 2 - _consts.TOOLTIP_TOTAL_PADDING > 0 && containerLeftPure + containerWidth / 2 - _consts.TOOLTIP_TOTAL_PADDING + tooltipWidth < windowWidth), _defineProperty(_POSITION_DIRECTIONS$, _consts.ALIGNS.CENTER, containerLeftPure + containerWidth / 2 - tooltipWidth / 2 > 0 && containerLeftPure + containerWidth / 2 + tooltipWidth / 2 < windowWidth), _defineProperty(_POSITION_DIRECTIONS$, _consts.ALIGNS.END, containerLeftPure + containerWidth + _consts.TOOLTIP_TOTAL_PADDING < windowWidth && containerLeftPure + containerWidth / 2 + _consts.TOOLTIP_TOTAL_PADDING - tooltipWidth > 0), _POSITION_DIRECTIONS$)), _defineProperty(_ref2, _consts.POSITION_DIRECTIONS.HORIZONTAL, (_POSITION_DIRECTIONS$2 = {}, _defineProperty(_POSITION_DIRECTIONS$2, _consts.ALIGNS.START, containerTopPure + containerHeight / 2 - _consts.TOOLTIP_TOTAL_PADDING > 0 && containerTopPure + containerHeight / 2 + (tooltipHeight - _consts.TOOLTIP_TOTAL_PADDING) < windowHeight), _defineProperty(_POSITION_DIRECTIONS$2, _consts.ALIGNS.CENTER, containerTopPure + containerHeight / 2 - tooltipHeight / 2 > 0 && containerTopPure + containerHeight / 2 + tooltipHeight / 2 < windowHeight), _defineProperty(_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;