@lucasols/dnd-kit-core 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/dist/index.d.ts +655 -12
  2. package/dist/index.js +2626 -7
  3. package/dist/index.js.map +1 -0
  4. package/package.json +21 -17
  5. package/dist/components/Accessibility/Accessibility.d.ts +0 -10
  6. package/dist/components/Accessibility/components/RestoreFocus.d.ts +0 -5
  7. package/dist/components/Accessibility/components/index.d.ts +0 -1
  8. package/dist/components/Accessibility/defaults.d.ts +0 -3
  9. package/dist/components/Accessibility/index.d.ts +0 -4
  10. package/dist/components/Accessibility/types.d.ts +0 -15
  11. package/dist/components/DndContext/DndContext.d.ts +0 -35
  12. package/dist/components/DndContext/defaults.d.ts +0 -13
  13. package/dist/components/DndContext/hooks/index.d.ts +0 -2
  14. package/dist/components/DndContext/hooks/useLayoutShiftScrollCompensation.d.ts +0 -14
  15. package/dist/components/DndContext/hooks/useMeasuringConfiguration.d.ts +0 -3
  16. package/dist/components/DndContext/index.d.ts +0 -3
  17. package/dist/components/DndContext/types.d.ts +0 -16
  18. package/dist/components/DndMonitor/context.d.ts +0 -3
  19. package/dist/components/DndMonitor/index.d.ts +0 -4
  20. package/dist/components/DndMonitor/types.d.ts +0 -14
  21. package/dist/components/DndMonitor/useDndMonitor.d.ts +0 -2
  22. package/dist/components/DndMonitor/useDndMonitorProvider.d.ts +0 -2
  23. package/dist/components/DragOverlay/DragOverlay.d.ts +0 -11
  24. package/dist/components/DragOverlay/components/AnimationManager/AnimationManager.d.ts +0 -8
  25. package/dist/components/DragOverlay/components/AnimationManager/index.d.ts +0 -2
  26. package/dist/components/DragOverlay/components/NullifiedContextProvider/NullifiedContextProvider.d.ts +0 -6
  27. package/dist/components/DragOverlay/components/NullifiedContextProvider/index.d.ts +0 -1
  28. package/dist/components/DragOverlay/components/PositionedOverlay/PositionedOverlay.d.ts +0 -18
  29. package/dist/components/DragOverlay/components/PositionedOverlay/index.d.ts +0 -2
  30. package/dist/components/DragOverlay/components/index.d.ts +0 -5
  31. package/dist/components/DragOverlay/hooks/index.d.ts +0 -3
  32. package/dist/components/DragOverlay/hooks/useDropAnimation.d.ts +0 -65
  33. package/dist/components/DragOverlay/hooks/useKey.d.ts +0 -2
  34. package/dist/components/DragOverlay/index.d.ts +0 -4
  35. package/dist/components/index.d.ts +0 -8
  36. package/dist/dnd-kit-core.cjs.development.js +0 -3924
  37. package/dist/dnd-kit-core.cjs.development.js.map +0 -1
  38. package/dist/dnd-kit-core.cjs.production.min.js +0 -2
  39. package/dist/dnd-kit-core.cjs.production.min.js.map +0 -1
  40. package/dist/dnd-kit-core.esm.js +0 -3903
  41. package/dist/dnd-kit-core.esm.js.map +0 -1
  42. package/dist/hooks/index.d.ts +0 -8
  43. package/dist/hooks/useDndContext.d.ts +0 -4
  44. package/dist/hooks/useDraggable.d.ts +0 -37
  45. package/dist/hooks/useDroppable.d.ts +0 -29
  46. package/dist/hooks/utilities/index.d.ts +0 -21
  47. package/dist/hooks/utilities/useAutoScroller.d.ts +0 -40
  48. package/dist/hooks/utilities/useCachedNode.d.ts +0 -3
  49. package/dist/hooks/utilities/useCombineActivators.d.ts +0 -3
  50. package/dist/hooks/utilities/useDragOverlayMeasuring.d.ts +0 -7
  51. package/dist/hooks/utilities/useDroppableMeasuring.d.ts +0 -27
  52. package/dist/hooks/utilities/useInitialRect.d.ts +0 -2
  53. package/dist/hooks/utilities/useInitialValue.d.ts +0 -3
  54. package/dist/hooks/utilities/useMutationObserver.d.ts +0 -10
  55. package/dist/hooks/utilities/useRect.d.ts +0 -2
  56. package/dist/hooks/utilities/useRectDelta.d.ts +0 -2
  57. package/dist/hooks/utilities/useRects.d.ts +0 -5
  58. package/dist/hooks/utilities/useResizeObserver.d.ts +0 -10
  59. package/dist/hooks/utilities/useScrollIntoViewIfNeeded.d.ts +0 -1
  60. package/dist/hooks/utilities/useScrollOffsets.d.ts +0 -2
  61. package/dist/hooks/utilities/useScrollOffsetsDelta.d.ts +0 -2
  62. package/dist/hooks/utilities/useScrollableAncestors.d.ts +0 -1
  63. package/dist/hooks/utilities/useSensorSetup.d.ts +0 -2
  64. package/dist/hooks/utilities/useSyntheticListeners.d.ts +0 -9
  65. package/dist/hooks/utilities/useWindowRect.d.ts +0 -1
  66. package/dist/modifiers/applyModifiers.d.ts +0 -3
  67. package/dist/modifiers/index.d.ts +0 -2
  68. package/dist/modifiers/types.d.ts +0 -17
  69. package/dist/sensors/events.d.ts +0 -11
  70. package/dist/sensors/index.d.ts +0 -11
  71. package/dist/sensors/keyboard/KeyboardSensor.d.ts +0 -27
  72. package/dist/sensors/keyboard/defaults.d.ts +0 -3
  73. package/dist/sensors/keyboard/index.d.ts +0 -4
  74. package/dist/sensors/keyboard/types.d.ts +0 -21
  75. package/dist/sensors/mouse/MouseSensor.d.ts +0 -13
  76. package/dist/sensors/mouse/index.d.ts +0 -2
  77. package/dist/sensors/pointer/AbstractPointerSensor.d.ts +0 -48
  78. package/dist/sensors/pointer/PointerSensor.d.ts +0 -13
  79. package/dist/sensors/pointer/index.d.ts +0 -4
  80. package/dist/sensors/touch/TouchSensor.d.ts +0 -14
  81. package/dist/sensors/touch/index.d.ts +0 -2
  82. package/dist/sensors/types.d.ts +0 -60
  83. package/dist/sensors/useSensor.d.ts +0 -2
  84. package/dist/sensors/useSensors.d.ts +0 -2
  85. package/dist/sensors/utilities/Listeners.d.ts +0 -7
  86. package/dist/sensors/utilities/getEventListenerTarget.d.ts +0 -1
  87. package/dist/sensors/utilities/hasExceededDistance.d.ts +0 -2
  88. package/dist/sensors/utilities/index.d.ts +0 -3
  89. package/dist/store/actions.d.ts +0 -36
  90. package/dist/store/constructors.d.ts +0 -10
  91. package/dist/store/context.d.ts +0 -6
  92. package/dist/store/index.d.ts +0 -4
  93. package/dist/store/reducer.d.ts +0 -4
  94. package/dist/store/types.d.ts +0 -98
  95. package/dist/types/coordinates.d.ts +0 -9
  96. package/dist/types/direction.d.ts +0 -4
  97. package/dist/types/events.d.ts +0 -21
  98. package/dist/types/index.d.ts +0 -6
  99. package/dist/types/other.d.ts +0 -1
  100. package/dist/types/react.d.ts +0 -3
  101. package/dist/types/rect.d.ts +0 -8
  102. package/dist/utilities/algorithms/closestCenter.d.ts +0 -6
  103. package/dist/utilities/algorithms/closestCorners.d.ts +0 -6
  104. package/dist/utilities/algorithms/helpers.d.ts +0 -24
  105. package/dist/utilities/algorithms/index.d.ts +0 -6
  106. package/dist/utilities/algorithms/pointerWithin.d.ts +0 -5
  107. package/dist/utilities/algorithms/rectIntersection.d.ts +0 -11
  108. package/dist/utilities/algorithms/types.d.ts +0 -20
  109. package/dist/utilities/coordinates/constants.d.ts +0 -2
  110. package/dist/utilities/coordinates/distanceBetweenPoints.d.ts +0 -5
  111. package/dist/utilities/coordinates/getRelativeTransformOrigin.d.ts +0 -2
  112. package/dist/utilities/coordinates/index.d.ts +0 -3
  113. package/dist/utilities/index.d.ts +0 -6
  114. package/dist/utilities/nodes/getMeasurableNode.d.ts +0 -1
  115. package/dist/utilities/nodes/index.d.ts +0 -1
  116. package/dist/utilities/other/index.d.ts +0 -1
  117. package/dist/utilities/other/noop.d.ts +0 -1
  118. package/dist/utilities/rect/Rect.d.ts +0 -11
  119. package/dist/utilities/rect/adjustScale.d.ts +0 -3
  120. package/dist/utilities/rect/getRect.d.ts +0 -25
  121. package/dist/utilities/rect/getRectDelta.d.ts +0 -2
  122. package/dist/utilities/rect/getWindowClientRect.d.ts +0 -2
  123. package/dist/utilities/rect/index.d.ts +0 -6
  124. package/dist/utilities/rect/rectAdjustment.d.ts +0 -3
  125. package/dist/utilities/scroll/documentScrollingElement.d.ts +0 -1
  126. package/dist/utilities/scroll/getScrollCoordinates.d.ts +0 -4
  127. package/dist/utilities/scroll/getScrollDirectionAndSpeed.d.ts +0 -17
  128. package/dist/utilities/scroll/getScrollElementRect.d.ts +0 -8
  129. package/dist/utilities/scroll/getScrollOffsets.d.ts +0 -4
  130. package/dist/utilities/scroll/getScrollPosition.d.ts +0 -14
  131. package/dist/utilities/scroll/getScrollableAncestors.d.ts +0 -2
  132. package/dist/utilities/scroll/getScrollableElement.d.ts +0 -1
  133. package/dist/utilities/scroll/index.d.ts +0 -10
  134. package/dist/utilities/scroll/isFixed.d.ts +0 -1
  135. package/dist/utilities/scroll/isScrollable.d.ts +0 -1
  136. package/dist/utilities/scroll/scrollIntoViewIfNeeded.d.ts +0 -2
  137. package/dist/utilities/transform/index.d.ts +0 -2
  138. package/dist/utilities/transform/inverseTransform.d.ts +0 -2
  139. package/dist/utilities/transform/parseTransform.d.ts +0 -2
