react-bounded-draggable-modal
Version:
A draggable modal component with boundary support for React and Next.js
221 lines (210 loc) • 8.45 kB
JavaScript
;
var React = require('react');
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var handleTouchMove = function (event) {
event.preventDefault();
};
var useBoundedDrag = function (options) {
var dragging = React.useRef(false);
var overThreshold = React.useRef(false);
var dragXVal = React.useRef(0);
var dragYVal = React.useRef(0);
var startY = React.useRef(0);
var startX = React.useRef(0);
var startYPos = React.useRef(0);
var startXPos = React.useRef(0);
var updatePosition = function (element, left, top) {
requestAnimationFrame(function () {
element.style.left = left;
element.style.top = top;
element.style.position = 'absolute';
});
};
var calcX = function (val, target) {
var _a, _b, _c, _d;
var max = (_b = (_a = void 0 ) === null || _a === void 0 ? void 0 : _a.x) === null || _b === void 0 ? void 0 : _b.max;
var min = (_d = (_c = void 0 ) === null || _c === void 0 ? void 0 : _c.x) === null || _d === void 0 ? void 0 : _d.min;
if (max === 'infinity') {
max = Infinity;
}
else if (max != null) {
max = Number(max);
}
else if (target.parentElement) {
max = (target.parentElement.clientWidth - target.clientWidth);
}
else {
max = Infinity;
}
if (min === 'infinity') {
min = -Infinity;
}
else if (min != null) {
min = Number(min);
}
else if (target.parentElement) {
min = 0;
}
else {
min = 0;
}
return "".concat(Math.min(max, Math.max(min, val)), "px");
};
var calcY = function (val, target) {
var _a, _b, _c, _d;
var max = (_b = (_a = void 0 ) === null || _a === void 0 ? void 0 : _a.y) === null || _b === void 0 ? void 0 : _b.max;
var min = (_d = (_c = void 0 ) === null || _c === void 0 ? void 0 : _c.y) === null || _d === void 0 ? void 0 : _d.min;
if (max === 'infinity') {
max = Infinity;
}
else if (max != null) {
max = Number(max);
}
else if (target.parentElement) {
max = (target.parentElement.clientHeight - target.clientHeight);
}
else {
max = Infinity;
}
if (min === 'infinity') {
min = -Infinity;
}
else if (min != null) {
min = Number(min);
}
else if (target.parentElement) {
min = 0;
}
else {
min = 0;
}
return "".concat(Math.min(max, Math.max(min, val)), "px");
};
var onCommonDragStart = function (e, callback) {
document.addEventListener('touchmove', handleTouchMove, { passive: false });
callback();
};
var onTouchStart = function (e) {
if (e.target.tagName.toLowerCase() === 'input')
return;
onCommonDragStart(e, function () {
startY.current = e.currentTarget.offsetTop - e.touches[0].pageY;
startX.current = e.currentTarget.offsetLeft - e.touches[0].pageX;
startYPos.current = e.touches[0].pageY;
startXPos.current = e.touches[0].pageX;
});
};
var onPointerDown = function (e) {
if (e.target.tagName.toLowerCase() === 'input')
return;
e.currentTarget.draggable = false;
onCommonDragStart(e, function () {
dragging.current = true;
dragYVal.current = 0;
dragXVal.current = 0;
});
};
var onTouchMove = function (e) {
e.currentTarget.draggable = false;
var left; {
var left = calcX(e.changedTouches[0].pageX + startX.current, e.currentTarget);
var top_2 = calcY(e.changedTouches[0].pageY + startY.current, e.currentTarget);
updatePosition(e.currentTarget, left, top_2);
}
};
var onPointerMove = function (e) {
if (!e.buttons || !dragging.current)
return;
e.currentTarget.setPointerCapture(e.pointerId);
e.currentTarget.draggable = false;
var left; {
dragXVal.current += e.movementX;
dragYVal.current += e.movementY;
var left = calcX(e.currentTarget.offsetLeft + e.movementX, e.currentTarget);
var top_4 = calcY(e.currentTarget.offsetTop + e.movementY, e.currentTarget);
updatePosition(e.currentTarget, left, top_4);
}
};
var onCommonDragEnd = function (e) {
dragging.current = false;
overThreshold.current = false;
dragXVal.current = 0;
dragYVal.current = 0;
document.removeEventListener('touchmove', handleTouchMove);
};
return {
onTouchStart: onTouchStart,
onTouchMove: onTouchMove,
onTouchEnd: onCommonDragEnd,
onPointerMove: onPointerMove,
onPointerDown: onPointerDown,
onPointerUp: onCommonDragEnd,
};
};
var Modal = function (_a) {
var isOpen = _a.isOpen, onOutsideClick = _a.onOutsideClick, header = _a.header, children = _a.children, classNames = _a.classNames;
var drag = useBoundedDrag();
if (!isOpen)
return null;
return (React.createElement("div", { style: styles.overlay, onClick: onOutsideClick },
React.createElement("div", __assign({ className: classNames === null || classNames === void 0 ? void 0 : classNames.modal, style: !(classNames === null || classNames === void 0 ? void 0 : classNames.modal) ? styles.modal : undefined }, drag, { onClick: function (e) { return e.stopPropagation(); } }),
React.createElement("div", { className: classNames === null || classNames === void 0 ? void 0 : classNames.header, style: !(classNames === null || classNames === void 0 ? void 0 : classNames.header) ? styles.header : undefined }, header),
React.createElement("div", { className: classNames === null || classNames === void 0 ? void 0 : classNames.body, style: !(classNames === null || classNames === void 0 ? void 0 : classNames.body) ? styles.body : undefined, onTouchStart: function (e) { return e.stopPropagation(); }, onPointerDown: function (e) { return e.stopPropagation(); } }, children))));
};
var styles = {
overlay: {
position: "fixed",
top: 0,
left: 0,
width: "100vw",
height: "100vh",
backgroundColor: "rgba(0, 0, 0, 0.5)",
display: "flex",
alignItems: "center",
justifyContent: "center",
},
modal: {
backgroundColor: "#fff",
position: "relative",
minWidth: "300px",
borderRadius: "5px",
},
header: {
cursor: "move",
display: "flex",
userSelect: "none",
justifyContent: "center",
},
body: {
display: "flex",
justifyContent: "center",
},
};
exports.Modal = Modal;
//# sourceMappingURL=index.js.map