@react-aria/dnd 3.0.0-alpha.9 → 3.0.0-nightly-4980928d3-240906

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 (275) hide show
  1. package/dist/DragManager.main.js +432 -0
  2. package/dist/DragManager.main.js.map +1 -0
  3. package/dist/DragManager.mjs +423 -0
  4. package/dist/DragManager.module.js +423 -0
  5. package/dist/DragManager.module.js.map +1 -0
  6. package/dist/DragPreview.main.js +59 -0
  7. package/dist/DragPreview.main.js.map +1 -0
  8. package/dist/DragPreview.mjs +50 -0
  9. package/dist/DragPreview.module.js +50 -0
  10. package/dist/DragPreview.module.js.map +1 -0
  11. package/dist/ListDropTargetDelegate.main.js +123 -0
  12. package/dist/ListDropTargetDelegate.main.js.map +1 -0
  13. package/dist/ListDropTargetDelegate.mjs +118 -0
  14. package/dist/ListDropTargetDelegate.module.js +118 -0
  15. package/dist/ListDropTargetDelegate.module.js.map +1 -0
  16. package/dist/ar-AE.main.js +44 -0
  17. package/dist/ar-AE.main.js.map +1 -0
  18. package/dist/ar-AE.mjs +46 -0
  19. package/dist/ar-AE.module.js +46 -0
  20. package/dist/ar-AE.module.js.map +1 -0
  21. package/dist/bg-BG.main.js +44 -0
  22. package/dist/bg-BG.main.js.map +1 -0
  23. package/dist/bg-BG.mjs +46 -0
  24. package/dist/bg-BG.module.js +46 -0
  25. package/dist/bg-BG.module.js.map +1 -0
  26. package/dist/constants.main.js +65 -0
  27. package/dist/constants.main.js.map +1 -0
  28. package/dist/constants.mjs +53 -0
  29. package/dist/constants.module.js +53 -0
  30. package/dist/constants.module.js.map +1 -0
  31. package/dist/cs-CZ.main.js +45 -0
  32. package/dist/cs-CZ.main.js.map +1 -0
  33. package/dist/cs-CZ.mjs +47 -0
  34. package/dist/cs-CZ.module.js +47 -0
  35. package/dist/cs-CZ.module.js.map +1 -0
  36. package/dist/da-DK.main.js +44 -0
  37. package/dist/da-DK.main.js.map +1 -0
  38. package/dist/da-DK.mjs +46 -0
  39. package/dist/da-DK.module.js +46 -0
  40. package/dist/da-DK.module.js.map +1 -0
  41. package/dist/de-DE.main.js +44 -0
  42. package/dist/de-DE.main.js.map +1 -0
  43. package/dist/de-DE.mjs +46 -0
  44. package/dist/de-DE.module.js +46 -0
  45. package/dist/de-DE.module.js.map +1 -0
  46. package/dist/el-GR.main.js +44 -0
  47. package/dist/el-GR.main.js.map +1 -0
  48. package/dist/el-GR.mjs +46 -0
  49. package/dist/el-GR.module.js +46 -0
  50. package/dist/el-GR.module.js.map +1 -0
  51. package/dist/en-US.main.js +44 -0
  52. package/dist/en-US.main.js.map +1 -0
  53. package/dist/en-US.mjs +46 -0
  54. package/dist/en-US.module.js +46 -0
  55. package/dist/en-US.module.js.map +1 -0
  56. package/dist/es-ES.main.js +44 -0
  57. package/dist/es-ES.main.js.map +1 -0
  58. package/dist/es-ES.mjs +46 -0
  59. package/dist/es-ES.module.js +46 -0
  60. package/dist/es-ES.module.js.map +1 -0
  61. package/dist/et-EE.main.js +44 -0
  62. package/dist/et-EE.main.js.map +1 -0
  63. package/dist/et-EE.mjs +46 -0
  64. package/dist/et-EE.module.js +46 -0
  65. package/dist/et-EE.module.js.map +1 -0
  66. package/dist/fi-FI.main.js +44 -0
  67. package/dist/fi-FI.main.js.map +1 -0
  68. package/dist/fi-FI.mjs +46 -0
  69. package/dist/fi-FI.module.js +46 -0
  70. package/dist/fi-FI.module.js.map +1 -0
  71. package/dist/fr-FR.main.js +44 -0
  72. package/dist/fr-FR.main.js.map +1 -0
  73. package/dist/fr-FR.mjs +46 -0
  74. package/dist/fr-FR.module.js +46 -0
  75. package/dist/fr-FR.module.js.map +1 -0
  76. package/dist/he-IL.main.js +44 -0
  77. package/dist/he-IL.main.js.map +1 -0
  78. package/dist/he-IL.mjs +46 -0
  79. package/dist/he-IL.module.js +46 -0
  80. package/dist/he-IL.module.js.map +1 -0
  81. package/dist/hr-HR.main.js +44 -0
  82. package/dist/hr-HR.main.js.map +1 -0
  83. package/dist/hr-HR.mjs +46 -0
  84. package/dist/hr-HR.module.js +46 -0
  85. package/dist/hr-HR.module.js.map +1 -0
  86. package/dist/hu-HU.main.js +44 -0
  87. package/dist/hu-HU.main.js.map +1 -0
  88. package/dist/hu-HU.mjs +46 -0
  89. package/dist/hu-HU.module.js +46 -0
  90. package/dist/hu-HU.module.js.map +1 -0
  91. package/dist/import.mjs +39 -0
  92. package/dist/intlStrings.main.js +108 -0
  93. package/dist/intlStrings.main.js.map +1 -0
  94. package/dist/intlStrings.mjs +110 -0
  95. package/dist/intlStrings.module.js +110 -0
  96. package/dist/intlStrings.module.js.map +1 -0
  97. package/dist/it-IT.main.js +44 -0
  98. package/dist/it-IT.main.js.map +1 -0
  99. package/dist/it-IT.mjs +46 -0
  100. package/dist/it-IT.module.js +46 -0
  101. package/dist/it-IT.module.js.map +1 -0
  102. package/dist/ja-JP.main.js +44 -0
  103. package/dist/ja-JP.main.js.map +1 -0
  104. package/dist/ja-JP.mjs +46 -0
  105. package/dist/ja-JP.module.js +46 -0
  106. package/dist/ja-JP.module.js.map +1 -0
  107. package/dist/ko-KR.main.js +44 -0
  108. package/dist/ko-KR.main.js.map +1 -0
  109. package/dist/ko-KR.mjs +46 -0
  110. package/dist/ko-KR.module.js +46 -0
  111. package/dist/ko-KR.module.js.map +1 -0
  112. package/dist/lt-LT.main.js +44 -0
  113. package/dist/lt-LT.main.js.map +1 -0
  114. package/dist/lt-LT.mjs +46 -0
  115. package/dist/lt-LT.module.js +46 -0
  116. package/dist/lt-LT.module.js.map +1 -0
  117. package/dist/lv-LV.main.js +44 -0
  118. package/dist/lv-LV.main.js.map +1 -0
  119. package/dist/lv-LV.mjs +46 -0
  120. package/dist/lv-LV.module.js +46 -0
  121. package/dist/lv-LV.module.js.map +1 -0
  122. package/dist/main.js +39 -1873
  123. package/dist/main.js.map +1 -1
  124. package/dist/module.js +24 -1853
  125. package/dist/module.js.map +1 -1
  126. package/dist/nb-NO.main.js +44 -0
  127. package/dist/nb-NO.main.js.map +1 -0
  128. package/dist/nb-NO.mjs +46 -0
  129. package/dist/nb-NO.module.js +46 -0
  130. package/dist/nb-NO.module.js.map +1 -0
  131. package/dist/nl-NL.main.js +44 -0
  132. package/dist/nl-NL.main.js.map +1 -0
  133. package/dist/nl-NL.mjs +46 -0
  134. package/dist/nl-NL.module.js +46 -0
  135. package/dist/nl-NL.module.js.map +1 -0
  136. package/dist/pl-PL.main.js +44 -0
  137. package/dist/pl-PL.main.js.map +1 -0
  138. package/dist/pl-PL.mjs +46 -0
  139. package/dist/pl-PL.module.js +46 -0
  140. package/dist/pl-PL.module.js.map +1 -0
  141. package/dist/pt-BR.main.js +44 -0
  142. package/dist/pt-BR.main.js.map +1 -0
  143. package/dist/pt-BR.mjs +46 -0
  144. package/dist/pt-BR.module.js +46 -0
  145. package/dist/pt-BR.module.js.map +1 -0
  146. package/dist/pt-PT.main.js +44 -0
  147. package/dist/pt-PT.main.js.map +1 -0
  148. package/dist/pt-PT.mjs +46 -0
  149. package/dist/pt-PT.module.js +46 -0
  150. package/dist/pt-PT.module.js.map +1 -0
  151. package/dist/ro-RO.main.js +44 -0
  152. package/dist/ro-RO.main.js.map +1 -0
  153. package/dist/ro-RO.mjs +46 -0
  154. package/dist/ro-RO.module.js +46 -0
  155. package/dist/ro-RO.module.js.map +1 -0
  156. package/dist/ru-RU.main.js +44 -0
  157. package/dist/ru-RU.main.js.map +1 -0
  158. package/dist/ru-RU.mjs +46 -0
  159. package/dist/ru-RU.module.js +46 -0
  160. package/dist/ru-RU.module.js.map +1 -0
  161. package/dist/sk-SK.main.js +44 -0
  162. package/dist/sk-SK.main.js.map +1 -0
  163. package/dist/sk-SK.mjs +46 -0
  164. package/dist/sk-SK.module.js +46 -0
  165. package/dist/sk-SK.module.js.map +1 -0
  166. package/dist/sl-SI.main.js +44 -0
  167. package/dist/sl-SI.main.js.map +1 -0
  168. package/dist/sl-SI.mjs +46 -0
  169. package/dist/sl-SI.module.js +46 -0
  170. package/dist/sl-SI.module.js.map +1 -0
  171. package/dist/sr-SP.main.js +44 -0
  172. package/dist/sr-SP.main.js.map +1 -0
  173. package/dist/sr-SP.mjs +46 -0
  174. package/dist/sr-SP.module.js +46 -0
  175. package/dist/sr-SP.module.js.map +1 -0
  176. package/dist/sv-SE.main.js +44 -0
  177. package/dist/sv-SE.main.js.map +1 -0
  178. package/dist/sv-SE.mjs +46 -0
  179. package/dist/sv-SE.module.js +46 -0
  180. package/dist/sv-SE.module.js.map +1 -0
  181. package/dist/tr-TR.main.js +44 -0
  182. package/dist/tr-TR.main.js.map +1 -0
  183. package/dist/tr-TR.mjs +46 -0
  184. package/dist/tr-TR.module.js +46 -0
  185. package/dist/tr-TR.module.js.map +1 -0
  186. package/dist/types.d.ts +177 -29
  187. package/dist/types.d.ts.map +1 -1
  188. package/dist/uk-UA.main.js +44 -0
  189. package/dist/uk-UA.main.js.map +1 -0
  190. package/dist/uk-UA.mjs +46 -0
  191. package/dist/uk-UA.module.js +46 -0
  192. package/dist/uk-UA.module.js.map +1 -0
  193. package/dist/useAutoScroll.main.js +89 -0
  194. package/dist/useAutoScroll.main.js.map +1 -0
  195. package/dist/useAutoScroll.mjs +84 -0
  196. package/dist/useAutoScroll.module.js +84 -0
  197. package/dist/useAutoScroll.module.js.map +1 -0
  198. package/dist/useClipboard.main.js +107 -0
  199. package/dist/useClipboard.main.js.map +1 -0
  200. package/dist/useClipboard.mjs +102 -0
  201. package/dist/useClipboard.module.js +102 -0
  202. package/dist/useClipboard.module.js.map +1 -0
  203. package/dist/useDrag.main.js +286 -0
  204. package/dist/useDrag.main.js.map +1 -0
  205. package/dist/useDrag.mjs +281 -0
  206. package/dist/useDrag.module.js +281 -0
  207. package/dist/useDrag.module.js.map +1 -0
  208. package/dist/useDraggableCollection.main.js +27 -0
  209. package/dist/useDraggableCollection.main.js.map +1 -0
  210. package/dist/useDraggableCollection.mjs +22 -0
  211. package/dist/useDraggableCollection.module.js +22 -0
  212. package/dist/useDraggableCollection.module.js.map +1 -0
  213. package/dist/useDraggableItem.main.js +128 -0
  214. package/dist/useDraggableItem.main.js.map +1 -0
  215. package/dist/useDraggableItem.mjs +123 -0
  216. package/dist/useDraggableItem.module.js +123 -0
  217. package/dist/useDraggableItem.module.js.map +1 -0
  218. package/dist/useDrop.main.js +299 -0
  219. package/dist/useDrop.main.js.map +1 -0
  220. package/dist/useDrop.mjs +294 -0
  221. package/dist/useDrop.module.js +294 -0
  222. package/dist/useDrop.module.js.map +1 -0
  223. package/dist/useDropIndicator.main.js +93 -0
  224. package/dist/useDropIndicator.main.js.map +1 -0
  225. package/dist/useDropIndicator.mjs +88 -0
  226. package/dist/useDropIndicator.module.js +88 -0
  227. package/dist/useDropIndicator.module.js.map +1 -0
  228. package/dist/useDroppableCollection.main.js +592 -0
  229. package/dist/useDroppableCollection.main.js.map +1 -0
  230. package/dist/useDroppableCollection.mjs +587 -0
  231. package/dist/useDroppableCollection.module.js +587 -0
  232. package/dist/useDroppableCollection.module.js.map +1 -0
  233. package/dist/useDroppableItem.main.js +79 -0
  234. package/dist/useDroppableItem.main.js.map +1 -0
  235. package/dist/useDroppableItem.mjs +74 -0
  236. package/dist/useDroppableItem.module.js +74 -0
  237. package/dist/useDroppableItem.module.js.map +1 -0
  238. package/dist/useVirtualDrop.main.js +56 -0
  239. package/dist/useVirtualDrop.main.js.map +1 -0
  240. package/dist/useVirtualDrop.mjs +51 -0
  241. package/dist/useVirtualDrop.module.js +51 -0
  242. package/dist/useVirtualDrop.module.js.map +1 -0
  243. package/dist/utils.main.js +283 -0
  244. package/dist/utils.main.js.map +1 -0
  245. package/dist/utils.mjs +255 -0
  246. package/dist/utils.module.js +255 -0
  247. package/dist/utils.module.js.map +1 -0
  248. package/dist/zh-CN.main.js +44 -0
  249. package/dist/zh-CN.main.js.map +1 -0
  250. package/dist/zh-CN.mjs +46 -0
  251. package/dist/zh-CN.module.js +46 -0
  252. package/dist/zh-CN.module.js.map +1 -0
  253. package/dist/zh-TW.main.js +44 -0
  254. package/dist/zh-TW.main.js.map +1 -0
  255. package/dist/zh-TW.mjs +46 -0
  256. package/dist/zh-TW.module.js +46 -0
  257. package/dist/zh-TW.module.js.map +1 -0
  258. package/package.json +20 -16
  259. package/src/DragManager.ts +87 -52
  260. package/src/DragPreview.tsx +3 -3
  261. package/src/ListDropTargetDelegate.ts +173 -0
  262. package/src/constants.ts +2 -0
  263. package/src/index.ts +55 -9
  264. package/src/useAutoScroll.ts +26 -6
  265. package/src/useClipboard.ts +65 -59
  266. package/src/useDrag.ts +179 -31
  267. package/src/useDraggableCollection.ts +29 -0
  268. package/src/useDraggableItem.ts +105 -12
  269. package/src/useDrop.ts +273 -93
  270. package/src/useDropIndicator.ts +47 -21
  271. package/src/useDroppableCollection.ts +315 -115
  272. package/src/useDroppableItem.ts +33 -17
  273. package/src/useVirtualDrop.ts +5 -5
  274. package/src/utils.ts +92 -17
  275. package/LICENSE +0 -201
