@dnd-kit/core
Version:
dnd kit – a lightweight React library for building performant and accessible drag and drop experiences
1,936 lines (1,661 loc) • 107 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = require('react');
var React__default = _interopDefault(React);
var reactDom = require('react-dom');
var utilities = require('@dnd-kit/utilities');
var accessibility = require('@dnd-kit/accessibility');
const DndMonitorContext = /*#__PURE__*/React.createContext(null);
function useDndMonitor(listener) {
const registerListener = React.useContext(DndMonitorContext);
React.useEffect(() => {
if (!registerListener) {
throw new Error('useDndMonitor must be used within a children of <DndContext>');
}
const unsubscribe = registerListener(listener);
return unsubscribe;
}, [listener, registerListener]);
}
function useDndMonitorProvider() {
const [listeners] = React.useState(() => new Set());
const registerListener = React.useCallback(listener => {
listeners.add(listener);
return () => listeners.delete(listener);
}, [listeners]);
const dispatch = React.useCallback(_ref => {
let {
type,
event
} = _ref;
listeners.forEach(listener => {
var _listener$type;
return (_listener$type = listener[type]) == null ? void 0 : _listener$type.call(listener, event);
});
}, [listeners]);
return [dispatch, registerListener];
}
const defaultScreenReaderInstructions = {
draggable: "\n To pick up a draggable item, press the space bar.\n While dragging, use the arrow keys to move the item.\n Press space again to drop the item in its new position, or press escape to cancel.\n "
};
const defaultAnnouncements = {
onDragStart(_ref) {
let {
active
} = _ref;
return "Picked up draggable item " + active.id + ".";
},
onDragOver(_ref2) {
let {
active,
over
} = _ref2;
if (over) {
return "Draggable item " + active.id + " was moved over droppable area " + over.id + ".";
}
return "Draggable item " + active.id + " is no longer over a droppable area.";
},
onDragEnd(_ref3) {
let {
active,
over
} = _ref3;
if (over) {
return "Draggable item " + active.id + " was dropped over droppable area " + over.id;
}
return "Draggable item " + active.id + " was dropped.";
},
onDragCancel(_ref4) {
let {
active
} = _ref4;
return "Dragging was cancelled. Draggable item " + active.id + " was dropped.";
}
};
function Accessibility(_ref) {
let {
announcements = defaultAnnouncements,
container,
hiddenTextDescribedById,
screenReaderInstructions = defaultScreenReaderInstructions
} = _ref;
const {
announce,
announcement
} = accessibility.useAnnouncement();
const liveRegionId = utilities.useUniqueId("DndLiveRegion");
const [mounted, setMounted] = React.useState(false);
React.useEffect(() => {
setMounted(true);
}, []);
useDndMonitor(React.useMemo(() => ({
onDragStart(_ref2) {
let {
active
} = _ref2;
announce(announcements.onDragStart({
active
}));
},
onDragMove(_ref3) {
let {
active,
over
} = _ref3;
if (announcements.onDragMove) {
announce(announcements.onDragMove({
active,
over
}));
}
},
onDragOver(_ref4) {
let {
active,
over
} = _ref4;
announce(announcements.onDragOver({
active,
over
}));
},
onDragEnd(_ref5) {
let {
active,
over
} = _ref5;
announce(announcements.onDragEnd({
active,
over
}));
},
onDragCancel(_ref6) {
let {
active,
over
} = _ref6;
announce(announcements.onDragCancel({
active,
over
}));
}
}), [announce, announcements]));
if (!mounted) {
return null;
}
const markup = React__default.createElement(React__default.Fragment, null, React__default.createElement(accessibility.HiddenText, {
id: hiddenTextDescribedById,
value: screenReaderInstructions.draggable
}), React__default.createElement(accessibility.LiveRegion, {
id: liveRegionId,
announcement: announcement
}));
return container ? reactDom.createPortal(markup, container) : markup;
}
var Action;
(function (Action) {
Action["DragStart"] = "dragStart";
Action["DragMove"] = "dragMove";
Action["DragEnd"] = "dragEnd";
Action["DragCancel"] = "dragCancel";
Action["DragOver"] = "dragOver";
Action["RegisterDroppable"] = "registerDroppable";
Action["SetDroppableDisabled"] = "setDroppableDisabled";
Action["UnregisterDroppable"] = "unregisterDroppable";
})(Action || (Action = {}));
function noop() {}
function useSensor(sensor, options) {
return React.useMemo(() => ({
sensor,
options: options != null ? options : {}
}), // eslint-disable-next-line react-hooks/exhaustive-deps
[sensor, options]);
}
function useSensors() {
for (var _len = arguments.length, sensors = new Array(_len), _key = 0; _key < _len; _key++) {
sensors[_key] = arguments[_key];
}
return React.useMemo(() => [...sensors].filter(sensor => sensor != null), // eslint-disable-next-line react-hooks/exhaustive-deps
[...sensors]);
}
const defaultCoordinates = /*#__PURE__*/Object.freeze({
x: 0,
y: 0
});
/**
* Returns the distance between two points
*/
function distanceBetween(p1, p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
function getRelativeTransformOrigin(event, rect) {
const eventCoordinates = utilities.getEventCoordinates(event);
if (!eventCoordinates) {
return '0 0';
}
const transformOrigin = {
x: (eventCoordinates.x - rect.left) / rect.width * 100,
y: (eventCoordinates.y - rect.top) / rect.height * 100
};
return transformOrigin.x + "% " + transformOrigin.y + "%";
}
/**
* Sort collisions from smallest to greatest value
*/
function sortCollisionsAsc(_ref, _ref2) {
let {
data: {
value: a
}
} = _ref;
let {
data: {
value: b
}
} = _ref2;
return a - b;
}
/**
* Sort collisions from greatest to smallest value
*/
function sortCollisionsDesc(_ref3, _ref4) {
let {
data: {
value: a
}
} = _ref3;
let {
data: {
value: b
}
} = _ref4;
return b - a;
}
/**
* Returns the coordinates of the corners of a given rectangle:
* [TopLeft {x, y}, TopRight {x, y}, BottomLeft {x, y}, BottomRight {x, y}]
*/
function cornersOfRectangle(_ref5) {
let {
left,
top,
height,
width
} = _ref5;
return [{
x: left,
y: top
}, {
x: left + width,
y: top
}, {
x: left,
y: top + height
}, {
x: left + width,
y: top + height
}];
}
function getFirstCollision(collisions, property) {
if (!collisions || collisions.length === 0) {
return null;
}
const [firstCollision] = collisions;
return property ? firstCollision[property] : firstCollision;
}
/**
* Returns the coordinates of the center of a given ClientRect
*/
function centerOfRectangle(rect, left, top) {
if (left === void 0) {
left = rect.left;
}
if (top === void 0) {
top = rect.top;
}
return {
x: left + rect.width * 0.5,
y: top + rect.height * 0.5
};
}
/**
* Returns the closest rectangles from an array of rectangles to the center of a given
* rectangle.
*/
const closestCenter = _ref => {
let {
collisionRect,
droppableRects,
droppableContainers
} = _ref;
const centerRect = centerOfRectangle(collisionRect, collisionRect.left, collisionRect.top);
const collisions = [];
for (const droppableContainer of droppableContainers) {
const {
id
} = droppableContainer;
const rect = droppableRects.get(id);
if (rect) {
const distBetween = distanceBetween(centerOfRectangle(rect), centerRect);
collisions.push({
id,
data: {
droppableContainer,
value: distBetween
}
});
}
}
return collisions.sort(sortCollisionsAsc);
};
/**
* Returns the closest rectangles from an array of rectangles to the corners of
* another rectangle.
*/
const closestCorners = _ref => {
let {
collisionRect,
droppableRects,
droppableContainers
} = _ref;
const corners = cornersOfRectangle(collisionRect);
const collisions = [];
for (const droppableContainer of droppableContainers) {
const {
id
} = droppableContainer;
const rect = droppableRects.get(id);
if (rect) {
const rectCorners = cornersOfRectangle(rect);
const distances = corners.reduce((accumulator, corner, index) => {
return accumulator + distanceBetween(rectCorners[index], corner);
}, 0);
const effectiveDistance = Number((distances / 4).toFixed(4));
collisions.push({
id,
data: {
droppableContainer,
value: effectiveDistance
}
});
}
}
return collisions.sort(sortCollisionsAsc);
};
/**
* Returns the intersecting rectangle area between two rectangles
*/
function getIntersectionRatio(entry, target) {
const top = Math.max(target.top, entry.top);
const left = Math.max(target.left, entry.left);
const right = Math.min(target.left + target.width, entry.left + entry.width);
const bottom = Math.min(target.top + target.height, entry.top + entry.height);
const width = right - left;
const height = bottom - top;
if (left < right && top < bottom) {
const targetArea = target.width * target.height;
const entryArea = entry.width * entry.height;
const intersectionArea = width * height;
const intersectionRatio = intersectionArea / (targetArea + entryArea - intersectionArea);
return Number(intersectionRatio.toFixed(4));
} // Rectangles do not overlap, or overlap has an area of zero (edge/corner overlap)
return 0;
}
/**
* Returns the rectangles that has the greatest intersection area with a given
* rectangle in an array of rectangles.
*/
const rectIntersection = _ref => {
let {
collisionRect,
droppableRects,
droppableContainers
} = _ref;
const collisions = [];
for (const droppableContainer of droppableContainers) {
const {
id
} = droppableContainer;
const rect = droppableRects.get(id);
if (rect) {
const intersectionRatio = getIntersectionRatio(rect, collisionRect);
if (intersectionRatio > 0) {
collisions.push({
id,
data: {
droppableContainer,
value: intersectionRatio
}
});
}
}
}
return collisions.sort(sortCollisionsDesc);
};
/**
* Check if a given point is contained within a bounding rectangle
*/
function isPointWithinRect(point, rect) {
const {
top,
left,
bottom,
right
} = rect;
return top <= point.y && point.y <= bottom && left <= point.x && point.x <= right;
}
/**
* Returns the rectangles that the pointer is hovering over
*/
const pointerWithin = _ref => {
let {
droppableContainers,
droppableRects,
pointerCoordinates
} = _ref;
if (!pointerCoordinates) {
return [];
}
const collisions = [];
for (const droppableContainer of droppableContainers) {
const {
id
} = droppableContainer;
const rect = droppableRects.get(id);
if (rect && isPointWithinRect(pointerCoordinates, rect)) {
/* There may be more than a single rectangle intersecting
* with the pointer coordinates. In order to sort the
* colliding rectangles, we measure the distance between
* the pointer and the corners of the intersecting rectangle
*/
const corners = cornersOfRectangle(rect);
const distances = corners.reduce((accumulator, corner) => {
return accumulator + distanceBetween(pointerCoordinates, corner);
}, 0);
const effectiveDistance = Number((distances / 4).toFixed(4));
collisions.push({
id,
data: {
droppableContainer,
value: effectiveDistance
}
});
}
}
return collisions.sort(sortCollisionsAsc);
};
function adjustScale(transform, rect1, rect2) {
return { ...transform,
scaleX: rect1 && rect2 ? rect1.width / rect2.width : 1,
scaleY: rect1 && rect2 ? rect1.height / rect2.height : 1
};
}
function getRectDelta(rect1, rect2) {
return rect1 && rect2 ? {
x: rect1.left - rect2.left,
y: rect1.top - rect2.top
} : defaultCoordinates;
}
function createRectAdjustmentFn(modifier) {
return function adjustClientRect(rect) {
for (var _len = arguments.length, adjustments = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
adjustments[_key - 1] = arguments[_key];
}
return adjustments.reduce((acc, adjustment) => ({ ...acc,
top: acc.top + modifier * adjustment.y,
bottom: acc.bottom + modifier * adjustment.y,
left: acc.left + modifier * adjustment.x,
right: acc.right + modifier * adjustment.x
}), { ...rect
});
};
}
const getAdjustedRect = /*#__PURE__*/createRectAdjustmentFn(1);
function parseTransform(transform) {
if (transform.startsWith('matrix3d(')) {
const transformArray = transform.slice(9, -1).split(/, /);
return {
x: +transformArray[12],
y: +transformArray[13],
scaleX: +transformArray[0],
scaleY: +transformArray[5]
};
} else if (transform.startsWith('matrix(')) {
const transformArray = transform.slice(7, -1).split(/, /);
return {
x: +transformArray[4],
y: +transformArray[5],
scaleX: +transformArray[0],
scaleY: +transformArray[3]
};
}
return null;
}
function inverseTransform(rect, transform, transformOrigin) {
const parsedTransform = parseTransform(transform);
if (!parsedTransform) {
return rect;
}
const {
scaleX,
scaleY,
x: translateX,
y: translateY
} = parsedTransform;
const x = rect.left - translateX - (1 - scaleX) * parseFloat(transformOrigin);
const y = rect.top - translateY - (1 - scaleY) * parseFloat(transformOrigin.slice(transformOrigin.indexOf(' ') + 1));
const w = scaleX ? rect.width / scaleX : rect.width;
const h = scaleY ? rect.height / scaleY : rect.height;
return {
width: w,
height: h,
top: y,
right: x + w,
bottom: y + h,
left: x
};
}
const defaultOptions = {
ignoreTransform: false
};
/**
* Returns the bounding client rect of an element relative to the viewport.
*/
function getClientRect(element, options) {
if (options === void 0) {
options = defaultOptions;
}
let rect = element.getBoundingClientRect();
if (options.ignoreTransform) {
const {
transform,
transformOrigin
} = utilities.getWindow(element).getComputedStyle(element);
if (transform) {
rect = inverseTransform(rect, transform, transformOrigin);
}
}
const {
top,
left,
width,
height,
bottom,
right
} = rect;
return {
top,
left,
width,
height,
bottom,
right
};
}
/**
* Returns the bounding client rect of an element relative to the viewport.
*
* @remarks
* The ClientRect returned by this method does not take into account transforms
* applied to the element it measures.
*
*/
function getTransformAgnosticClientRect(element) {
return getClientRect(element, {
ignoreTransform: true
});
}
function getWindowClientRect(element) {
const width = element.innerWidth;
const height = element.innerHeight;
return {
top: 0,
left: 0,
right: width,
bottom: height,
width,
height
};
}
function isFixed(node, computedStyle) {
if (computedStyle === void 0) {
computedStyle = utilities.getWindow(node).getComputedStyle(node);
}
return computedStyle.position === 'fixed';
}
function isScrollable(element, computedStyle) {
if (computedStyle === void 0) {
computedStyle = utilities.getWindow(element).getComputedStyle(element);
}
const overflowRegex = /(auto|scroll|overlay)/;
const properties = ['overflow', 'overflowX', 'overflowY'];
return properties.some(property => {
const value = computedStyle[property];
return typeof value === 'string' ? overflowRegex.test(value) : false;
});
}
function getScrollableAncestors(element, limit) {
const scrollParents = [];
function findScrollableAncestors(node) {
if (limit != null && scrollParents.length >= limit) {
return scrollParents;
}
if (!node) {
return scrollParents;
}
if (utilities.isDocument(node) && node.scrollingElement != null && !scrollParents.includes(node.scrollingElement)) {
scrollParents.push(node.scrollingElement);
return scrollParents;
}
if (!utilities.isHTMLElement(node) || utilities.isSVGElement(node)) {
return scrollParents;
}
if (scrollParents.includes(node)) {
return scrollParents;
}
const computedStyle = utilities.getWindow(element).getComputedStyle(node);
if (node !== element) {
if (isScrollable(node, computedStyle)) {
scrollParents.push(node);
}
}
if (isFixed(node, computedStyle)) {
return scrollParents;
}
return findScrollableAncestors(node.parentNode);
}
if (!element) {
return scrollParents;
}
return findScrollableAncestors(element);
}
function getFirstScrollableAncestor(node) {
const [firstScrollableAncestor] = getScrollableAncestors(node, 1);
return firstScrollableAncestor != null ? firstScrollableAncestor : null;
}
function getScrollableElement(element) {
if (!utilities.canUseDOM || !element) {
return null;
}
if (utilities.isWindow(element)) {
return element;
}
if (!utilities.isNode(element)) {
return null;
}
if (utilities.isDocument(element) || element === utilities.getOwnerDocument(element).scrollingElement) {
return window;
}
if (utilities.isHTMLElement(element)) {
return element;
}
return null;
}
function getScrollXCoordinate(element) {
if (utilities.isWindow(element)) {
return element.scrollX;
}
return element.scrollLeft;
}
function getScrollYCoordinate(element) {
if (utilities.isWindow(element)) {
return element.scrollY;
}
return element.scrollTop;
}
function getScrollCoordinates(element) {
return {
x: getScrollXCoordinate(element),
y: getScrollYCoordinate(element)
};
}
var Direction;
(function (Direction) {
Direction[Direction["Forward"] = 1] = "Forward";
Direction[Direction["Backward"] = -1] = "Backward";
})(Direction || (Direction = {}));
function isDocumentScrollingElement(element) {
if (!utilities.canUseDOM || !element) {
return false;
}
return element === document.scrollingElement;
}
function getScrollPosition(scrollingContainer) {
const minScroll = {
x: 0,
y: 0
};
const dimensions = isDocumentScrollingElement(scrollingContainer) ? {
height: window.innerHeight,
width: window.innerWidth
} : {
height: scrollingContainer.clientHeight,
width: scrollingContainer.clientWidth
};
const maxScroll = {
x: scrollingContainer.scrollWidth - dimensions.width,
y: scrollingContainer.scrollHeight - dimensions.height
};
const isTop = scrollingContainer.scrollTop <= minScroll.y;
const isLeft = scrollingContainer.scrollLeft <= minScroll.x;
const isBottom = scrollingContainer.scrollTop >= maxScroll.y;
const isRight = scrollingContainer.scrollLeft >= maxScroll.x;
return {
isTop,
isLeft,
isBottom,
isRight,
maxScroll,
minScroll
};
}
const defaultThreshold = {
x: 0.2,
y: 0.2
};
function getScrollDirectionAndSpeed(scrollContainer, scrollContainerRect, _ref, acceleration, thresholdPercentage) {
let {
top,
left,
right,
bottom
} = _ref;
if (acceleration === void 0) {
acceleration = 10;
}
if (thresholdPercentage === void 0) {
thresholdPercentage = defaultThreshold;
}
const {
isTop,
isBottom,
isLeft,
isRight
} = getScrollPosition(scrollContainer);
const direction = {
x: 0,
y: 0
};
const speed = {
x: 0,
y: 0
};
const threshold = {
height: scrollContainerRect.height * thresholdPercentage.y,
width: scrollContainerRect.width * thresholdPercentage.x
};
if (!isTop && top <= scrollContainerRect.top + threshold.height) {
// Scroll Up
direction.y = Direction.Backward;
speed.y = acceleration * Math.abs((scrollContainerRect.top + threshold.height - top) / threshold.height);
} else if (!isBottom && bottom >= scrollContainerRect.bottom - threshold.height) {
// Scroll Down
direction.y = Direction.Forward;
speed.y = acceleration * Math.abs((scrollContainerRect.bottom - threshold.height - bottom) / threshold.height);
}
if (!isRight && right >= scrollContainerRect.right - threshold.width) {
// Scroll Right
direction.x = Direction.Forward;
speed.x = acceleration * Math.abs((scrollContainerRect.right - threshold.width - right) / threshold.width);
} else if (!isLeft && left <= scrollContainerRect.left + threshold.width) {
// Scroll Left
direction.x = Direction.Backward;
speed.x = acceleration * Math.abs((scrollContainerRect.left + threshold.width - left) / threshold.width);
}
return {
direction,
speed
};
}
function getScrollElementRect(element) {
if (element === document.scrollingElement) {
const {
innerWidth,
innerHeight
} = window;
return {
top: 0,
left: 0,
right: innerWidth,
bottom: innerHeight,
width: innerWidth,
height: innerHeight
};
}
const {
top,
left,
right,
bottom
} = element.getBoundingClientRect();
return {
top,
left,
right,
bottom,
width: element.clientWidth,
height: element.clientHeight
};
}
function getScrollOffsets(scrollableAncestors) {
return scrollableAncestors.reduce((acc, node) => {
return utilities.add(acc, getScrollCoordinates(node));
}, defaultCoordinates);
}
function getScrollXOffset(scrollableAncestors) {
return scrollableAncestors.reduce((acc, node) => {
return acc + getScrollXCoordinate(node);
}, 0);
}
function getScrollYOffset(scrollableAncestors) {
return scrollableAncestors.reduce((acc, node) => {
return acc + getScrollYCoordinate(node);
}, 0);
}
function scrollIntoViewIfNeeded(element, measure) {
if (measure === void 0) {
measure = getClientRect;
}
if (!element) {
return;
}
const {
top,
left,
bottom,
right
} = measure(element);
const firstScrollableAncestor = getFirstScrollableAncestor(element);
if (!firstScrollableAncestor) {
return;
}
if (bottom <= 0 || right <= 0 || top >= window.innerHeight || left >= window.innerWidth) {
element.scrollIntoView({
block: 'center',
inline: 'center'
});
}
}
const properties = [['x', ['left', 'right'], getScrollXOffset], ['y', ['top', 'bottom'], getScrollYOffset]];
class Rect {
constructor(rect, element) {
this.rect = void 0;
this.width = void 0;
this.height = void 0;
this.top = void 0;
this.bottom = void 0;
this.right = void 0;
this.left = void 0;
const scrollableAncestors = getScrollableAncestors(element);
const scrollOffsets = getScrollOffsets(scrollableAncestors);
this.rect = { ...rect
};
this.width = rect.width;
this.height = rect.height;
for (const [axis, keys, getScrollOffset] of properties) {
for (const key of keys) {
Object.defineProperty(this, key, {
get: () => {
const currentOffsets = getScrollOffset(scrollableAncestors);
const scrollOffsetsDeltla = scrollOffsets[axis] - currentOffsets;
return this.rect[key] + scrollOffsetsDeltla;
},
enumerable: true
});
}
}
Object.defineProperty(this, 'rect', {
enumerable: false
});
}
}
class Listeners {
constructor(target) {
this.target = void 0;
this.listeners = [];
this.removeAll = () => {
this.listeners.forEach(listener => {
var _this$target;
return (_this$target = this.target) == null ? void 0 : _this$target.removeEventListener(...listener);
});
};
this.target = target;
}
add(eventName, handler, options) {
var _this$target2;
(_this$target2 = this.target) == null ? void 0 : _this$target2.addEventListener(eventName, handler, options);
this.listeners.push([eventName, handler, options]);
}
}
function getEventListenerTarget(target) {
// If the `event.target` element is removed from the document events will still be targeted
// at it, and hence won't always bubble up to the window or document anymore.
// If there is any risk of an element being removed while it is being dragged,
// the best practice is to attach the event listeners directly to the target.
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
const {
EventTarget
} = utilities.getWindow(target);
return target instanceof EventTarget ? target : utilities.getOwnerDocument(target);
}
function hasExceededDistance(delta, measurement) {
const dx = Math.abs(delta.x);
const dy = Math.abs(delta.y);
if (typeof measurement === 'number') {
return Math.sqrt(dx ** 2 + dy ** 2) > measurement;
}
if ('x' in measurement && 'y' in measurement) {
return dx > measurement.x && dy > measurement.y;
}
if ('x' in measurement) {
return dx > measurement.x;
}
if ('y' in measurement) {
return dy > measurement.y;
}
return false;
}
var EventName;
(function (EventName) {
EventName["Click"] = "click";
EventName["DragStart"] = "dragstart";
EventName["Keydown"] = "keydown";
EventName["ContextMenu"] = "contextmenu";
EventName["Resize"] = "resize";
EventName["SelectionChange"] = "selectionchange";
EventName["VisibilityChange"] = "visibilitychange";
})(EventName || (EventName = {}));
function preventDefault(event) {
event.preventDefault();
}
function stopPropagation(event) {
event.stopPropagation();
}
(function (KeyboardCode) {
KeyboardCode["Space"] = "Space";
KeyboardCode["Down"] = "ArrowDown";
KeyboardCode["Right"] = "ArrowRight";
KeyboardCode["Left"] = "ArrowLeft";
KeyboardCode["Up"] = "ArrowUp";
KeyboardCode["Esc"] = "Escape";
KeyboardCode["Enter"] = "Enter";
KeyboardCode["Tab"] = "Tab";
})(exports.KeyboardCode || (exports.KeyboardCode = {}));
const defaultKeyboardCodes = {
start: [exports.KeyboardCode.Space, exports.KeyboardCode.Enter],
cancel: [exports.KeyboardCode.Esc],
end: [exports.KeyboardCode.Space, exports.KeyboardCode.Enter, exports.KeyboardCode.Tab]
};
const defaultKeyboardCoordinateGetter = (event, _ref) => {
let {
currentCoordinates
} = _ref;
switch (event.code) {
case exports.KeyboardCode.Right:
return { ...currentCoordinates,
x: currentCoordinates.x + 25
};
case exports.KeyboardCode.Left:
return { ...currentCoordinates,
x: currentCoordinates.x - 25
};
case exports.KeyboardCode.Down:
return { ...currentCoordinates,
y: currentCoordinates.y + 25
};
case exports.KeyboardCode.Up:
return { ...currentCoordinates,
y: currentCoordinates.y - 25
};
}
return undefined;
};
class KeyboardSensor {
constructor(props) {
this.props = void 0;
this.autoScrollEnabled = false;
this.referenceCoordinates = void 0;
this.listeners = void 0;
this.windowListeners = void 0;
this.props = props;
const {
event: {
target
}
} = props;
this.props = props;
this.listeners = new Listeners(utilities.getOwnerDocument(target));
this.windowListeners = new Listeners(utilities.getWindow(target));
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleCancel = this.handleCancel.bind(this);
this.attach();
}
attach() {
this.handleStart();
this.windowListeners.add(EventName.Resize, this.handleCancel);
this.windowListeners.add(EventName.VisibilityChange, this.handleCancel);
setTimeout(() => this.listeners.add(EventName.Keydown, this.handleKeyDown));
}
handleStart() {
const {
activeNode,
onStart
} = this.props;
const node = activeNode.node.current;
if (node) {
scrollIntoViewIfNeeded(node);
}
onStart(defaultCoordinates);
}
handleKeyDown(event) {
if (utilities.isKeyboardEvent(event)) {
const {
active,
context,
options
} = this.props;
const {
keyboardCodes = defaultKeyboardCodes,
coordinateGetter = defaultKeyboardCoordinateGetter,
scrollBehavior = 'smooth'
} = options;
const {
code
} = event;
if (keyboardCodes.end.includes(code)) {
this.handleEnd(event);
return;
}
if (keyboardCodes.cancel.includes(code)) {
this.handleCancel(event);
return;
}
const {
collisionRect
} = context.current;
const currentCoordinates = collisionRect ? {
x: collisionRect.left,
y: collisionRect.top
} : defaultCoordinates;
if (!this.referenceCoordinates) {
this.referenceCoordinates = currentCoordinates;
}
const newCoordinates = coordinateGetter(event, {
active,
context: context.current,
currentCoordinates
});
if (newCoordinates) {
const coordinatesDelta = utilities.subtract(newCoordinates, currentCoordinates);
const scrollDelta = {
x: 0,
y: 0
};
const {
scrollableAncestors
} = context.current;
for (const scrollContainer of scrollableAncestors) {
const direction = event.code;
const {
isTop,
isRight,
isLeft,
isBottom,
maxScroll,
minScroll
} = getScrollPosition(scrollContainer);
const scrollElementRect = getScrollElementRect(scrollContainer);
const clampedCoordinates = {
x: Math.min(direction === exports.KeyboardCode.Right ? scrollElementRect.right - scrollElementRect.width / 2 : scrollElementRect.right, Math.max(direction === exports.KeyboardCode.Right ? scrollElementRect.left : scrollElementRect.left + scrollElementRect.width / 2, newCoordinates.x)),
y: Math.min(direction === exports.KeyboardCode.Down ? scrollElementRect.bottom - scrollElementRect.height / 2 : scrollElementRect.bottom, Math.max(direction === exports.KeyboardCode.Down ? scrollElementRect.top : scrollElementRect.top + scrollElementRect.height / 2, newCoordinates.y))
};
const canScrollX = direction === exports.KeyboardCode.Right && !isRight || direction === exports.KeyboardCode.Left && !isLeft;
const canScrollY = direction === exports.KeyboardCode.Down && !isBottom || direction === exports.KeyboardCode.Up && !isTop;
if (canScrollX && clampedCoordinates.x !== newCoordinates.x) {
const newScrollCoordinates = scrollContainer.scrollLeft + coordinatesDelta.x;
const canScrollToNewCoordinates = direction === exports.KeyboardCode.Right && newScrollCoordinates <= maxScroll.x || direction === exports.KeyboardCode.Left && newScrollCoordinates >= minScroll.x;
if (canScrollToNewCoordinates && !coordinatesDelta.y) {
// We don't need to update coordinates, the scroll adjustment alone will trigger
// logic to auto-detect the new container we are over
scrollContainer.scrollTo({
left: newScrollCoordinates,
behavior: scrollBehavior
});
return;
}
if (canScrollToNewCoordinates) {
scrollDelta.x = scrollContainer.scrollLeft - newScrollCoordinates;
} else {
scrollDelta.x = direction === exports.KeyboardCode.Right ? scrollContainer.scrollLeft - maxScroll.x : scrollContainer.scrollLeft - minScroll.x;
}
if (scrollDelta.x) {
scrollContainer.scrollBy({
left: -scrollDelta.x,
behavior: scrollBehavior
});
}
break;
} else if (canScrollY && clampedCoordinates.y !== newCoordinates.y) {
const newScrollCoordinates = scrollContainer.scrollTop + coordinatesDelta.y;
const canScrollToNewCoordinates = direction === exports.KeyboardCode.Down && newScrollCoordinates <= maxScroll.y || direction === exports.KeyboardCode.Up && newScrollCoordinates >= minScroll.y;
if (canScrollToNewCoordinates && !coordinatesDelta.x) {
// We don't need to update coordinates, the scroll adjustment alone will trigger
// logic to auto-detect the new container we are over
scrollContainer.scrollTo({
top: newScrollCoordinates,
behavior: scrollBehavior
});
return;
}
if (canScrollToNewCoordinates) {
scrollDelta.y = scrollContainer.scrollTop - newScrollCoordinates;
} else {
scrollDelta.y = direction === exports.KeyboardCode.Down ? scrollContainer.scrollTop - maxScroll.y : scrollContainer.scrollTop - minScroll.y;
}
if (scrollDelta.y) {
scrollContainer.scrollBy({
top: -scrollDelta.y,
behavior: scrollBehavior
});
}
break;
}
}
this.handleMove(event, utilities.add(utilities.subtract(newCoordinates, this.referenceCoordinates), scrollDelta));
}
}
}
handleMove(event, coordinates) {
const {
onMove
} = this.props;
event.preventDefault();
onMove(coordinates);
}
handleEnd(event) {
const {
onEnd
} = this.props;
event.preventDefault();
this.detach();
onEnd();
}
handleCancel(event) {
const {
onCancel
} = this.props;
event.preventDefault();
this.detach();
onCancel();
}
detach() {
this.listeners.removeAll();
this.windowListeners.removeAll();
}
}
KeyboardSensor.activators = [{
eventName: 'onKeyDown',
handler: (event, _ref, _ref2) => {
let {
keyboardCodes = defaultKeyboardCodes,
onActivation
} = _ref;
let {
active
} = _ref2;
const {
code
} = event.nativeEvent;
if (keyboardCodes.start.includes(code)) {
const activator = active.activatorNode.current;
if (activator && event.target !== activator) {
return false;
}
event.preventDefault();
onActivation == null ? void 0 : onActivation({
event: event.nativeEvent
});
return true;
}
return false;
}
}];
function isDistanceConstraint(constraint) {
return Boolean(constraint && 'distance' in constraint);
}
function isDelayConstraint(constraint) {
return Boolean(constraint && 'delay' in constraint);
}
class AbstractPointerSensor {
constructor(props, events, listenerTarget) {
var _getEventCoordinates;
if (listenerTarget === void 0) {
listenerTarget = getEventListenerTarget(props.event.target);
}
this.props = void 0;
this.events = void 0;
this.autoScrollEnabled = true;
this.document = void 0;
this.activated = false;
this.initialCoordinates = void 0;
this.timeoutId = null;
this.listeners = void 0;
this.documentListeners = void 0;
this.windowListeners = void 0;
this.props = props;
this.events = events;
const {
event
} = props;
const {
target
} = event;
this.props = props;
this.events = events;
this.document = utilities.getOwnerDocument(target);
this.documentListeners = new Listeners(this.document);
this.listeners = new Listeners(listenerTarget);
this.windowListeners = new Listeners(utilities.getWindow(target));
this.initialCoordinates = (_getEventCoordinates = utilities.getEventCoordinates(event)) != null ? _getEventCoordinates : defaultCoordinates;
this.handleStart = this.handleStart.bind(this);
this.handleMove = this.handleMove.bind(this);
this.handleEnd = this.handleEnd.bind(this);
this.handleCancel = this.handleCancel.bind(this);
this.handleKeydown = this.handleKeydown.bind(this);
this.removeTextSelection = this.removeTextSelection.bind(this);
this.attach();
}
attach() {
const {
events,
props: {
options: {
activationConstraint,
bypassActivationConstraint
}
}
} = this;
this.listeners.add(events.move.name, this.handleMove, {
passive: false
});
this.listeners.add(events.end.name, this.handleEnd);
if (events.cancel) {
this.listeners.add(events.cancel.name, this.handleCancel);
}
this.windowListeners.add(EventName.Resize, this.handleCancel);
this.windowListeners.add(EventName.DragStart, preventDefault);
this.windowListeners.add(EventName.VisibilityChange, this.handleCancel);
this.windowListeners.add(EventName.ContextMenu, preventDefault);
this.documentListeners.add(EventName.Keydown, this.handleKeydown);
if (activationConstraint) {
if (bypassActivationConstraint != null && bypassActivationConstraint({
event: this.props.event,
activeNode: this.props.activeNode,
options: this.props.options
})) {
return this.handleStart();
}
if (isDelayConstraint(activationConstraint)) {
this.timeoutId = setTimeout(this.handleStart, activationConstraint.delay);
this.handlePending(activationConstraint);
return;
}
if (isDistanceConstraint(activationConstraint)) {
this.handlePending(activationConstraint);
return;
}
}
this.handleStart();
}
detach() {
this.listeners.removeAll();
this.windowListeners.removeAll(); // Wait until the next event loop before removing document listeners
// This is necessary because we listen for `click` and `selection` events on the document
setTimeout(this.documentListeners.removeAll, 50);
if (this.timeoutId !== null) {
clearTimeout(this.timeoutId);
this.timeoutId = null;
}
}
handlePending(constraint, offset) {
const {
active,
onPending
} = this.props;
onPending(active, constraint, this.initialCoordinates, offset);
}
handleStart() {
const {
initialCoordinates
} = this;
const {
onStart
} = this.props;
if (initialCoordinates) {
this.activated = true; // Stop propagation of click events once activation constraints are met
this.documentListeners.add(EventName.Click, stopPropagation, {
capture: true
}); // Remove any text selection from the document
this.removeTextSelection(); // Prevent further text selection while dragging
this.documentListeners.add(EventName.SelectionChange, this.removeTextSelection);
onStart(initialCoordinates);
}
}
handleMove(event) {
var _getEventCoordinates2;
const {
activated,
initialCoordinates,
props
} = this;
const {
onMove,
options: {
activationConstraint
}
} = props;
if (!initialCoordinates) {
return;
}
const coordinates = (_getEventCoordinates2 = utilities.getEventCoordinates(event)) != null ? _getEventCoordinates2 : defaultCoordinates;
const delta = utilities.subtract(initialCoordinates, coordinates); // Constraint validation
if (!activated && activationConstraint) {
if (isDistanceConstraint(activationConstraint)) {
if (activationConstraint.tolerance != null && hasExceededDistance(delta, activationConstraint.tolerance)) {
return this.handleCancel();
}
if (hasExceededDistance(delta, activationConstraint.distance)) {
return this.handleStart();
}
}
if (isDelayConstraint(activationConstraint)) {
if (hasExceededDistance(delta, activationConstraint.tolerance)) {
return this.handleCancel();
}
}
this.handlePending(activationConstraint, delta);
return;
}
if (event.cancelable) {
event.preventDefault();
}
onMove(coordinates);
}
handleEnd() {
const {
onAbort,
onEnd
} = this.props;
this.detach();
if (!this.activated) {
onAbort(this.props.active);
}
onEnd();
}
handleCancel() {
const {
onAbort,
onCancel
} = this.props;
this.detach();
if (!this.activated) {
onAbort(this.props.active);
}
onCancel();
}
handleKeydown(event) {
if (event.code === exports.KeyboardCode.Esc) {
this.handleCancel();
}
}
removeTextSelection() {
var _this$document$getSel;
(_this$document$getSel = this.document.getSelection()) == null ? void 0 : _this$document$getSel.removeAllRanges();
}
}
const events = {
cancel: {
name: 'pointercancel'
},
move: {
name: 'pointermove'
},
end: {
name: 'pointerup'
}
};
class PointerSensor extends AbstractPointerSensor {
constructor(props) {
const {
event
} = props; // Pointer events stop firing if the target is unmounted while dragging
// Therefore we attach listeners to the owner document instead
const listenerTarget = utilities.getOwnerDocument(event.target);
super(props, events, listenerTarget);
}
}
PointerSensor.activators = [{
eventName: 'onPointerDown',
handler: (_ref, _ref2) => {
let {
nativeEvent: event
} = _ref;
let {
onActivation
} = _ref2;
if (!event.isPrimary || event.button !== 0) {
return false;
}
onActivation == null ? void 0 : onActivation({
event
});
return true;
}
}];
const events$1 = {
move: {
name: 'mousemove'
},
end: {
name: 'mouseup'
}
};
var MouseButton;
(function (MouseButton) {
MouseButton[MouseButton["RightClick"] = 2] = "RightClick";
})(MouseButton || (MouseButton = {}));
class MouseSensor extends AbstractPointerSensor {
constructor(props) {
super(props, events$1, utilities.getOwnerDocument(props.event.target));
}
}
MouseSensor.activators = [{
eventName: 'onMouseDown',
handler: (_ref, _ref2) => {
let {
nativeEvent: event
} = _ref;
let {
onActivation
} = _ref2;
if (event.button === MouseButton.RightClick) {
return false;
}
onActivation == null ? void 0 : onActivation({
event
});
return true;
}
}];
const events$2 = {
cancel: {
name: 'touchcancel'
},
move: {
name: 'touchmove'
},
end: {
name: 'touchend'
}
};
class TouchSensor extends AbstractPointerSensor {
constructor(props) {
super(props, events$2);
}
static setup() {
// Adding a non-capture and non-passive `touchmove` listener in order
// to force `event.preventDefault()` calls to work in dynamically added
// touchmove event handlers. This is required for iOS Safari.
window.addEventListener(events$2.move.name, noop, {
capture: false,
passive: false
});
return function teardown() {
window.removeEventListener(events$2.move.name, noop);
}; // We create a new handler because the teardown function of another sensor
// could remove our event listener if we use a referentially equal listener.
function noop() {}
}
}
TouchSensor.activators = [{
eventName: 'onTouchStart',
handler: (_ref, _ref2) => {
let {
nativeEvent: event
} = _ref;
let {
onActivation
} = _ref2;
const {
touches
} = event;
if (touches.length > 1) {
return false;
}
onActivation == null ? void 0 : onActivation({
event
});
return true;
}
}];
(function (AutoScrollActivator) {
AutoScrollActivator[AutoScrollActivator["Pointer"] = 0] = "Pointer";
AutoScrollActivator[AutoScrollActivator["DraggableRect"] = 1] = "DraggableRect";
})(exports.AutoScrollActivator || (exports.AutoScrollActivator = {}));
(function (TraversalOrder) {
TraversalOrder[TraversalOrder["TreeOrder"] = 0] = "TreeOrder";
TraversalOrder[TraversalOrder["ReversedTreeOrder"] = 1] = "ReversedTreeOrder";
})(exports.TraversalOrder || (exports.TraversalOrder = {}));
function useAutoScroller(_ref) {
let {
acceleration,
activator = exports.AutoScrollActivator.Pointer,
canScroll,
draggingRect,
enabled,
interval = 5,
order = exports.TraversalOrder.TreeOrder,
pointerCoordinates,
scrollableAncestors,
scrollableAncestorRects,
delta,
threshold
} = _ref;
const scrollIntent = useScrollIntent({
delta,
disabled: !enabled
});
const [setAutoScrollInterval, clearAutoScrollInterval] = utilities.useInterval();
const scrollSpeed = React.useRef({
x: 0,
y: 0
});
const scrollDirection = React.useRef({
x: 0,
y: 0
});
const rect = React.useMemo(() => {
switch (activator) {
case exports.AutoScrollActivator.Pointer:
return pointerCoordinates ? {
top: pointerCoordinates.y,
bottom: pointerCoordinates.y,
left: pointerCoordinates.x,
right: pointerCoordinates.x
} : null;
case exports.AutoScrollActivator.DraggableRect:
return draggingRect;
}
}, [activator, draggingRect, pointerCoordinates]);
const scrollContainerRef = React.useRef(null);
const autoScroll = React.useCallback(() => {
const scrollContainer = scrollContainerRef.current;
if (!scrollContainer) {
return;
}
const scrollLeft = scrollSpeed.current.x * scrollDirection.current.x;
const scrollTop = scrollSpeed.current.y * scrollDirection.current.y;
scrollContainer.scrollBy(scrollLeft, scrollTop);
}, []);
const sortedScrollableAncestors = React.useMemo(() => order === exports.TraversalOrder.TreeOrder ? [...scrollableAncestors].reverse() : scrollableAncestors, [order, scrollableAncestors]);
React.useEffect(() => {
if (!enabled || !scrollableAncestors.length || !rect) {
clearAutoScrollInterval();
return;
}
for (const scrollContainer of sortedScrollableAncestors) {
if ((canScroll == null ? void 0 : canScroll(scrollContainer)) === false) {
continue;
}
const index = scrollableAncestors.indexOf(scrollContainer);
const scrollContainerRect = scrollableAncestorRects[index];
if (!scrollContainerRect) {
continue;
}
const {
direction,
speed
} = getScrollDirectionAndSpeed(scrollContainer, scrollContainerRect, rect, acceleration, threshold);
for (const axis of ['x', 'y']) {
if (!scrollIntent[axis][direction[axis]]) {
speed[axis] = 0;
direction[axis] = 0;
}
}
if (speed.x > 0 || speed.y > 0) {
clearAutoScrollInterval();
scrollContainerRef.current = scrollContainer;
setAutoScrollInterval(autoScroll, interval);
scrollSpeed.current = speed;
scrollDirection.current = direction;
return;
}
}
scrollSpeed.current = {
x: 0,
y: 0
};
scrollDirection.current = {
x: 0,
y: 0
};
clearAutoScrollInterval();
}, // eslint-disable-next-line react-hooks/exhaustive-deps
[acceleration, autoScroll, canScroll, clearAutoScrollInterval, enabled, interval, // eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(rect), // eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(scrollIntent), setAutoScrollInterval, scrollableAncestors, sortedScrollableAncestors, scrollableAncestorRects, // eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(threshold)]);
}
const defaultScrollIntent = {
x: {
[Direction.Backward]: false,
[Direction.Forward]: false
},
y: {
[Direction.Backward]: false,
[Direction.Forward]: false
}
};
function useScrollIntent(_ref2) {
let {
delta,
disabled
} = _ref2;
const previousDelta = utilities.usePrevious(delta);
return utilities.useLazyMemo(previousIntent => {
if (disabled || !previousDelta || !previousIntent) {
// Reset scroll intent tracking when auto-scrolling is disabled
return defaultScrollIntent;
}
const direction = {
x: Math.sign(delta.x - previousDelta.x),
y: Math.sign(delta.y - previousDelta.y)
}; // Keep track of the user intent to scroll in each direction for both axis
return {
x: {
[Direction.Backward]: previousIntent.x[Direction.Backward] || direction.x === -1,
[Direction.Forward]: previousIntent.x[Direction.Forward] || direction.x === 1
},
y: {
[Direction.Backward]: previousIntent.y[Direction.Backward] || direction.y === -1,
[Direction.Forward]: previousIntent.y[Direction.Forward] || direction.y === 1
}
};
}, [disabled, delta, previousDelta]);
}
function useCachedNode(draggableNodes, id) {
const draggableNode = id != null ? draggableNodes.get(id) : undefined;
const node = draggableNode ? draggableNode.node.current : null;
return utilities.useLazyMemo(cachedNode => {
var _ref;
if (id == null) {
return null;
} // In some cases, the draggable node can unmount while dragging
// This is the case for virtualized lists. In those situations,
// we fall back to the last known value for that node.
return (_ref = node != null ? node : cachedNode) != null ? _ref : null;
}, [node, id]);
}
function useCombineActivators(sensors, getSyntheticHandler) {
return React.useMemo(() => sensors.reduce((accumulator, sensor) => {
cons