Skip to content

Commit c22d969

Browse files
authored
Update Spectrum DnD docs and rename Spectrum DnD hook (#3548)
* updating ListView drag and drop examples with updated api * updating dnd.mdx * fixing lint and adding example with getDropOperation * fixing DragHooks type in docs The DragHooks type looks really weird in the types popover without this change * fixing typos and stuff * collapsing similar code and highlighting changes in ListView dnd stories * adding images from aria/dnd.mdx * adding low level api example and fixing other sections * fixing merge * fix merge conflict * adding description for spectrum dnd hook * pulling out ListView dnd docs changes * trying to fix links for build * forgot two links * Rename useDnDHooks and ListView dndHooks prop (#3577) * updating useDnDHooks and dndHooks ListView prop name * fix merge
1 parent f258ead commit c22d969

File tree

9 files changed

+374
-180
lines changed

9 files changed

+374
-180
lines changed

packages/@react-spectrum/dnd/docs/dnd.mdx

Lines changed: 284 additions & 102 deletions
Large diffs are not rendered by default.

packages/@react-spectrum/dnd/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
/// <reference types="css-module-types" />
1414

15-
export type {DnDOptions, DnDHooks} from './useDnDHooks';
16-
export {useDnDHooks} from './useDnDHooks';
15+
export type {DragAndDropOptions, DragAndDropHooks} from './useDragAndDrop';
16+
export {useDragAndDrop} from './useDragAndDrop';
1717
export {DIRECTORY_DRAG_TYPE} from '@react-aria/dnd';

packages/@react-spectrum/dnd/src/useDnDHooks.ts renamed to packages/@react-spectrum/dnd/src/useDragAndDrop.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ import {
3737
useDraggableCollectionState,
3838
useDroppableCollectionState
3939
} from '@react-stately/dnd';
40-
4140
import {DroppableCollectionProps} from '@react-types/shared';
4241
import {Key, RefObject, useMemo} from 'react';
4342

43+
interface DraggableCollectionStateOpts extends Omit<DraggableCollectionStateOptions, 'getItems'> {}
44+
4445
interface DragHooks {
45-
useDraggableCollectionState?: (props: Omit<DraggableCollectionStateOptions, 'getItems'>) => DraggableCollectionState,
46+
useDraggableCollectionState?: (props: DraggableCollectionStateOpts) => DraggableCollectionState,
4647
useDraggableCollection?: (props: DraggableCollectionOptions, state: DraggableCollectionState, ref: RefObject<HTMLElement>) => void,
4748
useDraggableItem?: (props: DraggableItemProps, state: DraggableCollectionState) => DraggableItemResult,
4849
DragPreview?: typeof DragPreview
@@ -55,20 +56,24 @@ interface DropHooks {
5556
useDropIndicator?: (props: DropIndicatorProps, state: DroppableCollectionState, ref: RefObject<HTMLElement>) => DropIndicatorAria
5657
}
5758

58-
export interface DnDHooks {
59-
dndHooks: DragHooks & DropHooks & {isVirtualDragging?: () => boolean}
59+
export interface DragAndDropHooks {
60+
/** Drag and drop hooks for the collection element. */
61+
dragAndDropHooks: DragHooks & DropHooks & {isVirtualDragging?: () => boolean}
6062
}
6163

62-
export interface DnDOptions extends Omit<DraggableCollectionProps, 'preview' | 'getItems'>, DroppableCollectionProps {
64+
export interface DragAndDropOptions extends Omit<DraggableCollectionProps, 'preview' | 'getItems'>, DroppableCollectionProps {
6365
/**
6466
* A function that returns the items being dragged. If not specified, we assume that the collection is not draggable.
6567
* @default () => []
6668
*/
6769
getItems?: (keys: Set<Key>) => DragItem[]
6870
}
6971

70-
export function useDnDHooks(options: DnDOptions): DnDHooks {
71-
let dndHooks = useMemo(() => {
72+
/**
73+
* Provides the hooks required to enable drag and drop behavior for a drag and drop compatible React Spectrum component.
74+
*/
75+
export function useDragAndDrop(options: DragAndDropOptions): DragAndDropHooks {
76+
let dragAndDropHooks = useMemo(() => {
7277
let {
7378
onDrop,
7479
onInsert,
@@ -110,6 +115,6 @@ export function useDnDHooks(options: DnDOptions): DnDHooks {
110115
}, [options]);
111116

112117
return {
113-
dndHooks: dndHooks
118+
dragAndDropHooks: dragAndDropHooks
114119
};
115120
}

packages/@react-spectrum/list/src/InsertionIndicator.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ interface InsertionIndicatorProps {
1111
}
1212

1313
export default function InsertionIndicator(props: InsertionIndicatorProps) {
14-
let {dropState, dndHooks} = useContext(ListViewContext);
14+
let {dropState, dragAndDropHooks} = useContext(ListViewContext);
1515
const {target, isPresentationOnly} = props;
1616

1717
let ref = useRef();
18-
let {dropIndicatorProps} = dndHooks.useDropIndicator(props, dropState, ref);
18+
let {dropIndicatorProps} = dragAndDropHooks.useDropIndicator(props, dropState, ref);
1919
let {visuallyHiddenProps} = useVisuallyHidden();
2020

2121
let isDropTarget = dropState.isDropTarget(target);

packages/@react-spectrum/list/src/ListView.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import {AriaGridListProps, useGridList} from '@react-aria/gridlist';
1414
import {AsyncLoadable, DOMRef, LoadingState, SpectrumSelectionProps, StyleProps} from '@react-types/shared';
1515
import {classNames, useDOMRef, useStyleProps} from '@react-spectrum/utils';
16-
import type {DnDHooks} from '@react-spectrum/dnd';
16+
import type {DragAndDropHooks} from '@react-spectrum/dnd';
1717
import type {DraggableCollectionState, DroppableCollectionState} from '@react-stately/dnd';
1818
import type {DroppableCollectionResult} from '@react-aria/dnd';
1919
import {filterDOMProps, mergeProps, useLayoutEffect} from '@react-aria/utils';
@@ -56,17 +56,16 @@ export interface SpectrumListViewProps<T> extends AriaGridListProps<T>, StylePro
5656
*/
5757
onAction?: (key: Key) => void,
5858
/**
59-
* The drag and drop hooks returned by `useDnDHooks` used to enable drag and drop behavior for the ListView.
60-
* @private
59+
* The drag and drop hooks returned by `useDragAndDrop` used to enable drag and drop behavior for the ListView.
6160
*/
62-
dndHooks?: DnDHooks['dndHooks']
61+
dragAndDropHooks?: DragAndDropHooks['dragAndDropHooks']
6362
}
6463

6564
interface ListViewContextValue<T> {
6665
state: ListState<T>,
6766
dragState: DraggableCollectionState,
6867
dropState: DroppableCollectionState,
69-
dndHooks: DnDHooks['dndHooks'],
68+
dragAndDropHooks: DragAndDropHooks['dragAndDropHooks'],
7069
onAction:(key: Key) => void,
7170
isListDraggable: boolean,
7271
isListDroppable: boolean,
@@ -118,11 +117,11 @@ function ListView<T extends object>(props: SpectrumListViewProps<T>, ref: DOMRef
118117
isQuiet,
119118
overflowMode = 'truncate',
120119
onAction,
121-
dndHooks,
120+
dragAndDropHooks,
122121
...otherProps
123122
} = props;
124-
let isListDraggable = !!dndHooks?.useDraggableCollectionState;
125-
let isListDroppable = !!dndHooks?.useDroppableCollectionState;
123+
let isListDraggable = !!dragAndDropHooks?.useDraggableCollectionState;
124+
let isListDroppable = !!dragAndDropHooks?.useDroppableCollectionState;
126125
let dragHooksProvided = useRef(isListDraggable);
127126
let dropHooksProvided = useRef(isListDroppable);
128127
if (dragHooksProvided.current !== isListDraggable) {
@@ -144,12 +143,12 @@ function ListView<T extends object>(props: SpectrumListViewProps<T>, ref: DOMRef
144143
let dragState: DraggableCollectionState;
145144
let preview = useRef(null);
146145
if (isListDraggable) {
147-
dragState = dndHooks.useDraggableCollectionState({
146+
dragState = dragAndDropHooks.useDraggableCollectionState({
148147
collection,
149148
selectionManager,
150149
preview
151150
});
152-
dndHooks.useDraggableCollection({}, dragState, domRef);
151+
dragAndDropHooks.useDraggableCollection({}, dragState, domRef);
153152
}
154153
let layout = useListLayout(
155154
state,
@@ -160,16 +159,16 @@ function ListView<T extends object>(props: SpectrumListViewProps<T>, ref: DOMRef
160159
layout.allowDisabledKeyFocus = state.selectionManager.disabledBehavior === 'selection' || !!dragState?.draggingKeys.size;
161160

162161

163-
let DragPreview = dndHooks?.DragPreview;
162+
let DragPreview = dragAndDropHooks?.DragPreview;
164163
let dropState: DroppableCollectionState;
165164
let droppableCollection: DroppableCollectionResult;
166165
let isRootDropTarget: boolean;
167166
if (isListDroppable) {
168-
dropState = dndHooks.useDroppableCollectionState({
167+
dropState = dragAndDropHooks.useDroppableCollectionState({
169168
collection,
170169
selectionManager
171170
});
172-
droppableCollection = dndHooks.useDroppableCollection({
171+
droppableCollection = dragAndDropHooks.useDroppableCollection({
173172
keyboardDelegate: layout,
174173
dropTargetDelegate: layout
175174
}, dropState, domRef);
@@ -206,7 +205,7 @@ function ListView<T extends object>(props: SpectrumListViewProps<T>, ref: DOMRef
206205
let hasAnyChildren = useMemo(() => [...collection].some(item => item.hasChildNodes), [collection]);
207206

208207
return (
209-
<ListViewContext.Provider value={{state, dragState, dropState, dndHooks, onAction, isListDraggable, isListDroppable, layout, loadingState}}>
208+
<ListViewContext.Provider value={{state, dragState, dropState, dragAndDropHooks, onAction, isListDraggable, isListDroppable, layout, loadingState}}>
210209
<FocusScope>
211210
<FocusRing focusRingClass={classNames(listStyles, 'focus-ring')}>
212211
<Virtualizer

packages/@react-spectrum/list/src/ListViewItem.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function ListViewItem<T>(props: ListViewItemProps<T>) {
4949
isListDraggable,
5050
isListDroppable,
5151
layout,
52-
dndHooks,
52+
dragAndDropHooks,
5353
loadingState
5454
} = useContext(ListViewContext);
5555
let {direction} = useLocale();
@@ -82,7 +82,7 @@ export function ListViewItem<T>(props: ListViewItemProps<T>) {
8282
let draggableItem: DraggableItemResult;
8383
if (isListDraggable) {
8484
// eslint-disable-next-line react-hooks/rules-of-hooks
85-
draggableItem = dndHooks.useDraggableItem({key: item.key, hasDragButton: true}, dragState);
85+
draggableItem = dragAndDropHooks.useDraggableItem({key: item.key, hasDragButton: true}, dragState);
8686
if (isDisabled) {
8787
draggableItem = null;
8888
}
@@ -95,7 +95,7 @@ export function ListViewItem<T>(props: ListViewItemProps<T>) {
9595
let target = {type: 'item', key: item.key, dropPosition: 'on'} as DropTarget;
9696
isDropTarget = dropState.isDropTarget(target);
9797
// eslint-disable-next-line react-hooks/rules-of-hooks
98-
dropIndicator = dndHooks.useDropIndicator({target}, dropState, dropIndicatorRef);
98+
dropIndicator = dragAndDropHooks.useDropIndicator({target}, dropState, dropIndicatorRef);
9999
}
100100

101101
let dragButtonRef = React.useRef();
@@ -147,7 +147,7 @@ export function ListViewItem<T>(props: ListViewItemProps<T>) {
147147
focusProps,
148148
// Remove tab index from list row if performing a screenreader drag. This prevents TalkBack from focusing the row,
149149
// allowing for single swipe navigation between row drop indicator
150-
dndHooks?.isVirtualDragging() && {tabIndex: null}
150+
dragAndDropHooks?.isVirtualDragging() && {tabIndex: null}
151151
);
152152

153153
let isFirstRow = item.prevKey == null;

packages/@react-spectrum/list/src/RootDropIndicator.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import React, {useContext, useRef} from 'react';
33
import {useVisuallyHidden} from '@react-aria/visually-hidden';
44

55
export default function RootDropIndicator() {
6-
let {dropState, dndHooks} = useContext(ListViewContext);
6+
let {dropState, dragAndDropHooks} = useContext(ListViewContext);
77
let ref = useRef();
8-
let {dropIndicatorProps} = dndHooks.useDropIndicator({
8+
let {dropIndicatorProps} = dragAndDropHooks.useDropIndicator({
99
target: {type: 'root'}
1010
}, dropState, ref);
1111
let isDropTarget = dropState.isDropTarget({type: 'root'});

0 commit comments

Comments
 (0)