@nex-ui/react
Version:
🎉 A beautiful, modern, and reliable React component library.
58 lines (54 loc) • 1.89 kB
JavaScript
;
var utils = require('./utils.cjs');
const computeCoordinatesByPlacement = (rects, placement)=>{
const side = utils.getSide(placement);
const alignment = utils.getAlignment(placement);
const crossAxis = utils.getAlignmentAxis(placement);
const crossAxisLength = utils.getAxisLength(crossAxis);
let coordinates;
const commonX = rects.reference.x + rects.reference.width / 2 - rects.popper.width / 2;
const commonY = rects.reference.y + rects.reference.height / 2 - rects.popper.height / 2;
const commonCrossAxis = rects.reference[crossAxisLength] / 2 - rects.popper[crossAxisLength] / 2;
switch(side){
case 'top':
coordinates = {
x: commonX,
y: rects.reference.y - rects.popper.height
};
break;
case 'bottom':
coordinates = {
x: commonX,
y: rects.reference.y + rects.reference.height
};
break;
case 'right':
coordinates = {
x: rects.reference.x + rects.reference.width,
y: commonY
};
break;
case 'left':
coordinates = {
x: rects.reference.x - rects.popper.width,
y: commonY
};
break;
default:
console.error('computeCoordinatesByPlacement: Invalid placement');
coordinates = {
x: commonX,
y: rects.reference.y + rects.reference.height
};
}
switch(alignment){
case 'start':
coordinates[crossAxis] -= commonCrossAxis;
break;
case 'end':
coordinates[crossAxis] += commonCrossAxis;
break;
}
return coordinates;
};
exports.computeCoordinatesByPlacement = computeCoordinatesByPlacement;