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.

54 lines (42 loc) 2.92 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import { isHorizontal, isVertical } from "./isPosition"; import { ALIGNS, POSITION_DIRECTIONS, TOOLTIP_TOTAL_PADDING } from "../consts"; var getPositionDirection = function getPositionDirection(position) { if (isVertical(position)) { return POSITION_DIRECTIONS.VERTICAL; } if (isHorizontal(position)) { return 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, POSITION_DIRECTIONS.VERTICAL, (_POSITION_DIRECTIONS$ = {}, _defineProperty(_POSITION_DIRECTIONS$, ALIGNS.START, containerLeftPure + containerWidth / 2 - TOOLTIP_TOTAL_PADDING > 0 && containerLeftPure + containerWidth / 2 - TOOLTIP_TOTAL_PADDING + tooltipWidth < windowWidth), _defineProperty(_POSITION_DIRECTIONS$, ALIGNS.CENTER, containerLeftPure + containerWidth / 2 - tooltipWidth / 2 > 0 && containerLeftPure + containerWidth / 2 + tooltipWidth / 2 < windowWidth), _defineProperty(_POSITION_DIRECTIONS$, ALIGNS.END, containerLeftPure + containerWidth + TOOLTIP_TOTAL_PADDING < windowWidth && containerLeftPure + containerWidth / 2 + TOOLTIP_TOTAL_PADDING - tooltipWidth > 0), _POSITION_DIRECTIONS$)), _defineProperty(_ref2, POSITION_DIRECTIONS.HORIZONTAL, (_POSITION_DIRECTIONS$2 = {}, _defineProperty(_POSITION_DIRECTIONS$2, ALIGNS.START, containerTopPure + containerHeight / 2 - TOOLTIP_TOTAL_PADDING > 0 && containerTopPure + containerHeight / 2 + (tooltipHeight - TOOLTIP_TOTAL_PADDING) < windowHeight), _defineProperty(_POSITION_DIRECTIONS$2, ALIGNS.CENTER, containerTopPure + containerHeight / 2 - tooltipHeight / 2 > 0 && containerTopPure + containerHeight / 2 + tooltipHeight / 2 < windowHeight), _defineProperty(_POSITION_DIRECTIONS$2, ALIGNS.END, containerTopPure + containerHeight + TOOLTIP_TOTAL_PADDING - tooltipHeight > 0 && containerTopPure + containerHeight / 2 + 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; }; export default calculateTooltipAlign;