@react-aria/dnd 3.11.5 → 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 -519
  10. package/dist/DragManager.main.js.map +0 -1
  11. package/dist/DragManager.mjs +0 -510
  12. package/dist/DragManager.module.js +0 -510
  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 -205
  20. package/dist/DropTargetKeyboardNavigation.main.js.map +0 -1
  21. package/dist/DropTargetKeyboardNavigation.mjs +0 -200
  22. package/dist/DropTargetKeyboardNavigation.module.js +0 -200
  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 -656
  267. package/src/DragPreview.tsx +0 -81
  268. package/src/DropTargetKeyboardNavigation.ts +0 -282
  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,105 +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 {getScrollParent, isIOS, isScrollable, isWebKit} from '@react-aria/utils';
14
- import {RefObject} from '@react-types/shared';
15
- import {useCallback, useEffect, useRef} from 'react';
16
-
17
- const AUTOSCROLL_AREA_SIZE = 20;
18
-
19
- interface AutoScrollAria {
20
- move(x: number, y: number): void,
21
- stop(): void
22
- }
23
-
24
- export function useAutoScroll(ref: RefObject<Element | null>): AutoScrollAria {
25
- let scrollableRef = useRef<Element>(null);
26
- let scrollableX = useRef(true);
27
- let scrollableY = useRef(true);
28
- useEffect(() => {
29
- if (ref.current) {
30
- scrollableRef.current = isScrollable(ref.current) ? ref.current : getScrollParent(ref.current);
31
- let style = window.getComputedStyle(scrollableRef.current);
32
- scrollableX.current = /(auto|scroll)/.test(style.overflowX);
33
- scrollableY.current = /(auto|scroll)/.test(style.overflowY);
34
- }
35
- }, [ref]);
36
-
37
- let state = useRef<{timer: ReturnType<typeof requestAnimationFrame> | undefined, dx: number, dy: number}>({
38
- timer: undefined,
39
- dx: 0,
40
- dy: 0
41
- }).current;
42
-
43
- useEffect(() => {
44
- return () => {
45
- if (state.timer) {
46
- cancelAnimationFrame(state.timer);
47
- state.timer = undefined;
48
- }
49
- };
50
- // state will become a new object, so it's ok to use in the dependency array for unmount
51
- }, [state]);
52
-
53
- let scroll = useCallback(() => {
54
- if (scrollableX.current && scrollableRef.current) {
55
- scrollableRef.current.scrollLeft += state.dx;
56
- }
57
- if (scrollableY.current && scrollableRef.current) {
58
- scrollableRef.current.scrollTop += state.dy;
59
- }
60
-
61
- if (state.timer) {
62
- state.timer = requestAnimationFrame(scroll);
63
- }
64
- }, [scrollableRef, state]);
65
-
66
- return {
67
- move(x, y) {
68
- // Most browsers auto scroll natively, but WebKit on macOS does not (iOS does 🤷‍♂️).
69
- // https://bugs.webkit.org/show_bug.cgi?id=222636
70
- if (!isWebKit() || isIOS() || !scrollableRef.current) {
71
- return;
72
- }
73
-
74
- let box = scrollableRef.current.getBoundingClientRect();
75
- let left = AUTOSCROLL_AREA_SIZE;
76
- let top = AUTOSCROLL_AREA_SIZE;
77
- let bottom = box.height - AUTOSCROLL_AREA_SIZE;
78
- let right = box.width - AUTOSCROLL_AREA_SIZE;
79
- if (x < left || x > right || y < top || y > bottom) {
80
- if (x < left) {
81
- state.dx = x - left;
82
- } else if (x > right) {
83
- state.dx = x - right;
84
- }
85
- if (y < top) {
86
- state.dy = y - top;
87
- } else if (y > bottom) {
88
- state.dy = y - bottom;
89
- }
90
-
91
- if (!state.timer) {
92
- state.timer = requestAnimationFrame(scroll);
93
- }
94
- } else {
95
- this.stop();
96
- }
97
- },
98
- stop() {
99
- if (state.timer) {
100
- cancelAnimationFrame(state.timer);
101
- state.timer = undefined;
102
- }
103
- }
104
- };
105
- }
@@ -1,151 +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 {chain, useEffectEvent} from '@react-aria/utils';
14
- import {DOMAttributes, DragItem, DropItem} from '@react-types/shared';
15
- import {readFromDataTransfer, writeToDataTransfer} from './utils';
16
- import {useEffect, useRef} from 'react';
17
- import {useFocus} from '@react-aria/interactions';
18
-
19
- export interface ClipboardProps {
20
- /** A function that returns the items to copy. */
21
- getItems?: (details: {action: 'cut' | 'copy'}) => DragItem[],
22
- /** Handler that is called when the user triggers a copy interaction. */
23
- onCopy?: () => void,
24
- /** Handler that is called when the user triggers a cut interaction. */
25
- onCut?: () => 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
30
- }
31
-
32
- export interface ClipboardResult {
33
- /** Props for the element that will handle clipboard events. */
34
- clipboardProps: DOMAttributes
35
- }
36
-
37
- const globalEvents = new Map();
38
- function addGlobalEventListener(event, fn) {
39
- let eventData = globalEvents.get(event);
40
- if (!eventData) {
41
- let handlers = new Set<(e: Event) => void>();
42
- let listener = (e) => {
43
- for (let handler of handlers) {
44
- handler(e);
45
- }
46
- };
47
-
48
- eventData = {listener, handlers};
49
- globalEvents.set(event, eventData);
50
-
51
- document.addEventListener(event, listener);
52
- }
53
-
54
- eventData.handlers.add(fn);
55
- return () => {
56
- eventData.handlers.delete(fn);
57
- if (eventData.handlers.size === 0) {
58
- document.removeEventListener(event, eventData.listener);
59
- globalEvents.delete(event);
60
- }
61
- };
62
- }
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
- */
68
- export function useClipboard(options: ClipboardProps): ClipboardResult {
69
- let {isDisabled} = options;
70
- let isFocusedRef = useRef(false);
71
- let {focusProps} = useFocus({
72
- onFocusChange: (isFocused) => {
73
- isFocusedRef.current = isFocused;
74
- }
75
- });
76
-
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) {
80
- e.preventDefault();
81
- }
82
- });
83
-
84
- let onCopy = useEffectEvent((e: ClipboardEvent) => {
85
- if (!isFocusedRef.current || !options.getItems) {
86
- return;
87
- }
88
-
89
- e.preventDefault();
90
- if (e.clipboardData) {
91
- writeToDataTransfer(e.clipboardData, options.getItems({action: 'copy'}));
92
- options.onCopy?.();
93
- }
94
- });
95
-
96
- let onBeforeCut = useEffectEvent((e: ClipboardEvent) => {
97
- if (isFocusedRef.current && options.onCut && options.getItems) {
98
- e.preventDefault();
99
- }
100
- });
101
-
102
- let onCut = useEffectEvent((e: ClipboardEvent) => {
103
- if (!isFocusedRef.current || !options.onCut || !options.getItems) {
104
- return;
105
- }
106
-
107
- e.preventDefault();
108
- if (e.clipboardData) {
109
- writeToDataTransfer(e.clipboardData, options.getItems({action: 'cut'}));
110
- options.onCut();
111
- }
112
- });
113
-
114
- let onBeforePaste = useEffectEvent((e: ClipboardEvent) => {
115
- // Unfortunately, e.clipboardData.types is not available in this event so we always
116
- // have to enable the Paste menu item even if the type of data is unsupported.
117
- if (isFocusedRef.current && options.onPaste) {
118
- e.preventDefault();
119
- }
120
- });
121
-
122
- let onPaste = useEffectEvent((e: ClipboardEvent) => {
123
- if (!isFocusedRef.current || !options.onPaste) {
124
- return;
125
- }
126
-
127
- e.preventDefault();
128
- if (e.clipboardData) {
129
- let items = readFromDataTransfer(e.clipboardData);
130
- options.onPaste(items);
131
- }
132
- });
133
-
134
- useEffect(() => {
135
- if (isDisabled) {
136
- return;
137
- }
138
- return chain(
139
- addGlobalEventListener('beforecopy', onBeforeCopy),
140
- addGlobalEventListener('copy', onCopy),
141
- addGlobalEventListener('beforecut', onBeforeCut),
142
- addGlobalEventListener('cut', onCut),
143
- addGlobalEventListener('beforepaste', onBeforePaste),
144
- addGlobalEventListener('paste', onPaste)
145
- );
146
- }, [isDisabled]);
147
-
148
- return {
149
- clipboardProps: focusProps
150
- };
151
- }
package/src/useDrag.ts DELETED
@@ -1,390 +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 {AriaButtonProps} from '@react-types/button';
14
- import {DragEndEvent, DragItem, DragMoveEvent, DragPreviewRenderer, DragStartEvent, DropOperation, PressEvent, RefObject} from '@react-types/shared';
15
- import {DragEvent, HTMLAttributes, version as ReactVersion, useEffect, useRef, useState} from 'react';
16
- import * as DragManager from './DragManager';
17
- import {DROP_EFFECT_TO_DROP_OPERATION, DROP_OPERATION, EFFECT_ALLOWED} from './constants';
18
- import {globalDropEffect, setGlobalAllowedDropOperations, setGlobalDropEffect, useDragModality, writeToDataTransfer} from './utils';
19
- // @ts-ignore
20
- import intlMessages from '../intl/*.json';
21
- import {isVirtualClick, isVirtualPointerEvent, useDescription, useGlobalListeners} from '@react-aria/utils';
22
- import {useLocalizedStringFormatter} from '@react-aria/i18n';
23
-
24
- export interface DragOptions {
25
- /** Handler that is called when a drag operation is started. */
26
- onDragStart?: (e: DragStartEvent) => void,
27
- /** Handler that is called when the drag is moved. */
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. */
30
- onDragEnd?: (e: DragEndEvent) => void,
31
- /** A function that returns the items being dragged. */
32
- getItems: () => DragItem[],
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
46
- }
47
-
48
- export interface DragResult {
49
- /** Props for the draggable element. */
50
- dragProps: HTMLAttributes<HTMLElement>,
51
- /** Props for the explicit drag button affordance, if any. */
52
- dragButtonProps: AriaButtonProps,
53
- /** Whether the element is currently being dragged. */
54
- isDragging: boolean
55
- }
56
-
57
- const MESSAGES = {
58
- keyboard: {
59
- start: 'dragDescriptionKeyboard',
60
- end: 'endDragKeyboard'
61
- },
62
- touch: {
63
- start: 'dragDescriptionTouch',
64
- end: 'endDragTouch'
65
- },
66
- virtual: {
67
- start: 'dragDescriptionVirtual',
68
- end: 'endDragVirtual'
69
- }
70
- };
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
- */
76
- export function useDrag(options: DragOptions): DragResult {
77
- let {hasDragButton, isDisabled} = options;
78
- let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/dnd');
79
- let state = useRef({
80
- options,
81
- x: 0,
82
- y: 0
83
- }).current;
84
- state.options = options;
85
- let isDraggingRef = useRef<Element | null>(null);
86
- let [isDragging, setDraggingState] = useState(false);
87
- let setDragging = (element: Element | null) => {
88
- isDraggingRef.current = element;
89
- setDraggingState(!!element);
90
- };
91
- let {addGlobalListener, removeAllGlobalListeners} = useGlobalListeners();
92
- let modalityOnPointerDown = useRef<string>(null);
93
-
94
- let onDragStart = (e: DragEvent) => {
95
- if (e.defaultPrevented) {
96
- return;
97
- }
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
-
110
- if (typeof options.onDragStart === 'function') {
111
- options.onDragStart({
112
- type: 'dragstart',
113
- x: e.clientX,
114
- y: e.clientY
115
- });
116
- }
117
-
118
- let items = options.getItems();
119
- // Clear existing data (e.g. selected text on the page would be included in some browsers)
120
- e.dataTransfer.clearData?.();
121
- writeToDataTransfer(e.dataTransfer, items);
122
-
123
- let allowed = DROP_OPERATION.all;
124
- if (typeof options.getAllowedDropOperations === 'function') {
125
- let allowedOperations = options.getAllowedDropOperations();
126
- allowed = DROP_OPERATION.none;
127
- for (let operation of allowedOperations) {
128
- allowed |= DROP_OPERATION[operation] || DROP_OPERATION.none;
129
- }
130
- }
131
-
132
- setGlobalAllowedDropOperations(allowed);
133
- let effectAllowed = EFFECT_ALLOWED[allowed] || 'none';
134
- e.dataTransfer.effectAllowed = effectAllowed === 'cancel' ? 'none' : effectAllowed;
135
-
136
- // If there is a preview option, use it to render a custom preview image that will
137
- // appear under the pointer while dragging. If not, the element itself is dragged by the browser.
138
- if (typeof options.preview?.current === 'function') {
139
- options.preview.current(items, (node, userX, userY) => {
140
- if (!node) {
141
- return;
142
- }
143
- // Compute the offset that the preview will appear under the mouse.
144
- // If possible, this is based on the point the user clicked on the target.
145
- // If the preview is much smaller, then just use the center point of the preview.
146
- let size = node.getBoundingClientRect();
147
- let rect = e.currentTarget.getBoundingClientRect();
148
- let defaultX = e.clientX - rect.x;
149
- let defaultY = e.clientY - rect.y;
150
- if (defaultX > size.width || defaultY > size.height) {
151
- defaultX = size.width / 2;
152
- defaultY = size.height / 2;
153
- }
154
-
155
- // Start with default offsets.
156
- let offsetX = defaultX;
157
- let offsetY = defaultY;
158
-
159
- // If the preview renderer supplied explicit offsets, use those.
160
- if (typeof userX === 'number' && typeof userY === 'number') {
161
- offsetX = userX;
162
- offsetY = userY;
163
- }
164
-
165
- // Clamp the offset so it stays within the preview bounds. Browsers
166
- // automatically clamp out-of-range values, but doing it ourselves
167
- // prevents the visible "snap" that can occur when the browser adjusts
168
- // them after the first drag update.
169
- offsetX = Math.max(0, Math.min(offsetX, size.width));
170
- offsetY = Math.max(0, Math.min(offsetY, size.height));
171
-
172
- // Rounding height to an even number prevents blurry preview seen on some screens
173
- let height = 2 * Math.round(size.height / 2);
174
- node.style.height = `${height}px`;
175
-
176
- e.dataTransfer.setDragImage(node, offsetX, offsetY);
177
- });
178
- }
179
-
180
- // Enforce that drops are handled by useDrop.
181
- addGlobalListener(window, 'drop', e => {
182
- e.preventDefault();
183
- e.stopPropagation();
184
- 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.');
185
- }, {once: true});
186
- state.x = e.clientX;
187
- state.y = e.clientY;
188
-
189
- // Wait a frame before we set dragging to true so that the browser has time to
190
- // render the preview image before we update the element that has been dragged.
191
- let target = e.target;
192
- requestAnimationFrame(() => {
193
- setDragging(target as Element);
194
- });
195
- };
196
-
197
- let onDrag = (e: DragEvent) => {
198
- // Prevent the drag event from propagating to any parent draggables
199
- e.stopPropagation();
200
-
201
- if (e.clientX === state.x && e.clientY === state.y) {
202
- return;
203
- }
204
-
205
- if (typeof options.onDragMove === 'function') {
206
- options.onDragMove({
207
- type: 'dragmove',
208
- x: e.clientX,
209
- y: e.clientY
210
- });
211
- }
212
-
213
- state.x = e.clientX;
214
- state.y = e.clientY;
215
- };
216
-
217
- let onDragEnd = (e: DragEvent) => {
218
- // Prevent the drag event from propagating to any parent draggables
219
- e.stopPropagation();
220
-
221
- if (typeof options.onDragEnd === 'function') {
222
- let event: DragEndEvent = {
223
- type: 'dragend',
224
- x: e.clientX,
225
- y: e.clientY,
226
- dropOperation: DROP_EFFECT_TO_DROP_OPERATION[e.dataTransfer.dropEffect]
227
- };
228
-
229
- // Chrome Android always returns none as its dropEffect so we use the drop effect set in useDrop via
230
- // onDragEnter/onDragOver instead. https://bugs.chromium.org/p/chromium/issues/detail?id=1353951
231
- if (globalDropEffect) {
232
- event.dropOperation = DROP_EFFECT_TO_DROP_OPERATION[globalDropEffect];
233
- }
234
- options.onDragEnd(event);
235
- }
236
-
237
- setDragging(null);
238
- removeAllGlobalListeners();
239
- setGlobalAllowedDropOperations(DROP_OPERATION.none);
240
- setGlobalDropEffect(undefined);
241
- };
242
-
243
- // If the dragged element is removed from the DOM via onDrop, onDragEnd won't fire: https://bugzilla.mozilla.org/show_bug.cgi?id=460801
244
- // In this case, we need to manually call onDragEnd on cleanup
245
-
246
- useEffect(() => {
247
- return () => {
248
- // Check that the dragged element has actually unmounted from the DOM and not a React Strict Mode false positive.
249
- // https://github.com/facebook/react/issues/29585
250
- // React 16 ran effect cleanups before removing elements from the DOM but did not have this issue.
251
- if (isDraggingRef.current && (!isDraggingRef.current.isConnected || parseInt(ReactVersion, 10) < 17)) {
252
- if (typeof state.options.onDragEnd === 'function') {
253
- let event: DragEndEvent = {
254
- type: 'dragend',
255
- x: 0,
256
- y: 0,
257
- dropOperation: DROP_EFFECT_TO_DROP_OPERATION[globalDropEffect || 'none']
258
- };
259
- state.options.onDragEnd(event);
260
- }
261
-
262
- setDragging(null);
263
- setGlobalAllowedDropOperations(DROP_OPERATION.none);
264
- setGlobalDropEffect(undefined);
265
- }
266
- };
267
- }, [state]);
268
-
269
- let onPress = (e: PressEvent) => {
270
- if (e.pointerType !== 'keyboard' && e.pointerType !== 'virtual') {
271
- return;
272
- }
273
-
274
- startDragging(e.target as HTMLElement);
275
- };
276
-
277
- let startDragging = (target: HTMLElement) => {
278
- if (typeof state.options.onDragStart === 'function') {
279
- let rect = target.getBoundingClientRect();
280
- state.options.onDragStart({
281
- type: 'dragstart',
282
- x: rect.x + (rect.width / 2),
283
- y: rect.y + (rect.height / 2)
284
- });
285
- }
286
-
287
- DragManager.beginDragging({
288
- element: target,
289
- items: state.options.getItems(),
290
- allowedDropOperations: typeof state.options.getAllowedDropOperations === 'function'
291
- ? state.options.getAllowedDropOperations()
292
- : ['move', 'copy', 'link'],
293
- onDragEnd(e) {
294
- setDragging(null);
295
- if (typeof state.options.onDragEnd === 'function') {
296
- state.options.onDragEnd(e);
297
- }
298
- }
299
- }, stringFormatter);
300
-
301
- setDragging(target);
302
- };
303
-
304
- let modality = useDragModality();
305
- let message = !isDragging ? MESSAGES[modality].start : MESSAGES[modality].end;
306
-
307
- let descriptionProps = useDescription(stringFormatter.format(message));
308
-
309
- let interactions: HTMLAttributes<HTMLElement> = {};
310
- if (!hasDragButton) {
311
- // If there's no separate button to trigger accessible drag and drop mode,
312
- // then add event handlers to the draggable element itself to start dragging.
313
- // For keyboard, we use the Enter key in a capturing listener to prevent other
314
- // events such as selection from also occurring. We attempt to infer whether a
315
- // pointer event (e.g. long press) came from a touch screen reader, and then initiate
316
- // dragging in the native onDragStart listener above.
317
-
318
- interactions = {
319
- ...descriptionProps,
320
- onPointerDown(e) {
321
- modalityOnPointerDown.current = isVirtualPointerEvent(e.nativeEvent) ? 'virtual' : e.pointerType;
322
-
323
- // Try to detect virtual drag passthrough gestures.
324
- if (e.width < 1 && e.height < 1) {
325
- // iOS VoiceOver.
326
- modalityOnPointerDown.current = 'virtual';
327
- } else {
328
- let rect = e.currentTarget.getBoundingClientRect();
329
- let offsetX = e.clientX - rect.x;
330
- let offsetY = e.clientY - rect.y;
331
- let centerX = rect.width / 2;
332
- let centerY = rect.height / 2;
333
-
334
- if (Math.abs(offsetX - centerX) <= 0.5 && Math.abs(offsetY - centerY) <= 0.5) {
335
- // Android TalkBack.
336
- modalityOnPointerDown.current = 'virtual';
337
- } else {
338
- modalityOnPointerDown.current = e.pointerType;
339
- }
340
- }
341
- },
342
- onKeyDownCapture(e) {
343
- if (e.target === e.currentTarget && e.key === 'Enter') {
344
- e.preventDefault();
345
- e.stopPropagation();
346
- }
347
- },
348
- onKeyUpCapture(e) {
349
- if (e.target === e.currentTarget && e.key === 'Enter') {
350
- e.preventDefault();
351
- e.stopPropagation();
352
- startDragging(e.target as HTMLElement);
353
- }
354
- },
355
- onClick(e) {
356
- // Handle NVDA/JAWS in browse mode, and touch screen readers. In this case, no keyboard events are fired.
357
- if (isVirtualClick(e.nativeEvent) || modalityOnPointerDown.current === 'virtual') {
358
- e.preventDefault();
359
- e.stopPropagation();
360
- startDragging(e.target as HTMLElement);
361
- }
362
- }
363
- };
364
- }
365
-
366
- if (isDisabled) {
367
- return {
368
- dragProps: {
369
- draggable: 'false'
370
- },
371
- dragButtonProps: {},
372
- isDragging: false
373
- };
374
- }
375
-
376
- return {
377
- dragProps: {
378
- ...interactions,
379
- draggable: 'true',
380
- onDragStart,
381
- onDrag,
382
- onDragEnd
383
- },
384
- dragButtonProps: {
385
- ...descriptionProps,
386
- onPress
387
- },
388
- isDragging
389
- };
390
- }
@@ -1,29 +0,0 @@
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
- }