Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update simplified DnD handler api and memoize useDnDHooks #3533

Merged
merged 19 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/@react-aria/dnd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@react-aria/utils": "^3.13.3",
"@react-aria/visually-hidden": "^3.4.1",
"@react-stately/dnd": "3.0.0-alpha.10",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was doing some auditing and have a clarifying question: should things like @react-stately/dnd/@react-types/* be in devDeps instead if they are only used to import types/interfaces? I was under the impression that we could do a import type for things like that and thus avoid needing to include them within the dependencies because they only matter during dev (aka typechecking)

"@react-stately/selection": "^3.10.3",
"@react-types/button": "^3.6.1",
"@react-types/shared": "^3.14.1"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/@react-aria/dnd/src/DragManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {announce} from '@react-aria/live-announcer';
import {ariaHideOutside} from '@react-aria/overlays';
import {DragEndEvent, DragItem, DropActivateEvent, DropEnterEvent, DropEvent, DropExitEvent, DropItem, DropOperation, DropTarget as DroppableCollectionTarget, FocusableElement} from '@react-types/shared';
import {flushSync} from 'react-dom';
import {getDragModality, getTypes, setDropCollectionRef} from './utils';
import {getDragModality, getTypes} from './utils';
import {getInteractionModality} from '@react-aria/interactions';
import type {LocalizedStringFormatter} from '@internationalized/string';
import {useEffect, useState} from 'react';
Expand Down Expand Up @@ -517,7 +517,6 @@ class DragSession {
}

cancel() {
setDropCollectionRef(undefined);
this.end();
if (!this.dragTarget.element.closest('[aria-hidden="true"]')) {
this.dragTarget.element.focus();
Expand Down
1 change: 1 addition & 0 deletions packages/@react-aria/dnd/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type {DropOptions, DropResult} from './useDrop';
export type {ClipboardProps, ClipboardResult} from './useClipboard';
export type {DropTargetDelegate} from '@react-types/shared';

export {DIRECTORY_DRAG_TYPE} from './utils';
export {useDrag} from './useDrag';
export {useDrop} from './useDrop';
export {useDroppableCollection} from './useDroppableCollection';
Expand Down
8 changes: 4 additions & 4 deletions packages/@react-aria/dnd/src/useDraggableItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import {AriaButtonProps} from '@react-types/button';
import {clearGlobalDnDState, globalDndState, setDraggingKeys} from './utils';
import {clearGlobalDnDState, isInternalDropOperation, setDraggingKeys} from './utils';
import {DraggableCollectionState} from '@react-stately/dnd';
import {HTMLAttributes, Key} from 'react';
// @ts-ignore
Expand Down Expand Up @@ -77,9 +77,9 @@ export function useDraggableItem(props: DraggableItemProps, state: DraggableColl
state.moveDrag(e);
},
onDragEnd(e) {
let {draggingCollectionRef, dropCollectionRef} = globalDndState;
let isInternalDrop = draggingCollectionRef?.current != null && draggingCollectionRef.current === dropCollectionRef?.current;
state.endDrag({...e, keys: state.draggingKeys, isInternalDrop});
let {dropOperation} = e;
let isInternal = dropOperation === 'cancel' ? false : isInternalDropOperation();
state.endDrag({...e, keys: state.draggingKeys, isInternal});
clearGlobalDnDState();
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-aria/dnd/src/useDrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export function useDrop(options: DropOptions): DropResult {
if (dndStateSnapshot.draggingCollectionRef == null) {
setGlobalDropEffect(undefined);
} else {
// Otherwise we need to preserve the global dnd state for onDragEnd's isInternalDrop check.
// Otherwise we need to preserve the global dnd state for onDragEnd's isInternal check.
// At the moment fireDropExit may clear dropCollectionRef (i.e. useDroppableCollection's provided onDropExit, required to clear dropCollectionRef when exiting a valid drop target)
setGlobalDnDState(dndStateSnapshot);
}
Expand Down
82 changes: 42 additions & 40 deletions packages/@react-aria/dnd/src/useDroppableCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
* governing permissions and limitations under the License.
*/

