@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.
30 lines (23 loc) • 785 B
JavaScript
import { ANCHORS } from "../consts";
const isInside = (p, canBe) => {
if (p === ANCHORS.START && canBe[p]) {
return ANCHORS.START;
}
if (p === ANCHORS.END && canBe[p]) {
return ANCHORS.END;
}
return false;
};
const calculateHorizontalPosition = (desiredAnchor, positions) => {
const canBe = {
[ANCHORS.START]: positions.containerLeft + positions.popoverWidth < positions.windowWidth,
[ANCHORS.END]: positions.containerLeft + positions.containerWidth >= positions.popoverWidth
};
const possibleAnchor = desiredAnchor.map(p => isInside(p, canBe)).filter(p => typeof p === "string");
const posAnchor = possibleAnchor[0];
if (typeof posAnchor === "string") {
return posAnchor;
}
return null;
};
export default calculateHorizontalPosition;