react-widget-trigger
Version:
98 lines (97 loc) • 2.62 kB
JavaScript
function getOffset(h, v, offset) {
let offsetLeft = offset[0];
let offsetTop = offset[1];
if (v === "top") {
offsetTop *= -1;
}
if (h === "left") {
offsetLeft *= -1;
}
if (offsetLeft) {
h += offsetLeft > 0 ? "+" + offsetLeft : offsetLeft;
}
if (offsetTop) {
v += offsetTop > 0 ? "+" + offsetTop : offsetTop;
}
return [h, v].join(" ");
}
const placements = {
left: function (offset) {
return {
at: getOffset("left", "center", offset),
my: "right center",
};
},
top: function (offset) {
return {
at: getOffset("center", "top", offset),
my: "center bottom",
};
},
right: function (offset) {
return {
at: getOffset("right", "center", offset),
my: "left center",
};
},
bottom: function (offset) {
return {
at: getOffset("center", "bottom", offset),
my: "center top",
};
},
topLeft: function (offset) {
return {
at: getOffset("left", "top", offset),
my: "left bottom",
};
},
topRight: function (offset) {
return {
at: getOffset("right", "top", offset),
my: "right bottom",
};
},
leftTop: function (offset) {
return {
at: getOffset("left", "top", offset),
my: "right top",
};
},
leftBottom: function (offset) {
return {
at: getOffset("left", "bottom", offset),
my: "right bottom",
};
},
rightTop: function (offset) {
return {
at: getOffset("right", "top", offset),
my: "left top",
};
},
rightBottom: function (offset) {
return {
at: getOffset("right", "bottom", offset),
my: "left bottom",
};
},
bottomLeft: function (offset) {
return {
at: getOffset("left", "bottom", offset),
my: "left top",
};
},
bottomRight: function (offset) {
return {
at: getOffset("right", "bottom", offset),
my: "right top",
};
},
};
export default function getPlacement(placement, offset) {
if (typeof offset === "number") {
offset = /^left|right/.test(placement) ? [offset, 0] : [0, offset];
}
return placements[placement] ? placements[placement](offset || [0, 0]) : null;
}