@react-aria/dnd 3.11.6 → 3.12.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 (281) hide show
  1. package/dist/import.mjs +8 -18
  2. package/dist/main.js +22 -32
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +8 -18
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +13 -0
  7. package/package.json +16 -21
  8. package/src/index.ts +13 -56
  9. package/dist/DragManager.main.js +0 -522
  10. package/dist/DragManager.main.js.map +0 -1
  11. package/dist/DragManager.mjs +0 -513
  12. package/dist/DragManager.module.js +0 -513
  13. package/dist/DragManager.module.js.map +0 -1
  14. package/dist/DragPreview.main.js +0 -73
  15. package/dist/DragPreview.main.js.map +0 -1
  16. package/dist/DragPreview.mjs +0 -64
  17. package/dist/DragPreview.module.js +0 -64
  18. package/dist/DragPreview.module.js.map +0 -1
  19. package/dist/DropTargetKeyboardNavigation.main.js +0 -215
  20. package/dist/DropTargetKeyboardNavigation.main.js.map +0 -1
  21. package/dist/DropTargetKeyboardNavigation.mjs +0 -210
  22. package/dist/DropTargetKeyboardNavigation.module.js +0 -210
  23. package/dist/DropTargetKeyboardNavigation.module.js.map +0 -1
  24. package/dist/ListDropTargetDelegate.main.js +0 -129
  25. package/dist/ListDropTargetDelegate.main.js.map +0 -1
  26. package/dist/ListDropTargetDelegate.mjs +0 -124
  27. package/dist/ListDropTargetDelegate.module.js +0 -124
  28. package/dist/ListDropTargetDelegate.module.js.map +0 -1
  29. package/dist/ar-AE.main.js +0 -44
  30. package/dist/ar-AE.main.js.map +0 -1
  31. package/dist/ar-AE.mjs +0 -46
  32. package/dist/ar-AE.module.js +0 -46
  33. package/dist/ar-AE.module.js.map +0 -1
  34. package/dist/bg-BG.main.js +0 -44
  35. package/dist/bg-BG.main.js.map +0 -1
  36. package/dist/bg-BG.mjs +0 -46
  37. package/dist/bg-BG.module.js +0 -46
  38. package/dist/bg-BG.module.js.map +0 -1
  39. package/dist/constants.main.js +0 -64
  40. package/dist/constants.main.js.map +0 -1
  41. package/dist/constants.mjs +0 -52
  42. package/dist/constants.module.js +0 -52
  43. package/dist/constants.module.js.map +0 -1
  44. package/dist/cs-CZ.main.js +0 -45
  45. package/dist/cs-CZ.main.js.map +0 -1
  46. package/dist/cs-CZ.mjs +0 -47
  47. package/dist/cs-CZ.module.js +0 -47
  48. package/dist/cs-CZ.module.js.map +0 -1
  49. package/dist/da-DK.main.js +0 -44
  50. package/dist/da-DK.main.js.map +0 -1
  51. package/dist/da-DK.mjs +0 -46
  52. package/dist/da-DK.module.js +0 -46
  53. package/dist/da-DK.module.js.map +0 -1
  54. package/dist/de-DE.main.js +0 -44
  55. package/dist/de-DE.main.js.map +0 -1
  56. package/dist/de-DE.mjs +0 -46
  57. package/dist/de-DE.module.js +0 -46
  58. package/dist/de-DE.module.js.map +0 -1
  59. package/dist/el-GR.main.js +0 -44
  60. package/dist/el-GR.main.js.map +0 -1
  61. package/dist/el-GR.mjs +0 -46
  62. package/dist/el-GR.module.js +0 -46
  63. package/dist/el-GR.module.js.map +0 -1
  64. package/dist/en-US.main.js +0 -44
  65. package/dist/en-US.main.js.map +0 -1
  66. package/dist/en-US.mjs +0 -46
  67. package/dist/en-US.module.js +0 -46
  68. package/dist/en-US.module.js.map +0 -1
  69. package/dist/es-ES.main.js +0 -44
  70. package/dist/es-ES.main.js.map +0 -1
  71. package/dist/es-ES.mjs +0 -46
  72. package/dist/es-ES.module.js +0 -46
  73. package/dist/es-ES.module.js.map +0 -1
  74. package/dist/et-EE.main.js +0 -44
  75. package/dist/et-EE.main.js.map +0 -1
  76. package/dist/et-EE.mjs +0 -46
  77. package/dist/et-EE.module.js +0 -46
  78. package/dist/et-EE.module.js.map +0 -1
  79. package/dist/fi-FI.main.js +0 -44
  80. package/dist/fi-FI.main.js.map +0 -1
  81. package/dist/fi-FI.mjs +0 -46
  82. package/dist/fi-FI.module.js +0 -46
  83. package/dist/fi-FI.module.js.map +0 -1
  84. package/dist/fr-FR.main.js +0 -44
  85. package/dist/fr-FR.main.js.map +0 -1
  86. package/dist/fr-FR.mjs +0 -46
  87. package/dist/fr-FR.module.js +0 -46
  88. package/dist/fr-FR.module.js.map +0 -1
  89. package/dist/he-IL.main.js +0 -44
  90. package/dist/he-IL.main.js.map +0 -1
  91. package/dist/he-IL.mjs +0 -46
  92. package/dist/he-IL.module.js +0 -46
  93. package/dist/he-IL.module.js.map +0 -1
  94. package/dist/hr-HR.main.js +0 -44
  95. package/dist/hr-HR.main.js.map +0 -1
  96. package/dist/hr-HR.mjs +0 -46
  97. package/dist/hr-HR.module.js +0 -46
  98. package/dist/hr-HR.module.js.map +0 -1
  99. package/dist/hu-HU.main.js +0 -44
  100. package/dist/hu-HU.main.js.map +0 -1
  101. package/dist/hu-HU.mjs +0 -46
  102. package/dist/hu-HU.module.js +0 -46
  103. package/dist/hu-HU.module.js.map +0 -1
  104. package/dist/intlStrings.main.js +0 -108
  105. package/dist/intlStrings.main.js.map +0 -1
  106. package/dist/intlStrings.mjs +0 -110
  107. package/dist/intlStrings.module.js +0 -110
  108. package/dist/intlStrings.module.js.map +0 -1
  109. package/dist/it-IT.main.js +0 -44
  110. package/dist/it-IT.main.js.map +0 -1
  111. package/dist/it-IT.mjs +0 -46
  112. package/dist/it-IT.module.js +0 -46
  113. package/dist/it-IT.module.js.map +0 -1
  114. package/dist/ja-JP.main.js +0 -44
  115. package/dist/ja-JP.main.js.map +0 -1
  116. package/dist/ja-JP.mjs +0 -46
  117. package/dist/ja-JP.module.js +0 -46
  118. package/dist/ja-JP.module.js.map +0 -1
  119. package/dist/ko-KR.main.js +0 -44
  120. package/dist/ko-KR.main.js.map +0 -1
  121. package/dist/ko-KR.mjs +0 -46
  122. package/dist/ko-KR.module.js +0 -46
  123. package/dist/ko-KR.module.js.map +0 -1
  124. package/dist/lt-LT.main.js +0 -44
  125. package/dist/lt-LT.main.js.map +0 -1
  126. package/dist/lt-LT.mjs +0 -46
  127. package/dist/lt-LT.module.js +0 -46
  128. package/dist/lt-LT.module.js.map +0 -1
  129. package/dist/lv-LV.main.js +0 -44
  130. package/dist/lv-LV.main.js.map +0 -1
  131. package/dist/lv-LV.mjs +0 -46
  132. package/dist/lv-LV.module.js +0 -46
  133. package/dist/lv-LV.module.js.map +0 -1
  134. package/dist/nb-NO.main.js +0 -44
  135. package/dist/nb-NO.main.js.map +0 -1
  136. package/dist/nb-NO.mjs +0 -46
  137. package/dist/nb-NO.module.js +0 -46
  138. package/dist/nb-NO.module.js.map +0 -1
  139. package/dist/nl-NL.main.js +0 -44
  140. package/dist/nl-NL.main.js.map +0 -1
  141. package/dist/nl-NL.mjs +0 -46
  142. package/dist/nl-NL.module.js +0 -46
  143. package/dist/nl-NL.module.js.map +0 -1
  144. package/dist/pl-PL.main.js +0 -44
  145. package/dist/pl-PL.main.js.map +0 -1
  146. package/dist/pl-PL.mjs +0 -46
  147. package/dist/pl-PL.module.js +0 -46
  148. package/dist/pl-PL.module.js.map +0 -1
  149. package/dist/pt-BR.main.js +0 -44
  150. package/dist/pt-BR.main.js.map +0 -1
  151. package/dist/pt-BR.mjs +0 -46
  152. package/dist/pt-BR.module.js +0 -46
  153. package/dist/pt-BR.module.js.map +0 -1
  154. package/dist/pt-PT.main.js +0 -44
  155. package/dist/pt-PT.main.js.map +0 -1
  156. package/dist/pt-PT.mjs +0 -46
  157. package/dist/pt-PT.module.js +0 -46
  158. package/dist/pt-PT.module.js.map +0 -1
  159. package/dist/ro-RO.main.js +0 -44
  160. package/dist/ro-RO.main.js.map +0 -1
  161. package/dist/ro-RO.mjs +0 -46
  162. package/dist/ro-RO.module.js +0 -46
  163. package/dist/ro-RO.module.js.map +0 -1
  164. package/dist/ru-RU.main.js +0 -44
  165. package/dist/ru-RU.main.js.map +0 -1
  166. package/dist/ru-RU.mjs +0 -46
  167. package/dist/ru-RU.module.js +0 -46
  168. package/dist/ru-RU.module.js.map +0 -1
  169. package/dist/sk-SK.main.js +0 -44
  170. package/dist/sk-SK.main.js.map +0 -1
  171. package/dist/sk-SK.mjs +0 -46
  172. package/dist/sk-SK.module.js +0 -46
  173. package/dist/sk-SK.module.js.map +0 -1
  174. package/dist/sl-SI.main.js +0 -44
  175. package/dist/sl-SI.main.js.map +0 -1
  176. package/dist/sl-SI.mjs +0 -46
  177. package/dist/sl-SI.module.js +0 -46
  178. package/dist/sl-SI.module.js.map +0 -1
  179. package/dist/sr-SP.main.js +0 -44
  180. package/dist/sr-SP.main.js.map +0 -1
  181. package/dist/sr-SP.mjs +0 -46
  182. package/dist/sr-SP.module.js +0 -46
  183. package/dist/sr-SP.module.js.map +0 -1
  184. package/dist/sv-SE.main.js +0 -44
  185. package/dist/sv-SE.main.js.map +0 -1
  186. package/dist/sv-SE.mjs +0 -46
  187. package/dist/sv-SE.module.js +0 -46
  188. package/dist/sv-SE.module.js.map +0 -1
  189. package/dist/tr-TR.main.js +0 -44
  190. package/dist/tr-TR.main.js.map +0 -1
  191. package/dist/tr-TR.mjs +0 -46
  192. package/dist/tr-TR.module.js +0 -46
  193. package/dist/tr-TR.module.js.map +0 -1
  194. package/dist/types.d.ts +0 -244
  195. package/dist/types.d.ts.map +0 -1
  196. package/dist/uk-UA.main.js +0 -44
  197. package/dist/uk-UA.main.js.map +0 -1
  198. package/dist/uk-UA.mjs +0 -46
  199. package/dist/uk-UA.module.js +0 -46
  200. package/dist/uk-UA.module.js.map +0 -1
  201. package/dist/useAutoScroll.main.js +0 -89
  202. package/dist/useAutoScroll.main.js.map +0 -1
  203. package/dist/useAutoScroll.mjs +0 -84
  204. package/dist/useAutoScroll.module.js +0 -84
  205. package/dist/useAutoScroll.module.js.map +0 -1
  206. package/dist/useClipboard.main.js +0 -111
  207. package/dist/useClipboard.main.js.map +0 -1
  208. package/dist/useClipboard.mjs +0 -106
  209. package/dist/useClipboard.module.js +0 -106
  210. package/dist/useClipboard.module.js.map +0 -1
  211. package/dist/useDrag.main.js +0 -307
  212. package/dist/useDrag.main.js.map +0 -1
  213. package/dist/useDrag.mjs +0 -302
  214. package/dist/useDrag.module.js +0 -302
  215. package/dist/useDrag.module.js.map +0 -1
  216. package/dist/useDraggableCollection.main.js +0 -27
  217. package/dist/useDraggableCollection.main.js.map +0 -1
  218. package/dist/useDraggableCollection.mjs +0 -22
  219. package/dist/useDraggableCollection.module.js +0 -22
  220. package/dist/useDraggableCollection.module.js.map +0 -1
  221. package/dist/useDraggableItem.main.js +0 -128
  222. package/dist/useDraggableItem.main.js.map +0 -1
  223. package/dist/useDraggableItem.mjs +0 -123
  224. package/dist/useDraggableItem.module.js +0 -123
  225. package/dist/useDraggableItem.module.js.map +0 -1
  226. package/dist/useDrop.main.js +0 -295
  227. package/dist/useDrop.main.js.map +0 -1
  228. package/dist/useDrop.mjs +0 -290
  229. package/dist/useDrop.module.js +0 -290
  230. package/dist/useDrop.module.js.map +0 -1
  231. package/dist/useDropIndicator.main.js +0 -102
  232. package/dist/useDropIndicator.main.js.map +0 -1
  233. package/dist/useDropIndicator.mjs +0 -97
  234. package/dist/useDropIndicator.module.js +0 -97
  235. package/dist/useDropIndicator.module.js.map +0 -1
  236. package/dist/useDroppableCollection.main.js +0 -536
  237. package/dist/useDroppableCollection.main.js.map +0 -1
  238. package/dist/useDroppableCollection.mjs +0 -531
  239. package/dist/useDroppableCollection.module.js +0 -531
  240. package/dist/useDroppableCollection.module.js.map +0 -1
  241. package/dist/useDroppableItem.main.js +0 -81
  242. package/dist/useDroppableItem.main.js.map +0 -1
  243. package/dist/useDroppableItem.mjs +0 -76
  244. package/dist/useDroppableItem.module.js +0 -76
  245. package/dist/useDroppableItem.module.js.map +0 -1
  246. package/dist/useVirtualDrop.main.js +0 -56
  247. package/dist/useVirtualDrop.main.js.map +0 -1
  248. package/dist/useVirtualDrop.mjs +0 -51
  249. package/dist/useVirtualDrop.module.js +0 -51
  250. package/dist/useVirtualDrop.module.js.map +0 -1
  251. package/dist/utils.main.js +0 -285
  252. package/dist/utils.main.js.map +0 -1
  253. package/dist/utils.mjs +0 -257
  254. package/dist/utils.module.js +0 -257
  255. package/dist/utils.module.js.map +0 -1
  256. package/dist/zh-CN.main.js +0 -44
  257. package/dist/zh-CN.main.js.map +0 -1
  258. package/dist/zh-CN.mjs +0 -46
  259. package/dist/zh-CN.module.js +0 -46
  260. package/dist/zh-CN.module.js.map +0 -1
  261. package/dist/zh-TW.main.js +0 -44
  262. package/dist/zh-TW.main.js.map +0 -1
  263. package/dist/zh-TW.mjs +0 -46
  264. package/dist/zh-TW.module.js +0 -46
  265. package/dist/zh-TW.module.js.map +0 -1
  266. package/src/DragManager.ts +0 -659
  267. package/src/DragPreview.tsx +0 -81
  268. package/src/DropTargetKeyboardNavigation.ts +0 -293
  269. package/src/ListDropTargetDelegate.ts +0 -182
  270. package/src/constants.ts +0 -85
  271. package/src/useAutoScroll.ts +0 -105
  272. package/src/useClipboard.ts +0 -151
  273. package/src/useDrag.ts +0 -390
  274. package/src/useDraggableCollection.ts +0 -29
  275. package/src/useDraggableItem.ts +0 -158
  276. package/src/useDrop.ts +0 -444
  277. package/src/useDropIndicator.ts +0 -124
  278. package/src/useDroppableCollection.ts +0 -665
  279. package/src/useDroppableItem.ts +0 -86
  280. package/src/useVirtualDrop.ts +0 -49
  281. package/src/utils.ts +0 -385