import {clearGlobalDnDState, globalDndState, setDropCollectionRef, useDroppableCollectionId} from './utils';
import {clearGlobalDnDState, globalDndState, isInternalDropOperation, setDropCollectionRef, useDroppableCollectionId} from './utils';
import {Collection, DropEvent, DropOperation, DroppableCollectionDropEvent, DroppableCollectionProps, DropPosition, DropTarget, DropTargetDelegate, KeyboardDelegate, Node} from '@react-types/shared';
import {DIRECTORY_DRAG_TYPE, getTypes} from './utils';
import * as DragManager from './DragManager';
import {DroppableCollectionState} from '@react-stately/dnd';
import {getTypes} from './utils';
import {HTMLAttributes, Key, RefObject, useCallback, useEffect, useRef} from 'react';
import {mergeProps, useLayoutEffect} from '@react-aria/utils';
import {setInteractionModality} from '@react-aria/interactions';
Expand Down Expand Up @@ -55,24 +55,24 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:
onRootDrop,
onItemDrop,
onReorder,
acceptedDragTypes,
acceptedDragTypes = 'all',
shouldAcceptItemDrop
} = localState.props;

let {draggingCollectionRef, draggingKeys} = globalDndState;
let isInternalDrop = draggingCollectionRef?.current != null && draggingCollectionRef?.current === ref?.current;
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation(ref);
Copy link
Member Author

@LFDanLu LFDanLu Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of these isInternal checks use ref instead of relying on the global state's dropCollectionRef because I felt it was safer this way (global state is spooky in general). Since these are all within useDroppableCollection, the ref should be equal to dropCollectionRef for the most part (some exceptions exist where we want to calculate isInternal before we even set dropCollectionRef)

let {
target,
dropOperation,
items
} = e;

