react-selectable-box
Version:
A React component used hooks that allows you to select elements in the drag area using the mouse
55 lines (53 loc) • 2.37 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/utils.ts
var utils_exports = {};
__export(utils_exports, {
checkInRange: () => checkInRange,
getClientXY: () => getClientXY
});
module.exports = __toCommonJS(utils_exports);
var getClientXY = (e) => {
const obj = "touches" in e ? e.touches[0] : e;
return {
clientX: obj.clientX,
clientY: obj.clientY
};
};
var checkInRange = (rule, rect, scrollContainer, boxRect, boxRef) => {
if (typeof rule === "function" && boxRef.current) {
return rule(boxRef.current, boxRect);
}
if (!rect || !scrollContainer) {
return false;
}
const { left: rectLeft, top: rectTop, width: rectWidth, height: rectHeight } = rect;
const { top: containerTop, left: containerLeft } = scrollContainer.getBoundingClientRect();
const scrollLeft = scrollContainer.scrollLeft;
const scrollTop = scrollContainer.scrollTop;
const { top: boxTop, left: boxLeft, width: boxWidth, height: boxHeight } = boxRect;
if (rule === "collision") {
return rectLeft - containerLeft + scrollLeft < boxLeft + boxWidth && rectLeft + rectWidth - containerLeft + scrollLeft > boxLeft && rectTop - containerTop + scrollTop < boxTop + boxHeight && rectTop + rectHeight - containerTop + scrollTop > boxTop;
}
return rectLeft - containerLeft + scrollLeft >= boxLeft && rectLeft + rectWidth - containerLeft + scrollLeft <= boxLeft + boxWidth && rectTop - containerTop + scrollTop >= boxTop && rectTop + rectHeight - containerTop + scrollTop <= boxTop + boxHeight;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
checkInRange,
getClientXY
});