@@ -1,81 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {DragItem, DragPreviewRenderer} from '@react-types/shared';
14
- import {flushSync} from 'react-dom';
15
- import React, {ForwardedRef, JSX, useEffect, useImperativeHandle, useRef, useState} from 'react';
16
-
17
- export interface DragPreviewProps {
18
- /**
19
- * A render function which returns a preview element, or an object containing the element
20
- * and a custom offset. If an object is returned, the provided `x` and `y` values will be
21
- * used as the drag preview offset instead of the default calculation.
22
- */
23
- children: (items: DragItem[]) => JSX.Element | {element: JSX.Element, x: number, y: number} | null
24
- }
25
-
26
- export const DragPreview:
27
- React.ForwardRefExoticComponent<DragPreviewProps & React.RefAttributes<DragPreviewRenderer | null>> =
28
- React.forwardRef(function DragPreview(props: DragPreviewProps, ref: ForwardedRef<DragPreviewRenderer | null>) {
29
- let render = props.children;
30
- let [children, setChildren] = useState<JSX.Element | null>(null);
31
- let domRef = useRef<HTMLDivElement | null>(null);
32
- let raf = useRef<ReturnType<typeof requestAnimationFrame> | undefined>(undefined);
33
-
34
- useImperativeHandle(ref, () => (items: DragItem[], callback: (node: HTMLElement | null, x?: number, y?: number) => void) => {
35
- // This will be called during the onDragStart event by useDrag. We need to render the
36
- // preview synchronously before this event returns so we can call event.dataTransfer.setDragImage.
37
-
38
- let result = render(items);
39
- let element: JSX.Element | null;
40
- let offsetX: number | undefined;
41
- let offsetY: number | undefined;
42
-
43
- if (result && typeof result === 'object' && 'element' in result) {
44
- element = result.element;
45
- offsetX = result.x;
46
- offsetY = result.y;
47
- } else {
48
- element = result as JSX.Element | null;
49
- }
50
-
51
- flushSync(() => {
52
- setChildren(element);
53
- });
54
-
55
- // Yield back to useDrag to set the drag image.
56
- callback(domRef.current, offsetX, offsetY);
57
-
58
- // Remove the preview from the DOM after a frame so the browser has time to paint.
59
- raf.current = requestAnimationFrame(() => {
60
- setChildren(null);
61
- });
62
- }, [render]);
63
-
64
- useEffect(() => {
65
- return () => {
66
- if (raf.current) {
67
- cancelAnimationFrame(raf.current);
68
- }
69
- };
70
- }, []);
71
-
72
- if (!children) {
73
- return null;
74
- }
75
-
76
- return (
77
- <div style={{zIndex: -100, position: 'fixed', top: 0, left: -100000}} ref={domRef}>
78
- {children}
79
- </div>
80
- );
81
- });
@@ -1,293 +0,0 @@
1
- import {Collection, DropTarget, Key, KeyboardDelegate, Node} from '@react-types/shared';
2
- import {getChildNodes} from '@react-stately/collections';
3
-
4
- export function navigate(
5
- keyboardDelegate: KeyboardDelegate,
6
- collection: Collection<Node<unknown>>,
7
- target: DropTarget | null | undefined,
8
- direction: 'left' | 'right' | 'up' | 'down',
9
- rtl = false,
10
- wrap = false
11
- ): DropTarget | null {
12
- switch (direction) {
13
- case 'left':
14
- return rtl
15
- ? nextDropTarget(keyboardDelegate, collection, target, wrap, 'left')
16
- : previousDropTarget(keyboardDelegate, collection, target, wrap, 'left');
17
- case 'right':
18
- return rtl
19
- ? previousDropTarget(keyboardDelegate, collection, target, wrap, 'right')
20
- : nextDropTarget(keyboardDelegate, collection, target, wrap, 'right');
21
- case 'up':
22
- return previousDropTarget(keyboardDelegate, collection, target, wrap);
23
- case 'down':
24
- return nextDropTarget(keyboardDelegate, collection, target, wrap);
25
- }
26
- }
27
-
28
- function nextDropTarget(
29
- keyboardDelegate: KeyboardDelegate,
30
- collection: Collection<Node<unknown>>,
31
- target: DropTarget | null | undefined,
32
- wrap = false,
33
- horizontal: 'left' | 'right' | null = null
34
- ): DropTarget | null {
35
- if (!target) {
36
- return {
37
- type: 'root'
38
- };
39
- }
40
-
41
- if (target.type === 'root') {
42
- let nextKey = keyboardDelegate.getFirstKey?.() ?? null;
43
- if (nextKey != null) {
44
- return {
45
- type: 'item',
46
- key: nextKey,
47
- dropPosition: 'before'
48
- };
49
- }
50
-
51
- return null;
52
- }
53
-
54
- if (target.type === 'item') {
55
- let nextKey: Key | null | undefined = null;
56
- if (horizontal) {
57
- nextKey = horizontal === 'right' ? keyboardDelegate.getKeyRightOf?.(target.key) : keyboardDelegate.getKeyLeftOf?.(target.key);
58
- } else {
59
- nextKey = keyboardDelegate.getKeyBelow?.(target.key);
60
- }
61
- let nextCollectionKey = getNextItem(collection, target.key, key => collection.getKeyAfter(key));
62
-
63
- // If the keyboard delegate did not move to the next key in the collection,
64
- // jump to that key with the same drop position. Otherwise, try the other
65
- // drop positions on the current key first.
66
- if (nextKey != null && nextKey !== nextCollectionKey) {
67
- return {
68
- type: 'item',
69
- key: nextKey,
70
- dropPosition: target.dropPosition
71
- };
72
- }
73
-
74
- switch (target.dropPosition) {
75
- case 'before': {
76
- return {
77
- type: 'item',
78
- key: target.key,
79
- dropPosition: 'on'
80
- };
81
- }
82
- case 'on': {
83
- // If there are nested items, traverse to them prior to the "after" position of this target.
84
- // If the next key is on the same level, then its "before" position is equivalent to this item's "after" position.
85
- let targetNode = collection.getItem(target.key);
86
- let nextNode = nextKey != null ? collection.getItem(nextKey) : null;
87
- if (targetNode && nextNode && nextNode.level >= targetNode.level) {
88
- return {
89
- type: 'item',
90
- key: nextNode.key,
91
- dropPosition: 'before'
92
- };
93
- }
94
-
95
- return {
96
- type: 'item',
97
- key: target.key,
98
- dropPosition: 'after'
99
- };
100
- }
101
- case 'after': {
102
- // If this is the last sibling in a level, traverse to the parent.
103
- let targetNode = collection.getItem(target.key);
104
- let nextItemInSameLevel = targetNode?.nextKey != null ? collection.getItem(targetNode.nextKey) : null;
105
- while (nextItemInSameLevel != null && nextItemInSameLevel.type !== 'item') {
106
- nextItemInSameLevel = nextItemInSameLevel.nextKey != null ? collection.getItem(nextItemInSameLevel.nextKey) : null;
107
- }
108
-
109
- if (targetNode && nextItemInSameLevel == null && targetNode.parentKey != null) {
110
- // If the parent item has an item after it, use the "before" position.
111
- let parentNode = collection.getItem(targetNode.parentKey);
112
- const nextNode = parentNode?.nextKey != null ? collection.getItem(parentNode.nextKey) : null;
113
- if (nextNode?.type === 'item') {
114
- return {
115
- type: 'item',
116
- key: nextNode.key,
117
- dropPosition: 'before'
118
- };
119
- }
120
-
121
- if (parentNode?.type === 'item') {
122
- return {
123
- type: 'item',
124
- key: parentNode.key,
125
- dropPosition: 'after'
126
- };
127
- }
128
- }
129
-
130
- if (nextItemInSameLevel) {
131
- return {
132
- type: 'item',
133
- key: nextItemInSameLevel.key,
134
- dropPosition: 'on'
135
- };
136
- }
137
- }
138
- }
139
- }
140
-
141
- if (wrap) {
142
- return {
143
- type: 'root'
144
- };
145
- }
146
-
147
- return null;
148
- }
149
-
150
- function previousDropTarget(
151
- keyboardDelegate: KeyboardDelegate,
152
- collection: Collection<Node<unknown>>,
153
- target: DropTarget | null | undefined,
154
- wrap = false,
155
- horizontal: 'left' | 'right' | null = null
156
- ): DropTarget | null {
157
- // Start after the last root-level item.
158
- if (!target || (wrap && target.type === 'root')) {
159
- // Keyboard delegate gets the deepest item but we want the shallowest.
160
- let prevKey: Key | null = null;
161
- let lastKey = keyboardDelegate.getLastKey?.();
162
- while (lastKey != null) {
163
- let node = collection.getItem(lastKey);
164
- if (node?.type !== 'item') {
165
- break;
166
- }
167
- prevKey = lastKey;
168
- lastKey = node?.parentKey;
169
- }
170
-
171
- if (prevKey != null) {
172
- return {
173
- type: 'item',
174
- key: prevKey,
175
- dropPosition: 'after'
176
- };
177
- }
178
-
179
- return null;
180
- }
181
-
182
- if (target.type === 'item') {
183
- let prevKey: Key | null | undefined = null;
184
- if (horizontal) {
185
- prevKey = horizontal === 'left' ? keyboardDelegate.getKeyLeftOf?.(target.key) : keyboardDelegate.getKeyRightOf?.(target.key);
186
- } else {
187
- prevKey = keyboardDelegate.getKeyAbove?.(target.key);
188
- }
189
- let prevCollectionKey = getNextItem(collection, target.key, key => collection.getKeyBefore(key));
190
-
191
- // If the keyboard delegate did not move to the next key in the collection,
192
- // jump to that key with the same drop position. Otherwise, try the other
193
- // drop positions on the current key first.
194
- if (prevKey != null && prevKey !== prevCollectionKey) {
195
- return {
196
- type: 'item',
197
- key: prevKey,
198
- dropPosition: target.dropPosition
199
- };
200
- }
201
-
202
- switch (target.dropPosition) {
203
- case 'before': {
204
- // Move after the last child of the previous item.
205
- let targetNode = collection.getItem(target.key);
206
- if (targetNode && targetNode.prevKey != null) {
207
- let lastChild = getLastChild(collection, targetNode.prevKey);
208
- if (lastChild) {
209
- return lastChild;
210
- }
211
- }
212
-
213
- if (prevKey != null) {
214
- return {
215
- type: 'item',
216
- key: prevKey,
217
- dropPosition: 'on'
218
- };
219
- }
220
-
221
- return {
222
- type: 'root'
223
- };
224
- }
225
- case 'on': {
226
- return {
227
- type: 'item',
228
- key: target.key,
229
- dropPosition: 'before'
230
- };
231
- }
232
- case 'after': {
233
- // Move after the last child of this item.
234
- let lastChild = getLastChild(collection, target.key);
235
- if (lastChild) {
236
- return lastChild;
237
- }
238
-
239
- return {
240
- type: 'item',
241
- key: target.key,
242
- dropPosition: 'on'
243
- };
244
- }
245
- }
246
- }
247
-
248
- if (target.type !== 'root') {
249
- return {
250
- type: 'root'
251
- };
252
- }
253
-
254
- return null;
255
- }
256
-
257
- function getLastChild(collection: Collection<Node<unknown>>, key: Key): DropTarget | null {
258
- // getChildNodes still returns child tree items even when the item is collapsed.
259
- // Checking if the next item has a greater level is a silly way to determine if the item is expanded.
260
- let targetNode = collection.getItem(key);
261
- let nextKey = getNextItem(collection, key, key => collection.getKeyAfter(key));
262
- let nextNode = nextKey != null ? collection.getItem(nextKey) : null;
263
- if (targetNode && nextNode && nextNode.level > targetNode.level) {
264
- let children = getChildNodes(targetNode, collection);
265
- let lastChild: Node<unknown> | null = null;
266
- for (let child of children) {
267
- if (child.type === 'item') {
268
- lastChild = child;
269
- }
270
- }
271
-
272
- if (lastChild) {
273
- return {
274
- type: 'item',
275
- key: lastChild.key,
276
- dropPosition: 'after'
277
- };
278
- }
279
- }
280
-
281
- return null;
282
- }
283
-
284
- // Find the next or previous item in a collection, skipping over other types of nodes (e.g. content).
285
- function getNextItem(collection: Collection<Node<unknown>>, key: Key, getNextKey: (key: Key) => Key | null): Key | null {
286
- let nextCollectionKey = getNextKey(key);
287
- let nextCollectionNode = nextCollectionKey != null ? collection.getItem(nextCollectionKey) : null;
288
- while (nextCollectionNode && nextCollectionNode.type !== 'item') {
289
- nextCollectionKey = getNextKey(nextCollectionNode.key);
290
- nextCollectionNode = nextCollectionKey != null ? collection.getItem(nextCollectionKey) : null;
291
- }
292
- return nextCollectionKey;
293
- }
@@ -1,182 +0,0 @@
1
- import {Direction, DropTarget, DropTargetDelegate, Node, Orientation, RefObject} from '@react-types/shared';
2
-
3
- interface ListDropTargetDelegateOptions {
4
- /**
5
- * Whether the items are arranged in a stack or grid.
6
- * @default 'stack'
7
- */
8
- layout?: 'stack' | 'grid',
9
- /**
10
- * The primary orientation of the items. Usually this is the
11
- * direction that the collection scrolls.
12
- * @default 'vertical'
13
- */
14
- orientation?: Orientation,
15
- /**
16
- * The horizontal layout direction.
17
- * @default 'ltr'
18
- */
19
- direction?: Direction
20
- }
21
-
22
- // Terms used in the below code:
23
- // * "Primary" – The main layout direction. For stacks, this is the direction
24
- // that the stack is arranged in (e.g. horizontal or vertical).
25
- // For grids, this is the main scroll direction.
26
- // * "Secondary" – The secondary layout direction. For stacks, there is no secondary
27
- // layout direction. For grids, this is the opposite of the primary direction.
28
- // * "Flow" – The flow direction of the items. For stacks, this is the the primary
29
- // direction. For grids, it is the secondary direction.
30
-
31
- export class ListDropTargetDelegate implements DropTargetDelegate {
32
- private collection: Iterable<Node<unknown>>;
33
- private ref: RefObject<HTMLElement | null>;
34
- private layout: 'stack' | 'grid';
35
- private orientation: Orientation;
36
- protected direction: Direction;
37
-
38
- constructor(collection: Iterable<Node<unknown>>, ref: RefObject<HTMLElement | null>, options?: ListDropTargetDelegateOptions) {
39
- this.collection = collection;
40
- this.ref = ref;
41
- this.layout = options?.layout || 'stack';
42
- this.orientation = options?.orientation || 'vertical';
43
- this.direction = options?.direction || 'ltr';
44
- }
45
-
46
- private getPrimaryStart(rect: DOMRect) {
47
- return this.orientation === 'horizontal' ? rect.left : rect.top;
48
- }
49
-
50
- private getPrimaryEnd(rect: DOMRect) {
51
- return this.orientation === 'horizontal' ? rect.right : rect.bottom;
52
- }
53
-
54
- private getSecondaryStart(rect: DOMRect) {
55
- return this.orientation === 'horizontal' ? rect.top : rect.left;
56
- }
57
-
58
- private getSecondaryEnd(rect: DOMRect) {
59
- return this.orientation === 'horizontal' ? rect.bottom : rect.right;
60
- }
61
-
62
- private getFlowStart(rect: DOMRect) {
63
- return this.layout === 'stack' ? this.getPrimaryStart(rect) : this.getSecondaryStart(rect);
64
- }
65
-
66
- private getFlowEnd(rect: DOMRect) {
67
- return this.layout === 'stack' ? this.getPrimaryEnd(rect) : this.getSecondaryEnd(rect);
68
- }
69
-
70
- private getFlowSize(rect: DOMRect) {
71
- return this.getFlowEnd(rect) - this.getFlowStart(rect);
72
- }
73
-
74
- getDropTargetFromPoint(x: number, y: number, isValidDropTarget: (target: DropTarget) => boolean): DropTarget {
75
- if (this.collection[Symbol.iterator]().next().done || !this.ref.current) {
76
- return {type: 'root'};
77
- }
78
-
79
- let rect: DOMRect | undefined = this.ref.current.getBoundingClientRect();
80
- let primary = this.orientation === 'horizontal' ? x : y;
81
- let secondary = this.orientation === 'horizontal' ? y : x;
82
- primary += this.getPrimaryStart(rect);
83
- secondary += this.getSecondaryStart(rect);
84
-
85
- let flow = this.layout === 'stack' ? primary : secondary;
86
- let isPrimaryRTL = this.orientation === 'horizontal' && this.direction === 'rtl';
87
- let isSecondaryRTL = this.layout === 'grid' && this.orientation === 'vertical' && this.direction === 'rtl';
88
- let isFlowRTL = this.layout === 'stack' ? isPrimaryRTL : isSecondaryRTL;
89
-
90
- let collection = this.ref.current?.dataset.collection;
91
- let elements = this.ref.current.querySelectorAll(collection ? `[data-collection="${CSS.escape(collection)}"]` : '[data-key]');
92
- let elementMap = new Map<string, HTMLElement>();
93
- for (let item of elements) {
94
- if (item instanceof HTMLElement && item.dataset.key != null) {
95
- elementMap.set(item.dataset.key, item);
96
- }
97
- }
98
-
99
- // TODO: assume that only item type items are valid drop targets. This is to prevent a crash when dragging over the loader
100
- // row since it doesn't have a data-key set on it. Will eventually need to handle the case with drag and drop and loaders located between rows aka tree.
101
- // Can see https://github.com/adobe/react-spectrum/pull/4210/files#diff-21e555e0c597a28215e36137f5be076a65a1e1456c92cd0fdd60f866929aae2a for additional logic
102
- // that may need to happen then
103
- let items = [...this.collection].filter(item => item.type === 'item');
104
-
105
- if (items.length < 1) {
106
- return {type: 'root'};
107
- }
108
-
109
- let low = 0;
110
- let high = items.length;
111
- while (low < high) {
112
- let mid = Math.floor((low + high) / 2);
113
- let item = items[mid];
114
- let element = elementMap.get(String(item.key));
115
- if (!element) {
116
- break;
117
- }
118
- let rect = element.getBoundingClientRect();
119
- let update = (isGreater: boolean) => {
120
- if (isGreater) {
121
- low = mid + 1;
122
- } else {
123
- high = mid;
124
- }
125
- };
126
-
127
- if (primary < this.getPrimaryStart(rect)) {
128
- update(isPrimaryRTL);
129
- } else if (primary > this.getPrimaryEnd(rect)) {
130
- update(!isPrimaryRTL);
131
- } else if (secondary < this.getSecondaryStart(rect)) {
132
- update(isSecondaryRTL);
133
- } else if (secondary > this.getSecondaryEnd(rect)) {
134
- update(!isSecondaryRTL);
135
- } else {
136
- let target: DropTarget = {
137
- type: 'item',
138
- key: item.key,
139
- dropPosition: 'on'
140
- };
141
-
142
- if (isValidDropTarget(target)) {
143
- // Otherwise, if dropping on the item is accepted, try the before/after positions if within 5px
144
- // of the start or end of the item.
145
- if (flow <= this.getFlowStart(rect) + 5 && isValidDropTarget({...target, dropPosition: 'before'})) {
146
- target.dropPosition = isFlowRTL ? 'after' : 'before';
147
- } else if (flow >= this.getFlowEnd(rect) - 5 && isValidDropTarget({...target, dropPosition: 'after'})) {
148
- target.dropPosition = isFlowRTL ? 'before' : 'after';
149
- }
150
- } else {
151
- // If dropping on the item isn't accepted, try the target before or after depending on the position.
152
- let mid = this.getFlowStart(rect) + this.getFlowSize(rect) / 2;
153
- if (flow <= mid && isValidDropTarget({...target, dropPosition: 'before'})) {
154
- target.dropPosition = isFlowRTL ? 'after' : 'before';
155
- } else if (flow >= mid && isValidDropTarget({...target, dropPosition: 'after'})) {
156
- target.dropPosition = isFlowRTL ? 'before' : 'after';
157
- }
158
- }
159
-
160
- return target;
161
- }
162
- }
163
-
164
- let item = items[Math.min(low, items.length - 1)];
165
- let element = elementMap.get(String(item.key));
166
- rect = element?.getBoundingClientRect();
167
-
168
- if (rect && (primary < this.getPrimaryStart(rect) || Math.abs(flow - this.getFlowStart(rect)) < Math.abs(flow - this.getFlowEnd(rect)))) {
169
- return {
170
- type: 'item',
171
- key: item.key,
172
- dropPosition: isFlowRTL ? 'after' : 'before'
173
- };
174
- }
175
-
176
- return {
177
- type: 'item',
178
- key: item.key,
179
- dropPosition: isFlowRTL ? 'before' : 'after'
180
- };
181
- }
182
- }
package/src/constants.ts DELETED
@@ -1,85 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {DropOperation} from '@react-types/shared';
14
-
15
- export interface IDropOperation {
16
- readonly none: 0,
17
- readonly cancel: 0,
18
- readonly move: number,
19
- readonly copy: number,
20
- readonly link: number,
21
- readonly all: number
22
- }
23
-
24
- export enum DROP_OPERATION {
25
- none = 0,
26
- cancel = 0,
27
- move = 1 << 0,
28
- copy = 1 << 1,
29
- link = 1 << 2,
30
- all = move | copy | link
31
- }
32
- interface DropOperationAllowed extends IDropOperation {
33
- readonly copyMove: number,
34
- readonly copyLink: number,
35
- readonly linkMove: number,
36
- readonly all: number,
37
- readonly uninitialized: number
38
- }
39
- // See https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/effectAllowed
40
- export const DROP_OPERATION_ALLOWED: DropOperationAllowed = {
41
- ...DROP_OPERATION,
42
- copyMove: DROP_OPERATION.copy | DROP_OPERATION.move,
43
- copyLink: DROP_OPERATION.copy | DROP_OPERATION.link,
44
- linkMove: DROP_OPERATION.link | DROP_OPERATION.move,
45
- all: DROP_OPERATION.all,
46
- uninitialized: DROP_OPERATION.all
47
- };
48
-
49
- interface EffectAllowed {
50
- 0: 'none' | 'cancel',
51
- 1: 'move',
52
- 2: 'copy',
53
- 3: 'copyMove',
54
- 4: 'link',
55
- 5: 'linkMove',
56
- 6: 'copyLink',
57
- 7: 'all'
58
- }
59
-
60
- export const EFFECT_ALLOWED: EffectAllowed = invert(DROP_OPERATION_ALLOWED) as unknown as EffectAllowed;
61
- EFFECT_ALLOWED[DROP_OPERATION.all] = 'all'; // ensure we don't map to 'uninitialized'.
62
-
63
- type DropEffect = 'none' | 'link' | 'copy' | 'move';
64
-
65
- export const DROP_EFFECT_TO_DROP_OPERATION: {[K in DropEffect]: DropOperation} = {
66
- none: 'cancel',
67
- link: 'link',
68
- copy: 'copy',
69
- move: 'move'
70
- };
71
-
72
- export const DROP_OPERATION_TO_DROP_EFFECT: {[K in DropOperation]: DropEffect} = invert(DROP_EFFECT_TO_DROP_OPERATION);
73
-
74
- function invert<T extends string | number, C extends string | number>(object: Record<T, C>): Record<C, T> {
75
- let res: Record<C, T> = {} as Record<C, T>;
76
- for (let key in object) {
77
- res[object[key]] = key as T;
78
- }
79
-
80
- return res;
81
- }
82
-
83
- export const NATIVE_DRAG_TYPES: Set<string> = new Set(['text/plain', 'text/uri-list', 'text/html']);
84
- export const CUSTOM_DRAG_TYPE = 'application/vnd.react-aria.items+json';
85
- export const GENERIC_TYPE = 'application/octet-stream';