let filteredItems = items;
if (acceptedDragTypes) {
if (acceptedDragTypes !== 'all' || shouldAcceptItemDrop) {
filteredItems = items.filter(item => {
let itemTypes: Set<string>;
let itemTypes: Set<string | symbol>;
if (item.kind === 'directory') {
itemTypes = new Set(['directory']);
itemTypes = new Set([DIRECTORY_DRAG_TYPE]);
} else {
itemTypes = item.kind === 'file' ? new Set([item.type]) : item.types;
}
Expand All @@ -90,22 +90,24 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:
});
}

if (target.type === 'root' && onRootDrop) {
await onRootDrop({items: filteredItems, dropOperation});
}

if (target.type === 'item') {
if (target.dropPosition === 'on' && onItemDrop) {
await onItemDrop({items: filteredItems, dropOperation, isInternalDrop, target: {key: target.key, dropPosition: 'on'}});
if (filteredItems.length > 0) {
if (target.type === 'root' && onRootDrop) {
await onRootDrop({items: filteredItems, dropOperation});
}

if (target.dropPosition !== 'on') {
if (!isInternalDrop && onInsert) {
await onInsert({items: filteredItems, dropOperation, target: {key: target.key, dropPosition: target.dropPosition}});
if (target.type === 'item') {
if (target.dropPosition === 'on' && onItemDrop) {
await onItemDrop({items: filteredItems, dropOperation, isInternal, target});
}

if (isInternalDrop && onReorder) {
await onReorder({keys: draggingKeys, dropOperation, target: {key: target.key, dropPosition: target.dropPosition}});
if (target.dropPosition !== 'on') {
if (!isInternal && onInsert) {
await onInsert({items: filteredItems, dropOperation, target});
}

if (isInternal && onReorder) {
await onReorder({keys: draggingKeys, dropOperation, target});
}
}
}
}
Expand All @@ -122,22 +124,22 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:
autoScroll.move(e.x, e.y);
},
getDropOperationForPoint(types, allowedOperations, x, y) {
let {draggingCollectionRef, draggingKeys, dropCollectionRef} = globalDndState;
let isInternalDrop = draggingCollectionRef?.current != null && draggingCollectionRef?.current === ref?.current;
let isValidDropTarget = (target) => state.getDropOperation({target, types, allowedOperations, isInternalDrop, draggingKeys}) !== 'cancel';
let {draggingKeys, dropCollectionRef} = globalDndState;
let isInternal = isInternalDropOperation(ref);
let isValidDropTarget = (target) => state.getDropOperation({target, types, allowedOperations, isInternal, draggingKeys}) !== 'cancel';
let target = props.dropTargetDelegate.getDropTargetFromPoint(x, y, isValidDropTarget);
if (!target) {
localState.dropOperation = 'cancel';
localState.nextTarget = null;
return 'cancel';
}

localState.dropOperation = state.getDropOperation({target, types, allowedOperations, isInternalDrop, draggingKeys});
localState.dropOperation = state.getDropOperation({target, types, allowedOperations, isInternal, draggingKeys});

// If the target doesn't accept the drop, see if the root accepts it instead.
if (localState.dropOperation === 'cancel') {
let rootTarget: DropTarget = {type: 'root'};
let dropOperation = state.getDropOperation({target: rootTarget, types, allowedOperations, isInternalDrop, draggingKeys});
let dropOperation = state.getDropOperation({target: rootTarget, types, allowedOperations, isInternal, draggingKeys});
if (dropOperation !== 'cancel') {
target = rootTarget;
localState.dropOperation = dropOperation;
Expand Down Expand Up @@ -375,15 +377,15 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:
): DropTarget => {
let seenRoot = 0;
let operation: DropOperation;
let {draggingCollectionRef, draggingKeys} = globalDndState;
let isInternalDrop = draggingCollectionRef?.current != null && draggingCollectionRef?.current === ref?.current;
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation(ref);
do {
let nextTarget = getNextTarget(target, wrap);
if (!nextTarget) {
return null;
}
target = nextTarget;
operation = localState.state.getDropOperation({target: nextTarget, types, allowedOperations: allowedDropOperations, isInternalDrop, draggingKeys});
operation = localState.state.getDropOperation({target: nextTarget, types, allowedOperations: allowedDropOperations, isInternal, draggingKeys});
if (target.type === 'root') {
seenRoot++;
}
Expand All @@ -404,9 +406,9 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:
element: ref.current,
getDropOperation(types, allowedOperations) {
if (localState.state.target) {
let {draggingCollectionRef, draggingKeys} = globalDndState;
let isInternalDrop = draggingCollectionRef?.current != null && draggingCollectionRef?.current === ref?.current;
return localState.state.getDropOperation({target: localState.state.target, types, allowedOperations, isInternalDrop, draggingKeys});
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation(ref);
return localState.state.getDropOperation({target: localState.state.target, types, allowedOperations, isInternal, draggingKeys});
}

// Check if any of the targets accept the drop.
Expand All @@ -418,7 +420,7 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:
let types = getTypes(drag.items);
let selectionManager = localState.state.selectionManager;
let target: DropTarget;
// Update the drop collection ref tracker for useDroppableItem's getDropOperation isInternalDrop check
// Update the drop collection ref tracker for useDroppableItem's getDropOperation isInternal check
setDropCollectionRef(ref);

// When entering the droppable collection for the first time, the default drop target
Expand Down Expand Up @@ -452,10 +454,10 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:
dropPosition
};

let {draggingCollectionRef, draggingKeys} = globalDndState;
let isInternalDrop = draggingCollectionRef?.current != null && draggingCollectionRef?.current === ref?.current;
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation(ref);
// If the default target is not valid, find the next one that is.
if (localState.state.getDropOperation({target, types, allowedOperations: drag.allowedDropOperations, isInternalDrop, draggingKeys}) === 'cancel') {
if (localState.state.getDropOperation({target, types, allowedOperations: drag.allowedDropOperations, isInternal, draggingKeys}) === 'cancel') {
target = nextValidTarget(target, types, drag.allowedDropOperations, getNextTarget, false)
?? nextValidTarget(target, types, drag.allowedDropOperations, getPreviousTarget, false);
}
Expand Down Expand Up @@ -556,8 +558,8 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:
// If the target does not accept the drop, find the next valid target.
// If no next valid target, find the previous valid target.
let {draggingCollectionRef, draggingKeys} = globalDndState;
let isInternalDrop = draggingCollectionRef?.current === ref?.current;
let operation = localState.state.getDropOperation({target, types, allowedOperations: drag.allowedDropOperations, isInternalDrop, draggingKeys});
let isInternal = draggingCollectionRef?.current === ref?.current;
let operation = localState.state.getDropOperation({target, types, allowedOperations: drag.allowedDropOperations, isInternal, draggingKeys});
if (operation === 'cancel') {
target = nextValidTarget(target, types, drag.allowedDropOperations, getNextTarget, false)
?? nextValidTarget(target, types, drag.allowedDropOperations, getPreviousTarget, false);
Expand Down Expand Up @@ -599,9 +601,9 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:

// If the target does not accept the drop, find the previous valid target.
// If no next valid target, find the next valid target.
let {draggingCollectionRef, draggingKeys} = globalDndState;
let isInternalDrop = draggingCollectionRef?.current != null && draggingCollectionRef?.current === ref?.current;
let operation = localState.state.getDropOperation({target, types, allowedOperations: drag.allowedDropOperations, isInternalDrop, draggingKeys});
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation(ref);
let operation = localState.state.getDropOperation({target, types, allowedOperations: drag.allowedDropOperations, isInternal, draggingKeys});
if (operation === 'cancel') {
target = nextValidTarget(target, types, drag.allowedDropOperations, getPreviousTarget, false)
?? nextValidTarget(target, types, drag.allowedDropOperations, getNextTarget, false);
Expand Down
14 changes: 7 additions & 7 deletions packages/@react-aria/dnd/src/useDroppableItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import * as DragManager from './DragManager';
import {DroppableCollectionState} from '@react-stately/dnd';
import {DropTarget} from '@react-types/shared';
import {getTypes, globalDndState} from './utils';
import {getTypes, globalDndState, isInternalDropOperation} from './utils';
import {HTMLAttributes, RefObject, useEffect} from 'react';
import {useVirtualDrop} from './useVirtualDrop';

Expand All @@ -35,13 +35,13 @@ export function useDroppableItem(options: DroppableItemOptions, state: Droppable
element: ref.current,
target: options.target,
getDropOperation(types, allowedOperations) {
let {draggingCollectionRef, draggingKeys, dropCollectionRef} = globalDndState;
let isInternalDrop = draggingCollectionRef?.current != null && draggingCollectionRef?.current === dropCollectionRef?.current;
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation();
return state.getDropOperation({
target: options.target,
types,
allowedOperations,
isInternalDrop,
isInternal,
draggingKeys
});
}
Expand All @@ -50,13 +50,13 @@ export function useDroppableItem(options: DroppableItemOptions, state: Droppable
}, [ref, options.target, state]);

let dragSession = DragManager.useDragSession();
let {draggingCollectionRef, draggingKeys, dropCollectionRef} = globalDndState;
let isInternalDrop = draggingCollectionRef?.current != null && draggingCollectionRef?.current === dropCollectionRef?.current;
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation();
let isValidDropTarget = dragSession && state.getDropOperation({
target: options.target,
types: getTypes(dragSession.dragTarget.items),
allowedOperations: dragSession.dragTarget.allowedDropOperations,
isInternalDrop,
isInternal,
draggingKeys
}) !== 'cancel';

Expand Down
14 changes: 11 additions & 3 deletions packages/@react-aria/dnd/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {Key, RefObject} from 'react';
import {useId} from '@react-aria/utils';

const droppableCollectionIds = new WeakMap<DroppableCollectionState, string>();
export const DIRECTORY_DRAG_TYPE = Symbol();

export function useDroppableCollectionId(state: DroppableCollectionState) {
let id = useId();
Expand Down Expand Up @@ -159,12 +160,12 @@ export class DragTypes implements IDragTypes {
this.includesUnknownTypes = !hasFiles && dataTransfer.types.includes('Files');
}

has(type: string) {
if (this.includesUnknownTypes || (type === 'directory' && this.types.has(GENERIC_TYPE))) {
has(type: string | symbol) {
if (this.includesUnknownTypes || (type === DIRECTORY_DRAG_TYPE && this.types.has(GENERIC_TYPE))) {
return true;
}

return this.types.has(type);
return typeof type === 'string' && this.types.has(type);
}
}

Expand Down Expand Up @@ -336,6 +337,13 @@ export function setGlobalDnDState(state: DnDState) {
globalDndState = state;
}

// Util function to check if the current dragging collection ref is the same as the current targeted droppable collection ref.
// Allows a droppable ref arg in case the global drop collection ref hasn't been set
export function isInternalDropOperation(ref?: RefObject<HTMLElement>) {
LFDanLu marked this conversation as resolved.
Show resolved Hide resolved
let {draggingCollectionRef, dropCollectionRef} = globalDndState;
return draggingCollectionRef?.current != null && draggingCollectionRef.current === (ref?.current || dropCollectionRef?.current);
}

type DropEffect = 'none' | 'copy' | 'link' | 'move';
export let globalDropEffect: DropEffect;
export function setGlobalDropEffect(dropEffect: DropEffect) {
Expand Down
3 changes: 2 additions & 1 deletion packages/@react-aria/dnd/stories/DroppableGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ const DroppableGrid = React.forwardRef(function (props: any, ref) {
selectionManager: gridState.selectionManager,
getDropOperation: props.getDropOperation || defaultGetDropOperation,
onDropEnter: props.onDropEnter,
onDropExit: props.onDropExit
onDropExit: props.onDropExit,
onDrop: props.onDrop
});

let {collectionProps} = useDroppableCollection({
Expand Down
Loading