@react-aria/dnd 3.0.0-alpha.1 → 3.0.0-alpha.10
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.
- package/dist/main.js +1684 -1981
- package/dist/main.js.map +1 -1
- package/dist/module.js +1647 -1908
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +15 -11
- package/dist/types.d.ts.map +1 -1
- package/package.json +14 -14
- package/src/DragManager.ts +70 -24
- package/src/DragPreview.tsx +54 -0
- package/src/constants.ts +3 -1
- package/src/index.ts +3 -0
- package/src/useDrag.ts +34 -33
- package/src/useDraggableItem.ts +6 -8
- package/src/useDrop.ts +7 -2
- package/src/useDropIndicator.ts +2 -2
- package/src/useDroppableCollection.ts +4 -9
- package/src/useDroppableItem.ts +2 -2
- package/src/utils.ts +1 -20
package/dist/types.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { AriaButtonProps } from "@react-types/button";
|
|
2
|
-
import { DragEndEvent, DragItem, DragMoveEvent, DragStartEvent, DropOperation, DropActivateEvent, DropEnterEvent, DropEvent, DropExitEvent, DropMoveEvent, DragTypes, DroppableCollectionProps, DropTarget, KeyboardDelegate, DropItem } from "@react-types/shared";
|
|
3
|
-
import { HTMLAttributes, RefObject, Key } from "react";
|
|
2
|
+
import { DragEndEvent, DragItem, DragMoveEvent, DragPreviewRenderer, DragStartEvent, DropOperation, DropActivateEvent, DropEnterEvent, DropEvent, DropExitEvent, DropMoveEvent, DragTypes, DroppableCollectionProps, DropTarget, KeyboardDelegate, DropItem } from "@react-types/shared";
|
|
3
|
+
import React, { HTMLAttributes, RefObject, Key } from "react";
|
|
4
4
|
import { DroppableCollectionState, DraggableCollectionState } from "@react-stately/dnd";
|
|
5
5
|
interface DragOptions {
|
|
6
6
|
onDragStart?: (e: DragStartEvent) => void;
|
|
7
7
|
onDragMove?: (e: DragMoveEvent) => void;
|
|
8
8
|
onDragEnd?: (e: DragEndEvent) => void;
|
|
9
9
|
getItems: () => DragItem[];
|
|
10
|
-
|
|
10
|
+
preview?: RefObject<DragPreviewRenderer>;
|
|
11
11
|
getAllowedDropOperations?: () => DropOperation[];
|
|
12
12
|
}
|
|
13
13
|
interface DragResult {
|
|
@@ -31,32 +31,32 @@ interface DropResult {
|
|
|
31
31
|
isDropTarget: boolean;
|
|
32
32
|
}
|
|
33
33
|
export function useDrop(options: DropOptions): DropResult;
|
|
34
|
-
interface DroppableCollectionOptions extends DroppableCollectionProps {
|
|
34
|
+
export interface DroppableCollectionOptions extends DroppableCollectionProps {
|
|
35
35
|
keyboardDelegate: KeyboardDelegate;
|
|
36
36
|
getDropTargetFromPoint: (x: number, y: number) => DropTarget | null;
|
|
37
37
|
}
|
|
38
|
-
interface DroppableCollectionResult {
|
|
38
|
+
export interface DroppableCollectionResult {
|
|
39
39
|
collectionProps: HTMLAttributes<HTMLElement>;
|
|
40
40
|
}
|
|
41
41
|
export function useDroppableCollection(props: DroppableCollectionOptions, state: DroppableCollectionState, ref: RefObject<HTMLElement>): DroppableCollectionResult;
|
|
42
|
-
interface DroppableItemOptions {
|
|
42
|
+
export interface DroppableItemOptions {
|
|
43
43
|
target: DropTarget;
|
|
44
44
|
}
|
|
45
|
-
interface DroppableItemResult {
|
|
45
|
+
export interface DroppableItemResult {
|
|
46
46
|
dropProps: HTMLAttributes<HTMLElement>;
|
|
47
47
|
}
|
|
48
48
|
export function useDroppableItem(options: DroppableItemOptions, state: DroppableCollectionState, ref: RefObject<HTMLElement>): DroppableItemResult;
|
|
49
|
-
interface DropIndicatorProps {
|
|
49
|
+
export interface DropIndicatorProps {
|
|
50
50
|
target: DropTarget;
|
|
51
51
|
}
|
|
52
|
-
interface DropIndicatorAria {
|
|
52
|
+
export interface DropIndicatorAria {
|
|
53
53
|
dropIndicatorProps: HTMLAttributes<HTMLElement>;
|
|
54
54
|
}
|
|
55
55
|
export function useDropIndicator(props: DropIndicatorProps, state: DroppableCollectionState, ref: RefObject<HTMLElement>): DropIndicatorAria;
|
|
56
|
-
interface DraggableItemProps {
|
|
56
|
+
export interface DraggableItemProps {
|
|
57
57
|
key: Key;
|
|
58
58
|
}
|
|
59
|
-
interface DraggableItemResult {
|
|
59
|
+
export interface DraggableItemResult {
|
|
60
60
|
dragProps: HTMLAttributes<HTMLElement>;
|
|
61
61
|
dragButtonProps: AriaButtonProps;
|
|
62
62
|
}
|
|
@@ -71,5 +71,9 @@ interface ClipboardResult {
|
|
|
71
71
|
clipboardProps: HTMLAttributes<HTMLElement>;
|
|
72
72
|
}
|
|
73
73
|
export function useClipboard(options: ClipboardProps): ClipboardResult;
|
|
74
|
+
export interface DragPreviewProps {
|
|
75
|
+
children: (items: DragItem[]) => JSX.Element;
|
|
76
|
+
}
|
|
77
|
+
export let DragPreview: React.ForwardRefExoticComponent<DragPreviewProps & React.RefAttributes<DragPreviewRenderer>>;
|
|
74
78
|
|
|
75
79
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":"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","sources":["packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/constants.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/utils.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/DragManager.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/useDrag.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/useVirtualDrop.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/useDrop.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/useAutoScroll.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/useDroppableCollection.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/useDroppableItem.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/useDropIndicator.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/useDraggableItem.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/useClipboard.ts","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/DragPreview.tsx","packages/@react-aria/dnd/src/packages/@react-aria/dnd/src/index.ts","packages/@react-aria/dnd/src/index.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport * from './useDrag';\nexport * from './useDrop';\nexport * from './useDroppableCollection';\nexport * from './useDroppableItem';\nexport * from './useDropIndicator';\nexport * from './useDraggableItem';\nexport * from './useClipboard';\nexport {DragPreview} from './DragPreview';\n\nexport type {DragPreviewProps} from './DragPreview';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/dnd",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.10",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -18,23 +18,23 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@babel/runtime": "^7.6.2",
|
|
21
|
-
"@react-aria/i18n": "^3.
|
|
22
|
-
"@react-aria/interactions": "^3.
|
|
23
|
-
"@react-aria/live-announcer": "^3.
|
|
24
|
-
"@react-aria/overlays": "^3.
|
|
25
|
-
"@react-aria/utils": "^3.
|
|
26
|
-
"@react-aria/visually-hidden": "^3.
|
|
27
|
-
"@react-stately/dnd": "3.0.0-alpha.
|
|
28
|
-
"@react-stately/selection": "^3.
|
|
29
|
-
"@react-types/button": "^3.
|
|
30
|
-
"@react-types/shared": "^3.
|
|
21
|
+
"@react-aria/i18n": "^3.4.1",
|
|
22
|
+
"@react-aria/interactions": "^3.9.1",
|
|
23
|
+
"@react-aria/live-announcer": "^3.1.0",
|
|
24
|
+
"@react-aria/overlays": "^3.9.1",
|
|
25
|
+
"@react-aria/utils": "^3.13.1",
|
|
26
|
+
"@react-aria/visually-hidden": "^3.3.1",
|
|
27
|
+
"@react-stately/dnd": "3.0.0-alpha.8",
|
|
28
|
+
"@react-stately/selection": "^3.10.1",
|
|
29
|
+
"@react-types/button": "^3.5.1",
|
|
30
|
+
"@react-types/shared": "^3.13.1"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"react": "^16.8.0 || ^17.0.0-rc.1",
|
|
34
|
-
"react-dom": "^16.8.0 || ^17.0.0-rc.1"
|
|
33
|
+
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0",
|
|
34
|
+
"react-dom": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"
|
|
35
35
|
},
|
|
36
36
|
"publishConfig": {
|
|
37
37
|
"access": "public"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "715c3f563ccf8c2e0102d3e18403d9db21a05a71"
|
|
40
40
|
}
|
package/src/DragManager.ts
CHANGED
|
@@ -101,6 +101,16 @@ function endDragging() {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
export function isValidDropTarget(element: Element): boolean {
|
|
105
|
+
for (let target of dropTargets.keys()) {
|
|
106
|
+
if (target.contains(element)) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
104
114
|
const CANCELED_EVENTS = [
|
|
105
115
|
'pointerdown',
|
|
106
116
|
'pointermove',
|
|
@@ -146,6 +156,8 @@ class DragSession {
|
|
|
146
156
|
private mutationImmediate: NodeJS.Immediate;
|
|
147
157
|
private restoreAriaHidden: () => void;
|
|
148
158
|
private formatMessage: (key: string) => string;
|
|
159
|
+
private isVirtualClick: boolean;
|
|
160
|
+
private initialFocused: boolean;
|
|
149
161
|
|
|
150
162
|
constructor(target: DragTarget, formatMessage: (key: string) => string) {
|
|
151
163
|
this.dragTarget = target;
|
|
@@ -155,7 +167,9 @@ class DragSession {
|
|
|
155
167
|
this.onFocus = this.onFocus.bind(this);
|
|
156
168
|
this.onBlur = this.onBlur.bind(this);
|
|
157
169
|
this.onClick = this.onClick.bind(this);
|
|
170
|
+
this.onPointerDown = this.onPointerDown.bind(this);
|
|
158
171
|
this.cancelEvent = this.cancelEvent.bind(this);
|
|
172
|
+
this.initialFocused = false;
|
|
159
173
|
}
|
|
160
174
|
|
|
161
175
|
setup() {
|
|
@@ -163,22 +177,15 @@ class DragSession {
|
|
|
163
177
|
window.addEventListener('focus', this.onFocus, true);
|
|
164
178
|
window.addEventListener('blur', this.onBlur, true);
|
|
165
179
|
document.addEventListener('click', this.onClick, true);
|
|
180
|
+
document.addEventListener('pointerdown', this.onPointerDown, true);
|
|
166
181
|
|
|
167
182
|
for (let event of CANCELED_EVENTS) {
|
|
168
183
|
document.addEventListener(event, this.cancelEvent, true);
|
|
169
184
|
}
|
|
170
185
|
|
|
171
|
-
this.mutationObserver = new MutationObserver(() =>
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
// the next tick to update valid drop targets.
|
|
175
|
-
// See https://github.com/jsdom/jsdom/issues/3096
|
|
176
|
-
if (typeof setImmediate === 'function') {
|
|
177
|
-
this.mutationImmediate = setImmediate(() => this.updateValidDropTargets());
|
|
178
|
-
} else {
|
|
179
|
-
this.updateValidDropTargets();
|
|
180
|
-
}
|
|
181
|
-
});
|
|
186
|
+
this.mutationObserver = new MutationObserver(() =>
|
|
187
|
+
this.updateValidDropTargets()
|
|
188
|
+
);
|
|
182
189
|
this.updateValidDropTargets();
|
|
183
190
|
|
|
184
191
|
announce(this.formatMessage(MESSAGES[getDragModality()]));
|
|
@@ -189,6 +196,7 @@ class DragSession {
|
|
|
189
196
|
window.removeEventListener('focus', this.onFocus, true);
|
|
190
197
|
window.removeEventListener('blur', this.onBlur, true);
|
|
191
198
|
document.removeEventListener('click', this.onClick, true);
|
|
199
|
+
document.removeEventListener('pointerdown', this.onPointerDown, true);
|
|
192
200
|
|
|
193
201
|
for (let event of CANCELED_EVENTS) {
|
|
194
202
|
document.removeEventListener(event, this.cancelEvent, true);
|
|
@@ -274,25 +282,34 @@ class DragSession {
|
|
|
274
282
|
|
|
275
283
|
onClick(e: MouseEvent) {
|
|
276
284
|
this.cancelEvent(e);
|
|
285
|
+
if (e.detail === 0 || this.isVirtualClick) {
|
|
286
|
+
if (e.target === this.dragTarget.element) {
|
|
287
|
+
this.cancel();
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
277
290
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
return;
|
|
291
|
+
let dropTarget = this.validDropTargets.find(target => target.element.contains(e.target as HTMLElement));
|
|
292
|
+
if (dropTarget) {
|
|
293
|
+
let item = dropItems.get(e.target as HTMLElement);
|
|
294
|
+
this.setCurrentDropTarget(dropTarget, item);
|
|
295
|
+
this.drop(item);
|
|
296
|
+
}
|
|
285
297
|
}
|
|
298
|
+
}
|
|
286
299
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
300
|
+
onPointerDown(e: PointerEvent) {
|
|
301
|
+
// Android Talkback double tap has e.detail = 1 for onClick. Detect the virtual click in onPointerDown before onClick fires
|
|
302
|
+
// so we can properly perform cancel and drop operations.
|
|
303
|
+
this.cancelEvent(e);
|
|
304
|
+
this.isVirtualClick = isVirtualPointerEvent(e);
|
|
293
305
|
}
|
|
294
306
|
|
|
295
307
|
cancelEvent(e: Event) {
|
|
308
|
+
// Allow focusin and focusout on the drag target so focus ring works properly.
|
|
309
|
+
if ((e.type === 'focusin' || e.type === 'focusout') && e.target === this.dragTarget?.element) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
296
313
|
// Allow default for events that might cancel a click event
|
|
297
314
|
if (!CLICK_EVENTS.includes(e.type)) {
|
|
298
315
|
e.preventDefault();
|
|
@@ -431,6 +448,13 @@ class DragSession {
|
|
|
431
448
|
|
|
432
449
|
item?.element.focus();
|
|
433
450
|
this.currentDropItem = item;
|
|
451
|
+
|
|
452
|
+
// Annouce first drop target after drag start announcement finishes.
|
|
453
|
+
// Otherwise, it will never get announced because drag start announcement is assertive.
|
|
454
|
+
if (!this.initialFocused) {
|
|
455
|
+
announce(item?.element.getAttribute('aria-label'), 'polite');
|
|
456
|
+
this.initialFocused = true;
|
|
457
|
+
}
|
|
434
458
|
}
|
|
435
459
|
}
|
|
436
460
|
|
|
@@ -448,6 +472,12 @@ class DragSession {
|
|
|
448
472
|
});
|
|
449
473
|
}
|
|
450
474
|
|
|
475
|
+
// Blur and re-focus the drop target so that the focus ring appears.
|
|
476
|
+
if (this.currentDropTarget) {
|
|
477
|
+
this.currentDropTarget.element.blur();
|
|
478
|
+
this.currentDropTarget.element.focus();
|
|
479
|
+
}
|
|
480
|
+
|
|
451
481
|
this.setCurrentDropTarget(null);
|
|
452
482
|
endDragging();
|
|
453
483
|
}
|
|
@@ -525,3 +555,19 @@ function findValidDropTargets(options: DragTarget) {
|
|
|
525
555
|
return true;
|
|
526
556
|
});
|
|
527
557
|
}
|
|
558
|
+
|
|
559
|
+
function isVirtualPointerEvent(event: PointerEvent) {
|
|
560
|
+
// If the pointer size is zero, then we assume it's from a screen reader.
|
|
561
|
+
// Android TalkBack double tap will sometimes return a event with width and height of 1
|
|
562
|
+
// and pointerType === 'mouse' so we need to check for a specific combination of event attributes.
|
|
563
|
+
// Cannot use "event.pressure === 0" as the sole check due to Safari pointer events always returning pressure === 0
|
|
564
|
+
// instead of .5, see https://bugs.webkit.org/show_bug.cgi?id=206216
|
|
565
|
+
return (
|
|
566
|
+
(event.width === 0 && event.height === 0) ||
|
|
567
|
+
(event.width === 1 &&
|
|
568
|
+
event.height === 1 &&
|
|
569
|
+
event.pressure === 0 &&
|
|
570
|
+
event.detail === 0
|
|
571
|
+
)
|
|
572
|
+
);
|
|
573
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
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, {RefObject, useImperativeHandle, useRef, useState} from 'react';
|
|
16
|
+
|
|
17
|
+
export interface DragPreviewProps {
|
|
18
|
+
children: (items: DragItem[]) => JSX.Element
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function DragPreview(props: DragPreviewProps, ref: RefObject<DragPreviewRenderer>) {
|
|
22
|
+
let render = props.children;
|
|
23
|
+
let [children, setChildren] = useState<JSX.Element>(null);
|
|
24
|
+
let domRef = useRef(null);
|
|
25
|
+
|
|
26
|
+
useImperativeHandle(ref, () => (items: DragItem[], callback: (node: HTMLElement) => void) => {
|
|
27
|
+
// This will be called during the onDragStart event by useDrag. We need to render the
|
|
28
|
+
// preview synchronously before this event returns so we can call event.dataTransfer.setDragImage.
|
|
29
|
+
flushSync(() => {
|
|
30
|
+
setChildren(render(items));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Yield back to useDrag to set the drag image.
|
|
34
|
+
callback(domRef.current);
|
|
35
|
+
|
|
36
|
+
// Remove the preview from the DOM after a frame so the browser has time to paint.
|
|
37
|
+
requestAnimationFrame(() => {
|
|
38
|
+
setChildren(null);
|
|
39
|
+
});
|
|
40
|
+
}, [render]);
|
|
41
|
+
|
|
42
|
+
if (!children) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<div style={{zIndex: -100, position: 'absolute', top: 0, left: -100000}} ref={domRef}>
|
|
48
|
+
{children}
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let _DragPreview = React.forwardRef(DragPreview);
|
|
54
|
+
export {_DragPreview as DragPreview};
|
package/src/constants.ts
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
import {DropOperation} from '@react-types/shared';
|
|
14
|
+
|
|
13
15
|
export enum DROP_OPERATION {
|
|
14
16
|
none = 0,
|
|
15
17
|
cancel = 0,
|
|
@@ -31,7 +33,7 @@ export const DROP_OPERATION_ALLOWED = {
|
|
|
31
33
|
|
|
32
34
|
export const EFFECT_ALLOWED = invert(DROP_OPERATION_ALLOWED);
|
|
33
35
|
export const DROP_EFFECT = invert(DROP_OPERATION);
|
|
34
|
-
export const DROP_EFFECT_TO_DROP_OPERATION = {
|
|
36
|
+
export const DROP_EFFECT_TO_DROP_OPERATION: {[name: string]: DropOperation} = {
|
|
35
37
|
none: 'cancel',
|
|
36
38
|
link: 'link',
|
|
37
39
|
copy: 'copy',
|
package/src/index.ts
CHANGED
package/src/useDrag.ts
CHANGED
|
@@ -11,14 +11,13 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import {AriaButtonProps} from '@react-types/button';
|
|
14
|
-
import {DragEndEvent, DragItem, DragMoveEvent, DragStartEvent, DropOperation, PressEvent} from '@react-types/shared';
|
|
15
|
-
import {DragEvent, HTMLAttributes, useRef, useState} from 'react';
|
|
14
|
+
import {DragEndEvent, DragItem, DragMoveEvent, DragPreviewRenderer, DragStartEvent, DropOperation, PressEvent} from '@react-types/shared';
|
|
15
|
+
import {DragEvent, HTMLAttributes, RefObject, 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
18
|
// @ts-ignore
|
|
19
19
|
import intlMessages from '../intl/*.json';
|
|
20
|
-
import
|
|
21
|
-
import {useDescription} from '@react-aria/utils';
|
|
20
|
+
import {useDescription, useGlobalListeners} from '@react-aria/utils';
|
|
22
21
|
import {useDragModality} from './utils';
|
|
23
22
|
import {useMessageFormatter} from '@react-aria/i18n';
|
|
24
23
|
import {writeToDataTransfer} from './utils';
|
|
@@ -28,7 +27,7 @@ interface DragOptions {
|
|
|
28
27
|
onDragMove?: (e: DragMoveEvent) => void,
|
|
29
28
|
onDragEnd?: (e: DragEndEvent) => void,
|
|
30
29
|
getItems: () => DragItem[],
|
|
31
|
-
|
|
30
|
+
preview?: RefObject<DragPreviewRenderer>,
|
|
32
31
|
getAllowedDropOperations?: () => DropOperation[]
|
|
33
32
|
}
|
|
34
33
|
|
|
@@ -62,8 +61,21 @@ export function useDrag(options: DragOptions): DragResult {
|
|
|
62
61
|
}).current;
|
|
63
62
|
state.options = options;
|
|
64
63
|
let [isDragging, setDragging] = useState(false);
|
|
64
|
+
let {addGlobalListener, removeAllGlobalListeners} = useGlobalListeners();
|
|
65
65
|
|
|
66
66
|
let onDragStart = (e: DragEvent) => {
|
|
67
|
+
if (e.defaultPrevented) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (typeof options.onDragStart === 'function') {
|
|
72
|
+
options.onDragStart({
|
|
73
|
+
type: 'dragstart',
|
|
74
|
+
x: e.clientX,
|
|
75
|
+
y: e.clientY
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
67
79
|
let items = options.getItems();
|
|
68
80
|
writeToDataTransfer(e.dataTransfer, items);
|
|
69
81
|
|
|
@@ -77,22 +89,10 @@ export function useDrag(options: DragOptions): DragResult {
|
|
|
77
89
|
e.dataTransfer.effectAllowed = EFFECT_ALLOWED[allowed] || 'none';
|
|
78
90
|
}
|
|
79
91
|
|
|
80
|
-
// If there is a
|
|
92
|
+
// If there is a preview option, use it to render a custom preview image that will
|
|
81
93
|
// appear under the pointer while dragging. If not, the element itself is dragged by the browser.
|
|
82
|
-
if (typeof options.
|
|
83
|
-
|
|
84
|
-
if (preview) {
|
|
85
|
-
// Create an off-screen div to render the preview into.
|
|
86
|
-
let node = document.createElement('div');
|
|
87
|
-
node.style.zIndex = '-100';
|
|
88
|
-
node.style.position = 'absolute';
|
|
89
|
-
node.style.top = '0';
|
|
90
|
-
node.style.left = '-100000px';
|
|
91
|
-
document.body.appendChild(node);
|
|
92
|
-
|
|
93
|
-
// Call renderPreview to get a JSX element, and render it into the div with React DOM.
|
|
94
|
-
ReactDOM.render(preview, node);
|
|
95
|
-
|
|
94
|
+
if (typeof options.preview?.current === 'function') {
|
|
95
|
+
options.preview.current(items, node => {
|
|
96
96
|
// Compute the offset that the preview will appear under the mouse.
|
|
97
97
|
// If possible, this is based on the point the user clicked on the target.
|
|
98
98
|
// If the preview is much smaller, then just use the center point of the preview.
|
|
@@ -105,23 +105,23 @@ export function useDrag(options: DragOptions): DragResult {
|
|
|
105
105
|
y = size.height / 2;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
requestAnimationFrame(() => {
|
|
112
|
-
document.body.removeChild(node);
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}
|
|
108
|
+
// Rounding height to an even number prevents blurry preview seen on some screens
|
|
109
|
+
let height = 2 * Math.round(rect.height / 2);
|
|
110
|
+
node.style.height = `${height}px`;
|
|
116
111
|
|
|
117
|
-
|
|
118
|
-
options.onDragStart({
|
|
119
|
-
type: 'dragstart',
|
|
120
|
-
x: e.clientX,
|
|
121
|
-
y: e.clientY
|
|
112
|
+
e.dataTransfer.setDragImage(node, x, y);
|
|
122
113
|
});
|
|
123
114
|
}
|
|
124
115
|
|
|
116
|
+
// Enforce that drops are handled by useDrop.
|
|
117
|
+
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
|
+
|
|
125
125
|
state.x = e.clientX;
|
|
126
126
|
state.y = e.clientY;
|
|
127
127
|
|
|
@@ -160,6 +160,7 @@ export function useDrag(options: DragOptions): DragResult {
|
|
|
160
160
|
}
|
|
161
161
|
|
|
162
162
|
setDragging(false);
|
|
163
|
+
removeAllGlobalListeners();
|
|
163
164
|
};
|
|
164
165
|
|
|
165
166
|
let onPress = (e: PressEvent) => {
|
package/src/useDraggableItem.ts
CHANGED
|
@@ -18,11 +18,11 @@ import intlMessages from '../intl/*.json';
|
|
|
18
18
|
import {useDrag} from './useDrag';
|
|
19
19
|
import {useMessageFormatter} from '@react-aria/i18n';
|
|
20
20
|
|
|
21
|
-
interface DraggableItemProps {
|
|
21
|
+
export interface DraggableItemProps {
|
|
22
22
|
key: Key
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
interface DraggableItemResult {
|
|
25
|
+
export interface DraggableItemResult {
|
|
26
26
|
dragProps: HTMLAttributes<HTMLElement>,
|
|
27
27
|
dragButtonProps: AriaButtonProps
|
|
28
28
|
}
|
|
@@ -33,9 +33,7 @@ export function useDraggableItem(props: DraggableItemProps, state: DraggableColl
|
|
|
33
33
|
getItems() {
|
|
34
34
|
return state.getItems(props.key);
|
|
35
35
|
},
|
|
36
|
-
|
|
37
|
-
return state.renderPreview(props.key);
|
|
38
|
-
},
|
|
36
|
+
preview: state.preview,
|
|
39
37
|
onDragStart(e) {
|
|
40
38
|
state.startDrag(props.key, e);
|
|
41
39
|
},
|
|
@@ -48,11 +46,11 @@ export function useDraggableItem(props: DraggableItemProps, state: DraggableColl
|
|
|
48
46
|
});
|
|
49
47
|
|
|
50
48
|
let item = state.collection.getItem(props.key);
|
|
51
|
-
let
|
|
49
|
+
let numKeysForDrag = state.getKeysForDrag(props.key).size;
|
|
52
50
|
let isSelected = state.selectionManager.isSelected(props.key);
|
|
53
51
|
let message: string;
|
|
54
|
-
if (isSelected &&
|
|
55
|
-
message = formatMessage('dragSelectedItems', {count:
|
|
52
|
+
if (isSelected && numKeysForDrag > 1) {
|
|
53
|
+
message = formatMessage('dragSelectedItems', {count: numKeysForDrag});
|
|
56
54
|
} else {
|
|
57
55
|
message = formatMessage('dragItem', {itemText: item?.textValue ?? ''});
|
|
58
56
|
}
|
package/src/useDrop.ts
CHANGED
|
@@ -10,11 +10,12 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {DragEvent, HTMLAttributes, RefObject,
|
|
13
|
+
import {DragEvent, HTMLAttributes, RefObject, useRef, useState} from 'react';
|
|
14
14
|
import * as DragManager from './DragManager';
|
|
15
15
|
import {DragTypes, readFromDataTransfer} from './utils';
|
|
16
16
|
import {DROP_EFFECT_TO_DROP_OPERATION, DROP_OPERATION, DROP_OPERATION_ALLOWED, DROP_OPERATION_TO_DROP_EFFECT} from './constants';
|
|
17
17
|
import {DropActivateEvent, DropEnterEvent, DropEvent, DropExitEvent, DropMoveEvent, DropOperation, DragTypes as IDragTypes} from '@react-types/shared';
|
|
18
|
+
import {useLayoutEffect} from '@react-aria/utils';
|
|
18
19
|
import {useVirtualDrop} from './useVirtualDrop';
|
|
19
20
|
|
|
20
21
|
interface DropOptions {
|
|
@@ -43,12 +44,13 @@ export function useDrop(options: DropOptions): DropResult {
|
|
|
43
44
|
x: 0,
|
|
44
45
|
y: 0,
|
|
45
46
|
dragEnterCount: 0,
|
|
46
|
-
dropEffect: 'none',
|
|
47
|
+
dropEffect: 'none' as DataTransfer['dropEffect'],
|
|
47
48
|
dropActivateTimer: null
|
|
48
49
|
}).current;
|
|
49
50
|
|
|
50
51
|
let onDragOver = (e: DragEvent) => {
|
|
51
52
|
e.preventDefault();
|
|
53
|
+
e.stopPropagation();
|
|
52
54
|
|
|
53
55
|
if (e.clientX === state.x && e.clientY === state.y) {
|
|
54
56
|
e.dataTransfer.dropEffect = state.dropEffect;
|
|
@@ -92,6 +94,7 @@ export function useDrop(options: DropOptions): DropResult {
|
|
|
92
94
|
};
|
|
93
95
|
|
|
94
96
|
let onDragEnter = (e: DragEvent) => {
|
|
97
|
+
e.stopPropagation();
|
|
95
98
|
state.dragEnterCount++;
|
|
96
99
|
if (state.dragEnterCount > 1) {
|
|
97
100
|
return;
|
|
@@ -132,6 +135,7 @@ export function useDrop(options: DropOptions): DropResult {
|
|
|
132
135
|
};
|
|
133
136
|
|
|
134
137
|
let onDragLeave = (e: DragEvent) => {
|
|
138
|
+
e.stopPropagation();
|
|
135
139
|
state.dragEnterCount--;
|
|
136
140
|
if (state.dragEnterCount > 0) {
|
|
137
141
|
return;
|
|
@@ -152,6 +156,7 @@ export function useDrop(options: DropOptions): DropResult {
|
|
|
152
156
|
|
|
153
157
|
let onDrop = (e: DragEvent) => {
|
|
154
158
|
e.preventDefault();
|
|
159
|
+
e.stopPropagation();
|
|
155
160
|
|
|
156
161
|
if (typeof options.onDrop === 'function') {
|
|
157
162
|
let dropOperation = DROP_EFFECT_TO_DROP_OPERATION[state.dropEffect];
|
package/src/useDropIndicator.ts
CHANGED
|
@@ -21,11 +21,11 @@ import {useDroppableItem} from './useDroppableItem';
|
|
|
21
21
|
import {useId} from '@react-aria/utils';
|
|
22
22
|
import {useMessageFormatter} from '@react-aria/i18n';
|
|
23
23
|
|
|
24
|
-
interface DropIndicatorProps {
|
|
24
|
+
export interface DropIndicatorProps {
|
|
25
25
|
target: DropTarget
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
interface DropIndicatorAria {
|
|
28
|
+
export interface DropIndicatorAria {
|
|
29
29
|
dropIndicatorProps: HTMLAttributes<HTMLElement>
|
|
30
30
|
}
|
|
31
31
|
|
|
@@ -14,19 +14,19 @@ import {Collection, DropEvent, DropOperation, DroppableCollectionProps, DropPosi
|
|
|
14
14
|
import * as DragManager from './DragManager';
|
|
15
15
|
import {DroppableCollectionState} from '@react-stately/dnd';
|
|
16
16
|
import {getTypes} from './utils';
|
|
17
|
-
import {HTMLAttributes, Key, RefObject, useCallback, useEffect,
|
|
18
|
-
import {mergeProps} from '@react-aria/utils';
|
|
17
|
+
import {HTMLAttributes, Key, RefObject, useCallback, useEffect, useRef} from 'react';
|
|
18
|
+
import {mergeProps, useLayoutEffect} from '@react-aria/utils';
|
|
19
19
|
import {setInteractionModality} from '@react-aria/interactions';
|
|
20
20
|
import {useAutoScroll} from './useAutoScroll';
|
|
21
21
|
import {useDrop} from './useDrop';
|
|
22
22
|
import {useDroppableCollectionId} from './utils';
|
|
23
23
|
|
|
24
|
-
interface DroppableCollectionOptions extends DroppableCollectionProps {
|
|
24
|
+
export interface DroppableCollectionOptions extends DroppableCollectionProps {
|
|
25
25
|
keyboardDelegate: KeyboardDelegate,
|
|
26
26
|
getDropTargetFromPoint: (x: number, y: number) => DropTarget | null
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
interface DroppableCollectionResult {
|
|
29
|
+
export interface DroppableCollectionResult {
|
|
30
30
|
collectionProps: HTMLAttributes<HTMLElement>
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -68,11 +68,6 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:
|
|
|
68
68
|
return 'cancel';
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
if (state.isDropTarget(target)) {
|
|
72
|
-
localState.nextTarget = target;
|
|
73
|
-
return localState.dropOperation;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
71
|
localState.dropOperation = state.getDropOperation(target, types, allowedOperations);
|
|
77
72
|
|
|
78
73
|
// If the target doesn't accept the drop, see if the root accepts it instead.
|
package/src/useDroppableItem.ts
CHANGED
|
@@ -17,11 +17,11 @@ import {getTypes} from './utils';
|
|
|
17
17
|
import {HTMLAttributes, RefObject, useEffect} from 'react';
|
|
18
18
|
import {useVirtualDrop} from './useVirtualDrop';
|
|
19
19
|
|
|
20
|
-
interface DroppableItemOptions {
|
|
20
|
+
export interface DroppableItemOptions {
|
|
21
21
|
target: DropTarget
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
interface DroppableItemResult {
|
|
24
|
+
export interface DroppableItemResult {
|
|
25
25
|
dropProps: HTMLAttributes<HTMLElement>
|
|
26
26
|
}
|
|
27
27
|
|