@@ -1,3903 +0,0 @@
1
- import React, { createContext, useContext, useEffect, useState, useCallback, useMemo, useRef, useReducer, memo, cloneElement, forwardRef } from 'react';
2
- import { createPortal, unstable_batchedUpdates } from 'react-dom';
3
- import { useUniqueId, getEventCoordinates, getWindow, isDocument, isHTMLElement, isSVGElement, canUseDOM, isWindow, isNode, getOwnerDocument, add, isKeyboardEvent, subtract, useLazyMemo, useInterval, usePrevious, useLatestValue, useEvent, useIsomorphicLayoutEffect, useNodeRef, findFirstFocusableNode, CSS } from '@lucasols/dnd-kit-utilities';
4
- import { useAnnouncement, HiddenText, LiveRegion } from '@lucasols/dnd-kit-accessibility';
5
-
6
- const DndMonitorContext = /*#__PURE__*/createContext(null);
7
-
8
- function useDndMonitor(listener) {
9
- const registerListener = useContext(DndMonitorContext);
10
- useEffect(() => {
11
- if (!registerListener) {
12
- throw new Error('useDndMonitor must be used within a children of <DndContext>');
13
- }
14
-
15
- const unsubscribe = registerListener(listener);
16
- return unsubscribe;
17
- }, [listener, registerListener]);
18
- }
19
-
20
- function useDndMonitorProvider() {
21
- const [listeners] = useState(() => new Set());
22
- const registerListener = useCallback(listener => {
23
- listeners.add(listener);
24
- return () => listeners.delete(listener);
25
- }, [listeners]);
26
- const dispatch = useCallback(_ref => {
27
- let {
28
- type,
29
- event
30
- } = _ref;
31
- listeners.forEach(listener => {
32
- var _listener$type;
33
-
34
- return (_listener$type = listener[type]) == null ? void 0 : _listener$type.call(listener, event);
35
- });
36
- }, [listeners]);
37
- return [dispatch, registerListener];
38
- }
39
-
40
- const defaultScreenReaderInstructions = {
41
- 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 "
42
- };
43
- const defaultAnnouncements = {
44
- onDragStart(_ref) {
45
- let {
46
- active
47
- } = _ref;
48
- return "Picked up draggable item " + active.id + ".";
49
- },
50
-
51
- onDragOver(_ref2) {
52
- let {
53
- active,
54
- over
55
- } = _ref2;
56
-
57
- if (over) {
58
- return "Draggable item " + active.id + " was moved over droppable area " + over.id + ".";
59
- }
60
-
61
- return "Draggable item " + active.id + " is no longer over a droppable area.";
62
- },
63
-
64
- onDragEnd(_ref3) {
65
- let {
66
- active,
67
- over
68
- } = _ref3;
69
-
70
- if (over) {
71
- return "Draggable item " + active.id + " was dropped over droppable area " + over.id;
72
- }
73
-
74
- return "Draggable item " + active.id + " was dropped.";
75
- },
76
-
77
- onDragCancel(_ref4) {
78
- let {
79
- active
80
- } = _ref4;
81
- return "Dragging was cancelled. Draggable item " + active.id + " was dropped.";
82
- }
83
-
84
- };
85
-
86
- function Accessibility(_ref) {
87
- let {
88
- announcements = defaultAnnouncements,
89
- container,
90
- hiddenTextDescribedById,
91
- screenReaderInstructions = defaultScreenReaderInstructions
92
- } = _ref;
93
- const {
94
- announce,
95
- announcement
96
- } = useAnnouncement();
97
- const liveRegionId = useUniqueId("DndLiveRegion");
98
- const [mounted, setMounted] = useState(false);
99
- useEffect(() => {
100
- setMounted(true);
101
- }, []);
102
- useDndMonitor(useMemo(() => ({
103
- onDragStart(_ref2) {
104
- let {
105
- active
106
- } = _ref2;
107
- announce(announcements.onDragStart({
108
- active
109
- }));
110
- },
111
-
112
- onDragMove(_ref3) {
113
- let {
114
- active,
115
- over
116
- } = _ref3;
117
-
118
- if (announcements.onDragMove) {
119
- announce(announcements.onDragMove({
120
- active,
121
- over
122
- }));
123
- }
124
- },
125
-
126
- onDragOver(_ref4) {
127
- let {
128
- active,
129
- over
130
- } = _ref4;
131
- announce(announcements.onDragOver({
132
- active,
133
- over
134
- }));
135
- },
136
-
137
- onDragEnd(_ref5) {
138
- let {
139
- active,
140
- over
141
- } = _ref5;
142
- announce(announcements.onDragEnd({
143
- active,
144
- over
145
- }));
146
- },
147
-
148
- onDragCancel(_ref6) {
149
- let {
150
- active,
151
- over
152
- } = _ref6;
153
- announce(announcements.onDragCancel({
154
- active,
155
- over
156
- }));
157
- }
158
-
159
- }), [announce, announcements]));
160
-
161
- if (!mounted) {
162
- return null;
163
- }
164
-
165
- const markup = React.createElement(React.Fragment, null, React.createElement(HiddenText, {
166
- id: hiddenTextDescribedById,
167
- value: screenReaderInstructions.draggable
168
- }), React.createElement(LiveRegion, {
169
- id: liveRegionId,
170
- announcement: announcement
171
- }));
172
- return container ? createPortal(markup, container) : markup;
173
- }
174
-
175
- var Action;
176
-
177
- (function (Action) {
178
- Action["DragStart"] = "dragStart";
179
- Action["DragMove"] = "dragMove";
180
- Action["DragEnd"] = "dragEnd";
181
- Action["DragCancel"] = "dragCancel";
182
- Action["DragOver"] = "dragOver";
183
- Action["RegisterDroppable"] = "registerDroppable";
184
- Action["SetDroppableDisabled"] = "setDroppableDisabled";
185
- Action["UnregisterDroppable"] = "unregisterDroppable";
186
- })(Action || (Action = {}));
187
-
188
- function noop() {}
189
-
190
- function useSensor(sensor, options) {
191
- return useMemo(() => ({
192
- sensor,
193
- options: options != null ? options : {}
194
- }), // eslint-disable-next-line react-hooks/exhaustive-deps
195
- [sensor, options]);
196
- }
197
-
198
- function useSensors() {
199
- for (var _len = arguments.length, sensors = new Array(_len), _key = 0; _key < _len; _key++) {
200
- sensors[_key] = arguments[_key];
201
- }
202
-
203
- return useMemo(() => [...sensors].filter(sensor => sensor != null), // eslint-disable-next-line react-hooks/exhaustive-deps
204
- [...sensors]);
205
- }
206
-
207
- const defaultCoordinates = /*#__PURE__*/Object.freeze({
208
- x: 0,
209
- y: 0
210
- });
211
-
212
- /**
213
- * Returns the distance between two points
214
- */
215
- function distanceBetween(p1, p2) {
216
- return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
217
- }
218
-
219
- function getRelativeTransformOrigin(event, rect) {
220
- const eventCoordinates = getEventCoordinates(event);
221
-
222
- if (!eventCoordinates) {
223
- return '0 0';
224
- }
225
-
226
- const transformOrigin = {
227
- x: (eventCoordinates.x - rect.left) / rect.width * 100,
228
- y: (eventCoordinates.y - rect.top) / rect.height * 100
229
- };
230
- return transformOrigin.x + "% " + transformOrigin.y + "%";
231
- }
232
-
233
- /**
234
- * Sort collisions from smallest to greatest value
235
- */
236
- function sortCollisionsAsc(_ref, _ref2) {
237
- let {
238
- data: {
239
- value: a
240
- }
241
- } = _ref;
242
- let {
243
- data: {
244
- value: b
245
- }
246
- } = _ref2;
247
- return a - b;
248
- }
249
- /**
250
- * Sort collisions from greatest to smallest value
251
- */
252
-
253
- function sortCollisionsDesc(_ref3, _ref4) {
254
- let {
255
- data: {
256
- value: a
257
- }
258
- } = _ref3;
259
- let {
260
- data: {
261
- value: b
262
- }
263
- } = _ref4;
264
- return b - a;
265
- }
266
- /**
267
- * Returns the coordinates of the corners of a given rectangle:
268
- * [TopLeft {x, y}, TopRight {x, y}, BottomLeft {x, y}, BottomRight {x, y}]
269
- */
270
-
271
- function cornersOfRectangle(_ref5) {
272
- let {
273
- left,
274
- top,
275
- height,
276
- width
277
- } = _ref5;
278
- return [{
279
- x: left,
280
- y: top
281
- }, {
282
- x: left + width,
283
- y: top
284
- }, {
285
- x: left,
286
- y: top + height
287
- }, {
288
- x: left + width,
289
- y: top + height
290
- }];
291
- }
292
- function getFirstCollision(collisions, property) {
293
- if (!collisions || collisions.length === 0) {
294
- return null;
295
- }
296
-
297
- const [firstCollision] = collisions;
298
- return property ? firstCollision[property] : firstCollision;
299
- }
300
-
301
- /**
302
- * Returns the coordinates of the center of a given ClientRect
303
- */
304
-
305
- function centerOfRectangle(rect, left, top) {
306
- if (left === void 0) {
307
- left = rect.left;
308
- }
309
-
310
- if (top === void 0) {
311
- top = rect.top;
312
- }
313
-
314
- return {
315
- x: left + rect.width * 0.5,
316
- y: top + rect.height * 0.5
317
- };
318
- }
319
- /**
320
- * Returns the closest rectangles from an array of rectangles to the center of a given
321
- * rectangle.
322
- */
323
-
324
-
325
- const closestCenter = _ref => {
326
- let {
327
- collisionRect,
328
- droppableRects,
329
- droppableContainers
330
- } = _ref;
331
- const centerRect = centerOfRectangle(collisionRect, collisionRect.left, collisionRect.top);
332
- const collisions = [];
333
-
334
- for (const droppableContainer of droppableContainers) {
335
- const {
336
- id
337
- } = droppableContainer;
338
- const rect = droppableRects.get(id);
339
-
340
- if (rect) {
341
- const distBetween = distanceBetween(centerOfRectangle(rect), centerRect);
342
- collisions.push({
343
- id,
344
- data: {
345
- droppableContainer,
346
- value: distBetween
347
- }
348
- });
349
- }
350
- }
351
-
352
- return collisions.sort(sortCollisionsAsc);
353
- };
354
-
355
- /**
356
- * Returns the closest rectangles from an array of rectangles to the corners of
357
- * another rectangle.
358
- */
359
-
360
- const closestCorners = _ref => {
361
- let {
362
- collisionRect,
363
- droppableRects,
364
- droppableContainers
365
- } = _ref;
366
- const corners = cornersOfRectangle(collisionRect);
367
- const collisions = [];
368
-
369
- for (const droppableContainer of droppableContainers) {
370
- const {
371
- id
372
- } = droppableContainer;
373
- const rect = droppableRects.get(id);
374
-
375
- if (rect) {
376
- const rectCorners = cornersOfRectangle(rect);
377
- const distances = corners.reduce((accumulator, corner, index) => {
378
- return accumulator + distanceBetween(rectCorners[index], corner);
379
- }, 0);
380
- const effectiveDistance = Number((distances / 4).toFixed(4));
381
- collisions.push({
382
- id,
383
- data: {
384
- droppableContainer,
385
- value: effectiveDistance
386
- }
387
- });
388
- }
389
- }
390
-
391
- return collisions.sort(sortCollisionsAsc);
392
- };
393
-
394
- /**
395
- * Returns the intersecting rectangle area between two rectangles
396
- */
397
-
398
- function getIntersectionRatio(entry, target) {
399
- const top = Math.max(target.top, entry.top);
400
- const left = Math.max(target.left, entry.left);
401
- const right = Math.min(target.left + target.width, entry.left + entry.width);
402
- const bottom = Math.min(target.top + target.height, entry.top + entry.height);
403
- const width = right - left;
404
- const height = bottom - top;
405
-
406
- if (left < right && top < bottom) {
407
- const targetArea = target.width * target.height;
408
- const entryArea = entry.width * entry.height;
409
- const intersectionArea = width * height;
410
- const intersectionRatio = intersectionArea / (targetArea + entryArea - intersectionArea);
411
- return Number(intersectionRatio.toFixed(4));
412
- } // Rectangles do not overlap, or overlap has an area of zero (edge/corner overlap)
413
-
414
-
415
- return 0;
416
- }
417
- /**
418
- * Returns the rectangles that has the greatest intersection area with a given
419
- * rectangle in an array of rectangles.
420
- */
421
-
422
- const rectIntersection = _ref => {
423
- let {
424
- collisionRect,
425
- droppableRects,
426
- droppableContainers
427
- } = _ref;
428
- const collisions = [];
429
-
430
- for (const droppableContainer of droppableContainers) {
431
- const {
432
- id
433
- } = droppableContainer;
434
- const rect = droppableRects.get(id);
435
-
436
- if (rect) {
437
- const intersectionRatio = getIntersectionRatio(rect, collisionRect);
438
-
439
- if (intersectionRatio > 0) {
440
- collisions.push({
441
- id,
442
- data: {
443
- droppableContainer,
444
- value: intersectionRatio
445
- }
446
- });
447
- }
448
- }
449
- }
450
-
451
- return collisions.sort(sortCollisionsDesc);
452
- };
453
-
454
- /**
455
- * Check if a given point is contained within a bounding rectangle
456
- */
457
-
458
- function isPointWithinRect(point, rect) {
459
- const {
460
- top,
461
- left,
462
- bottom,
463
- right
464
- } = rect;
465
- return top <= point.y && point.y <= bottom && left <= point.x && point.x <= right;
466
- }
467
- /**
468
- * Returns the rectangles that the pointer is hovering over
469
- */
470
-
471
-
472
- const pointerWithin = _ref => {
473
- let {
474
- droppableContainers,
475
- droppableRects,
476
- pointerCoordinates
477
- } = _ref;
478
-
479
- if (!pointerCoordinates) {
480
- return [];
481
- }
482
-
483
- const collisions = [];
484
-
485
- for (const droppableContainer of droppableContainers) {
486
- const {
487
- id
488
- } = droppableContainer;
489
- const rect = droppableRects.get(id);
490
-
491
- if (rect && isPointWithinRect(pointerCoordinates, rect)) {
492
- /* There may be more than a single rectangle intersecting
493
- * with the pointer coordinates. In order to sort the
494
- * colliding rectangles, we measure the distance between
495
- * the pointer and the corners of the intersecting rectangle
496
- */
497
- const corners = cornersOfRectangle(rect);
498
- const distances = corners.reduce((accumulator, corner) => {
499
- return accumulator + distanceBetween(pointerCoordinates, corner);
500
- }, 0);
501
- const effectiveDistance = Number((distances / 4).toFixed(4));
502
- collisions.push({
503
- id,
504
- data: {
505
- droppableContainer,
506
- value: effectiveDistance
507
- }
508
- });
509
- }
510
- }
511
-
512
- return collisions.sort(sortCollisionsAsc);
513
- };
514
-
515
- function adjustScale(transform, rect1, rect2) {
516
- return { ...transform,
517
- scaleX: rect1 && rect2 ? rect1.width / rect2.width : 1,
518
- scaleY: rect1 && rect2 ? rect1.height / rect2.height : 1
519
- };
520
- }
521
-
522
- function getRectDelta(rect1, rect2) {
523
- return rect1 && rect2 ? {
524
- x: rect1.left - rect2.left,
525
- y: rect1.top - rect2.top
526
- } : defaultCoordinates;
527
- }
528
-
529
- function createRectAdjustmentFn(modifier) {
530
- return function adjustClientRect(rect) {
531
- for (var _len = arguments.length, adjustments = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
532
- adjustments[_key - 1] = arguments[_key];
533
- }
534
-
535
- return adjustments.reduce((acc, adjustment) => ({ ...acc,
536
- top: acc.top + modifier * adjustment.y,
537
- bottom: acc.bottom + modifier * adjustment.y,
538
- left: acc.left + modifier * adjustment.x,
539
- right: acc.right + modifier * adjustment.x
540
- }), { ...rect
541
- });
542
- };
543
- }
544
- const getAdjustedRect = /*#__PURE__*/createRectAdjustmentFn(1);
545
-
546
- function parseTransform(transform) {
547
- if (transform.startsWith('matrix3d(')) {
548
- const transformArray = transform.slice(9, -1).split(/, /);
549
- return {
550
- x: +transformArray[12],
551
- y: +transformArray[13],
552
- scaleX: +transformArray[0],
553
- scaleY: +transformArray[5]
554
- };
555
- } else if (transform.startsWith('matrix(')) {
556
- const transformArray = transform.slice(7, -1).split(/, /);
557
- return {
558
- x: +transformArray[4],
559
- y: +transformArray[5],
560
- scaleX: +transformArray[0],
561
- scaleY: +transformArray[3]
562
- };
563
- }
564
-
565
- return null;
566
- }
567
-
568
- function inverseTransform(rect, transform, transformOrigin) {
569
- const parsedTransform = parseTransform(transform);
570
-
571
- if (!parsedTransform) {
572
- return rect;
573
- }
574
-
575
- const {
576
- scaleX,
577
- scaleY,
578
- x: translateX,
579
- y: translateY
580
- } = parsedTransform;
581
- const x = rect.left - translateX - (1 - scaleX) * parseFloat(transformOrigin);
582
- const y = rect.top - translateY - (1 - scaleY) * parseFloat(transformOrigin.slice(transformOrigin.indexOf(' ') + 1));
583
- const w = scaleX ? rect.width / scaleX : rect.width;
584
- const h = scaleY ? rect.height / scaleY : rect.height;
585
- return {
586
- width: w,
587
- height: h,
588
- top: y,
589
- right: x + w,
590
- bottom: y + h,
591
- left: x
592
- };
593
- }
594
-
595
- const defaultOptions = {
596
- ignoreTransform: false
597
- };
598
- /**
599
- * Returns the bounding client rect of an element relative to the viewport.
600
- */
601
-
602
- function getClientRect(element, options) {
603
- if (options === void 0) {
604
- options = defaultOptions;
605
- }
606
-
607
- let rect = element.getBoundingClientRect();
608
-
609
- if (options.ignoreTransform) {
610
- const {
611
- transform,
612
- transformOrigin
613
- } = getWindow(element).getComputedStyle(element);
614
-
615
- if (transform) {
616
- rect = inverseTransform(rect, transform, transformOrigin);
617
- }
618
- }
619
-
620
- const {
621
- top,
622
- left,
623
- width,
624
- height,
625
- bottom,
626
- right
627
- } = rect;
628
- return {
629
- top,
630
- left,
631
- width,
632
- height,
633
- bottom,
634
- right
635
- };
636
- }
637
- /**
638
- * Returns the bounding client rect of an element relative to the viewport.
639
- *
640
- * @remarks
641
- * The ClientRect returned by this method does not take into account transforms
642
- * applied to the element it measures.
643
- *
644
- */
645
-
646
- function getTransformAgnosticClientRect(element) {
647
- return getClientRect(element, {
648
- ignoreTransform: true
649
- });
650
- }
651
-
652
- function getWindowClientRect(element) {
653
- const width = element.innerWidth;
654
- const height = element.innerHeight;
655
- return {
656
- top: 0,
657
- left: 0,
658
- right: width,
659
- bottom: height,
660
- width,
661
- height
662
- };
663
- }
664
-
665
- function isFixed(node, computedStyle) {
666
- if (computedStyle === void 0) {
667
- computedStyle = getWindow(node).getComputedStyle(node);
668
- }
669
-
670
- return computedStyle.position === 'fixed';
671
- }
672
-
673
- function isScrollable(element, computedStyle) {
674
- if (computedStyle === void 0) {
675
- computedStyle = getWindow(element).getComputedStyle(element);
676
- }
677
-
678
- const overflowRegex = /(auto|scroll|overlay)/;
679
- const properties = ['overflow', 'overflowX', 'overflowY'];
680
- return properties.some(property => {
681
- const value = computedStyle[property];
682
- return typeof value === 'string' ? overflowRegex.test(value) : false;
683
- });
684
- }
685
-
686
- function getScrollableAncestors(element, limit) {
687
- const scrollParents = [];
688
-
689
- function findScrollableAncestors(node) {
690
- if (limit != null && scrollParents.length >= limit) {
691
- return scrollParents;
692
- }
693
-
694
- if (!node) {
695
- return scrollParents;
696
- }
697
-
698
- if (isDocument(node) && node.scrollingElement != null && !scrollParents.includes(node.scrollingElement)) {
699
- scrollParents.push(node.scrollingElement);
700
- return scrollParents;
701
- }
702
-
703
- if (!isHTMLElement(node) || isSVGElement(node)) {
704
- return scrollParents;
705
- }
706
-
707
- if (scrollParents.includes(node)) {
708
- return scrollParents;
709
- }
710
-
711
- const computedStyle = getWindow(element).getComputedStyle(node);
712
-
713
- if (node !== element) {
714
- if (isScrollable(node, computedStyle)) {
715
- scrollParents.push(node);
716
- }
717
- }
718
-
719
- if (isFixed(node, computedStyle)) {
720
- return scrollParents;
721
- }
722
-
723
- return findScrollableAncestors(node.parentNode);
724
- }
725
-
726
- if (!element) {
727
- return scrollParents;
728
- }
729
-
730
- return findScrollableAncestors(element);
731
- }
732
- function getFirstScrollableAncestor(node) {
733
- const [firstScrollableAncestor] = getScrollableAncestors(node, 1);
734
- return firstScrollableAncestor != null ? firstScrollableAncestor : null;
735
- }
736
-
737
- function getScrollableElement(element) {
738
- if (!canUseDOM || !element) {
739
- return null;
740
- }
741
-
742
- if (isWindow(element)) {
743
- return element;
744
- }
745
-
746
- if (!isNode(element)) {
747
- return null;
748
- }
749
-
750
- if (isDocument(element) || element === getOwnerDocument(element).scrollingElement) {
751
- return window;
752
- }
753
-
754
- if (isHTMLElement(element)) {
755
- return element;
756
- }
757
-
758
- return null;
759
- }
760
-
761
- function getScrollXCoordinate(element) {
762
- if (isWindow(element)) {
763
- return element.scrollX;
764
- }
765
-
766
- return element.scrollLeft;
767
- }
768
- function getScrollYCoordinate(element) {
769
- if (isWindow(element)) {
770
- return element.scrollY;
771
- }
772
-
773
- return element.scrollTop;
774
- }
775
- function getScrollCoordinates(element) {
776
- return {
777
- x: getScrollXCoordinate(element),
778
- y: getScrollYCoordinate(element)
779
- };
780
- }
781
-
782
- var Direction;
783
-
784
- (function (Direction) {
785
- Direction[Direction["Forward"] = 1] = "Forward";
786
- Direction[Direction["Backward"] = -1] = "Backward";
787
- })(Direction || (Direction = {}));
788
-
789
- function isDocumentScrollingElement(element) {
790
- if (!canUseDOM || !element) {
791
- return false;
792
- }
793
-
794
- return element === document.scrollingElement;
795
- }
796
-
797
- function getScrollPosition(scrollingContainer) {
798
- const minScroll = {
799
- x: 0,
800
- y: 0
801
- };
802
- const dimensions = isDocumentScrollingElement(scrollingContainer) ? {
803
- height: window.innerHeight,
804
- width: window.innerWidth
805
- } : {
806
- height: scrollingContainer.clientHeight,
807
- width: scrollingContainer.clientWidth
808
- };
809
- const maxScroll = {
810
- x: scrollingContainer.scrollWidth - dimensions.width,
811
- y: scrollingContainer.scrollHeight - dimensions.height
812
- };
813
- const isTop = scrollingContainer.scrollTop <= minScroll.y;
814
- const isLeft = scrollingContainer.scrollLeft <= minScroll.x;
815
- const isBottom = scrollingContainer.scrollTop >= maxScroll.y;
816
- const isRight = scrollingContainer.scrollLeft >= maxScroll.x;
817
- return {
818
- isTop,
819
- isLeft,
820
- isBottom,
821
- isRight,
822
- maxScroll,
823
- minScroll
824
- };
825
- }
826
-
827
- const defaultThreshold = {
828
- x: 0.2,
829
- y: 0.2
830
- };
831
- function getScrollDirectionAndSpeed(scrollContainer, scrollContainerRect, _ref, acceleration, thresholdPercentage) {
832
- let {
833
- top,
834
- left,
835
- right,
836
- bottom
837
- } = _ref;
838
-
839
- if (acceleration === void 0) {
840
- acceleration = 10;
841
- }
842
-
843
- if (thresholdPercentage === void 0) {
844
- thresholdPercentage = defaultThreshold;
845
- }
846
-
847
- const {
848
- isTop,
849
- isBottom,
850
- isLeft,
851
- isRight
852
- } = getScrollPosition(scrollContainer);
853
- const direction = {
854
- x: 0,
855
- y: 0
856
- };
857
- const speed = {
858
- x: 0,
859
- y: 0
860
- };
861
- const threshold = {
862
- height: scrollContainerRect.height * thresholdPercentage.y,
863
- width: scrollContainerRect.width * thresholdPercentage.x
864
- };
865
-
866
- if (!isTop && top <= scrollContainerRect.top + threshold.height) {
867
- // Scroll Up
868
- direction.y = Direction.Backward;
869
- speed.y = acceleration * Math.abs((scrollContainerRect.top + threshold.height - top) / threshold.height);
870
- } else if (!isBottom && bottom >= scrollContainerRect.bottom - threshold.height) {
871
- // Scroll Down
872
- direction.y = Direction.Forward;
873
- speed.y = acceleration * Math.abs((scrollContainerRect.bottom - threshold.height - bottom) / threshold.height);
874
- }
875
-
876
- if (!isRight && right >= scrollContainerRect.right - threshold.width) {
877
- // Scroll Right
878
- direction.x = Direction.Forward;
879
- speed.x = acceleration * Math.abs((scrollContainerRect.right - threshold.width - right) / threshold.width);
880
- } else if (!isLeft && left <= scrollContainerRect.left + threshold.width) {
881
- // Scroll Left
882
- direction.x = Direction.Backward;
883
- speed.x = acceleration * Math.abs((scrollContainerRect.left + threshold.width - left) / threshold.width);
884
- }
885
-
886
- return {
887
- direction,
888
- speed
889
- };
890
- }
891
-
892
- function getScrollElementRect(element) {
893
- if (element === document.scrollingElement) {
894
- const {
895
- innerWidth,
896
- innerHeight
897
- } = window;
898
- return {
899
- top: 0,
900
- left: 0,
901
- right: innerWidth,
902
- bottom: innerHeight,
903
- width: innerWidth,
904
- height: innerHeight
905
- };
906
- }
907
-
908
- const {
909
- top,
910
- left,
911
- right,
912
- bottom
913
- } = element.getBoundingClientRect();
914
- return {
915
- top,
916
- left,
917
- right,
918
- bottom,
919
- width: element.clientWidth,
920
- height: element.clientHeight
921
- };
922
- }
923
-
924
- function getScrollOffsets(scrollableAncestors) {
925
- return scrollableAncestors.reduce((acc, node) => {
926
- return add(acc, getScrollCoordinates(node));
927
- }, defaultCoordinates);
928
- }
929
- function getScrollXOffset(scrollableAncestors) {
930
- return scrollableAncestors.reduce((acc, node) => {
931
- return acc + getScrollXCoordinate(node);
932
- }, 0);
933
- }
934
- function getScrollYOffset(scrollableAncestors) {
935
- return scrollableAncestors.reduce((acc, node) => {
936
- return acc + getScrollYCoordinate(node);
937
- }, 0);
938
- }
939
-
940
- function scrollIntoViewIfNeeded(element, measure) {
941
- if (measure === void 0) {
942
- measure = getClientRect;
943
- }
944
-
945
- if (!element) {
946
- return;
947
- }
948
-
949
- const {
950
- top,
951
- left,
952
- bottom,
953
- right
954
- } = measure(element);
955
- const firstScrollableAncestor = getFirstScrollableAncestor(element);
956
-
957
- if (!firstScrollableAncestor) {
958
- return;
959
- }
960
-
961
- if (bottom <= 0 || right <= 0 || top >= window.innerHeight || left >= window.innerWidth) {
962
- element.scrollIntoView({
963
- block: 'center',
964
- inline: 'center'
965
- });
966
- }
967
- }
968
-
969
- const properties = [['x', ['left', 'right'], getScrollXOffset], ['y', ['top', 'bottom'], getScrollYOffset]];
970
- class Rect {
971
- constructor(rect, element) {
972
- this.rect = void 0;
973
- this.width = void 0;
974
- this.height = void 0;
975
- this.top = void 0;
976
- this.bottom = void 0;
977
- this.right = void 0;
978
- this.left = void 0;
979
- const scrollableAncestors = getScrollableAncestors(element);
980
- const scrollOffsets = getScrollOffsets(scrollableAncestors);
981
- this.rect = { ...rect
982
- };
983
- this.width = rect.width;
984
- this.height = rect.height;
985
-
986
- for (const [axis, keys, getScrollOffset] of properties) {
987
- for (const key of keys) {
988
- Object.defineProperty(this, key, {
989
- get: () => {
990
- const currentOffsets = getScrollOffset(scrollableAncestors);
991
- const scrollOffsetsDeltla = scrollOffsets[axis] - currentOffsets;
992
- return this.rect[key] + scrollOffsetsDeltla;
993
- },
994
- enumerable: true
995
- });
996
- }
997
- }
998
-
999
- Object.defineProperty(this, 'rect', {
1000
- enumerable: false
1001
- });
1002
- }
1003
-
1004
- }
1005
-
1006
- class Listeners {
1007
- constructor(target) {
1008
- this.target = void 0;
1009
- this.listeners = [];
1010
-
1011
- this.removeAll = () => {
1012
- this.listeners.forEach(listener => {
1013
- var _this$target;
1014
-
1015
- return (_this$target = this.target) == null ? void 0 : _this$target.removeEventListener(...listener);
1016
- });
1017
- };
1018
-
1019
- this.target = target;
1020
- }
1021
-
1022
- add(eventName, handler, options) {
1023
- var _this$target2;
1024
-
1025
- (_this$target2 = this.target) == null ? void 0 : _this$target2.addEventListener(eventName, handler, options);
1026
- this.listeners.push([eventName, handler, options]);
1027
- }
1028
-
1029
- }
1030
-
1031
- function getEventListenerTarget(target) {
1032
- // If the `event.target` element is removed from the document events will still be targeted
1033
- // at it, and hence won't always bubble up to the window or document anymore.
1034
- // If there is any risk of an element being removed while it is being dragged,
1035
- // the best practice is to attach the event listeners directly to the target.
1036
- // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
1037
- const {
1038
- EventTarget
1039
- } = getWindow(target);
1040
- return target instanceof EventTarget ? target : getOwnerDocument(target);
1041
- }
1042
-
1043
- function hasExceededDistance(delta, measurement) {
1044
- const dx = Math.abs(delta.x);
1045
- const dy = Math.abs(delta.y);
1046
-
1047
- if (typeof measurement === 'number') {
1048
- return Math.sqrt(dx ** 2 + dy ** 2) > measurement;
1049
- }
1050
-
1051
- if ('x' in measurement && 'y' in measurement) {
1052
- return dx > measurement.x && dy > measurement.y;
1053
- }
1054
-
1055
- if ('x' in measurement) {
1056
- return dx > measurement.x;
1057
- }
1058
-
1059
- if ('y' in measurement) {
1060
- return dy > measurement.y;
1061
- }
1062
-
1063
- return false;
1064
- }
1065
-
1066
- var EventName;
1067
-
1068
- (function (EventName) {
1069
- EventName["Click"] = "click";
1070
- EventName["DragStart"] = "dragstart";
1071
- EventName["Keydown"] = "keydown";
1072
- EventName["ContextMenu"] = "contextmenu";
1073
- EventName["Resize"] = "resize";
1074
- EventName["SelectionChange"] = "selectionchange";
1075
- EventName["VisibilityChange"] = "visibilitychange";
1076
- })(EventName || (EventName = {}));
1077
-
1078
- function preventDefault(event) {
1079
- event.preventDefault();
1080
- }
1081
- function stopPropagation(event) {
1082
- event.stopPropagation();
1083
- }
1084
-
1085
- var KeyboardCode;
1086
-
1087
- (function (KeyboardCode) {
1088
- KeyboardCode["Space"] = "Space";
1089
- KeyboardCode["Down"] = "ArrowDown";
1090
- KeyboardCode["Right"] = "ArrowRight";
1091
- KeyboardCode["Left"] = "ArrowLeft";
1092
- KeyboardCode["Up"] = "ArrowUp";
1093
- KeyboardCode["Esc"] = "Escape";
1094
- KeyboardCode["Enter"] = "Enter";
1095
- })(KeyboardCode || (KeyboardCode = {}));
1096
-
1097
- const defaultKeyboardCodes = {
1098
- start: [KeyboardCode.Space, KeyboardCode.Enter],
1099
- cancel: [KeyboardCode.Esc],
1100
- end: [KeyboardCode.Space, KeyboardCode.Enter]
1101
- };
1102
- const defaultKeyboardCoordinateGetter = (event, _ref) => {
1103
- let {
1104
- currentCoordinates
1105
- } = _ref;
1106
-
1107
- switch (event.code) {
1108
- case KeyboardCode.Right:
1109
- return { ...currentCoordinates,
1110
- x: currentCoordinates.x + 25
1111
- };
1112
-
1113
- case KeyboardCode.Left:
1114
- return { ...currentCoordinates,
1115
- x: currentCoordinates.x - 25
1116
- };
1117
-
1118
- case KeyboardCode.Down:
1119
- return { ...currentCoordinates,
1120
- y: currentCoordinates.y + 25
1121
- };
1122
-
1123
- case KeyboardCode.Up:
1124
- return { ...currentCoordinates,
1125
- y: currentCoordinates.y - 25
1126
- };
1127
- }
1128
-
1129
- return undefined;
1130
- };
1131
-
1132
- class KeyboardSensor {
1133
- constructor(props) {
1134
- this.props = void 0;
1135
- this.autoScrollEnabled = false;
1136
- this.referenceCoordinates = void 0;
1137
- this.listeners = void 0;
1138
- this.windowListeners = void 0;
1139
- this.props = props;
1140
- const {
1141
- event: {
1142
- target
1143
- }
1144
- } = props;
1145
- this.props = props;
1146
- this.listeners = new Listeners(getOwnerDocument(target));
1147
- this.windowListeners = new Listeners(getWindow(target));
1148
- this.handleKeyDown = this.handleKeyDown.bind(this);
1149
- this.handleCancel = this.handleCancel.bind(this);
1150
- this.attach();
1151
- }
1152
-
1153
- attach() {
1154
- this.handleStart();
1155
- this.windowListeners.add(EventName.Resize, this.handleCancel);
1156
- this.windowListeners.add(EventName.VisibilityChange, this.handleCancel);
1157
- setTimeout(() => this.listeners.add(EventName.Keydown, this.handleKeyDown));
1158
- }
1159
-
1160
- handleStart() {
1161
- const {
1162
- activeNode,
1163
- onStart
1164
- } = this.props;
1165
- const node = activeNode.node.current;
1166
-
1167
- if (node) {
1168
- scrollIntoViewIfNeeded(node);
1169
- }
1170
-
1171
- onStart(defaultCoordinates);
1172
- }
1173
-
1174
- handleKeyDown(event) {
1175
- if (isKeyboardEvent(event)) {
1176
- const {
1177
- active,
1178
- context,
1179
- options
1180
- } = this.props;
1181
- const {
1182
- keyboardCodes = defaultKeyboardCodes,
1183
- coordinateGetter = defaultKeyboardCoordinateGetter,
1184
- scrollBehavior = 'smooth'
1185
- } = options;
1186
- const {
1187
- code
1188
- } = event;
1189
-
1190
- if (keyboardCodes.end.includes(code)) {
1191
- this.handleEnd(event);
1192
- return;
1193
- }
1194
-
1195
- if (keyboardCodes.cancel.includes(code)) {
1196
- this.handleCancel(event);
1197
- return;
1198
- }
1199
-
1200
- const {
1201
- collisionRect
1202
- } = context.current;
1203
- const currentCoordinates = collisionRect ? {
1204
- x: collisionRect.left,
1205
- y: collisionRect.top
1206
- } : defaultCoordinates;
1207
-
1208
- if (!this.referenceCoordinates) {
1209
- this.referenceCoordinates = currentCoordinates;
1210
- }
1211
-
1212
- const newCoordinates = coordinateGetter(event, {
1213
- active,
1214
- context: context.current,
1215
- currentCoordinates
1216
- });
1217
-
1218
- if (newCoordinates) {
1219
- const coordinatesDelta = subtract(newCoordinates, currentCoordinates);
1220
- const scrollDelta = {
1221
- x: 0,
1222
- y: 0
1223
- };
1224
- const {
1225
- scrollableAncestors
1226
- } = context.current;
1227
-
1228
- for (const scrollContainer of scrollableAncestors) {
1229
- const direction = event.code;
1230
- const {
1231
- isTop,
1232
- isRight,
1233
- isLeft,
1234
- isBottom,
1235
- maxScroll,
1236
- minScroll
1237
- } = getScrollPosition(scrollContainer);
1238
- const scrollElementRect = getScrollElementRect(scrollContainer);
1239
- const clampedCoordinates = {
1240
- x: Math.min(direction === KeyboardCode.Right ? scrollElementRect.right - scrollElementRect.width / 2 : scrollElementRect.right, Math.max(direction === KeyboardCode.Right ? scrollElementRect.left : scrollElementRect.left + scrollElementRect.width / 2, newCoordinates.x)),
1241
- y: Math.min(direction === KeyboardCode.Down ? scrollElementRect.bottom - scrollElementRect.height / 2 : scrollElementRect.bottom, Math.max(direction === KeyboardCode.Down ? scrollElementRect.top : scrollElementRect.top + scrollElementRect.height / 2, newCoordinates.y))
1242
- };
1243
- const canScrollX = direction === KeyboardCode.Right && !isRight || direction === KeyboardCode.Left && !isLeft;
1244
- const canScrollY = direction === KeyboardCode.Down && !isBottom || direction === KeyboardCode.Up && !isTop;
1245
-
1246
- if (canScrollX && clampedCoordinates.x !== newCoordinates.x) {
1247
- const newScrollCoordinates = scrollContainer.scrollLeft + coordinatesDelta.x;
1248
- const canScrollToNewCoordinates = direction === KeyboardCode.Right && newScrollCoordinates <= maxScroll.x || direction === KeyboardCode.Left && newScrollCoordinates >= minScroll.x;
1249
-
1250
- if (canScrollToNewCoordinates && !coordinatesDelta.y) {
1251
- // We don't need to update coordinates, the scroll adjustment alone will trigger
1252
- // logic to auto-detect the new container we are over
1253
- scrollContainer.scrollTo({
1254
- left: newScrollCoordinates,
1255
- behavior: scrollBehavior
1256
- });
1257
- return;
1258
- }
1259
-
1260
- if (canScrollToNewCoordinates) {
1261
- scrollDelta.x = scrollContainer.scrollLeft - newScrollCoordinates;
1262
- } else {
1263
- scrollDelta.x = direction === KeyboardCode.Right ? scrollContainer.scrollLeft - maxScroll.x : scrollContainer.scrollLeft - minScroll.x;
1264
- }
1265
-
1266
- if (scrollDelta.x) {
1267
- scrollContainer.scrollBy({
1268
- left: -scrollDelta.x,
1269
- behavior: scrollBehavior
1270
- });
1271
- }
1272
-
1273
- break;
1274
- } else if (canScrollY && clampedCoordinates.y !== newCoordinates.y) {
1275
- const newScrollCoordinates = scrollContainer.scrollTop + coordinatesDelta.y;
1276
- const canScrollToNewCoordinates = direction === KeyboardCode.Down && newScrollCoordinates <= maxScroll.y || direction === KeyboardCode.Up && newScrollCoordinates >= minScroll.y;
1277
-
1278
- if (canScrollToNewCoordinates && !coordinatesDelta.x) {
1279
- // We don't need to update coordinates, the scroll adjustment alone will trigger
1280
- // logic to auto-detect the new container we are over
1281
- scrollContainer.scrollTo({
1282
- top: newScrollCoordinates,
1283
- behavior: scrollBehavior
1284
- });
1285
- return;
1286
- }
1287
-
1288
- if (canScrollToNewCoordinates) {
1289
- scrollDelta.y = scrollContainer.scrollTop - newScrollCoordinates;
1290
- } else {
1291
- scrollDelta.y = direction === KeyboardCode.Down ? scrollContainer.scrollTop - maxScroll.y : scrollContainer.scrollTop - minScroll.y;
1292
- }
1293
-
1294
- if (scrollDelta.y) {
1295
- scrollContainer.scrollBy({
1296
- top: -scrollDelta.y,
1297
- behavior: scrollBehavior
1298
- });
1299
- }
1300
-
1301
- break;
1302
- }
1303
- }
1304
-
1305
- this.handleMove(event, add(subtract(newCoordinates, this.referenceCoordinates), scrollDelta));
1306
- }
1307
- }
1308
- }
1309
-
1310
- handleMove(event, coordinates) {
1311
- const {
1312
- onMove
1313
- } = this.props;
1314
- event.preventDefault();
1315
- onMove(coordinates);
1316
- }
1317
-
1318
- handleEnd(event) {
1319
- const {
1320
- onEnd
1321
- } = this.props;
1322
- event.preventDefault();
1323
- this.detach();
1324
- onEnd();
1325
- }
1326
-
1327
- handleCancel(event) {
1328
- const {
1329
- onCancel
1330
- } = this.props;
1331
- event.preventDefault();
1332
- this.detach();
1333
- onCancel();
1334
- }
1335
-
1336
- detach() {
1337
- this.listeners.removeAll();
1338
- this.windowListeners.removeAll();
1339
- }
1340
-
1341
- }
1342
- KeyboardSensor.activators = [{
1343
- eventName: 'onKeyDown',
1344
- handler: (event, _ref, _ref2) => {
1345
- let {
1346
- keyboardCodes = defaultKeyboardCodes,
1347
- onActivation
1348
- } = _ref;
1349
- let {
1350
- active
1351
- } = _ref2;
1352
- const {
1353
- code
1354
- } = event.nativeEvent;
1355
-
1356
- if (keyboardCodes.start.includes(code)) {
1357
- const activator = active.activatorNode.current;
1358
-
1359
- if (activator && event.target !== activator) {
1360
- return false;
1361
- }
1362
-
1363
- event.preventDefault();
1364
- onActivation == null ? void 0 : onActivation({
1365
- event: event.nativeEvent
1366
- });
1367
- return true;
1368
- }
1369
-
1370
- return false;
1371
- }
1372
- }];
1373
-
1374
- function isDistanceConstraint(constraint) {
1375
- return Boolean(constraint && 'distance' in constraint);
1376
- }
1377
-
1378
- function isDelayConstraint(constraint) {
1379
- return Boolean(constraint && 'delay' in constraint);
1380
- }
1381
-
1382
- class AbstractPointerSensor {
1383
- constructor(props, events, listenerTarget) {
1384
- var _getEventCoordinates;
1385
-
1386
- if (listenerTarget === void 0) {
1387
- listenerTarget = getEventListenerTarget(props.event.target);
1388
- }
1389
-
1390
- this.props = void 0;
1391
- this.events = void 0;
1392
- this.autoScrollEnabled = true;
1393
- this.document = void 0;
1394
- this.activated = false;
1395
- this.initialCoordinates = void 0;
1396
- this.timeoutId = null;
1397
- this.listeners = void 0;
1398
- this.documentListeners = void 0;
1399
- this.windowListeners = void 0;
1400
- this.props = props;
1401
- this.events = events;
1402
- const {
1403
- event
1404
- } = props;
1405
- const {
1406
- target
1407
- } = event;
1408
- this.props = props;
1409
- this.events = events;
1410
- this.document = getOwnerDocument(target);
1411
- this.documentListeners = new Listeners(this.document);
1412
- this.listeners = new Listeners(listenerTarget);
1413
- this.windowListeners = new Listeners(getWindow(target));
1414
- this.initialCoordinates = (_getEventCoordinates = getEventCoordinates(event)) != null ? _getEventCoordinates : defaultCoordinates;
1415
- this.handleStart = this.handleStart.bind(this);
1416
- this.handleMove = this.handleMove.bind(this);
1417
- this.handleEnd = this.handleEnd.bind(this);
1418
- this.handleCancel = this.handleCancel.bind(this);
1419
- this.handleKeydown = this.handleKeydown.bind(this);
1420
- this.removeTextSelection = this.removeTextSelection.bind(this);
1421
- this.attach();
1422
- }
1423
-
1424
- attach() {
1425
- const {
1426
- events,
1427
- props: {
1428
- options: {
1429
- activationConstraint
1430
- }
1431
- }
1432
- } = this;
1433
- this.listeners.add(events.move.name, this.handleMove, {
1434
- passive: false
1435
- });
1436
- this.listeners.add(events.end.name, this.handleEnd);
1437
- this.windowListeners.add(EventName.Resize, this.handleCancel);
1438
- this.windowListeners.add(EventName.DragStart, preventDefault);
1439
- this.windowListeners.add(EventName.VisibilityChange, this.handleCancel);
1440
- this.windowListeners.add(EventName.ContextMenu, preventDefault);
1441
- this.documentListeners.add(EventName.Keydown, this.handleKeydown);
1442
-
1443
- if (activationConstraint) {
1444
- if (isDistanceConstraint(activationConstraint)) {
1445
- return;
1446
- }
1447
-
1448
- if (isDelayConstraint(activationConstraint)) {
1449
- this.timeoutId = setTimeout(this.handleStart, activationConstraint.delay);
1450
- return;
1451
- }
1452
- }
1453
-
1454
- this.handleStart();
1455
- }
1456
-
1457
- detach() {
1458
- this.listeners.removeAll();
1459
- this.windowListeners.removeAll(); // Wait until the next event loop before removing document listeners
1460
- // This is necessary because we listen for `click` and `selection` events on the document
1461
-
1462
- setTimeout(this.documentListeners.removeAll, 50);
1463
-
1464
- if (this.timeoutId !== null) {
1465
- clearTimeout(this.timeoutId);
1466
- this.timeoutId = null;
1467
- }
1468
- }
1469
-
1470
- handleStart() {
1471
- const {
1472
- initialCoordinates
1473
- } = this;
1474
- const {
1475
- onStart
1476
- } = this.props;
1477
-
1478
- if (initialCoordinates) {
1479
- this.activated = true; // Stop propagation of click events once activation constraints are met
1480
-
1481
- this.documentListeners.add(EventName.Click, stopPropagation, {
1482
- capture: true
1483
- }); // Remove any text selection from the document
1484
-
1485
- this.removeTextSelection(); // Prevent further text selection while dragging
1486
-
1487
- this.documentListeners.add(EventName.SelectionChange, this.removeTextSelection);
1488
- onStart(initialCoordinates);
1489
- }
1490
- }
1491
-
1492
- handleMove(event) {
1493
- var _getEventCoordinates2;
1494
-
1495
- const {
1496
- activated,
1497
- initialCoordinates,
1498
- props
1499
- } = this;
1500
- const {
1501
- onMove,
1502
- options: {
1503
- activationConstraint
1504
- }
1505
- } = props;
1506
-
1507
- if (!initialCoordinates) {
1508
- return;
1509
- }
1510
-
1511
- const coordinates = (_getEventCoordinates2 = getEventCoordinates(event)) != null ? _getEventCoordinates2 : defaultCoordinates;
1512
- const delta = subtract(initialCoordinates, coordinates);
1513
-
1514
- if (!activated && activationConstraint) {
1515
- // Constraint validation
1516
- if (isDelayConstraint(activationConstraint)) {
1517
- if (hasExceededDistance(delta, activationConstraint.tolerance)) {
1518
- return this.handleCancel();
1519
- }
1520
-
1521
- return;
1522
- }
1523
-
1524
- if (isDistanceConstraint(activationConstraint)) {
1525
- if (activationConstraint.tolerance != null && hasExceededDistance(delta, activationConstraint.tolerance)) {
1526
- return this.handleCancel();
1527
- }
1528
-
1529
- if (hasExceededDistance(delta, activationConstraint.distance)) {
1530
- return this.handleStart();
1531
- }
1532
-
1533
- return;
1534
- }
1535
- }
1536
-
1537
- if (event.cancelable) {
1538
- event.preventDefault();
1539
- }
1540
-
1541
- onMove(coordinates);
1542
- }
1543
-
1544
- handleEnd() {
1545
- const {
1546
- onEnd
1547
- } = this.props;
1548
- this.detach();
1549
- onEnd();
1550
- }
1551
-
1552
- handleCancel() {
1553
- const {
1554
- onCancel
1555
- } = this.props;
1556
- this.detach();
1557
- onCancel();
1558
- }
1559
-
1560
- handleKeydown(event) {
1561
- if (event.code === KeyboardCode.Esc) {
1562
- this.handleCancel();
1563
- }
1564
- }
1565
-
1566
- removeTextSelection() {
1567
- var _this$document$getSel;
1568
-
1569
- (_this$document$getSel = this.document.getSelection()) == null ? void 0 : _this$document$getSel.removeAllRanges();
1570
- }
1571
-
1572
- }
1573
-
1574
- const events = {
1575
- move: {
1576
- name: 'pointermove'
1577
- },
1578
- end: {
1579
- name: 'pointerup'
1580
- }
1581
- };
1582
- class PointerSensor extends AbstractPointerSensor {
1583
- constructor(props) {
1584
- const {
1585
- event
1586
- } = props; // Pointer events stop firing if the target is unmounted while dragging
1587
- // Therefore we attach listeners to the owner document instead
1588
-
1589
- const listenerTarget = getOwnerDocument(event.target);
1590
- super(props, events, listenerTarget);
1591
- }
1592
-
1593
- }
1594
- PointerSensor.activators = [{
1595
- eventName: 'onPointerDown',
1596
- handler: (_ref, _ref2) => {
1597
- let {
1598
- nativeEvent: event
1599
- } = _ref;
1600
- let {
1601
- onActivation
1602
- } = _ref2;
1603
-
1604
- if (!event.isPrimary || event.button !== 0) {
1605
- return false;
1606
- }
1607
-
1608
- onActivation == null ? void 0 : onActivation({
1609
- event
1610
- });
1611
- return true;
1612
- }
1613
- }];
1614
-
1615
- const events$1 = {
1616
- move: {
1617
- name: 'mousemove'
1618
- },
1619
- end: {
1620
- name: 'mouseup'
1621
- }
1622
- };
1623
- var MouseButton;
1624
-
1625
- (function (MouseButton) {
1626
- MouseButton[MouseButton["RightClick"] = 2] = "RightClick";
1627
- })(MouseButton || (MouseButton = {}));
1628
-
1629
- class MouseSensor extends AbstractPointerSensor {
1630
- constructor(props) {
1631
- super(props, events$1, getOwnerDocument(props.event.target));
1632
- }
1633
-
1634
- }
1635
- MouseSensor.activators = [{
1636
- eventName: 'onMouseDown',
1637
- handler: (_ref, _ref2) => {
1638
- let {
1639
- nativeEvent: event
1640
- } = _ref;
1641
- let {
1642
- onActivation
1643
- } = _ref2;
1644
-
1645
- if (event.button === MouseButton.RightClick) {
1646
- return false;
1647
- }
1648
-
1649
- onActivation == null ? void 0 : onActivation({
1650
- event
1651
- });
1652
- return true;
1653
- }
1654
- }];
1655
-
1656
- const events$2 = {
1657
- move: {
1658
- name: 'touchmove'
1659
- },
1660
- end: {
1661
- name: 'touchend'
1662
- }
1663
- };
1664
- class TouchSensor extends AbstractPointerSensor {
1665
- constructor(props) {
1666
- super(props, events$2);
1667
- }
1668
-
1669
- static setup() {
1670
- // Adding a non-capture and non-passive `touchmove` listener in order
1671
- // to force `event.preventDefault()` calls to work in dynamically added
1672
- // touchmove event handlers. This is required for iOS Safari.
1673
- window.addEventListener(events$2.move.name, noop, {
1674
- capture: false,
1675
- passive: false
1676
- });
1677
- return function teardown() {
1678
- window.removeEventListener(events$2.move.name, noop);
1679
- }; // We create a new handler because the teardown function of another sensor
1680
- // could remove our event listener if we use a referentially equal listener.
1681
-
1682
- function noop() {}
1683
- }
1684
-
1685
- }
1686
- TouchSensor.activators = [{
1687
- eventName: 'onTouchStart',
1688
- handler: (_ref, _ref2) => {
1689
- let {
1690
- nativeEvent: event
1691
- } = _ref;
1692
- let {
1693
- onActivation
1694
- } = _ref2;
1695
- const {
1696
- touches
1697
- } = event;
1698
-
1699
- if (touches.length > 1) {
1700
- return false;
1701
- }
1702
-
1703
- onActivation == null ? void 0 : onActivation({
1704
- event
1705
- });
1706
- return true;
1707
- }
1708
- }];
1709
-
1710
- var AutoScrollActivator;
1711
-
1712
- (function (AutoScrollActivator) {
1713
- AutoScrollActivator[AutoScrollActivator["Pointer"] = 0] = "Pointer";
1714
- AutoScrollActivator[AutoScrollActivator["DraggableRect"] = 1] = "DraggableRect";
1715
- })(AutoScrollActivator || (AutoScrollActivator = {}));
1716
-
1717
- var TraversalOrder;
1718
-
1719
- (function (TraversalOrder) {
1720
- TraversalOrder[TraversalOrder["TreeOrder"] = 0] = "TreeOrder";
1721
- TraversalOrder[TraversalOrder["ReversedTreeOrder"] = 1] = "ReversedTreeOrder";
1722
- })(TraversalOrder || (TraversalOrder = {}));
1723
-
1724
- function useAutoScroller(_ref) {
1725
- let {
1726
- acceleration,
1727
- activator = AutoScrollActivator.Pointer,
1728
- canScroll,
1729
- draggingRect,
1730
- enabled,
1731
- interval = 5,
1732
- order = TraversalOrder.TreeOrder,
1733
- pointerCoordinates,
1734
- scrollableAncestors,
1735
- scrollableAncestorRects,
1736
- delta,
1737
- threshold
1738
- } = _ref;
1739
- const scrollIntent = useScrollIntent({
1740
- delta,
1741
- disabled: !enabled
1742
- });
1743
- const [setAutoScrollInterval, clearAutoScrollInterval] = useInterval();
1744
- const scrollSpeed = useRef({
1745
- x: 0,
1746
- y: 0
1747
- });
1748
- const scrollDirection = useRef({
1749
- x: 0,
1750
- y: 0
1751
- });
1752
- const rect = useMemo(() => {
1753
- switch (activator) {
1754
- case AutoScrollActivator.Pointer:
1755
- return pointerCoordinates ? {
1756
- top: pointerCoordinates.y,
1757
- bottom: pointerCoordinates.y,
1758
- left: pointerCoordinates.x,
1759
- right: pointerCoordinates.x
1760
- } : null;
1761
-
1762
- case AutoScrollActivator.DraggableRect:
1763
- return draggingRect;
1764
- }
1765
- }, [activator, draggingRect, pointerCoordinates]);
1766
- const scrollContainerRef = useRef(null);
1767
- const autoScroll = useCallback(() => {
1768
- const scrollContainer = scrollContainerRef.current;
1769
-
1770
- if (!scrollContainer) {
1771
- return;
1772
- }
1773
-
1774
- const scrollLeft = scrollSpeed.current.x * scrollDirection.current.x;
1775
- const scrollTop = scrollSpeed.current.y * scrollDirection.current.y;
1776
- scrollContainer.scrollBy(Math.round(scrollLeft), Math.round(scrollTop));
1777
- }, []);
1778
- const sortedScrollableAncestors = useMemo(() => order === TraversalOrder.TreeOrder ? [...scrollableAncestors].reverse() : scrollableAncestors, [order, scrollableAncestors]);
1779
- useEffect(() => {
1780
- if (!enabled || !scrollableAncestors.length || !rect) {
1781
- clearAutoScrollInterval();
1782
- return;
1783
- }
1784
-
1785
- for (const scrollContainer of sortedScrollableAncestors) {
1786
- var _scrollableAncestorRe;
1787
-
1788
- if ((canScroll == null ? void 0 : canScroll(scrollContainer)) === false) {
1789
- continue;
1790
- }
1791
-
1792
- const scrollContainerRect = (_scrollableAncestorRe = scrollableAncestorRects.find(_ref2 => {
1793
- let {
1794
- element
1795
- } = _ref2;
1796
- return element === scrollContainer;
1797
- })) == null ? void 0 : _scrollableAncestorRe.rect;
1798
-
1799
- if (!scrollContainerRect) {
1800
- continue;
1801
- }
1802
-
1803
- const {
1804
- direction,
1805
- speed
1806
- } = getScrollDirectionAndSpeed(scrollContainer, scrollContainerRect, rect, acceleration, threshold);
1807
-
1808
- for (const axis of ['x', 'y']) {
1809
- if (!scrollIntent[axis][direction[axis]]) {
1810
- speed[axis] = 0;
1811
- direction[axis] = 0;
1812
- }
1813
- }
1814
-
1815
- if (speed.x > 0 || speed.y > 0) {
1816
- clearAutoScrollInterval();
1817
- scrollContainerRef.current = scrollContainer;
1818
- setAutoScrollInterval(autoScroll, interval);
1819
- scrollSpeed.current = speed;
1820
- scrollDirection.current = direction;
1821
- return;
1822
- }
1823
- }
1824
-
1825
- scrollSpeed.current = {
1826
- x: 0,
1827
- y: 0
1828
- };
1829
- scrollDirection.current = {
1830
- x: 0,
1831
- y: 0
1832
- };
1833
- clearAutoScrollInterval();
1834
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
1835
- [acceleration, autoScroll, canScroll, clearAutoScrollInterval, enabled, interval, // eslint-disable-next-line react-hooks/exhaustive-deps
1836
- JSON.stringify(rect), // eslint-disable-next-line react-hooks/exhaustive-deps
1837
- JSON.stringify(scrollIntent), setAutoScrollInterval, scrollableAncestors, sortedScrollableAncestors, scrollableAncestorRects, // eslint-disable-next-line react-hooks/exhaustive-deps
1838
- JSON.stringify(threshold)]);
1839
- }
1840
- const defaultScrollIntent = {
1841
- x: {
1842
- [Direction.Backward]: false,
1843
- [Direction.Forward]: false
1844
- },
1845
- y: {
1846
- [Direction.Backward]: false,
1847
- [Direction.Forward]: false
1848
- }
1849
- };
1850
-
1851
- function useScrollIntent(_ref3) {
1852
- let {
1853
- delta,
1854
- disabled
1855
- } = _ref3;
1856
- const previousDelta = usePrevious(delta);
1857
- return useLazyMemo(previousIntent => {
1858
- if (disabled || !previousDelta || !previousIntent) {
1859
- // Reset scroll intent tracking when auto-scrolling is disabled
1860
- return defaultScrollIntent;
1861
- }
1862
-
1863
- const direction = {
1864
- x: Math.sign(delta.x - previousDelta.x),
1865
- y: Math.sign(delta.y - previousDelta.y)
1866
- }; // Keep track of the user intent to scroll in each direction for both axis
1867
-
1868
- return {
1869
- x: {
1870
- [Direction.Backward]: previousIntent.x[Direction.Backward] || direction.x === -1,
1871
- [Direction.Forward]: previousIntent.x[Direction.Forward] || direction.x === 1
1872
- },
1873
- y: {
1874
- [Direction.Backward]: previousIntent.y[Direction.Backward] || direction.y === -1,
1875
- [Direction.Forward]: previousIntent.y[Direction.Forward] || direction.y === 1
1876
- }
1877
- };
1878
- }, [disabled, delta, previousDelta]);
1879
- }
1880
-
1881
- function useCachedNode(draggableNodes, id) {
1882
- const draggableNode = id !== null ? draggableNodes.get(id) : undefined;
1883
- const node = draggableNode ? draggableNode.node.current : null;
1884
- return useLazyMemo(cachedNode => {
1885
- var _ref;
1886
-
1887
- if (id === null) {
1888
- return null;
1889
- } // In some cases, the draggable node can unmount while dragging
1890
- // This is the case for virtualized lists. In those situations,
1891
- // we fall back to the last known value for that node.
1892
-
1893
-
1894
- return (_ref = node != null ? node : cachedNode) != null ? _ref : null;
1895
- }, [node, id]);
1896
- }
1897
-
1898
- function useCombineActivators(sensors, getSyntheticHandler) {
1899
- return useMemo(() => sensors.reduce((accumulator, sensor) => {
1900
- const {
1901
- sensor: Sensor
1902
- } = sensor;
1903
- const sensorActivators = Sensor.activators.map(activator => ({
1904
- eventName: activator.eventName,
1905
- handler: getSyntheticHandler(activator.handler, sensor)
1906
- }));
1907
- return [...accumulator, ...sensorActivators];
1908
- }, []), [sensors, getSyntheticHandler]);
1909
- }
1910
-
1911
- var MeasuringStrategy;
1912
-
1913
- (function (MeasuringStrategy) {
1914
- MeasuringStrategy[MeasuringStrategy["Always"] = 0] = "Always";
1915
- MeasuringStrategy[MeasuringStrategy["BeforeDragging"] = 1] = "BeforeDragging";
1916
- MeasuringStrategy[MeasuringStrategy["WhileDragging"] = 2] = "WhileDragging";
1917
- })(MeasuringStrategy || (MeasuringStrategy = {}));
1918
-
1919
- var MeasuringFrequency;
1920
-
1921
- (function (MeasuringFrequency) {
1922
- MeasuringFrequency["Optimized"] = "optimized";
1923
- })(MeasuringFrequency || (MeasuringFrequency = {}));
1924
-
1925
- const defaultValue = /*#__PURE__*/new Map();
1926
- function useDroppableMeasuring(containers, _ref) {
1927
- let {
1928
- dragging,
1929
- dependencies,
1930
- config
1931
- } = _ref;
1932
- const [queue, setQueue] = useState(null);
1933
- const {
1934
- frequency,
1935
- measure,
1936
- strategy
1937
- } = config;
1938
- const containersRef = useRef(containers);
1939
- const disabled = isDisabled();
1940
- const disabledRef = useLatestValue(disabled);
1941
- const measureDroppableContainers = useCallback(function (ids) {
1942
- if (ids === void 0) {
1943
- ids = [];
1944
- }
1945
-
1946
- if (disabledRef.current) {
1947
- return;
1948
- }
1949
-
1950
- setQueue(value => {
1951
- if (value === null) {
1952
- return ids;
1953
- }
1954
-
1955
- return value.concat(ids.filter(id => !value.includes(id)));
1956
- });
1957
- }, [disabledRef]);
1958
- const timeoutId = useRef(null);
1959
- const droppableRects = useLazyMemo(previousValue => {
1960
- if (disabled && !dragging) {
1961
- return defaultValue;
1962
- }
1963
-
1964
- if (!previousValue || previousValue === defaultValue || containersRef.current !== containers || queue != null) {
1965
- const map = new Map();
1966
-
1967
- for (let container of containers) {
1968
- if (!container) {
1969
- continue;
1970
- }
1971
-
1972
- if (queue && queue.length > 0 && !queue.includes(container.id) && container.rect.current) {
1973
- // This container does not need to be re-measured
1974
- map.set(container.id, container.rect.current);
1975
- continue;
1976
- }
1977
-
1978
- const node = container.node.current;
1979
- const rect = node ? new Rect(measure(node), node) : null;
1980
- container.rect.current = rect;
1981
-
1982
- if (rect) {
1983
- map.set(container.id, rect);
1984
- }
1985
- }
1986
-
1987
- return map;
1988
- }
1989
-
1990
- return previousValue;
1991
- }, [containers, queue, dragging, disabled, measure]);
1992
- useEffect(() => {
1993
- containersRef.current = containers;
1994
- }, [containers]);
1995
- useEffect(() => {
1996
- if (disabled) {
1997
- return;
1998
- }
1999
-
2000
- measureDroppableContainers();
2001
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
2002
- [dragging, disabled]);
2003
- useEffect(() => {
2004
- if (queue && queue.length > 0) {
2005
- setQueue(null);
2006
- }
2007
- }, //eslint-disable-next-line react-hooks/exhaustive-deps
2008
- [JSON.stringify(queue)]);
2009
- useEffect(() => {
2010
- if (disabled || typeof frequency !== 'number' || timeoutId.current !== null) {
2011
- return;
2012
- }
2013
-
2014
- timeoutId.current = setTimeout(() => {
2015
- measureDroppableContainers();
2016
- timeoutId.current = null;
2017
- }, frequency);
2018
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
2019
- [frequency, disabled, measureDroppableContainers, ...dependencies]);
2020
- return {
2021
- droppableRects,
2022
- measureDroppableContainers,
2023
- measuringScheduled: queue != null
2024
- };
2025
-
2026
- function isDisabled() {
2027
- switch (strategy) {
2028
- case MeasuringStrategy.Always:
2029
- return false;
2030
-
2031
- case MeasuringStrategy.BeforeDragging:
2032
- return dragging;
2033
-
2034
- default:
2035
- return !dragging;
2036
- }
2037
- }
2038
- }
2039
-
2040
- function useInitialValue(value, computeFn) {
2041
- return useLazyMemo(previousValue => {
2042
- if (!value) {
2043
- return null;
2044
- }
2045
-
2046
- if (previousValue) {
2047
- return previousValue;
2048
- }
2049
-
2050
- return typeof computeFn === 'function' ? computeFn(value) : value;
2051
- }, [computeFn, value]);
2052
- }
2053
-
2054
- function useInitialRect(node, measure) {
2055
- return useInitialValue(node, measure);
2056
- }
2057
-
2058
- /**
2059
- * Returns a new MutationObserver instance.
2060
- * If `MutationObserver` is undefined in the execution environment, returns `undefined`.
2061
- */
2062
-
2063
- function useMutationObserver(_ref) {
2064
- let {
2065
- callback,
2066
- disabled
2067
- } = _ref;
2068
- const handleMutations = useEvent(callback);
2069
- const mutationObserver = useMemo(() => {
2070
- if (disabled || typeof window === 'undefined' || typeof window.MutationObserver === 'undefined') {
2071
- return undefined;
2072
- }
2073
-
2074
- const {
2075
- MutationObserver
2076
- } = window;
2077
- return new MutationObserver(handleMutations);
2078
- }, [handleMutations, disabled]);
2079
- useEffect(() => {
2080
- return () => mutationObserver == null ? void 0 : mutationObserver.disconnect();
2081
- }, [mutationObserver]);
2082
- return mutationObserver;
2083
- }
2084
-
2085
- /**
2086
- * Returns a new ResizeObserver instance bound to the `onResize` callback.
2087
- * If `ResizeObserver` is undefined in the execution environment, returns `undefined`.
2088
- */
2089
-
2090
- function useResizeObserver(_ref) {
2091
- let {
2092
- callback,
2093
- disabled
2094
- } = _ref;
2095
- const handleResize = useEvent(callback);
2096
- const resizeObserver = useMemo(() => {
2097
- if (disabled || typeof window === 'undefined' || typeof window.ResizeObserver === 'undefined') {
2098
- return undefined;
2099
- }
2100
-
2101
- const {
2102
- ResizeObserver
2103
- } = window;
2104
- return new ResizeObserver(handleResize);
2105
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
2106
- [disabled]);
2107
- useEffect(() => {
2108
- return () => resizeObserver == null ? void 0 : resizeObserver.disconnect();
2109
- }, [resizeObserver]);
2110
- return resizeObserver;
2111
- }
2112
-
2113
- function defaultMeasure(element) {
2114
- return new Rect(getClientRect(element), element);
2115
- }
2116
-
2117
- function useRect(element, measure, fallbackRect) {
2118
- if (measure === void 0) {
2119
- measure = defaultMeasure;
2120
- }
2121
-
2122
- const [rect, measureRect] = useReducer(reducer, null);
2123
- const mutationObserver = useMutationObserver({
2124
- callback(records) {
2125
- if (!element) {
2126
- return;
2127
- }
2128
-
2129
- for (const record of records) {
2130
- const {
2131
- type,
2132
- target
2133
- } = record;
2134
-
2135
- if (type === 'childList' && target instanceof HTMLElement && target.contains(element)) {
2136
- measureRect();
2137
- break;
2138
- }
2139
- }
2140
- }
2141
-
2142
- });
2143
- const resizeObserver = useResizeObserver({
2144
- callback: measureRect
2145
- });
2146
- useIsomorphicLayoutEffect(() => {
2147
- measureRect();
2148
-
2149
- if (element) {
2150
- resizeObserver == null ? void 0 : resizeObserver.observe(element);
2151
- mutationObserver == null ? void 0 : mutationObserver.observe(document.body, {
2152
- childList: true,
2153
- subtree: true
2154
- });
2155
- } else {
2156
- resizeObserver == null ? void 0 : resizeObserver.disconnect();
2157
- mutationObserver == null ? void 0 : mutationObserver.disconnect();
2158
- }
2159
- }, [element]);
2160
- return rect;
2161
-
2162
- function reducer(currentRect) {
2163
- if (!element) {
2164
- return null;
2165
- }
2166
-
2167
- if (element.isConnected === false) {
2168
- var _ref;
2169
-
2170
- // Fall back to last rect we measured if the element is
2171
- // no longer connected to the DOM.
2172
- return (_ref = currentRect != null ? currentRect : fallbackRect) != null ? _ref : null;
2173
- }
2174
-
2175
- const newRect = measure(element);
2176
-
2177
- if (JSON.stringify(currentRect) === JSON.stringify(newRect)) {
2178
- return currentRect;
2179
- }
2180
-
2181
- return newRect;
2182
- }
2183
- }
2184
-
2185
- function useRectDelta(rect) {
2186
- const initialRect = useInitialValue(rect);
2187
- return getRectDelta(rect, initialRect);
2188
- }
2189
-
2190
- const defaultValue$1 = [];
2191
- function useScrollableAncestors(node) {
2192
- const previousNode = useRef(node);
2193
- const ancestors = useLazyMemo(previousValue => {
2194
- if (!node) {
2195
- return defaultValue$1;
2196
- }
2197
-
2198
- if (previousValue && previousValue !== defaultValue$1 && node && previousNode.current && node.parentNode === previousNode.current.parentNode) {
2199
- return previousValue;
2200
- }
2201
-
2202
- return getScrollableAncestors(node);
2203
- }, [node]);
2204
- useEffect(() => {
2205
- previousNode.current = node;
2206
- }, [node]);
2207
- return ancestors;
2208
- }
2209
-
2210
- function useScrollOffsets(elements) {
2211
- const [scrollCoordinates, setScrollCoordinates] = useState(null);
2212
- const prevElements = useRef(elements); // To-do: Throttle the handleScroll callback
2213
-
2214
- const handleScroll = useCallback(event => {
2215
- const scrollingElement = getScrollableElement(event.target);
2216
-
2217
- if (!scrollingElement) {
2218
- return;
2219
- }
2220
-
2221
- setScrollCoordinates(scrollCoordinates => {
2222
- if (!scrollCoordinates) {
2223
- return null;
2224
- }
2225
-
2226
- scrollCoordinates.set(scrollingElement, getScrollCoordinates(scrollingElement));
2227
- return new Map(scrollCoordinates);
2228
- });
2229
- }, []);
2230
- useEffect(() => {
2231
- const previousElements = prevElements.current;
2232
-
2233
- if (elements !== previousElements) {
2234
- cleanup(previousElements);
2235
- const entries = elements.map(element => {
2236
- const scrollableElement = getScrollableElement(element);
2237
-
2238
- if (scrollableElement) {
2239
- scrollableElement.addEventListener('scroll', handleScroll, {
2240
- passive: true
2241
- });
2242
- return [scrollableElement, getScrollCoordinates(scrollableElement)];
2243
- }
2244
-
2245
- return null;
2246
- }).filter(entry => entry != null);
2247
- setScrollCoordinates(entries.length ? new Map(entries) : null);
2248
- prevElements.current = elements;
2249
- }
2250
-
2251
- return () => {
2252
- cleanup(elements);
2253
- cleanup(previousElements);
2254
- };
2255
-
2256
- function cleanup(elements) {
2257
- elements.forEach(element => {
2258
- const scrollableElement = getScrollableElement(element);
2259
- scrollableElement == null ? void 0 : scrollableElement.removeEventListener('scroll', handleScroll);
2260
- });
2261
- }
2262
- }, [handleScroll, elements]);
2263
- return useMemo(() => {
2264
- if (elements.length) {
2265
- return scrollCoordinates ? Array.from(scrollCoordinates.values()).reduce((acc, coordinates) => add(acc, coordinates), defaultCoordinates) : getScrollOffsets(elements);
2266
- }
2267
-
2268
- return defaultCoordinates;
2269
- }, [elements, scrollCoordinates]);
2270
- }
2271
-
2272
- function useScrollOffsetsDelta(scrollOffsets, dependencies) {
2273
- if (dependencies === void 0) {
2274
- dependencies = [];
2275
- }
2276
-
2277
- const initialScrollOffsets = useRef(null);
2278
- useEffect(() => {
2279
- initialScrollOffsets.current = null;
2280
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
2281
- dependencies);
2282
- useEffect(() => {
2283
- const hasScrollOffsets = scrollOffsets !== defaultCoordinates;
2284
-
2285
- if (hasScrollOffsets && !initialScrollOffsets.current) {
2286
- initialScrollOffsets.current = scrollOffsets;
2287
- }
2288
-
2289
- if (!hasScrollOffsets && initialScrollOffsets.current) {
2290
- initialScrollOffsets.current = null;
2291
- }
2292
- }, [scrollOffsets]);
2293
- return initialScrollOffsets.current ? subtract(scrollOffsets, initialScrollOffsets.current) : defaultCoordinates;
2294
- }
2295
-
2296
- function useSensorSetup(sensors) {
2297
- useEffect(() => {
2298
- if (!canUseDOM) {
2299
- return;
2300
- }
2301
-
2302
- const teardownFns = sensors.map(_ref => {
2303
- let {
2304
- sensor
2305
- } = _ref;
2306
- return sensor.setup == null ? void 0 : sensor.setup();
2307
- });
2308
- return () => {
2309
- for (const teardown of teardownFns) {
2310
- teardown == null ? void 0 : teardown();
2311
- }
2312
- };
2313
- }, // TO-DO: Sensors length could theoretically change which would not be a valid dependency
2314
- // eslint-disable-next-line react-hooks/exhaustive-deps
2315
- sensors.map(_ref2 => {
2316
- let {
2317
- sensor
2318
- } = _ref2;
2319
- return sensor;
2320
- }));
2321
- }
2322
-
2323
- function useSyntheticListeners(listeners, id) {
2324
- return useMemo(() => {
2325
- return listeners.reduce((acc, _ref) => {
2326
- let {
2327
- eventName,
2328
- handler
2329
- } = _ref;
2330
-
2331
- acc[eventName] = event => {
2332
- handler(event, id);
2333
- };
2334
-
2335
- return acc;
2336
- }, {});
2337
- }, [listeners, id]);
2338
- }
2339
-
2340
- function useWindowRect(element) {
2341
- return useMemo(() => element ? getWindowClientRect(element) : null, [element]);
2342
- }
2343
-
2344
- const defaultValue$2 = [];
2345
- function useRects(elements, measure) {
2346
- if (measure === void 0) {
2347
- measure = getClientRect;
2348
- }
2349
-
2350
- const [firstElement] = elements;
2351
- const windowRect = useWindowRect(firstElement ? getWindow(firstElement) : null);
2352
- const [rects, measureRects] = useReducer(reducer, defaultValue$2);
2353
- const resizeObserver = useResizeObserver({
2354
- callback: measureRects
2355
- });
2356
-
2357
- if (elements.length > 0 && rects === defaultValue$2) {
2358
- measureRects();
2359
- }
2360
-
2361
- useIsomorphicLayoutEffect(() => {
2362
- if (elements.length) {
2363
- elements.forEach(element => resizeObserver == null ? void 0 : resizeObserver.observe(element));
2364
- } else {
2365
- resizeObserver == null ? void 0 : resizeObserver.disconnect();
2366
- measureRects();
2367
- }
2368
- }, [elements]);
2369
- return rects;
2370
-
2371
- function reducer() {
2372
- if (!elements.length) {
2373
- return defaultValue$2;
2374
- }
2375
-
2376
- return elements.map(element => isDocumentScrollingElement(element) ? {
2377
- rect: windowRect,
2378
- element
2379
- } : {
2380
- rect: new Rect(measure(element), element),
2381
- element
2382
- });
2383
- }
2384
- }
2385
-
2386
- function getMeasurableNode(node) {
2387
- if (!node) {
2388
- return null;
2389
- }
2390
-
2391
- if (node.children.length > 1) {
2392
- return node;
2393
- }
2394
-
2395
- const firstChild = node.children[0];
2396
- return isHTMLElement(firstChild) ? firstChild : node;
2397
- }
2398
-
2399
- function useDragOverlayMeasuring(_ref) {
2400
- let {
2401
- measure
2402
- } = _ref;
2403
- const [rect, setRect] = useState(null);
2404
- const handleResize = useCallback(entries => {
2405
- for (const {
2406
- target
2407
- } of entries) {
2408
- if (isHTMLElement(target)) {
2409
- setRect(rect => {
2410
- const newRect = measure(target);
2411
- return rect ? { ...rect,
2412
- width: newRect.width,
2413
- height: newRect.height
2414
- } : newRect;
2415
- });
2416
- break;
2417
- }
2418
- }
2419
- }, [measure]);
2420
- const resizeObserver = useResizeObserver({
2421
- callback: handleResize
2422
- });
2423
- const handleNodeChange = useCallback(element => {
2424
- const node = getMeasurableNode(element);
2425
- resizeObserver == null ? void 0 : resizeObserver.disconnect();
2426
-
2427
- if (node) {
2428
- resizeObserver == null ? void 0 : resizeObserver.observe(node);
2429
- }
2430
-
2431
- setRect(node ? measure(node) : null);
2432
- }, [measure, resizeObserver]);
2433
- const [nodeRef, setRef] = useNodeRef(handleNodeChange);
2434
- return useMemo(() => ({
2435
- nodeRef,
2436
- rect,
2437
- setRef
2438
- }), [rect, nodeRef, setRef]);
2439
- }
2440
-
2441
- const defaultSensors = [{
2442
- sensor: PointerSensor,
2443
- options: {}
2444
- }, {
2445
- sensor: KeyboardSensor,
2446
- options: {}
2447
- }];
2448
- const defaultData = {
2449
- current: {}
2450
- };
2451
- const defaultMeasuringConfiguration = {
2452
- draggable: {
2453
- measure: getTransformAgnosticClientRect
2454
- },
2455
- droppable: {
2456
- measure: getTransformAgnosticClientRect,
2457
- strategy: MeasuringStrategy.WhileDragging,
2458
- frequency: MeasuringFrequency.Optimized
2459
- },
2460
- dragOverlay: {
2461
- measure: getClientRect
2462
- }
2463
- };
2464
-
2465
- class DroppableContainersMap extends Map {
2466
- get(id) {
2467
- var _super$get;
2468
-
2469
- return id != null ? (_super$get = super.get(id)) != null ? _super$get : undefined : undefined;
2470
- }
2471
-
2472
- toArray() {
2473
- return Array.from(this.values());
2474
- }
2475
-
2476
- getEnabled() {
2477
- return this.toArray().filter(_ref => {
2478
- let {
2479
- disabled
2480
- } = _ref;
2481
- return !disabled;
2482
- });
2483
- }
2484
-
2485
- getNodeFor(id) {
2486
- var _this$get$node$curren, _this$get;
2487
-
2488
- return (_this$get$node$curren = (_this$get = this.get(id)) == null ? void 0 : _this$get.node.current) != null ? _this$get$node$curren : undefined;
2489
- }
2490
-
2491
- }
2492
-
2493
- const defaultPublicContext = {
2494
- activatorEvent: null,
2495
- active: null,
2496
- activeNode: null,
2497
- activeNodeRect: null,
2498
- collisions: null,
2499
- containerNodeRect: null,
2500
- draggableNodes: /*#__PURE__*/new Map(),
2501
- droppableRects: /*#__PURE__*/new Map(),
2502
- droppableContainers: /*#__PURE__*/new DroppableContainersMap(),
2503
- over: null,
2504
- dragOverlay: {
2505
- nodeRef: {
2506
- current: null
2507
- },
2508
- rect: null,
2509
- setRef: noop
2510
- },
2511
- scrollableAncestors: [],
2512
- scrollableAncestorRects: [],
2513
- measuringConfiguration: defaultMeasuringConfiguration,
2514
- measureDroppableContainers: noop,
2515
- windowRect: null,
2516
- measuringScheduled: false
2517
- };
2518
- const defaultInternalContext = {
2519
- activatorEvent: null,
2520
- activators: [],
2521
- active: null,
2522
- activeNodeRect: null,
2523
- ariaDescribedById: {
2524
- draggable: ''
2525
- },
2526
- dispatch: noop,
2527
- draggableNodes: /*#__PURE__*/new Map(),
2528
- over: null,
2529
- measureDroppableContainers: noop
2530
- };
2531
- const InternalContext = /*#__PURE__*/createContext(defaultInternalContext);
2532
- const PublicContext = /*#__PURE__*/createContext(defaultPublicContext);
2533
-
2534
- function getInitialState() {
2535
- return {
2536
- draggable: {
2537
- active: null,
2538
- initialCoordinates: {
2539
- x: 0,
2540
- y: 0
2541
- },
2542
- nodes: new Map(),
2543
- translate: {
2544
- x: 0,
2545
- y: 0
2546
- }
2547
- },
2548
- droppable: {
2549
- containers: new DroppableContainersMap()
2550
- }
2551
- };
2552
- }
2553
- function reducer(state, action) {
2554
- switch (action.type) {
2555
- case Action.DragStart:
2556
- return { ...state,
2557
- draggable: { ...state.draggable,
2558
- initialCoordinates: action.initialCoordinates,
2559
- active: action.active
2560
- }
2561
- };
2562
-
2563
- case Action.DragMove:
2564
- if (!state.draggable.active) {
2565
- return state;
2566
- }
2567
-
2568
- return { ...state,
2569
- draggable: { ...state.draggable,
2570
- translate: {
2571
- x: action.coordinates.x - state.draggable.initialCoordinates.x,
2572
- y: action.coordinates.y - state.draggable.initialCoordinates.y
2573
- }
2574
- }
2575
- };
2576
-
2577
- case Action.DragEnd:
2578
- case Action.DragCancel:
2579
- return { ...state,
2580
- draggable: { ...state.draggable,
2581
- active: null,
2582
- initialCoordinates: {
2583
- x: 0,
2584
- y: 0
2585
- },
2586
- translate: {
2587
- x: 0,
2588
- y: 0
2589
- }
2590
- }
2591
- };
2592
-
2593
- case Action.RegisterDroppable:
2594
- {
2595
- const {
2596
- element
2597
- } = action;
2598
- const {
2599
- id
2600
- } = element;
2601
- const containers = new DroppableContainersMap(state.droppable.containers);
2602
- containers.set(id, element);
2603
- return { ...state,
2604
- droppable: { ...state.droppable,
2605
- containers
2606
- }
2607
- };
2608
- }
2609
-
2610
- case Action.SetDroppableDisabled:
2611
- {
2612
- const {
2613
- id,
2614
- key,
2615
- disabled
2616
- } = action;
2617
- const element = state.droppable.containers.get(id);
2618
-
2619
- if (!element || key !== element.key) {
2620
- return state;
2621
- }
2622
-
2623
- const containers = new DroppableContainersMap(state.droppable.containers);
2624
- containers.set(id, { ...element,
2625
- disabled
2626
- });
2627
- return { ...state,
2628
- droppable: { ...state.droppable,
2629
- containers
2630
- }
2631
- };
2632
- }
2633
-
2634
- case Action.UnregisterDroppable:
2635
- {
2636
- const {
2637
- id,
2638
- key
2639
- } = action;
2640
- const element = state.droppable.containers.get(id);
2641
-
2642
- if (!element || key !== element.key) {
2643
- return state;
2644
- }
2645
-
2646
- const containers = new DroppableContainersMap(state.droppable.containers);
2647
- containers.delete(id);
2648
- return { ...state,
2649
- droppable: { ...state.droppable,
2650
- containers
2651
- }
2652
- };
2653
- }
2654
-
2655
- default:
2656
- {
2657
- return state;
2658
- }
2659
- }
2660
- }
2661
-
2662
- function RestoreFocus(_ref) {
2663
- let {
2664
- disabled
2665
- } = _ref;
2666
- const {
2667
- active,
2668
- activatorEvent,
2669
- draggableNodes
2670
- } = useContext(InternalContext);
2671
- const previousActivatorEvent = usePrevious(activatorEvent);
2672
- const previousActiveId = usePrevious(active == null ? void 0 : active.id); // Restore keyboard focus on the activator node
2673
-
2674
- useEffect(() => {
2675
- if (disabled) {
2676
- return;
2677
- }
2678
-
2679
- if (!activatorEvent && previousActivatorEvent && previousActiveId != null) {
2680
- if (!isKeyboardEvent(previousActivatorEvent)) {
2681
- return;
2682
- }
2683
-
2684
- if (document.activeElement === previousActivatorEvent.target) {
2685
- // No need to restore focus
2686
- return;
2687
- }
2688
-
2689
- const draggableNode = draggableNodes.get(previousActiveId);
2690
-
2691
- if (!draggableNode) {
2692
- return;
2693
- }
2694
-
2695
- const {
2696
- activatorNode,
2697
- node
2698
- } = draggableNode;
2699
-
2700
- if (!activatorNode.current && !node.current) {
2701
- return;
2702
- }
2703
-
2704
- requestAnimationFrame(() => {
2705
- for (const element of [activatorNode.current, node.current]) {
2706
- if (!element) {
2707
- continue;
2708
- }
2709
-
2710
- const focusableNode = findFirstFocusableNode(element);
2711
-
2712
- if (focusableNode) {
2713
- focusableNode.focus();
2714
- break;
2715
- }
2716
- }
2717
- });
2718
- }
2719
- }, [activatorEvent, disabled, draggableNodes, previousActiveId, previousActivatorEvent]);
2720
- return null;
2721
- }
2722
-
2723
- function applyModifiers(modifiers, _ref) {
2724
- let {
2725
- transform,
2726
- ...args
2727
- } = _ref;
2728
- return modifiers != null && modifiers.length ? modifiers.reduce((accumulator, modifier) => {
2729
- return modifier({
2730
- transform: accumulator,
2731
- ...args
2732
- });
2733
- }, transform) : transform;
2734
- }
2735
-
2736
- function useMeasuringConfiguration(config) {
2737
- return useMemo(() => ({
2738
- draggable: { ...defaultMeasuringConfiguration.draggable,
2739
- ...(config == null ? void 0 : config.draggable)
2740
- },
2741
- droppable: { ...defaultMeasuringConfiguration.droppable,
2742
- ...(config == null ? void 0 : config.droppable)
2743
- },
2744
- dragOverlay: { ...defaultMeasuringConfiguration.dragOverlay,
2745
- ...(config == null ? void 0 : config.dragOverlay)
2746
- }
2747
- }), // eslint-disable-next-line react-hooks/exhaustive-deps
2748
- [config == null ? void 0 : config.draggable, config == null ? void 0 : config.droppable, config == null ? void 0 : config.dragOverlay]);
2749
- }
2750
-
2751
- function useLayoutShiftScrollCompensation(_ref) {
2752
- let {
2753
- activeNode,
2754
- measure,
2755
- initialRect,
2756
- config = true
2757
- } = _ref;
2758
- const initialized = useRef(false);
2759
- const {
2760
- x,
2761
- y
2762
- } = typeof config === 'boolean' ? {
2763
- x: config,
2764
- y: config
2765
- } : config;
2766
- useIsomorphicLayoutEffect(() => {
2767
- const disabled = !x && !y;
2768
-
2769
- if (disabled || !activeNode) {
2770
- initialized.current = false;
2771
- return;
2772
- }
2773
-
2774
- if (initialized.current || !initialRect) {
2775
- // Return early if layout shift scroll compensation was already attempted
2776
- // or if there is no initialRect to compare to.
2777
- return;
2778
- } // Get the most up to date node ref for the active draggable
2779
-
2780
-
2781
- const node = activeNode == null ? void 0 : activeNode.node.current;
2782
-
2783
- if (!node || node.isConnected === false) {
2784
- // Return early if there is no attached node ref or if the node is
2785
- // disconnected from the document.
2786
- return;
2787
- }
2788
-
2789
- const rect = measure(node);
2790
- const rectDelta = getRectDelta(rect, initialRect);
2791
-
2792
- if (!x) {
2793
- rectDelta.x = 0;
2794
- }
2795
-
2796
- if (!y) {
2797
- rectDelta.y = 0;
2798
- } // Only perform layout shift scroll compensation once
2799
-
2800
-
2801
- initialized.current = true;
2802
-
2803
- if (Math.abs(rectDelta.x) > 0 || Math.abs(rectDelta.y) > 0) {
2804
- const firstScrollableAncestor = getFirstScrollableAncestor(node);
2805
-
2806
- if (firstScrollableAncestor) {
2807
- firstScrollableAncestor.scrollBy({
2808
- top: rectDelta.y,
2809
- left: rectDelta.x
2810
- });
2811
- }
2812
- }
2813
- }, [activeNode, x, y, initialRect, measure]);
2814
- }
2815
-
2816
- const ActiveDraggableContext = /*#__PURE__*/createContext({ ...defaultCoordinates,
2817
- scaleX: 1,
2818
- scaleY: 1
2819
- });
2820
- var Status;
2821
-
2822
- (function (Status) {
2823
- Status[Status["Uninitialized"] = 0] = "Uninitialized";
2824
- Status[Status["Initializing"] = 1] = "Initializing";
2825
- Status[Status["Initialized"] = 2] = "Initialized";
2826
- })(Status || (Status = {}));
2827
-
2828
- const DndContext = /*#__PURE__*/memo(function DndContext(_ref) {
2829
- var _sensorContext$curren, _dragOverlay$nodeRef$, _dragOverlay$rect, _over$rect;
2830
-
2831
- let {
2832
- id,
2833
- accessibility,
2834
- autoScroll = true,
2835
- children,
2836
- sensors = defaultSensors,
2837
- collisionDetection = rectIntersection,
2838
- measuring,
2839
- modifiers,
2840
- ...props
2841
- } = _ref;
2842
- const store = useReducer(reducer, undefined, getInitialState);
2843
- const [state, dispatch] = store;
2844
- const [dispatchMonitorEvent, registerMonitorListener] = useDndMonitorProvider();
2845
- const [status, setStatus] = useState(Status.Uninitialized);
2846
- const isInitialized = status === Status.Initialized;
2847
- const {
2848
- draggable: {
2849
- active: activeId,
2850
- nodes: draggableNodes,
2851
- translate
2852
- },
2853
- droppable: {
2854
- containers: droppableContainers
2855
- }
2856
- } = state;
2857
- const node = activeId ? draggableNodes.get(activeId) : null;
2858
- const activeRects = useRef({
2859
- initial: null,
2860
- translated: null
2861
- });
2862
- const active = useMemo(() => {
2863
- var _node$data;
2864
-
2865
- return activeId != null ? {
2866
- id: activeId,
2867
- // It's possible for the active node to unmount while dragging
2868
- data: (_node$data = node == null ? void 0 : node.data) != null ? _node$data : defaultData,
2869
- rect: activeRects
2870
- } : null;
2871
- }, [activeId, node]);
2872
- const activeRef = useRef(null);
2873
- const [activeSensor, setActiveSensor] = useState(null);
2874
- const [activatorEvent, setActivatorEvent] = useState(null);
2875
- const latestProps = useLatestValue(props, Object.values(props));
2876
- const draggableDescribedById = useUniqueId("DndDescribedBy", id);
2877
- const enabledDroppableContainers = useMemo(() => droppableContainers.getEnabled(), [droppableContainers]);
2878
- const measuringConfiguration = useMeasuringConfiguration(measuring);
2879
- const {
2880
- droppableRects,
2881
- measureDroppableContainers,
2882
- measuringScheduled
2883
- } = useDroppableMeasuring(enabledDroppableContainers, {
2884
- dragging: isInitialized,
2885
- dependencies: [translate.x, translate.y],
2886
- config: measuringConfiguration.droppable
2887
- });
2888
- const activeNode = useCachedNode(draggableNodes, activeId);
2889
- const activationCoordinates = useMemo(() => activatorEvent ? getEventCoordinates(activatorEvent) : null, [activatorEvent]);
2890
- const autoScrollOptions = getAutoScrollerOptions();
2891
- const initialActiveNodeRect = useInitialRect(activeNode, measuringConfiguration.draggable.measure);
2892
- useLayoutShiftScrollCompensation({
2893
- activeNode: activeId ? draggableNodes.get(activeId) : null,
2894
- config: autoScrollOptions.layoutShiftCompensation,
2895
- initialRect: initialActiveNodeRect,
2896
- measure: measuringConfiguration.draggable.measure
2897
- });
2898
- const activeNodeRect = useRect(activeNode, measuringConfiguration.draggable.measure, initialActiveNodeRect);
2899
- const containerNodeRect = useRect(activeNode ? activeNode.parentElement : null);
2900
- const sensorContext = useRef({
2901
- activatorEvent: null,
2902
- active: null,
2903
- activeNode,
2904
- collisionRect: null,
2905
- collisions: null,
2906
- droppableRects,
2907
- draggableNodes,
2908
- draggingNode: null,
2909
- draggingNodeRect: null,
2910
- droppableContainers,
2911
- over: null,
2912
- scrollableAncestors: [],
2913
- scrollAdjustedTranslate: null
2914
- });
2915
- const overNode = droppableContainers.getNodeFor((_sensorContext$curren = sensorContext.current.over) == null ? void 0 : _sensorContext$curren.id);
2916
- const dragOverlay = useDragOverlayMeasuring({
2917
- measure: measuringConfiguration.dragOverlay.measure
2918
- }); // Use the rect of the drag overlay if it is mounted
2919
-
2920
- const draggingNode = (_dragOverlay$nodeRef$ = dragOverlay.nodeRef.current) != null ? _dragOverlay$nodeRef$ : activeNode;
2921
- const draggingNodeRect = isInitialized ? (_dragOverlay$rect = dragOverlay.rect) != null ? _dragOverlay$rect : activeNodeRect : null;
2922
- const usesDragOverlay = Boolean(dragOverlay.nodeRef.current && dragOverlay.rect); // The delta between the previous and new position of the draggable node
2923
- // is only relevant when there is no drag overlay
2924
-
2925
- const nodeRectDelta = useRectDelta(usesDragOverlay ? null : activeNodeRect); // Get the window rect of the dragging node
2926
-
2927
- const windowRect = useWindowRect(draggingNode ? getWindow(draggingNode) : null); // Get scrollable ancestors of the dragging node
2928
-
2929
- const scrollableAncestors = useScrollableAncestors(isInitialized ? overNode != null ? overNode : activeNode : null);
2930
- const scrollableAncestorRects = useRects(scrollableAncestors);
2931
- const scrollableAncestorRectsRects = useMemo(() => scrollableAncestorRects.map(_ref2 => {
2932
- let {
2933
- rect
2934
- } = _ref2;
2935
- return rect;
2936
- }), [scrollableAncestorRects]); // Apply modifiers
2937
-
2938
- const modifiedTranslate = applyModifiers(modifiers, {
2939
- transform: {
2940
- x: translate.x - nodeRectDelta.x,
2941
- y: translate.y - nodeRectDelta.y,
2942
- scaleX: 1,
2943
- scaleY: 1
2944
- },
2945
- activatorEvent,
2946
- active,
2947
- activeNodeRect,
2948
- containerNodeRect,
2949
- draggingNodeRect,
2950
- over: sensorContext.current.over,
2951
- overlayNodeRect: dragOverlay.rect,
2952
- scrollableAncestors,
2953
- scrollableAncestorRects: scrollableAncestorRectsRects,
2954
- windowRect
2955
- });
2956
- const pointerCoordinates = activationCoordinates ? add(activationCoordinates, translate) : null;
2957
- const scrollOffsets = useScrollOffsets(scrollableAncestors); // Represents the scroll delta since dragging was initiated
2958
-
2959
- const scrollAdjustment = useScrollOffsetsDelta(scrollOffsets); // Represents the scroll delta since the last time the active node rect was measured
2960
-
2961
- const activeNodeScrollDelta = useScrollOffsetsDelta(scrollOffsets, [activeNodeRect]);
2962
- const scrollAdjustedTranslate = add(modifiedTranslate, scrollAdjustment);
2963
- const collisionRect = draggingNodeRect ? getAdjustedRect(draggingNodeRect, modifiedTranslate) : null;
2964
- const collisions = active && collisionRect ? collisionDetection({
2965
- active,
2966
- collisionRect,
2967
- droppableRects,
2968
- droppableContainers: enabledDroppableContainers,
2969
- pointerCoordinates
2970
- }) : null;
2971
- const overId = getFirstCollision(collisions, 'id');
2972
- const [over, setOver] = useState(null); // When there is no drag overlay used, we need to account for the
2973
- // window scroll delta
2974
-
2975
- const appliedTranslate = usesDragOverlay ? modifiedTranslate : add(modifiedTranslate, activeNodeScrollDelta);
2976
- const transform = adjustScale(appliedTranslate, (_over$rect = over == null ? void 0 : over.rect) != null ? _over$rect : null, activeNodeRect);
2977
- const instantiateSensor = useCallback((event, _ref3) => {
2978
- let {
2979
- sensor: Sensor,
2980
- options
2981
- } = _ref3;
2982
-
2983
- if (activeRef.current == null) {
2984
- return;
2985
- }
2986
-
2987
- const activeNode = draggableNodes.get(activeRef.current);
2988
-
2989
- if (!activeNode) {
2990
- return;
2991
- }
2992
-
2993
- const activatorEvent = event.nativeEvent;
2994
- const sensorInstance = new Sensor({
2995
- active: activeRef.current,
2996
- activeNode,
2997
- event: activatorEvent,
2998
- options,
2999
- // Sensors need to be instantiated with refs for arguments that change over time
3000
- // otherwise they are frozen in time with the stale arguments
3001
- context: sensorContext,
3002
-
3003
- onStart(initialCoordinates) {
3004
- const id = activeRef.current;
3005
-
3006
- if (id == null) {
3007
- return;
3008
- }
3009
-
3010
- const draggableNode = draggableNodes.get(id);
3011
-
3012
- if (!draggableNode) {
3013
- return;
3014
- }
3015
-
3016
- const {
3017
- onDragStart
3018
- } = latestProps.current;
3019
- const event = {
3020
- active: {
3021
- id,
3022
- data: draggableNode.data,
3023
- rect: activeRects
3024
- }
3025
- };
3026
- unstable_batchedUpdates(() => {
3027
- onDragStart == null ? void 0 : onDragStart(event);
3028
- setStatus(Status.Initializing);
3029
- dispatch({
3030
- type: Action.DragStart,
3031
- initialCoordinates,
3032
- active: id
3033
- });
3034
- dispatchMonitorEvent({
3035
- type: 'onDragStart',
3036
- event
3037
- });
3038
- });
3039
- },
3040
-
3041
- onMove(coordinates) {
3042
- dispatch({
3043
- type: Action.DragMove,
3044
- coordinates
3045
- });
3046
- },
3047
-
3048
- onEnd: createHandler(Action.DragEnd),
3049
- onCancel: createHandler(Action.DragCancel)
3050
- });
3051
- unstable_batchedUpdates(() => {
3052
- setActiveSensor(sensorInstance);
3053
- setActivatorEvent(event.nativeEvent);
3054
- });
3055
-
3056
- function createHandler(type) {
3057
- return async function handler() {
3058
- const {
3059
- active,
3060
- collisions,
3061
- over,
3062
- scrollAdjustedTranslate
3063
- } = sensorContext.current;
3064
- let event = null;
3065
-
3066
- if (active && scrollAdjustedTranslate) {
3067
- const {
3068
- cancelDrop
3069
- } = latestProps.current;
3070
- event = {
3071
- activatorEvent,
3072
- active: active,
3073
- collisions,
3074
- delta: scrollAdjustedTranslate,
3075
- over
3076
- };
3077
-
3078
- if (type === Action.DragEnd && typeof cancelDrop === 'function') {
3079
- const shouldCancel = await Promise.resolve(cancelDrop(event));
3080
-
3081
- if (shouldCancel) {
3082
- type = Action.DragCancel;
3083
- }
3084
- }
3085
- }
3086
-
3087
- activeRef.current = null;
3088
- unstable_batchedUpdates(() => {
3089
- dispatch({
3090
- type
3091
- });
3092
- setStatus(Status.Uninitialized);
3093
- setOver(null);
3094
- setActiveSensor(null);
3095
- setActivatorEvent(null);
3096
- const eventName = type === Action.DragEnd ? 'onDragEnd' : 'onDragCancel';
3097
-
3098
- if (event) {
3099
- const handler = latestProps.current[eventName];
3100
- handler == null ? void 0 : handler(event);
3101
- dispatchMonitorEvent({
3102
- type: eventName,
3103
- event
3104
- });
3105
- }
3106
- });
3107
- };
3108
- }
3109
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
3110
- [draggableNodes]);
3111
- const bindActivatorToSensorInstantiator = useCallback((handler, sensor) => {
3112
- return (event, active) => {
3113
- const nativeEvent = event.nativeEvent;
3114
- const activeDraggableNode = draggableNodes.get(active);
3115
-
3116
- if ( // Another sensor is already instantiating
3117
- activeRef.current !== null || // No active draggable
3118
- !activeDraggableNode || // Event has already been captured
3119
- nativeEvent.dndKit || nativeEvent.defaultPrevented) {
3120
- return;
3121
- }
3122
-
3123
- const activationContext = {
3124
- active: activeDraggableNode
3125
- };
3126
- const shouldActivate = handler(event, sensor.options, activationContext);
3127
-
3128
- if (shouldActivate === true) {
3129
- nativeEvent.dndKit = {
3130
- capturedBy: sensor.sensor
3131
- };
3132
- activeRef.current = active;
3133
- instantiateSensor(event, sensor);
3134
- }
3135
- };
3136
- }, [draggableNodes, instantiateSensor]);
3137
- const activators = useCombineActivators(sensors, bindActivatorToSensorInstantiator);
3138
- useSensorSetup(sensors);
3139
- useIsomorphicLayoutEffect(() => {
3140
- if (activeNodeRect && status === Status.Initializing) {
3141
- setStatus(Status.Initialized);
3142
- }
3143
- }, [activeNodeRect, status]);
3144
- useEffect(() => {
3145
- const {
3146
- onDragMove
3147
- } = latestProps.current;
3148
- const {
3149
- active,
3150
- activatorEvent,
3151
- collisions,
3152
- over
3153
- } = sensorContext.current;
3154
-
3155
- if (!active || !activatorEvent) {
3156
- return;
3157
- }
3158
-
3159
- const event = {
3160
- active,
3161
- activatorEvent,
3162
- collisions,
3163
- delta: {
3164
- x: scrollAdjustedTranslate.x,
3165
- y: scrollAdjustedTranslate.y
3166
- },
3167
- over
3168
- };
3169
- unstable_batchedUpdates(() => {
3170
- onDragMove == null ? void 0 : onDragMove(event);
3171
- dispatchMonitorEvent({
3172
- type: 'onDragMove',
3173
- event
3174
- });
3175
- });
3176
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
3177
- [scrollAdjustedTranslate.x, scrollAdjustedTranslate.y]);
3178
- useEffect(() => {
3179
- const {
3180
- active,
3181
- activatorEvent,
3182
- collisions,
3183
- droppableContainers,
3184
- scrollAdjustedTranslate
3185
- } = sensorContext.current;
3186
-
3187
- if (!active || activeRef.current == null || !activatorEvent || !scrollAdjustedTranslate) {
3188
- return;
3189
- }
3190
-
3191
- const {
3192
- onDragOver
3193
- } = latestProps.current;
3194
- const overContainer = droppableContainers.get(overId);
3195
- const over = overContainer && overContainer.rect.current ? {
3196
- id: overContainer.id,
3197
- rect: overContainer.rect.current,
3198
- data: overContainer.data,
3199
- disabled: overContainer.disabled
3200
- } : null;
3201
- const event = {
3202
- active,
3203
- activatorEvent,
3204
- collisions,
3205
- delta: {
3206
- x: scrollAdjustedTranslate.x,
3207
- y: scrollAdjustedTranslate.y
3208
- },
3209
- over
3210
- };
3211
- unstable_batchedUpdates(() => {
3212
- setOver(over);
3213
- onDragOver == null ? void 0 : onDragOver(event);
3214
- dispatchMonitorEvent({
3215
- type: 'onDragOver',
3216
- event
3217
- });
3218
- });
3219
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
3220
- [overId]);
3221
- useIsomorphicLayoutEffect(() => {
3222
- sensorContext.current = {
3223
- activatorEvent,
3224
- active,
3225
- activeNode,
3226
- collisionRect,
3227
- collisions,
3228
- droppableRects,
3229
- draggableNodes,
3230
- draggingNode,
3231
- draggingNodeRect,
3232
- droppableContainers,
3233
- over,
3234
- scrollableAncestors,
3235
- scrollAdjustedTranslate
3236
- };
3237
- activeRects.current = {
3238
- initial: draggingNodeRect,
3239
- translated: collisionRect
3240
- };
3241
- }, [active, activeNode, collisions, collisionRect, draggableNodes, draggingNode, draggingNodeRect, droppableRects, droppableContainers, over, scrollableAncestors, scrollAdjustedTranslate]);
3242
- useAutoScroller({ ...autoScrollOptions,
3243
- delta: translate,
3244
- draggingRect: collisionRect,
3245
- pointerCoordinates,
3246
- scrollableAncestors,
3247
- scrollableAncestorRects
3248
- });
3249
- const publicContext = useMemo(() => {
3250
- const context = {
3251
- active,
3252
- activeNode,
3253
- activeNodeRect,
3254
- activatorEvent,
3255
- collisions,
3256
- containerNodeRect,
3257
- dragOverlay,
3258
- draggableNodes,
3259
- droppableContainers,
3260
- droppableRects,
3261
- over,
3262
- measureDroppableContainers,
3263
- scrollableAncestors,
3264
- scrollableAncestorRects: scrollableAncestorRectsRects,
3265
- measuringConfiguration,
3266
- measuringScheduled,
3267
- windowRect
3268
- };
3269
- return context;
3270
- }, [active, activeNode, activeNodeRect, activatorEvent, collisions, containerNodeRect, dragOverlay, draggableNodes, droppableContainers, droppableRects, over, measureDroppableContainers, scrollableAncestors, scrollableAncestorRects, measuringConfiguration, measuringScheduled, windowRect]);
3271
- const internalContext = useMemo(() => {
3272
- const context = {
3273
- activatorEvent,
3274
- activators,
3275
- active,
3276
- activeNodeRect,
3277
- ariaDescribedById: {
3278
- draggable: draggableDescribedById
3279
- },
3280
- dispatch,
3281
- draggableNodes,
3282
- over,
3283
- measureDroppableContainers
3284
- };
3285
- return context;
3286
- }, [activatorEvent, activators, active, activeNodeRect, dispatch, draggableDescribedById, draggableNodes, over, measureDroppableContainers]);
3287
- return React.createElement(DndMonitorContext.Provider, {
3288
- value: registerMonitorListener
3289
- }, React.createElement(InternalContext.Provider, {
3290
- value: internalContext
3291
- }, React.createElement(PublicContext.Provider, {
3292
- value: publicContext
3293
- }, React.createElement(ActiveDraggableContext.Provider, {
3294
- value: transform
3295
- }, children)), React.createElement(RestoreFocus, {
3296
- disabled: (accessibility == null ? void 0 : accessibility.restoreFocus) === false
3297
- })), React.createElement(Accessibility, { ...accessibility,
3298
- hiddenTextDescribedById: draggableDescribedById
3299
- }));
3300
-
3301
- function getAutoScrollerOptions() {
3302
- const activeSensorDisablesAutoscroll = (activeSensor == null ? void 0 : activeSensor.autoScrollEnabled) === false;
3303
- const autoScrollGloballyDisabled = typeof autoScroll === 'object' ? autoScroll.enabled === false : autoScroll === false;
3304
- const enabled = isInitialized && !activeSensorDisablesAutoscroll && !autoScrollGloballyDisabled;
3305
-
3306
- if (typeof autoScroll === 'object') {
3307
- return { ...autoScroll,
3308
- enabled
3309
- };
3310
- }
3311
-
3312
- return {
3313
- enabled
3314
- };
3315
- }
3316
- });
3317
-
3318
- const NullContext = /*#__PURE__*/createContext(null);
3319
- const defaultRole = 'button';
3320
- const ID_PREFIX = 'Droppable';
3321
- function useDraggable(_ref) {
3322
- let {
3323
- id,
3324
- data,
3325
- disabled = false,
3326
- attributes
3327
- } = _ref;
3328
- const key = useUniqueId(ID_PREFIX);
3329
- const {
3330
- activators,
3331
- activatorEvent,
3332
- active,
3333
- activeNodeRect,
3334
- ariaDescribedById,
3335
- draggableNodes,
3336
- over
3337
- } = useContext(InternalContext);
3338
- const {
3339
- role = defaultRole,
3340
- roleDescription = 'draggable',
3341
- tabIndex = 0
3342
- } = attributes != null ? attributes : {};
3343
- const isDragging = (active == null ? void 0 : active.id) === id;
3344
- const transform = useContext(isDragging ? ActiveDraggableContext : NullContext);
3345
- const [node, setNodeRef] = useNodeRef();
3346
- const [activatorNode, setActivatorNodeRef] = useNodeRef();
3347
- const listeners = useSyntheticListeners(activators, id);
3348
- const dataRef = useLatestValue(data);
3349
- useIsomorphicLayoutEffect(() => {
3350
- draggableNodes.set(id, {
3351
- id,
3352
- key,
3353
- node,
3354
- activatorNode,
3355
- data: dataRef
3356
- });
3357
- return () => {
3358
- const node = draggableNodes.get(id);
3359
-
3360
- if (node && node.key === key) {
3361
- draggableNodes.delete(id);
3362
- }
3363
- };
3364
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
3365
- [draggableNodes, id]);
3366
- const memoizedAttributes = useMemo(() => ({
3367
- role,
3368
- tabIndex,
3369
- 'aria-disabled': disabled,
3370
- 'aria-pressed': isDragging && role === defaultRole ? true : undefined,
3371
- 'aria-roledescription': roleDescription,
3372
- 'aria-describedby': ariaDescribedById.draggable
3373
- }), [disabled, role, tabIndex, isDragging, roleDescription, ariaDescribedById.draggable]);
3374
- return {
3375
- active,
3376
- activatorEvent,
3377
- activeNodeRect,
3378
- attributes: memoizedAttributes,
3379
- isDragging,
3380
- listeners: disabled ? undefined : listeners,
3381
- node,
3382
- over,
3383
- setNodeRef,
3384
- setActivatorNodeRef,
3385
- transform
3386
- };
3387
- }
3388
-
3389
- function useDndContext() {
3390
- return useContext(PublicContext);
3391
- }
3392
-
3393
- const ID_PREFIX$1 = 'Droppable';
3394
- const defaultResizeObserverConfig = {
3395
- timeout: 25
3396
- };
3397
- function useDroppable(_ref) {
3398
- let {
3399
- data,
3400
- disabled = false,
3401
- id,
3402
- resizeObserverConfig
3403
- } = _ref;
3404
- const key = useUniqueId(ID_PREFIX$1);
3405
- const {
3406
- active,
3407
- dispatch,
3408
- over,
3409
- measureDroppableContainers
3410
- } = useContext(InternalContext);
3411
- const previous = useRef({
3412
- disabled
3413
- });
3414
- const resizeObserverConnected = useRef(false);
3415
- const rect = useRef(null);
3416
- const callbackId = useRef(null);
3417
- const {
3418
- disabled: resizeObserverDisabled,
3419
- updateMeasurementsFor,
3420
- timeout: resizeObserverTimeout
3421
- } = { ...defaultResizeObserverConfig,
3422
- ...resizeObserverConfig
3423
- };
3424
- const ids = useLatestValue(updateMeasurementsFor != null ? updateMeasurementsFor : id);
3425
- const handleResize = useCallback(() => {
3426
- if (!resizeObserverConnected.current) {
3427
- // ResizeObserver invokes the `handleResize` callback as soon as `observe` is called,
3428
- // assuming the element is rendered and displayed.
3429
- resizeObserverConnected.current = true;
3430
- return;
3431
- }
3432
-
3433
- if (callbackId.current != null) {
3434
- clearTimeout(callbackId.current);
3435
- }
3436
-
3437
- callbackId.current = setTimeout(() => {
3438
- measureDroppableContainers(Array.isArray(ids.current) ? ids.current : [ids.current]);
3439
- callbackId.current = null;
3440
- }, resizeObserverTimeout);
3441
- }, //eslint-disable-next-line react-hooks/exhaustive-deps
3442
- [resizeObserverTimeout]);
3443
- const resizeObserver = useResizeObserver({
3444
- callback: handleResize,
3445
- disabled: resizeObserverDisabled || !active
3446
- });
3447
- const handleNodeChange = useCallback((newElement, previousElement) => {
3448
- if (!resizeObserver) {
3449
- return;
3450
- }
3451
-
3452
- if (previousElement) {
3453
- resizeObserver.unobserve(previousElement);
3454
- resizeObserverConnected.current = false;
3455
- }
3456
-
3457
- if (newElement) {
3458
- resizeObserver.observe(newElement);
3459
- }
3460
- }, [resizeObserver]);
3461
- const [nodeRef, setNodeRef] = useNodeRef(handleNodeChange);
3462
- const dataRef = useLatestValue(data);
3463
- useEffect(() => {
3464
- if (!resizeObserver || !nodeRef.current) {
3465
- return;
3466
- }
3467
-
3468
- resizeObserver.disconnect();
3469
- resizeObserverConnected.current = false;
3470
- resizeObserver.observe(nodeRef.current);
3471
- }, [nodeRef, resizeObserver]);
3472
- useIsomorphicLayoutEffect(() => {
3473
- dispatch({
3474
- type: Action.RegisterDroppable,
3475
- element: {
3476
- id,
3477
- key,
3478
- disabled,
3479
- node: nodeRef,
3480
- rect,
3481
- data: dataRef
3482
- }
3483
- });
3484
- return () => dispatch({
3485
- type: Action.UnregisterDroppable,
3486
- key,
3487
- id
3488
- });
3489
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
3490
- [id]);
3491
- useEffect(() => {
3492
- if (disabled !== previous.current.disabled) {
3493
- dispatch({
3494
- type: Action.SetDroppableDisabled,
3495
- id,
3496
- key,
3497
- disabled
3498
- });
3499
- previous.current.disabled = disabled;
3500
- }
3501
- }, [id, key, disabled, dispatch]);
3502
- return {
3503
- active,
3504
- rect,
3505
- isOver: (over == null ? void 0 : over.id) === id,
3506
- node: nodeRef,
3507
- over,
3508
- setNodeRef
3509
- };
3510
- }
3511
-
3512
- function AnimationManager(_ref) {
3513
- let {
3514
- animation,
3515
- children
3516
- } = _ref;
3517
- const [clonedChildren, setClonedChildren] = useState(null);
3518
- const [element, setElement] = useState(null);
3519
- const previousChildren = usePrevious(children);
3520
-
3521
- if (!children && !clonedChildren && previousChildren) {
3522
- setClonedChildren(previousChildren);
3523
- }
3524
-
3525
- useIsomorphicLayoutEffect(() => {
3526
- if (!element) {
3527
- return;
3528
- }
3529
-
3530
- const key = clonedChildren == null ? void 0 : clonedChildren.key;
3531
- const id = clonedChildren == null ? void 0 : clonedChildren.props.id;
3532
-
3533
- if (key == null || id == null) {
3534
- setClonedChildren(null);
3535
- return;
3536
- }
3537
-
3538
- Promise.resolve(animation(id, element)).then(() => {
3539
- setClonedChildren(null);
3540
- });
3541
- }, [animation, clonedChildren, element]);
3542
- return React.createElement(React.Fragment, null, children, clonedChildren ? cloneElement(clonedChildren, {
3543
- ref: setElement
3544
- }) : null);
3545
- }
3546
-
3547
- const defaultTransform = {
3548
- x: 0,
3549
- y: 0,
3550
- scaleX: 1,
3551
- scaleY: 1
3552
- };
3553
- function NullifiedContextProvider(_ref) {
3554
- let {
3555
- children
3556
- } = _ref;
3557
- return React.createElement(InternalContext.Provider, {
3558
- value: defaultInternalContext
3559
- }, React.createElement(ActiveDraggableContext.Provider, {
3560
- value: defaultTransform
3561
- }, children));
3562
- }
3563
-
3564
- const baseStyles = {
3565
- position: 'fixed',
3566
- touchAction: 'none'
3567
- };
3568
-
3569
- const defaultTransition = activatorEvent => {
3570
- const isKeyboardActivator = isKeyboardEvent(activatorEvent);
3571
- return isKeyboardActivator ? 'transform 250ms ease' : undefined;
3572
- };
3573
-
3574
- const PositionedOverlay = /*#__PURE__*/forwardRef((_ref, ref) => {
3575
- let {
3576
- as,
3577
- activatorEvent,
3578
- adjustScale,
3579
- children,
3580
- className,
3581
- rect,
3582
- style,
3583
- transform,
3584
- transition = defaultTransition
3585
- } = _ref;
3586
-
3587
- if (!rect) {
3588
- return null;
3589
- }
3590
-
3591
- const scaleAdjustedTransform = adjustScale ? transform : { ...transform,
3592
- scaleX: 1,
3593
- scaleY: 1
3594
- };
3595
- const styles = { ...baseStyles,
3596
- width: rect.width,
3597
- height: rect.height,
3598
- top: rect.top,
3599
- left: rect.left,
3600
- transform: CSS.Transform.toString(scaleAdjustedTransform),
3601
- transformOrigin: adjustScale && activatorEvent ? getRelativeTransformOrigin(activatorEvent, rect) : undefined,
3602
- transition: typeof transition === 'function' ? transition(activatorEvent) : transition,
3603
- ...style
3604
- };
3605
- return React.createElement(as, {
3606
- className,
3607
- style: styles,
3608
- ref
3609
- }, children);
3610
- });
3611
-
3612
- const defaultDropAnimationSideEffects = options => _ref => {
3613
- let {
3614
- active,
3615
- dragOverlay
3616
- } = _ref;
3617
- const originalStyles = {};
3618
- const {
3619
- styles,
3620
- className
3621
- } = options;
3622
-
3623
- if (styles != null && styles.active) {
3624
- for (const [key, value] of Object.entries(styles.active)) {
3625
- if (value === undefined) {
3626
- continue;
3627
- }
3628
-
3629
- originalStyles[key] = active.node.style.getPropertyValue(key);
3630
- active.node.style.setProperty(key, value);
3631
- }
3632
- }
3633
-
3634
- if (styles != null && styles.dragOverlay) {
3635
- for (const [key, value] of Object.entries(styles.dragOverlay)) {
3636
- if (value === undefined) {
3637
- continue;
3638
- }
3639
-
3640
- dragOverlay.node.style.setProperty(key, value);
3641
- }
3642
- }
3643
-
3644
- if (className != null && className.active) {
3645
- active.node.classList.add(className.active);
3646
- }
3647
-
3648
- if (className != null && className.dragOverlay) {
3649
- dragOverlay.node.classList.add(className.dragOverlay);
3650
- }
3651
-
3652
- return function cleanup() {
3653
- for (const [key, value] of Object.entries(originalStyles)) {
3654
- active.node.style.setProperty(key, value);
3655
- }
3656
-
3657
- if (className != null && className.active) {
3658
- active.node.classList.remove(className.active);
3659
- }
3660
- };
3661
- };
3662
-
3663
- const defaultKeyframeResolver = _ref2 => {
3664
- let {
3665
- transform: {
3666
- initial,
3667
- final
3668
- }
3669
- } = _ref2;
3670
- return [{
3671
- transform: CSS.Transform.toString(initial)
3672
- }, {
3673
- transform: CSS.Transform.toString(final)
3674
- }];
3675
- };
3676
-
3677
- const defaultDropAnimationConfiguration = {
3678
- duration: 250,
3679
- easing: 'ease',
3680
- keyframes: defaultKeyframeResolver,
3681
- sideEffects: /*#__PURE__*/defaultDropAnimationSideEffects({
3682
- styles: {
3683
- active: {
3684
- opacity: '0'
3685
- }
3686
- }
3687
- })
3688
- };
3689
- function useDropAnimation(_ref3) {
3690
- let {
3691
- config,
3692
- draggableNodes,
3693
- droppableContainers,
3694
- measuringConfiguration
3695
- } = _ref3;
3696
- return useEvent((id, node) => {
3697
- if (config === null) {
3698
- return;
3699
- }
3700
-
3701
- const activeDraggable = draggableNodes.get(id);
3702
-
3703
- if (!activeDraggable) {
3704
- return;
3705
- }
3706
-
3707
- const activeNode = activeDraggable.node.current;
3708
-
3709
- if (!activeNode) {
3710
- return;
3711
- }
3712
-
3713
- const measurableNode = getMeasurableNode(node);
3714
-
3715
- if (!measurableNode) {
3716
- return;
3717
- }
3718
-
3719
- const {
3720
- transform
3721
- } = getWindow(node).getComputedStyle(node);
3722
- const parsedTransform = parseTransform(transform);
3723
-
3724
- if (!parsedTransform) {
3725
- return;
3726
- }
3727
-
3728
- const animation = typeof config === 'function' ? config : createDefaultDropAnimation(config);
3729
- scrollIntoViewIfNeeded(activeNode, measuringConfiguration.draggable.measure);
3730
- return animation({
3731
- active: {
3732
- id,
3733
- data: activeDraggable.data,
3734
- node: activeNode,
3735
- rect: measuringConfiguration.draggable.measure(activeNode)
3736
- },
3737
- draggableNodes,
3738
- dragOverlay: {
3739
- node,
3740
- rect: measuringConfiguration.dragOverlay.measure(measurableNode)
3741
- },
3742
- droppableContainers,
3743
- measuringConfiguration,
3744
- transform: parsedTransform
3745
- });
3746
- });
3747
- }
3748
-
3749
- function createDefaultDropAnimation(options) {
3750
- const {
3751
- duration,
3752
- easing,
3753
- sideEffects,
3754
- keyframes
3755
- } = { ...defaultDropAnimationConfiguration,
3756
- ...options
3757
- };
3758
- return _ref4 => {
3759
- let {
3760
- active,
3761
- dragOverlay,
3762
- transform,
3763
- ...rest
3764
- } = _ref4;
3765
-
3766
- if (!duration) {
3767
- // Do not animate if animation duration is zero.
3768
- return;
3769
- }
3770
-
3771
- const delta = {
3772
- x: dragOverlay.rect.left - active.rect.left,
3773
- y: dragOverlay.rect.top - active.rect.top
3774
- };
3775
- const scale = {
3776
- scaleX: transform.scaleX !== 1 ? active.rect.width * transform.scaleX / dragOverlay.rect.width : 1,
3777
- scaleY: transform.scaleY !== 1 ? active.rect.height * transform.scaleY / dragOverlay.rect.height : 1
3778
- };
3779
- const finalTransform = {
3780
- x: transform.x - delta.x,
3781
- y: transform.y - delta.y,
3782
- ...scale
3783
- };
3784
- const animationKeyframes = keyframes({ ...rest,
3785
- active,
3786
- dragOverlay,
3787
- transform: {
3788
- initial: transform,
3789
- final: finalTransform
3790
- }
3791
- });
3792
- const [firstKeyframe] = animationKeyframes;
3793
- const lastKeyframe = animationKeyframes[animationKeyframes.length - 1];
3794
-
3795
- if (JSON.stringify(firstKeyframe) === JSON.stringify(lastKeyframe)) {
3796
- // The start and end keyframes are the same, infer that there is no animation needed.
3797
- return;
3798
- }
3799
-
3800
- const cleanup = sideEffects == null ? void 0 : sideEffects({
3801
- active,
3802
- dragOverlay,
3803
- ...rest
3804
- });
3805
- const animation = dragOverlay.node.animate(animationKeyframes, {
3806
- duration,
3807
- easing,
3808
- fill: 'forwards'
3809
- });
3810
- return new Promise(resolve => {
3811
- animation.onfinish = () => {
3812
- cleanup == null ? void 0 : cleanup();
3813
- resolve();
3814
- };
3815
- });
3816
- };
3817
- }
3818
-
3819
- let key = 0;
3820
- function useKey(id) {
3821
- return useMemo(() => {
3822
- if (id == null) {
3823
- return;
3824
- }
3825
-
3826
- key++;
3827
- return key;
3828
- }, [id]);
3829
- }
3830
-
3831
- const DragOverlay = /*#__PURE__*/React.memo(_ref => {
3832
- let {
3833
- adjustScale = false,
3834
- children,
3835
- dropAnimation: dropAnimationConfig,
3836
- style,
3837
- transition,
3838
- modifiers,
3839
- wrapperElement = 'div',
3840
- className,
3841
- zIndex = 999
3842
- } = _ref;
3843
- const {
3844
- activatorEvent,
3845
- active,
3846
- activeNodeRect,
3847
- containerNodeRect,
3848
- draggableNodes,
3849
- droppableContainers,
3850
- dragOverlay,
3851
- over,
3852
- measuringConfiguration,
3853
- scrollableAncestors,
3854
- scrollableAncestorRects,
3855
- windowRect
3856
- } = useDndContext();
3857
- const transform = useContext(ActiveDraggableContext);
3858
- const key = useKey(active == null ? void 0 : active.id);
3859
- const modifiedTransform = applyModifiers(modifiers, {
3860
- activatorEvent,
3861
- active,
3862
- activeNodeRect,
3863
- containerNodeRect,
3864
- draggingNodeRect: dragOverlay.rect,
3865
- over,
3866
- overlayNodeRect: dragOverlay.rect,
3867
- scrollableAncestors,
3868
- scrollableAncestorRects,
3869
- transform,
3870
- windowRect
3871
- });
3872
- const initialRect = useInitialValue(activeNodeRect);
3873
- const dropAnimation = useDropAnimation({
3874
- config: dropAnimationConfig,
3875
- draggableNodes,
3876
- droppableContainers,
3877
- measuringConfiguration
3878
- }); // We need to wait for the active node to be measured before connecting the drag overlay ref
3879
- // otherwise collisions can be computed against a mispositioned drag overlay
3880
-
3881
- const ref = initialRect ? dragOverlay.setRef : undefined;
3882
- return React.createElement(NullifiedContextProvider, null, React.createElement(AnimationManager, {
3883
- animation: dropAnimation
3884
- }, active && key ? React.createElement(PositionedOverlay, {
3885
- key: key,
3886
- id: active.id,
3887
- ref: ref,
3888
- as: wrapperElement,
3889
- activatorEvent: activatorEvent,
3890
- adjustScale: adjustScale,
3891
- className: className,
3892
- transition: transition,
3893
- rect: initialRect,
3894
- style: {
3895
- zIndex,
3896
- ...style
3897
- },
3898
- transform: modifiedTransform
3899
- }, children) : null));
3900
- });
3901
-
3902
- export { AutoScrollActivator, DndContext, DragOverlay, KeyboardCode, KeyboardSensor, MeasuringFrequency, MeasuringStrategy, MouseSensor, PointerSensor, TouchSensor, TraversalOrder, applyModifiers, closestCenter, closestCorners, defaultAnnouncements, defaultCoordinates, defaultDropAnimationConfiguration as defaultDropAnimation, defaultDropAnimationSideEffects, defaultScreenReaderInstructions, getClientRect, getFirstCollision, getScrollableAncestors, pointerWithin, rectIntersection, useDndContext, useDndMonitor, useDraggable, useDroppable, useSensor, useSensors };
3903
- //# sourceMappingURL=dnd-kit-core.esm.js.map