@@ -10,16 +10,22 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {getScrollParent, isIOS, isWebKit} from '@react-aria/utils';
14
- import {RefObject, useCallback, useEffect, useRef} from 'react';
13
+ import {getScrollParent, isIOS, isScrollable, isWebKit} from '@react-aria/utils';
14
+ import {RefObject} from '@react-types/shared';
15
+ import {useCallback, useEffect, useRef} from 'react';
15
16
 
16
17
  const AUTOSCROLL_AREA_SIZE = 20;
17
18
 
18
- export function useAutoScroll(ref: RefObject<Element>) {
19
+ export function useAutoScroll(ref: RefObject<Element | null>) {
19
20
  let scrollableRef = useRef<Element>(null);
21
+ let scrollableX = useRef(true);
22
+ let scrollableY = useRef(true);
20
23
  useEffect(() => {
21
24
  if (ref.current) {
22
- scrollableRef.current = getScrollParent(ref.current);
25
+ scrollableRef.current = isScrollable(ref.current) ? ref.current : getScrollParent(ref.current);
26
+ let style = window.getComputedStyle(scrollableRef.current);
27
+ scrollableX.current = /(auto|scroll)/.test(style.overflowX);
28
+ scrollableY.current = /(auto|scroll)/.test(style.overflowY);
23
29
  }
24
30
  }, [ref]);
25
31
 
@@ -29,9 +35,23 @@ export function useAutoScroll(ref: RefObject<Element>) {
29
35
  dy: 0
30
36
  }).current;
31
37
 
38
+ useEffect(() => {
39
+ return () => {
40
+ if (state.timer) {
41
+ cancelAnimationFrame(state.timer);
42
+ state.timer = null;
43
+ }
44
+ };
45
+ // state will become a new object, so it's ok to use in the dependency array for unmount
46
+ }, [state]);
47
+
32
48
  let scroll = useCallback(() => {
33
- scrollableRef.current.scrollLeft += state.dx;
34
- scrollableRef.current.scrollTop += state.dy;
49
+ if (scrollableX.current) {
50
+ scrollableRef.current.scrollLeft += state.dx;
51
+ }
52
+ if (scrollableY.current) {
53
+ scrollableRef.current.scrollTop += state.dy;
54
+ }
35
55
 
36
56
  if (state.timer) {
37
57
  state.timer = requestAnimationFrame(scroll);
@@ -10,21 +10,28 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {chain} from '@react-aria/utils';
14
- import {DragItem, DropItem} from '@react-types/shared';
15
- import {HTMLAttributes, useEffect, useRef} from 'react';
13
+ import {chain, useEffectEvent} from '@react-aria/utils';
14
+ import {DOMAttributes, DragItem, DropItem} from '@react-types/shared';
16
15
  import {readFromDataTransfer, writeToDataTransfer} from './utils';
16
+ import {useEffect, useRef} from 'react';
17
17
  import {useFocus} from '@react-aria/interactions';
18
18
 
19
- interface ClipboardProps {
19
+ export interface ClipboardProps {
20
+ /** A function that returns the items to copy. */
20
21
  getItems?: () => DragItem[],
22
+ /** Handler that is called when the user triggers a copy interaction. */
21
23
  onCopy?: () => void,
24
+ /** Handler that is called when the user triggers a cut interaction. */
22
25
  onCut?: () => void,
23
- onPaste?: (items: DropItem[]) => void
26
+ /** Handler that is called when the user triggers a paste interaction. */
27
+ onPaste?: (items: DropItem[]) => void,
28
+ /** Whether the clipboard is disabled. */
29
+ isDisabled?: boolean
24
30
  }
25
31
 
26
- interface ClipboardResult {
27
- clipboardProps: HTMLAttributes<HTMLElement>
32
+ export interface ClipboardResult {
33
+ /** Props for the element that will handle clipboard events. */
34
+ clipboardProps: DOMAttributes
28
35
  }
29
36
 
30
37
  const globalEvents = new Map();
@@ -54,10 +61,12 @@ function addGlobalEventListener(event, fn) {
54
61
  };
55
62
  }
56
63
 
64
+ /**
65
+ * Handles clipboard interactions for a focusable element. Supports items of multiple
66
+ * data types, and integrates with the operating system native clipboard.
67
+ */
57
68
  export function useClipboard(options: ClipboardProps): ClipboardResult {
58
- let ref = useRef(options);
59
- ref.current = options;
60
-
69
+ let {isDisabled} = options;
61
70
  let isFocusedRef = useRef(false);
62
71
  let {focusProps} = useFocus({
63
72
  onFocusChange: (isFocused) => {
@@ -65,64 +74,61 @@ export function useClipboard(options: ClipboardProps): ClipboardResult {
65
74
  }
66
75
  });
67
76
 
68
- useEffect(() => {
69
- let onBeforeCopy = (e: ClipboardEvent) => {
70
- // Enable the "Copy" menu item in Safari if this element is focused and copying is supported.
71
- let options = ref.current;
72
- if (isFocusedRef.current && options.getItems) {
73
- e.preventDefault();
74
- }
75
- };
76
-
77
- let onCopy = (e: ClipboardEvent) => {
78
- let options = ref.current;
79
- if (!isFocusedRef.current || !options.getItems) {
80
- return;
81
- }
82
-
77
+ let onBeforeCopy = useEffectEvent((e: ClipboardEvent) => {
78
+ // Enable the "Copy" menu item in Safari if this element is focused and copying is supported.
79
+ if (isFocusedRef.current && options.getItems) {
83
80
  e.preventDefault();
84
- writeToDataTransfer(e.clipboardData, options.getItems());
85
- options.onCopy?.();
86
- };
81
+ }
82
+ });
87
83
 
88
- let onBeforeCut = (e: ClipboardEvent) => {
89
- let options = ref.current;
90
- if (isFocusedRef.current && options.onCut && options.getItems) {
91
- e.preventDefault();
92
- }
93
- };
84
+ let onCopy = useEffectEvent((e: ClipboardEvent) => {
85
+ if (!isFocusedRef.current || !options.getItems) {
86
+ return;
87
+ }
94
88
 
95
- let onCut = (e: ClipboardEvent) => {
96
- let options = ref.current;
97
- if (!isFocusedRef.current || !options.onCut || !options.getItems) {
98
- return;
99
- }
89
+ e.preventDefault();
90
+ writeToDataTransfer(e.clipboardData, options.getItems());
91
+ options.onCopy?.();
92
+ });
100
93
 
94
+ let onBeforeCut = useEffectEvent((e: ClipboardEvent) => {
95
+ if (isFocusedRef.current && options.onCut && options.getItems) {
101
96
  e.preventDefault();
102
- writeToDataTransfer(e.clipboardData, options.getItems());
103
- options.onCut();
104
- };
97
+ }
98
+ });
105
99
 
106
- let onBeforePaste = (e: ClipboardEvent) => {
107
- let options = ref.current;
108
- // Unfortunately, e.clipboardData.types is not available in this event so we always
109
- // have to enable the Paste menu item even if the type of data is unsupported.
110
- if (isFocusedRef.current && options.onPaste) {
111
- e.preventDefault();
112
- }
113
- };
100
+ let onCut = useEffectEvent((e: ClipboardEvent) => {
101
+ if (!isFocusedRef.current || !options.onCut || !options.getItems) {
102
+ return;
103
+ }
114
104
 
115
- let onPaste = (e: ClipboardEvent) => {
116
- let options = ref.current;
117
- if (!isFocusedRef.current || !options.onPaste) {
118
- return;
119
- }
105
+ e.preventDefault();
106
+ writeToDataTransfer(e.clipboardData, options.getItems());
107
+ options.onCut();
108
+ });
120
109
 
110
+ let onBeforePaste = useEffectEvent((e: ClipboardEvent) => {
111
+ // Unfortunately, e.clipboardData.types is not available in this event so we always
112
+ // have to enable the Paste menu item even if the type of data is unsupported.
113
+ if (isFocusedRef.current && options.onPaste) {
121
114
  e.preventDefault();
122
- let items = readFromDataTransfer(e.clipboardData);
123
- options.onPaste(items);
124
- };
115
+ }
116
+ });
117
+
118
+ let onPaste = useEffectEvent((e: ClipboardEvent) => {
119
+ if (!isFocusedRef.current || !options.onPaste) {
120
+ return;
121
+ }
125
122
 
123
+ e.preventDefault();
124
+ let items = readFromDataTransfer(e.clipboardData);
125
+ options.onPaste(items);
126
+ });
127
+
128
+ useEffect(() => {
129
+ if (isDisabled) {
130
+ return;
131
+ }
126
132
  return chain(
127
133
  addGlobalEventListener('beforecopy', onBeforeCopy),
128
134
  addGlobalEventListener('copy', onCopy),
@@ -131,7 +137,7 @@ export function useClipboard(options: ClipboardProps): ClipboardResult {
131
137
  addGlobalEventListener('beforepaste', onBeforePaste),
132
138
  addGlobalEventListener('paste', onPaste)
133
139
  );
134
- }, []);
140
+ }, [isDisabled, onBeforeCopy, onCopy, onBeforeCut, onCut, onBeforePaste, onPaste]);
135
141
 
136
142
  return {
137
143
  clipboardProps: focusProps
package/src/useDrag.ts CHANGED
@@ -11,29 +11,46 @@
11
11
  */
12
12
 
13
13
  import {AriaButtonProps} from '@react-types/button';
14
- import {DragEndEvent, DragItem, DragMoveEvent, DragPreviewRenderer, DragStartEvent, DropOperation, PressEvent} from '@react-types/shared';
15
- import {DragEvent, HTMLAttributes, RefObject, useRef, useState} from 'react';
14
+ import {DragEndEvent, DragItem, DragMoveEvent, DragPreviewRenderer, DragStartEvent, DropOperation, PressEvent, RefObject} from '@react-types/shared';
15
+ import {DragEvent, HTMLAttributes, useRef, useState} from 'react';
16
16
  import * as DragManager from './DragManager';
17
17
  import {DROP_EFFECT_TO_DROP_OPERATION, DROP_OPERATION, EFFECT_ALLOWED} from './constants';
18
+ import {globalDropEffect, setGlobalAllowedDropOperations, setGlobalDropEffect, useDragModality, writeToDataTransfer} from './utils';
18
19
  // @ts-ignore
19
20
  import intlMessages from '../intl/*.json';
20
- import {useDescription, useGlobalListeners} from '@react-aria/utils';
21
- import {useDragModality} from './utils';
22
- import {useMessageFormatter} from '@react-aria/i18n';
23
- import {writeToDataTransfer} from './utils';
21
+ import {isVirtualClick, isVirtualPointerEvent, useDescription, useGlobalListeners, useLayoutEffect} from '@react-aria/utils';
22
+ import {useLocalizedStringFormatter} from '@react-aria/i18n';
24
23
 
25
- interface DragOptions {
24
+ export interface DragOptions {
25
+ /** Handler that is called when a drag operation is started. */
26
26
  onDragStart?: (e: DragStartEvent) => void,
27
+ /** Handler that is called when the drag is moved. */
27
28
  onDragMove?: (e: DragMoveEvent) => void,
29
+ /** Handler that is called when the drag operation is ended, either as a result of a drop or a cancellation. */
28
30
  onDragEnd?: (e: DragEndEvent) => void,
31
+ /** A function that returns the items being dragged. */
29
32
  getItems: () => DragItem[],
30
- preview?: RefObject<DragPreviewRenderer>,
31
- getAllowedDropOperations?: () => DropOperation[]
33
+ /** The ref of the element that will be rendered as the drag preview while dragging. */
34
+ preview?: RefObject<DragPreviewRenderer | null>,
35
+ /** Function that returns the drop operations that are allowed for the dragged items. If not provided, all drop operations are allowed. */
36
+ getAllowedDropOperations?: () => DropOperation[],
37
+ /**
38
+ * Whether the item has an explicit focusable drag affordance to initiate accessible drag and drop mode.
39
+ * If true, the dragProps will omit these event handlers, and they will be applied to dragButtonProps instead.
40
+ */
41
+ hasDragButton?: boolean,
42
+ /**
43
+ * Whether the drag operation is disabled. If true, the element will not be draggable.
44
+ */
45
+ isDisabled?: boolean
32
46
  }
33
47
 
34
- interface DragResult {
48
+ export interface DragResult {
49
+ /** Props for the draggable element. */
35
50
  dragProps: HTMLAttributes<HTMLElement>,
51
+ /** Props for the explicit drag button affordance, if any. */
36
52
  dragButtonProps: AriaButtonProps,
53
+ /** Whether the element is currently being dragged. */
37
54
  isDragging: boolean
38
55
  }
39
56
 
@@ -52,22 +69,44 @@ const MESSAGES = {
52
69
  }
53
70
  };
54
71
 
72
+ /**
73
+ * Handles drag interactions for an element, with support for traditional mouse and touch
74
+ * based drag and drop, in addition to full parity for keyboard and screen reader users.
75
+ */
55
76
  export function useDrag(options: DragOptions): DragResult {
56
- let formatMessage = useMessageFormatter(intlMessages);
77
+ let {hasDragButton, isDisabled} = options;
78
+ let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/dnd');
57
79
  let state = useRef({
58
80
  options,
59
81
  x: 0,
60
82
  y: 0
61
83
  }).current;
62
84
  state.options = options;
63
- let [isDragging, setDragging] = useState(false);
85
+ let isDraggingRef = useRef(false);
86
+ let [isDragging, setDraggingState] = useState(false);
87
+ let setDragging = (isDragging) => {
88
+ isDraggingRef.current = isDragging;
89
+ setDraggingState(isDragging);
90
+ };
64
91
  let {addGlobalListener, removeAllGlobalListeners} = useGlobalListeners();
92
+ let modalityOnPointerDown = useRef<string>(null);
65
93
 
66
94
  let onDragStart = (e: DragEvent) => {
67
95
  if (e.defaultPrevented) {
68
96
  return;
69
97
  }
70
98
 
99
+ // Prevent the drag event from propagating to any parent draggables
100
+ e.stopPropagation();
101
+
102
+ // If this drag was initiated by a mobile screen reader (e.g. VoiceOver or TalkBack), enter virtual dragging mode.
103
+ if (modalityOnPointerDown.current === 'virtual') {
104
+ e.preventDefault();
105
+ startDragging(e.target as HTMLElement);
106
+ modalityOnPointerDown.current = null;
107
+ return;
108
+ }
109
+
71
110
  if (typeof options.onDragStart === 'function') {
72
111
  options.onDragStart({
73
112
  type: 'dragstart',
@@ -79,16 +118,18 @@ export function useDrag(options: DragOptions): DragResult {
79
118
  let items = options.getItems();
80
119
  writeToDataTransfer(e.dataTransfer, items);
81
120
 
121
+ let allowed = DROP_OPERATION.all;
82
122
  if (typeof options.getAllowedDropOperations === 'function') {
83
123
  let allowedOperations = options.getAllowedDropOperations();
84
- let allowed = DROP_OPERATION.none;
124
+ allowed = DROP_OPERATION.none;
85
125
  for (let operation of allowedOperations) {
86
126
  allowed |= DROP_OPERATION[operation] || DROP_OPERATION.none;
87
127
  }
88
-
89
- e.dataTransfer.effectAllowed = EFFECT_ALLOWED[allowed] || 'none';
90
128
  }
91
129
 
130
+ setGlobalAllowedDropOperations(allowed);
131
+ e.dataTransfer.effectAllowed = EFFECT_ALLOWED[allowed] || 'none';
132
+
92
133
  // If there is a preview option, use it to render a custom preview image that will
93
134
  // appear under the pointer while dragging. If not, the element itself is dragged by the browser.
94
135
  if (typeof options.preview?.current === 'function') {
@@ -106,7 +147,7 @@ export function useDrag(options: DragOptions): DragResult {
106
147
  }
107
148
 
108
149
  // Rounding height to an even number prevents blurry preview seen on some screens
109
- let height = 2 * Math.round(rect.height / 2);
150
+ let height = 2 * Math.round(size.height / 2);
110
151
  node.style.height = `${height}px`;
111
152
 
112
153
  e.dataTransfer.setDragImage(node, x, y);
@@ -115,13 +156,10 @@ export function useDrag(options: DragOptions): DragResult {
115
156
 
116
157
  // Enforce that drops are handled by useDrop.
117
158
  addGlobalListener(window, 'drop', e => {
118
- if (!DragManager.isValidDropTarget(e.target as Element)) {
119
- e.preventDefault();
120
- e.stopPropagation();
121
- throw new Error('Drags initiated from the React Aria useDrag hook may only be dropped on a target created with useDrop. This ensures that a keyboard and screen reader accessible alternative is available.');
122
- }
123
- }, {capture: true, once: true});
124
-
159
+ e.preventDefault();
160
+ e.stopPropagation();
161
+ console.warn('Drags initiated from the React Aria useDrag hook may only be dropped on a target created with useDrop. This ensures that a keyboard and screen reader accessible alternative is available.');
162
+ }, {once: true});
125
163
  state.x = e.clientX;
126
164
  state.y = e.clientY;
127
165
 
@@ -133,6 +171,9 @@ export function useDrag(options: DragOptions): DragResult {
133
171
  };
134
172
 
135
173
  let onDrag = (e: DragEvent) => {
174
+ // Prevent the drag event from propagating to any parent draggables
175
+ e.stopPropagation();
176
+
136
177
  if (e.clientX === state.x && e.clientY === state.y) {
137
178
  return;
138
179
  }
@@ -150,26 +191,65 @@ export function useDrag(options: DragOptions): DragResult {
150
191
  };
151
192
 
152
193
  let onDragEnd = (e: DragEvent) => {
194
+ // Prevent the drag event from propagating to any parent draggables
195
+ e.stopPropagation();
196
+
153
197
  if (typeof options.onDragEnd === 'function') {
154
- options.onDragEnd({
198
+ let event: DragEndEvent = {
155
199
  type: 'dragend',
156
200
  x: e.clientX,
157
201
  y: e.clientY,
158
202
  dropOperation: DROP_EFFECT_TO_DROP_OPERATION[e.dataTransfer.dropEffect]
159
- });
203
+ };
204
+
205
+ // Chrome Android always returns none as its dropEffect so we use the drop effect set in useDrop via
206
+ // onDragEnter/onDragOver instead. https://bugs.chromium.org/p/chromium/issues/detail?id=1353951
207
+ if (globalDropEffect) {
208
+ event.dropOperation = DROP_EFFECT_TO_DROP_OPERATION[globalDropEffect];
209
+ }
210
+ options.onDragEnd(event);
160
211
  }
161
212
 
162
213
  setDragging(false);
163
214
  removeAllGlobalListeners();
215
+ setGlobalAllowedDropOperations(DROP_OPERATION.none);
216
+ setGlobalDropEffect(undefined);
164
217
  };
165
218
 
219
+ // If the dragged element is removed from the DOM via onDrop, onDragEnd won't fire: https://bugzilla.mozilla.org/show_bug.cgi?id=460801
220
+ // In this case, we need to manually call onDragEnd on cleanup
221
+ // eslint-disable-next-line arrow-body-style
222
+ useLayoutEffect(() => {
223
+ return () => {
224
+ if (isDraggingRef.current) {
225
+ if (typeof state.options.onDragEnd === 'function') {
226
+ let event: DragEndEvent = {
227
+ type: 'dragend',
228
+ x: 0,
229
+ y: 0,
230
+ dropOperation: DROP_EFFECT_TO_DROP_OPERATION[globalDropEffect || 'none']
231
+ };
232
+ state.options.onDragEnd(event);
233
+ }
234
+
235
+ setDragging(false);
236
+ setGlobalAllowedDropOperations(DROP_OPERATION.none);
237
+ setGlobalDropEffect(undefined);
238
+ }
239
+ };
240
+ }, [state]);
241
+
166
242
  let onPress = (e: PressEvent) => {
167
243
  if (e.pointerType !== 'keyboard' && e.pointerType !== 'virtual') {
168
244
  return;
169
245
  }
170
246
 
247
+ startDragging(e.target as HTMLElement);
248
+ };
249
+
250
+ let startDragging = (target: HTMLElement) => {
171
251
  if (typeof state.options.onDragStart === 'function') {
172
- let rect = (e.target as HTMLElement).getBoundingClientRect();
252
+ let rect = target.getBoundingClientRect();
173
253
  state.options.onDragStart({
174
254
  type: 'dragstart',
175
255
  x: rect.x + (rect.width / 2),
@@ -178,7 +258,7 @@ export function useDrag(options: DragOptions): DragResult {
178
258
  }
179
259
 
180
260
  DragManager.beginDragging({
181
- element: e.target as HTMLElement,
261
+ element: target,
182
262
  items: state.options.getItems(),
183
263
  allowedDropOperations: typeof state.options.getAllowedDropOperations === 'function'
184
264
  ? state.options.getAllowedDropOperations()
@@ -189,18 +269,86 @@ export function useDrag(options: DragOptions): DragResult {
189
269
  state.options.onDragEnd(e);
190
270
  }
191
271
  }
192
- }, formatMessage);
272
+ }, stringFormatter);
193
273
 
194
274
  setDragging(true);
195
275
  };
196
276
 
197
277
  let modality = useDragModality();
198
- let descriptionProps = useDescription(
199
- formatMessage(!isDragging ? MESSAGES[modality].start : MESSAGES[modality].end)
200
- );
278
+ let message = !isDragging ? MESSAGES[modality].start : MESSAGES[modality].end;
279
+
280
+ let descriptionProps = useDescription(stringFormatter.format(message));
281
+
282
+ let interactions: HTMLAttributes<HTMLElement>;
283
+ if (!hasDragButton) {
284
+ // If there's no separate button to trigger accessible drag and drop mode,
285
+ // then add event handlers to the draggable element itself to start dragging.
286
+ // For keyboard, we use the Enter key in a capturing listener to prevent other
287
+ // events such as selection from also occurring. We attempt to infer whether a
288
+ // pointer event (e.g. long press) came from a touch screen reader, and then initiate
289
+ // dragging in the native onDragStart listener above.
290
+
291
+ interactions = {
292
+ ...descriptionProps,
293
+ onPointerDown(e) {
294
+ modalityOnPointerDown.current = isVirtualPointerEvent(e.nativeEvent) ? 'virtual' : e.pointerType;
295
+
296
+ // Try to detect virtual drag passthrough gestures.
297
+ if (e.width < 1 && e.height < 1) {
298
+ // iOS VoiceOver.
299
+ modalityOnPointerDown.current = 'virtual';
300
+ } else {
301
+ let rect = e.currentTarget.getBoundingClientRect();
302
+ let offsetX = e.clientX - rect.x;
303
+ let offsetY = e.clientY - rect.y;
304
+ let centerX = rect.width / 2;
305
+ let centerY = rect.height / 2;
306
+
307
+ if (Math.abs(offsetX - centerX) <= 0.5 && Math.abs(offsetY - centerY) <= 0.5) {
308
+ // Android TalkBack.
309
+ modalityOnPointerDown.current = 'virtual';
310
+ } else {
311
+ modalityOnPointerDown.current = e.pointerType;
312
+ }
313
+ }
314
+ },
315
+ onKeyDownCapture(e) {
316
+ if (e.target === e.currentTarget && e.key === 'Enter') {
317
+ e.preventDefault();
318
+ e.stopPropagation();
319
+ }
320
+ },
321
+ onKeyUpCapture(e) {
322
+ if (e.target === e.currentTarget && e.key === 'Enter') {
323
+ e.preventDefault();
324
+ e.stopPropagation();
325
+ startDragging(e.target as HTMLElement);
326
+ }
327
+ },
328
+ onClick(e) {
329
+ // Handle NVDA/JAWS in browse mode, and touch screen readers. In this case, no keyboard events are fired.
330
+ if (isVirtualClick(e.nativeEvent) || modalityOnPointerDown.current === 'virtual') {
331
+ e.preventDefault();
332
+ e.stopPropagation();
333
+ startDragging(e.target as HTMLElement);
334
+ }
335
+ }
336
+ };
337
+ }
338
+
339
+ if (isDisabled) {
340
+ return {
341
+ dragProps: {
342
+ draggable: 'false'
343
+ },
344
+ dragButtonProps: {},
345
+ isDragging: false
346
+ };
347
+ }
201
348
 
202
349
  return {
203
350
  dragProps: {
351
+ ...interactions,
204
352
  draggable: 'true',
205
353
  onDragStart,
206
354
  onDrag,
@@ -0,0 +1,29 @@
1
+ /*
2
+ * Copyright 2022 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 {DraggableCollectionState} from '@react-stately/dnd';
14
+ import {globalDndState, setDraggingCollectionRef} from './utils';
15
+ import {RefObject} from '@react-types/shared';
16
+
17
+ export interface DraggableCollectionOptions {}
18
+
19
+ /**
20
+ * Handles drag interactions for a collection component, with support for traditional mouse and touch
21
+ * based drag and drop, in addition to full parity for keyboard and screen reader users.
22
+ */
23
+ export function useDraggableCollection(props: DraggableCollectionOptions, state: DraggableCollectionState, ref: RefObject<HTMLElement | null>): void {
24
+ // Update global DnD state if this keys within this collection are dragged
25
+ let {draggingCollectionRef} = globalDndState;
26
+ if (state.draggingKeys.size > 0 && draggingCollectionRef?.current !== ref.current) {
27
+ setDraggingCollectionRef(ref);
28
+ }
29
+ }