tdesign-react
Version:
TDesign Component for React
68 lines (62 loc) • 1.91 kB
JavaScript
/**
* tdesign v1.13.2
* (c) 2025 tdesign
* @license MIT
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
var useDrag = function useDrag(ref, options) {
var start = options.start,
end = options.end,
drag = options.drag;
var isDraggingRef = React.useRef(false);
var getCoordinate = function getCoordinate(event) {
try {
var _ref$current;
var rect = (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.getBoundingClientRect();
var left = event.clientX - rect.left;
var top = event.clientY - rect.top;
return {
y: Math.min(Math.max(0, top), rect.height),
x: Math.min(Math.max(0, left), rect.width)
};
} catch (error) {
return {
y: null,
x: null
};
}
};
var handlePointerMove = function handlePointerMove(e) {
if (isDraggingRef.current) drag(getCoordinate(e), e);
};
var _handlePointerUp = function handlePointerUp(e) {
isDraggingRef.current = false;
end(getCoordinate(e), e);
document.removeEventListener("mouseup", _handlePointerUp);
document.removeEventListener("mousemove", handlePointerMove);
};
var handlePointerDown = function handlePointerDown(e) {
isDraggingRef.current = true;
start(getCoordinate(e), e);
document.addEventListener("mouseup", _handlePointerUp);
document.addEventListener("mousemove", handlePointerMove);
};
React.useEffect(function () {
var element = ref.current;
if (element) {
element.addEventListener("mousedown", handlePointerDown);
}
return function () {
if (element) {
element.removeEventListener("mousedown", handlePointerDown);
}
};
}, []);
return {
isDragging: isDraggingRef.current
};
};
exports["default"] = useDrag;
//# sourceMappingURL=useDrag.js.map