UNPKG

@activecollab/components

Version:

ActiveCollab Components

89 lines (88 loc) 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var Position = function Position(targetTop, targetBottom, targetLeft, targetRight, contentHeight, contentWidth) { var windowHeight = window.innerHeight; var windowWidth = window.innerWidth; var repositionY = (targetTop + targetBottom) / 2 - contentHeight / 2; var repositionX = (targetLeft + targetRight) / 2 - contentWidth / 2; function handleY() { if (targetBottom + contentHeight + 6 < windowHeight) { repositionY = targetBottom + 6; return true; } if (targetTop - contentHeight - 6 > 0) { repositionY = targetTop - contentHeight - 6; return true; } return false; } function handleX() { if (targetRight + contentWidth + 6 < windowWidth) { repositionX = targetRight + 6; return true; } if (targetLeft - contentWidth - 6 > 0) { repositionX = targetLeft - contentWidth - 6; return true; } return false; } // Y middle if (repositionX > 0 && repositionX + contentWidth < windowWidth && handleY()) { return { top: repositionY, left: repositionX }; } // Y right repositionX = targetLeft; if (targetLeft > 0 && targetLeft + contentWidth < windowWidth && handleY()) { return { top: repositionY, left: repositionX }; } // Y left repositionX = targetRight - contentWidth; if (targetRight < windowWidth && targetRight - contentWidth > 0 && handleY()) { return { top: repositionY, left: repositionX }; } // X middle if (repositionY > 0 && repositionY + contentHeight < windowHeight && handleX()) { return { top: repositionY, left: repositionX }; } // X bottom repositionY = targetTop; if (targetTop > 0 && targetTop + contentHeight < windowHeight && handleX()) { return { top: repositionY, left: repositionX }; } // X top repositionY = targetBottom - contentHeight; if (targetBottom < windowHeight && targetBottom - contentHeight > 0 && handleX()) { return { top: repositionY, left: repositionX }; } // center repositionY = (targetTop + targetBottom) / 2 - contentHeight / 2; repositionX = (targetLeft + targetRight) / 2 - contentWidth / 2; return { top: repositionY, left: repositionX }; }; var _default = exports.default = Position; //# sourceMappingURL=usePosition.js.map