useDroppableCollection
| install | yarn add @react-aria/dnd |
|---|---|
| version | 3.0.0-alpha.11 |
| usage | import {useDroppableCollection, useDroppableItem, useDropIndicator} from '@react-aria/dnd' |
API#
useDroppableCollection(
props: DroppableCollectionOptions,
state: DroppableCollectionState,
ref: RefObject<HTMLElement>
): DroppableCollectionResult
useDroppableItem(
options: DroppableItemOptions,
state: DroppableCollectionState,
ref: RefObject<HTMLElement>
): DroppableItemResult
useDropIndicator(
props: DropIndicatorProps,
state: DroppableCollectionState,
ref: RefObject<HTMLElement>
): DropIndicatorAria
Introduction#
Collection components built with hooks such as useListBox, useTable, and useGridList can support drag and drop interactions. Users can drop data on the collection as a whole, on individual items, insert new items between existing ones, or reorder items.
React Aria supports traditional mouse and touch based drag and drop, but also implements keyboard and screen reader friendly interactions. Users can press Enter on a draggable element to enter drag and drop mode. Then, they can press Tab to navigate between drop targets. A droppable collection is treated as a single drop target, so that users can easily tab past it to get to the next drop target. Within a droppable collection, keys such as ArrowDown and ArrowUp can be used to select a drop position, such as on an item, or between items. These are represented using DropTarget objects.
The keyboard interactions used within a collection may differ depending on the type or layout. For example, in a grid the ArrowLeft and ArrowRight may also be used, whereas they may not within a list. In general, the keyboard interactions used during drag and drop match those used when navigating the collection normally.
Implementation#
The useDroppableCollection hook implements drop interactions within any collection component. The props it returns should be combined with those from the collection component you're using, such as useListBox. The useDroppableItem hook should be added to each individual item within the collection, combining props from the relevant hook (e.g. useOption).
To support dropping between items, the useDropIndicator hook can be used to add additional elements between each item, for example, rendering a line when a user drags between two items. These elements must be implemented according to the relevant ARIA pattern. For example, within a listbox, drop indicators must be implemented using role="option", and within a grid, they must use role="row" and role="gridcell" to ensure the accessibility tree is valid.
Interactions like keyboard navigation, and drop target positioning may differ depending on the component and layout of items. These are implemented using the KeyboardDelegate and DropTargetDelegate interfaces, provided to useDroppableCollection. In most cases, you can use a default implementation provided by React Aria such as ListKeyboardDelegate and ListDropTargetDelegate, but you may also provide your own if you need to customize the behavior.
Dropping on items#
This example renders a ListBox using the useListBox hook, and adds support for dropping data onto items. The highlighted code sections below show the main additions for drag and drop compared with a normal listbox.
import {useListBox, useOption} from '@react-aria/listbox';
import {useListState} from '@react-stately/list';
import {Item} from '@react-stately/collections';
import {useFocusRing} from '@react-aria/focus';
import {mergeProps} from '@react-aria/utils';
import {useDroppableCollectionState} from '@react-stately/dnd';
import {
ListDropTargetDelegate,
useDroppableCollection,
useDroppableItem
} from '@react-aria/dnd';
import {ListKeyboardDelegate} from '@react-aria/selection';
function ListBox(props) {
// Setup listbox as normal. See the useListBox docs for more details.
let state = useListState(props);
let ref = React.useRef();
let { listBoxProps } = useListBox(props, state, ref);
// Setup react-stately and react-aria hooks for drag and drop.
let dropState = useDroppableCollectionState({
// Collection and selection manager come from list state.
collection: state.collection,
selectionManager: state.selectionManager,
getDropOperation: props.getDropOperation
});
let { collectionProps } = useDroppableCollection(
{
// Provide drop targets for keyboard and pointer-based drag and drop.
keyboardDelegate: new ListKeyboardDelegate(
state.collection,
state.disabledKeys,
ref
),
dropTargetDelegate: new ListDropTargetDelegate(state.collection, ref),
onDrop: props.onDrop
},
dropState,
ref
);
// Merge listbox props and dnd props, and render the items as normal.
return (
<ul {...mergeProps(listBoxProps, collectionProps)} ref={ref}> {[...state.collection].map((item) => (
<Option
key={item.key}
item={item}
state={state}
dropState={dropState}
/>
))}
</ul>
);
}
function Option({ item, state, dropState }) {
// Setup listbox option as normal. See useListBox docs for details.
let ref = React.useRef();
let { optionProps, isSelected, isDisabled } = useOption(
{ key: item.key },
state,
ref
);
let { isFocusVisible, focusProps } = useFocusRing();
// Register the item as a drop target.
let { dropProps, isDropTarget } = useDroppableItem(
{
target: { type: 'item', key: item.key, dropPosition: 'on' }
},
dropState,
ref
);
// Merge option props and dnd props, and render the item.
return (
<li
{...mergeProps(optionProps, dropProps, focusProps)} ref={ref}
// Apply a class when the item is the active drop target.
className={`option `} >
{item.rendered}
</li>
);
}
<Draggable>Octopus</Draggable>
<ListBox
aria-label="Categories"
selectionMode="single"
getDropOperation={(target) =>
target.dropPosition === 'on' ? 'copy' : 'cancel'}
>
<Item>Animals</Item>
<Item>People</Item>
<Item>Plants</Item>
</ListBox>
import {useListBox, useOption} from '@react-aria/listbox';
import {useListState} from '@react-stately/list';
import {Item} from '@react-stately/collections';
import {useFocusRing} from '@react-aria/focus';
import {mergeProps} from '@react-aria/utils';
import {useDroppableCollectionState} from '@react-stately/dnd';
import {
ListDropTargetDelegate,
useDroppableCollection,
useDroppableItem
} from '@react-aria/dnd';
import {ListKeyboardDelegate} from '@react-aria/selection';
function ListBox(props) {
// Setup listbox as normal. See the useListBox docs for more details.
let state = useListState(props);
let ref = React.useRef();
let { listBoxProps } = useListBox(props, state, ref);
// Setup react-stately and react-aria hooks for drag and drop.
let dropState = useDroppableCollectionState({
// Collection and selection manager come from list state.
collection: state.collection,
selectionManager: state.selectionManager,
getDropOperation: props.getDropOperation
});
let { collectionProps } = useDroppableCollection(
{
// Provide drop targets for keyboard and pointer-based drag and drop.
keyboardDelegate: new ListKeyboardDelegate(
state.collection,
state.disabledKeys,
ref
),
dropTargetDelegate: new ListDropTargetDelegate(
state.collection,
ref
),
onDrop: props.onDrop
},
dropState,
ref
);
// Merge listbox props and dnd props, and render the items as normal.
return (
<ul
{...mergeProps(listBoxProps, collectionProps)}
ref={ref}
> {[...state.collection].map((item) => (
<Option
key={item.key}
item={item}
state={state}
dropState={dropState}
/>
))}
</ul>
);
}
function Option({ item, state, dropState }) {
// Setup listbox option as normal. See useListBox docs for details.
let ref = React.useRef();
let { optionProps, isSelected, isDisabled } = useOption(
{ key: item.key },
state,
ref
);
let { isFocusVisible, focusProps } = useFocusRing();
// Register the item as a drop target.
let { dropProps, isDropTarget } = useDroppableItem(
{
target: {
type: 'item',
key: item.key,
dropPosition: 'on'
}
},
dropState,
ref
);
// Merge option props and dnd props, and render the item.
return (
<li
{...mergeProps(optionProps, dropProps, focusProps)} ref={ref}
// Apply a class when the item is the active drop target.
className={`option `} >
{item.rendered}
</li>
);
}
<Draggable>Octopus</Draggable>
<ListBox
aria-label="Categories"
selectionMode="single"
getDropOperation={(target) =>
target.dropPosition === 'on' ? 'copy' : 'cancel'}
>
<Item>Animals</Item>
<Item>People</Item>
<Item>Plants</Item>
</ListBox>
import {
useListBox,
useOption
} from '@react-aria/listbox';
import {useListState} from '@react-stately/list';
import {Item} from '@react-stately/collections';
import {useFocusRing} from '@react-aria/focus';
import {mergeProps} from '@react-aria/utils';
import {useDroppableCollectionState} from '@react-stately/dnd';
import {
ListDropTargetDelegate,
useDroppableCollection,
useDroppableItem
} from '@react-aria/dnd';
import {ListKeyboardDelegate} from '@react-aria/selection';
function ListBox(props) {
// Setup listbox as normal. See the useListBox docs for more details.
let state =
useListState(props);
let ref = React
.useRef();
let { listBoxProps } =
useListBox(
props,
state,
ref
);
// Setup react-stately and react-aria hooks for drag and drop.
let dropState =
useDroppableCollectionState(
{
// Collection and selection manager come from list state.
collection:
state
.collection,
selectionManager:
state
.selectionManager,
getDropOperation:
props
.getDropOperation
}
);
let {
collectionProps
} =
useDroppableCollection(
{
// Provide drop targets for keyboard and pointer-based drag and drop.
keyboardDelegate:
new ListKeyboardDelegate(
state
.collection,
state
.disabledKeys,
ref
),
dropTargetDelegate:
new ListDropTargetDelegate(
state
.collection,
ref
),
onDrop:
props.onDrop
},
dropState,
ref
);
// Merge listbox props and dnd props, and render the items as normal.
return (
<ul
{...mergeProps(
listBoxProps,
collectionProps
)}
ref={ref}
> {[
...state
.collection
].map((item) => (
<Option
key={item.key}
item={item}
state={state}
dropState={dropState}
/>
))}
</ul>
);
}
function Option(
{
item,
state,
dropState
}
) {
// Setup listbox option as normal. See useListBox docs for details.
let ref = React
.useRef();
let {
optionProps,
isSelected,
isDisabled
} = useOption(
{ key: item.key },
state,
ref
);
let {
isFocusVisible,
focusProps
} = useFocusRing();
// Register the item as a drop target.
let {
dropProps,
isDropTarget
} = useDroppableItem(
{
target: {
type: 'item',
key: item.key,
dropPosition:
'on'
}
},
dropState,
ref
);
// Merge option props and dnd props, and render the item.
return (
<li
{...mergeProps(
optionProps,
dropProps,
focusProps
)} ref={ref}
// Apply a class when the item is the active drop target.
className={`option `} >
{item.rendered}
</li>
);
}
<Draggable>
Octopus
</Draggable>
<ListBox
aria-label="Categories"
selectionMode="single"
getDropOperation={(target) =>
target
.dropPosition ===
'on'
? 'copy'
: 'cancel'}
>
<Item>Animals</Item>
<Item>People</Item>
<Item>Plants</Item>
</ListBox>
Show CSS
[role=listbox] {
padding: 0;
margin: 5px 0;
list-style: none;
border: 1px solid gray;
max-width: 250px;
outline: none;
min-height: 50px;
}
[role=listbox]:empty {
border-style: dashed;
}
.option {
padding: 2px 5px;
outline: none;
}
.option[aria-selected=true] {
background: blueviolet;
color: white;
}
.option.focus-visible {
outline: 2px solid orange;
}
.option.drop-target {
outline: 2px solid var(--blue);
}[role=listbox] {
padding: 0;
margin: 5px 0;
list-style: none;
border: 1px solid gray;
max-width: 250px;
outline: none;
min-height: 50px;
}
[role=listbox]:empty {
border-style: dashed;
}
.option {
padding: 2px 5px;
outline: none;
}
.option[aria-selected=true] {
background: blueviolet;
color: white;
}
.option.focus-visible {
outline: 2px solid orange;
}
.option.drop-target {
outline: 2px solid var(--blue);
}[role=listbox] {
padding: 0;
margin: 5px 0;
list-style: none;
border: 1px solid gray;
max-width: 250px;
outline: none;
min-height: 50px;
}
[role=listbox]:empty {
border-style: dashed;
}
.option {
padding: 2px 5px;
outline: none;
}
.option[aria-selected=true] {
background: blueviolet;
color: white;
}
.option.focus-visible {
outline: 2px solid orange;
}
.option.drop-target {
outline: 2px solid var(--blue);
}Draggable#
The Draggable component used above is defined below. See useDrag for more details and documentation.
Show code
import {useDrag} from '@react-aria/dnd';
import {mergeProps} from '@react-aria/utils';
import {useButton} from '@react-aria/button';
function Draggable({ children }) {
let { dragProps, dragButtonProps, isDragging } = useDrag({
getItems() {
return [{
'text/plain': children,
'my-app-custom-type': JSON.stringify({ message: children })
}];
}
});
let ref = React.useRef();
let { buttonProps } = useButton(
{ ...dragButtonProps, elementType: 'div' },
ref
);
return (
<div
{...mergeProps(dragProps, buttonProps)}
ref={ref}
className={`draggable `}
>
<span aria-hidden="true">≡</span> {children}
</div>
);
}
import {useDrag} from '@react-aria/dnd';
import {mergeProps} from '@react-aria/utils';
import {useButton} from '@react-aria/button';
function Draggable({ children }) {
let { dragProps, dragButtonProps, isDragging } = useDrag({
getItems() {
return [{
'text/plain': children,
'my-app-custom-type': JSON.stringify({
message: children
})
}];
}
});
let ref = React.useRef();
let { buttonProps } = useButton({
...dragButtonProps,
elementType: 'div'
}, ref);
return (
<div
{...mergeProps(dragProps, buttonProps)}
ref={ref}
className={`draggable `}
>
<span aria-hidden="true">≡</span> {children}
</div>
);
}
import {useDrag} from '@react-aria/dnd';
import {mergeProps} from '@react-aria/utils';
import {useButton} from '@react-aria/button';
function Draggable(
{ children }
) {
let {
dragProps,
dragButtonProps,
isDragging
} = useDrag({
getItems() {
return [{
'text/plain':
children,
'my-app-custom-type':
JSON.stringify(
{
message:
children
}
)
}];
}
});
let ref = React
.useRef();
let { buttonProps } =
useButton({
...dragButtonProps,
elementType: 'div'
}, ref);
return (
<div
{...mergeProps(
dragProps,
buttonProps
)}
ref={ref}
className={`draggable `}
>
<span aria-hidden="true">
≡
</span>{' '}
{children}
</div>
);
}
Show CSS
.draggable {
display: inline-block;
vertical-align: top;
border: 1px solid gray;
padding: 5px 10px;
margin-right: 20px;
}
.draggable.dragging {
opacity: 0.5;
}.draggable {
display: inline-block;
vertical-align: top;
border: 1px solid gray;
padding: 5px 10px;
margin-right: 20px;
}
.draggable.dragging {
opacity: 0.5;
}.draggable {
display: inline-block;
vertical-align: top;
border: 1px solid gray;
padding: 5px 10px;
margin-right: 20px;
}
.draggable.dragging {
opacity: 0.5;
}Dropping between items#
To add support for dropping between items, first implement the DropIndicator component using the useDropIndicator hook. This will render a line between items indicating the insertion position. Within a listbox, these must have role=option, and since our listbox is rendered as a <ul>, they must also be <li> elements to ensure the accessibility and HTML semantics are correct.
useDropIndicator returns isHidden when the drop indicator is not needed (e.g. if there is no drag session in progress), in which case we can return null to prevent any extra elements from being rendered to the DOM. When isDropTarget is true, the drop indicator is active and should be visible. Note that for accessibility, an element must always be rendered while a drag session is in progress, even when the drop indicator is not currently active, so that screen readers can navigate to it.
import {useDropIndicator} from '@react-aria/dnd';
function DropIndicator(props) {
let ref = React.useRef();
let { dropIndicatorProps, isHidden, isDropTarget } = useDropIndicator(
props,
props.dropState,
ref
);
if (isHidden) {
return null;
}
let className = props.target.type === 'item'
? `drop-indicator `
: '';
return (
<li
{...dropIndicatorProps}
role="option"
ref={ref}
className={className}
/>
);
}
import {useDropIndicator} from '@react-aria/dnd';
function DropIndicator(props) {
let ref = React.useRef();
let { dropIndicatorProps, isHidden, isDropTarget } =
useDropIndicator(props, props.dropState, ref);
if (isHidden) {
return null;
}
let className = props.target.type === 'item'
? `drop-indicator `
: '';
return (
<li
{...dropIndicatorProps}
role="option"
ref={ref}
className={className}
/>
);
}
import {useDropIndicator} from '@react-aria/dnd';
function DropIndicator(
props
) {
let ref = React
.useRef();
let {
dropIndicatorProps,
isHidden,
isDropTarget
} = useDropIndicator(
props,
props.dropState,
ref
);
if (isHidden) {
return null;
}
let className =
props.target.type ===
'item'
? `drop-indicator `
: '';
return (
<li
{...dropIndicatorProps}
role="option"
ref={ref}
className={className}
/>
);
}
Now that the DropIndicator component is implemented, we can render an instance between each item in the list. This uses the before drop position by default, except for after the last item in the list.
function Option({ item, state, dropState }) {
// ...
return (
<>
<DropIndicator
target={{ type: 'item', key: item.key, dropPosition: 'before' }}
dropState={dropState}
/> <li
{...mergeProps(optionProps, dropProps, focusProps)}
ref={ref}
className={`option `}
>
{item.rendered}
</li>
{state.collection.getKeyAfter(item.key) == null &&
(
<DropIndicator
target={{ type: 'item', key: item.key, dropPosition: 'after' }}
dropState={dropState}
/>
)} </>
);
}
function Option({ item, state, dropState }) {
// ...
return (
<>
<DropIndicator
target={{
type: 'item',
key: item.key,
dropPosition: 'before'
}}
dropState={dropState}
/> <li
{...mergeProps(optionProps, dropProps, focusProps)}
ref={ref}
className={`option `}
>
{item.rendered}
</li>
{state.collection.getKeyAfter(item.key) == null &&
(
<DropIndicator
target={{
type: 'item',
key: item.key,
dropPosition: 'after'
}}
dropState={dropState}
/>
)} </>
);
}
function Option(
{
item,
state,
dropState
}
) {
// ...
return (
<>
<DropIndicator
target={{
type: 'item',
key: item.key,
dropPosition:
'before'
}}
dropState={dropState}
/> <li
{...mergeProps(
optionProps,
dropProps,
focusProps
)}
ref={ref}
className={`option `}
>
{item.rendered}
</li>
{state.collection
.getKeyAfter(
item.key
) == null &&
(
<DropIndicator
target={{
type:
'item',
key:
item.key,
dropPosition:
'after'
}}
dropState={dropState}
/>
)} </>
);
}
Now, we can render an example ListBox, which inserts a new item on drop.
import {useListData} from '@react-stately/data';
function Example() {
let list = useListData({
initialItems: [
{ id: 1, name: 'Cat' },
{ id: 2, name: 'Dog' },
{ id: 3, name: 'Kangaroo' }
]
});
let onDrop = async (e) => {
let name = await e.items[0].getText('text/plain');
let item = { id: list.items.length + 1, name };
if (e.target.dropPosition === 'before') {
list.insertBefore(e.target.key, item);
} else if (e.target.dropPosition === 'after') {
list.insertAfter(e.target.key, item);
}
};
return (
<>
<Draggable>Octopus</Draggable>
<ListBox
aria-label="Favorite animals"
selectionMode="single"
items={list.items}
getDropOperation={(target) =>
target.type === 'item' && target.dropPosition !== 'on'
? 'copy'
: 'cancel'}
onDrop={onDrop}
>
{(item) => <Item>{item.name}</Item>}
</ListBox>
</>
);
}
import {useListData} from '@react-stately/data';
function Example() {
let list = useListData({
initialItems: [
{ id: 1, name: 'Cat' },
{ id: 2, name: 'Dog' },
{ id: 3, name: 'Kangaroo' }
]
});
let onDrop = async (e) => {
let name = await e.items[0].getText('text/plain');
let item = { id: list.items.length + 1, name };
if (e.target.dropPosition === 'before') {
list.insertBefore(e.target.key, item);
} else if (e.target.dropPosition === 'after') {
list.insertAfter(e.target.key, item);
}
};
return (
<>
<Draggable>Octopus</Draggable>
<ListBox
aria-label="Favorite animals"
selectionMode="single"
items={list.items}
getDropOperation={(target) =>
target.type === 'item' &&
target.dropPosition !== 'on'
? 'copy'
: 'cancel'}
onDrop={onDrop}
>
{(item) => <Item>{item.name}</Item>}
</ListBox>
</>
);
}
import {useListData} from '@react-stately/data';
function Example() {
let list = useListData(
{
initialItems: [
{
id: 1,
name: 'Cat'
},
{
id: 2,
name: 'Dog'
},
{
id: 3,
name:
'Kangaroo'
}
]
}
);
let onDrop =
async (e) => {
let name = await e
.items[0]
.getText(
'text/plain'
);
let item = {
id:
list.items
.length + 1,
name
};
if (
e.target
.dropPosition ===
'before'
) {
list
.insertBefore(
e.target.key,
item
);
} else if (
e.target
.dropPosition ===
'after'
) {
list.insertAfter(
e.target.key,
item
);
}
};
return (
<>
<Draggable>
Octopus
</Draggable>
<ListBox
aria-label="Favorite animals"
selectionMode="single"
items={list
.items}
getDropOperation={(target) =>
target.type ===
'item' &&
target
.dropPosition !==
'on'
? 'copy'
: 'cancel'}
onDrop={onDrop}
>
{(item) => (
<Item>
{item.name}
</Item>
)}
</ListBox>
</>
);
}
Show CSS
.drop-indicator {
width: 100%;
margin-left: 0;
height: 2px;
margin-bottom: -2px;
outline: none;
background: transparent;
}
.drop-indicator.drop-target {
background: var(--blue);
}.drop-indicator {
width: 100%;
margin-left: 0;
height: 2px;
margin-bottom: -2px;
outline: none;
background: transparent;
}
.drop-indicator.drop-target {
background: var(--blue);
}.drop-indicator {
width: 100%;
margin-left: 0;
height: 2px;
margin-bottom: -2px;
outline: none;
background: transparent;
}
.drop-indicator.drop-target {
background: var(--blue);
}Dropping on the collection#
To add support for dropping on the collection as a whole, an additional DropIndicator can be rendered at the start of the list, representing the root target. The isDropTarget method of the state object can be used to apply a class to the list when the root target is active.
function ListBox(props) {
// ...
let isDropTarget = dropState.isDropTarget({ type: 'root' });
return (
<ul
{...mergeProps(listBoxProps, collectionProps)}
ref={ref}
className={isDropTarget ? 'drop-target' : ''} >
<DropIndicator target={{ type: 'root' }} dropState={dropState} /> {[...state.collection].map((item) => (
<Option
key={item.key}
item={item}
state={state}
dropState={dropState}
/>
))}
</ul>
);
}
<Draggable>budget.xls</Draggable>
<ListBox
aria-label="Files"
selectionMode="single"
getDropOperation={(target) =>
target.type === 'root' ||
(target.key === 'documents' && target.dropPosition === 'on')
? 'copy'
: 'cancel'}
>
<Item key="documents">Documents</Item>
<Item>proposal.doc</Item>
<Item>presentation.ppt</Item>
</ListBox>
function ListBox(props) {
// ...
let isDropTarget = dropState.isDropTarget({
type: 'root'
});
return (
<ul
{...mergeProps(listBoxProps, collectionProps)}
ref={ref}
className={isDropTarget ? 'drop-target' : ''} >
<DropIndicator
target={{ type: 'root' }}
dropState={dropState}
/> {[...state.collection].map((item) => (
<Option
key={item.key}
item={item}
state={state}
dropState={dropState}
/>
))}
</ul>
);
}
<Draggable>budget.xls</Draggable>
<ListBox
aria-label="Files"
selectionMode="single"
getDropOperation={(target) =>
target.type === 'root' ||
(target.key === 'documents' &&
target.dropPosition === 'on')
? 'copy'
: 'cancel'}
>
<Item key="documents">Documents</Item>
<Item>proposal.doc</Item>
<Item>presentation.ppt</Item>
</ListBox>
function ListBox(props) {
// ...
let isDropTarget =
dropState
.isDropTarget({
type: 'root'
});
return (
<ul
{...mergeProps(
listBoxProps,
collectionProps
)}
ref={ref}
className={isDropTarget
? 'drop-target'
: ''} >
<DropIndicator
target={{
type: 'root'
}}
dropState={dropState}
/> {[
...state
.collection
].map((item) => (
<Option
key={item.key}
item={item}
state={state}
dropState={dropState}
/>
))}
</ul>
);
}
<Draggable>
budget.xls
</Draggable>
<ListBox
aria-label="Files"
selectionMode="single"
getDropOperation={(target) =>
target.type ===
'root' ||
(target.key ===
'documents' &&
target
.dropPosition ===
'on')
? 'copy'
: 'cancel'}
>
<Item key="documents">
Documents
</Item>
<Item>
proposal.doc
</Item>
<Item>
presentation.ppt
</Item>
</ListBox>
Show CSS
[role=listbox].drop-target {
border-color: var(--blue);
outline: 1px solid var(--blue);
}[role=listbox].drop-target {
border-color: var(--blue);
outline: 1px solid var(--blue);
}[role=listbox].drop-target {
border-color: var(--blue);
outline: 1px solid var(--blue);
}Reordering#
Drag and drop can be combined in the same collection component to allow reordering items. This example builds on the dropping between items example above to add support for dragging items as well. This is done using the useDraggableCollectionState and useDraggableItem hooks. See the docs for more details on these hooks.
import {useDraggableCollectionState} from '@react-stately/dnd';
import {useDraggableItem} from '@react-aria/dnd';
function ReorderableListBox(props) {
// See useListBox docs for more details.
let state = useListState(props);
let ref = React.useRef();
let { listBoxProps } = useListBox(
{
...props,
shouldSelectOnPressUp: true },
state,
ref
);
let dropState = useDroppableCollectionState({
collection: state.collection,
selectionManager: state.selectionManager,
getDropOperation: props.getDropOperation
});
let { collectionProps } = useDroppableCollection(
{
keyboardDelegate: new ListKeyboardDelegate(
state.collection,
state.disabledKeys,
ref
),
dropTargetDelegate: new ListDropTargetDelegate(state.collection, ref),
onDrop: props.onDrop
},
dropState,
ref
);
// Setup drag state for the collection.
let dragState = useDraggableCollectionState({
...props,
// Collection and selection manager come from list state.
collection: state.collection,
selectionManager: state.selectionManager,
// Provide data for each dragged item. This function could
// also be provided by the user of the component.
getItems: props.getItems || ((keys) => {
return [...keys].map((key) => {
let item = state.collection.getItem(key);
return {
'text/plain': item.textValue
};
});
})
});
return (
<ul
{...mergeProps(listBoxProps, collectionProps)}
ref={ref}
>
{[...state.collection].map((item) => (
<ReorderableOption
key={item.key}
item={item}
state={state}
dragState={dragState}
dropState={dropState}
/>
))}
</ul>
);
}
function ReorderableOption({ item, state, dragState, dropState }) {
// ...
// Register the item as a drag source.
let { dragProps } = useDraggableItem({
key: item.key
}, dragState);
return (
<>
<DropIndicator
target={{ type: 'item', key: item.key, dropPosition: 'before' }}
dropState={dropState}
/>
<li
...mergeProps(optionProps, dragProps, dropProps, focusProps)} ref={ref}
className={`option `}
>
{item.rendered}
</li>
{state.collection.getKeyAfter(item.key) == null &&
(
<DropIndicator
target={{ type: 'item', key: item.key, dropPosition: 'after' }}
dropState={dropState}
/>
)}
</>
);
}
import {useDraggableCollectionState} from '@react-stately/dnd';
import {useDraggableItem} from '@react-aria/dnd';
function ReorderableListBox(props) {
// See useListBox docs for more details.
let state = useListState(props);
let ref = React.useRef();
let { listBoxProps } = useListBox(
{
...props,
shouldSelectOnPressUp: true },
state,
ref
);
let dropState = useDroppableCollectionState({
collection: state.collection,
selectionManager: state.selectionManager,
getDropOperation: props.getDropOperation
});
let { collectionProps } = useDroppableCollection(
{
keyboardDelegate: new ListKeyboardDelegate(
state.collection,
state.disabledKeys,
ref
),
dropTargetDelegate: new ListDropTargetDelegate(
state.collection,
ref
),
onDrop: props.onDrop
},
dropState,
ref
);
// Setup drag state for the collection.
let dragState = useDraggableCollectionState({
...props,
// Collection and selection manager come from list state.
collection: state.collection,
selectionManager: state.selectionManager,
// Provide data for each dragged item. This function could
// also be provided by the user of the component.
getItems: props.getItems || ((keys) => {
return [...keys].map((key) => {
let item = state.collection.getItem(key);
return {
'text/plain': item.textValue
};
});
})
});
return (
<ul
{...mergeProps(listBoxProps, collectionProps)}
ref={ref}
>
{[...state.collection].map((item) => (
<ReorderableOption
key={item.key}
item={item}
state={state}
dragState={dragState}
dropState={dropState}
/>
))}
</ul>
);
}
function ReorderableOption(
{ item, state, dragState, dropState }
) {
// ...
// Register the item as a drag source.
let { dragProps } = useDraggableItem({
key: item.key
}, dragState);
return (
<>
<DropIndicator
target={{
type: 'item',
key: item.key,
dropPosition: 'before'
}}
dropState={dropState}
/>
<li
...mergeProps(
optionProps,
dragProps,
dropProps,
focusProps
)} ref={ref}
className={`option `}
>
{item.rendered}
</li>
{state.collection.getKeyAfter(item.key) == null &&
(
<DropIndicator
target={{
type: 'item',
key: item.key,
dropPosition: 'after'
}}
dropState={dropState}
/>
)}
</>
);
}
import {useDraggableCollectionState} from '@react-stately/dnd';
import {useDraggableItem} from '@react-aria/dnd';
function ReorderableListBox(
props
) {
// See useListBox docs for more details.
let state =
useListState(props);
let ref = React
.useRef();
let { listBoxProps } =
useListBox(
{
...props,
shouldSelectOnPressUp:
true },
state,
ref
);
let dropState =
useDroppableCollectionState(
{
collection:
state
.collection,
selectionManager:
state
.selectionManager,
getDropOperation:
props
.getDropOperation
}
);
let {
collectionProps
} =
useDroppableCollection(
{
keyboardDelegate:
new ListKeyboardDelegate(
state
.collection,
state
.disabledKeys,
ref
),
dropTargetDelegate:
new ListDropTargetDelegate(
state
.collection,
ref
),
onDrop:
props.onDrop
},
dropState,
ref
);
// Setup drag state for the collection.
let dragState =
useDraggableCollectionState(
{
...props,
// Collection and selection manager come from list state.
collection:
state
.collection,
selectionManager:
state
.selectionManager,
// Provide data for each dragged item. This function could
// also be provided by the user of the component.
getItems:
props
.getItems ||
((keys) => {
return [
...keys
].map(
(key) => {
let item =
state
.collection
.getItem(
key
);
return {
'text/plain':
item
.textValue
};
}
);
})
}
);
return (
<ul
{...mergeProps(
listBoxProps,
collectionProps
)}
ref={ref}
>
{[
...state
.collection
].map((item) => (
<ReorderableOption
key={item.key}
item={item}
state={state}
dragState={dragState}
dropState={dropState}
/>
))}
</ul>
);
}
function ReorderableOption(
{
item,
state,
dragState,
dropState
}
) {
// ...
// Register the item as a drag source.
let { dragProps } =
useDraggableItem({
key: item.key
}, dragState);
return (
<>
<DropIndicator
target={{
type: 'item',
key: item.key,
dropPosition:
'before'
}}
dropState={dropState}
/>
<li
...mergeProps(
optionProps,
dragProps,
dropProps,
focusProps
)} ref={ref}
className={`option `}
>
{item.rendered}
</li>
{state.collection
.getKeyAfter(
item.key
) == null &&
(
<DropIndicator
target={{
type:
'item',
key:
item.key,
dropPosition:
'after'
}}
dropState={dropState}
/>
)}
</>
);
}
Now, we can render an example ListBox, which allows the user to reorder items.
import {useListData} from '@react-stately/data';
function Example() {
let list = useListData({
initialItems: [
{ id: 1, name: 'Cat' },
{ id: 2, name: 'Dog' },
{ id: 3, name: 'Kangaroo' },
{ id: 4, name: 'Panda' },
{ id: 5, name: 'Snake' }
]
});
let draggedKeys = React.useRef(null);
let onDragStart = (e) => {
draggedKeys.current = [...e.keys];
};
let onDragEnd = (e) => {
draggedKeys.current = null;
};
let onDrop = async (e) => {
if (e.target.dropPosition === 'before') {
list.moveBefore(e.target.key, draggedKeys.current);
} else if (e.target.dropPosition === 'after') {
list.moveAfter(e.target.key, draggedKeys.current);
}
};
return (
<ReorderableListBox
aria-label="Favorite animals"
selectionMode="multiple"
selectionBehavior="replace"
items={list.items}
getDropOperation={(target) =>
draggedKeys.current && target.type === 'item' &&
target.dropPosition !== 'on'
? 'move'
: 'cancel'}
onDragStart={onDragStart}
onDragEnd={onDragEnd}
onDrop={onDrop}
>
{(item) => <Item>{item.name}</Item>}
</ReorderableListBox>
);
}
import {useListData} from '@react-stately/data';
function Example() {
let list = useListData({
initialItems: [
{ id: 1, name: 'Cat' },
{ id: 2, name: 'Dog' },
{ id: 3, name: 'Kangaroo' },
{ id: 4, name: 'Panda' },
{ id: 5, name: 'Snake' }
]
});
let draggedKeys = React.useRef(null);
let onDragStart = (e) => {
draggedKeys.current = [...e.keys];
};
let onDragEnd = (e) => {
draggedKeys.current = null;
};
let onDrop = async (e) => {
if (e.target.dropPosition === 'before') {
list.moveBefore(e.target.key, draggedKeys.current);
} else if (e.target.dropPosition === 'after') {
list.moveAfter(e.target.key, draggedKeys.current);
}
};
return (
<ReorderableListBox
aria-label="Favorite animals"
selectionMode="multiple"
selectionBehavior="replace"
items={list.items}
getDropOperation={(target) =>
draggedKeys.current && target.type === 'item' &&
target.dropPosition !== 'on'
? 'move'
: 'cancel'}
onDragStart={onDragStart}
onDragEnd={onDragEnd}
onDrop={onDrop}
>
{(item) => <Item>{item.name}</Item>}
</ReorderableListBox>
);
}
import {useListData} from '@react-stately/data';
function Example() {
let list = useListData(
{
initialItems: [
{
id: 1,
name: 'Cat'
},
{
id: 2,
name: 'Dog'
},
{
id: 3,
name:
'Kangaroo'
},
{
id: 4,
name: 'Panda'
},
{
id: 5,
name: 'Snake'
}
]
}
);
let draggedKeys = React
.useRef(null);
let onDragStart =
(e) => {
draggedKeys
.current = [
...e.keys
];
};
let onDragEnd =
(e) => {
draggedKeys
.current = null;
};
let onDrop =
async (e) => {
if (
e.target
.dropPosition ===
'before'
) {
list.moveBefore(
e.target.key,
draggedKeys
.current
);
} else if (
e.target
.dropPosition ===
'after'
) {
list.moveAfter(
e.target.key,
draggedKeys
.current
);
}
};
return (
<ReorderableListBox
aria-label="Favorite animals"
selectionMode="multiple"
selectionBehavior="replace"
items={list.items}
getDropOperation={(target) =>
draggedKeys
.current &&
target.type ===
'item' &&
target
.dropPosition !==
'on'
? 'move'
: 'cancel'}
onDragStart={onDragStart}
onDragEnd={onDragEnd}
onDrop={onDrop}
>
{(item) => (
<Item>
{item.name}
</Item>
)}
</ReorderableListBox>
);
}
Drop data#
useDroppableCollection allows users to drop one or more drag items, each of which contains data to be transferred from the drag source to drop target. There are three kinds of drag items:
text– represents data inline as a string in one or more formatsfile– references a file on the user's devicedirectory– references the contents of a directory
Text#
A TextItem represents textual data in one or more different formats. These may be either standard mime types or custom app-specific formats. Representing data in multiple formats allows drop targets both within and outside an application to choose data in a format that they understand. For example, a complex object may be serialized in a custom format for use within an application, with fallbacks in plain text and/or rich HTML that can be used when a user drops data from an external application.
The example below finds the first available item that includes a custom app-specific type. The same draggable component as used in the above example is used here, but rather than displaying the plain text representation, the custom format is used instead.
function Example() {
let list = useListData({
initialItems: [
{ id: 1, name: 'Cat' },
{ id: 2, name: 'Dog' },
{ id: 3, name: 'Kangaroo' }
]
});
let onDrop = async (e) => {
let dragItem = e.items.find((item) =>
item.kind === 'text' && item.types.has('my-app-custom-type')
);
if (!dragItem) {
return;
}
let value = JSON.parse(await dragItem.getText('my-app-custom-type'));
let item = { id: list.items.length + 1, name: value.message };
if (e.target.dropPosition === 'before') {
list.insertBefore(e.target.key, item);
} else if (e.target.dropPosition === 'after') {
list.insertAfter(e.target.key, item);
}
};
return (
<>
<Draggable>Octopus</Draggable>
<ListBox
aria-label="Favorite animals"
selectionMode="single"
items={list.items}
getDropOperation={(target) =>
target.type === 'item' && target.dropPosition !== 'on'
? 'copy'
: 'cancel'}
onDrop={onDrop}
>
{(item) => <Item>{item.name}</Item>}
</ListBox>
</>
);
}
function Example() {
let list = useListData({
initialItems: [
{ id: 1, name: 'Cat' },
{ id: 2, name: 'Dog' },
{ id: 3, name: 'Kangaroo' }
]
});
let onDrop = async (e) => {
let dragItem = e.items.find((item) =>
item.kind === 'text' &&
item.types.has('my-app-custom-type')
);
if (!dragItem) {
return;
}
let value = JSON.parse(
await dragItem.getText('my-app-custom-type')
);
let item = {
id: list.items.length + 1,
name: value.message
};
if (e.target.dropPosition === 'before') {
list.insertBefore(e.target.key, item);
} else if (e.target.dropPosition === 'after') {
list.insertAfter(e.target.key, item);
}
};
return (
<>
<Draggable>Octopus</Draggable>
<ListBox
aria-label="Favorite animals"
selectionMode="single"
items={list.items}
getDropOperation={(target) =>
target.type === 'item' &&
target.dropPosition !== 'on'
? 'copy'
: 'cancel'}
onDrop={onDrop}
>
{(item) => <Item>{item.name}</Item>}
</ListBox>
</>
);
}
function Example() {
let list = useListData(
{
initialItems: [
{
id: 1,
name: 'Cat'
},
{
id: 2,
name: 'Dog'
},
{
id: 3,
name:
'Kangaroo'
}
]
}
);
let onDrop =
async (e) => {
let dragItem = e
.items.find(
(item) =>
item.kind ===
'text' &&
item.types
.has(
'my-app-custom-type'
)
);
if (!dragItem) {
return;
}
let value = JSON
.parse(
await dragItem
.getText(
'my-app-custom-type'
)
);
let item = {
id:
list.items
.length + 1,
name:
value.message
};
if (
e.target
.dropPosition ===
'before'
) {
list
.insertBefore(
e.target.key,
item
);
} else if (
e.target
.dropPosition ===
'after'
) {
list.insertAfter(
e.target.key,
item
);
}
};
return (
<>
<Draggable>
Octopus
</Draggable>
<ListBox
aria-label="Favorite animals"
selectionMode="single"
items={list
.items}
getDropOperation={(target) =>
target.type ===
'item' &&
target
.dropPosition !==
'on'
? 'copy'
: 'cancel'}
onDrop={onDrop}
>
{(item) => (
<Item>
{item.name}
</Item>
)}
</ListBox>
</>
);
}
Files#
A FileItem references a file on the user's device. It includes the name and mime type of the file, and methods to read the contents as plain text, or retrieve a native File object which can be attached to form data for uploading.
This example accepts JPEG and PNG image files, and renders them by creating a local object URL. When the list is empty, you can drop on the whole collection, and otherwise items can be inserted.
function Example() {
let list = useListData({});
let onDrop = async (e) => {
let items = await Promise.all(
e.items
.filter((item) =>
item.kind === 'file' &&
(item.type === 'image/jpeg' || item.type === 'image/png')
)
.map(async (item) => ({
id: Math.random(),
url: URL.createObjectURL(await item.getFile()),
name: item.name
}))
);
if (e.target.type === 'root') {
list.prepend(...items);
} else if (e.target.dropPosition === 'before') {
list.insertBefore(e.target.key, ...items);
} else if (e.target.dropPosition === 'after') {
list.insertAfter(e.target.key, ...items);
}
};
return (
<ListBox
aria-label="Images"
items={list.items}
getDropOperation={(target) =>
target.dropPosition !== 'on' ? 'copy' : 'cancel'}
onDrop={onDrop}
>
{(item) => (
<Item>
<div className="image-item">
<img src={item.url} />
<span>{item.name}</span>
</div>
</Item>
)}
</ListBox>
);
}
function Example() {
let list = useListData({});
let onDrop = async (e) => {
let items = await Promise.all(
e.items
.filter((item) =>
item.kind === 'file' &&
(item.type === 'image/jpeg' ||
item.type === 'image/png')
)
.map(async (item) => ({
id: Math.random(),
url: URL.createObjectURL(await item.getFile()),
name: item.name
}))
);
if (e.target.type === 'root') {
list.prepend(...items);
} else if (e.target.dropPosition === 'before') {
list.insertBefore(e.target.key, ...items);
} else if (e.target.dropPosition === 'after') {
list.insertAfter(e.target.key, ...items);
}
};
return (
<ListBox
aria-label="Images"
items={list.items}
getDropOperation={(target) =>
target.dropPosition !== 'on' ? 'copy' : 'cancel'}
onDrop={onDrop}
>
{(item) => (
<Item>
<div className="image-item">
<img src={item.url} />
<span>{item.name}</span>
</div>
</Item>
)}
</ListBox>
);
}
function Example() {
let list = useListData(
{}
);
let onDrop =
async (e) => {
let items =
await Promise
.all(
e.items
.filter(
(item) =>
item
.kind ===
'file' &&
(item
.type ===
'image/jpeg' ||
item
.type ===
'image/png')
)
.map(
async (item) => ({
id:
Math
.random(),
url:
URL
.createObjectURL(
await item
.getFile()
),
name:
item
.name
})
)
);
if (
e.target.type ===
'root'
) {
list.prepend(
...items
);
} else if (
e.target
.dropPosition ===
'before'
) {
list
.insertBefore(
e.target.key,
...items
);
} else if (
e.target
.dropPosition ===
'after'
) {
list.insertAfter(
e.target.key,
...items
);
}
};
return (
<ListBox
aria-label="Images"
items={list.items}
getDropOperation={(target) =>
target
.dropPosition !==
'on'
? 'copy'
: 'cancel'}
onDrop={onDrop}
>
{(item) => (
<Item>
<div className="image-item">
<img
src={item
.url}
/>
<span>
{item.name}
</span>
</div>
</Item>
)}
</ListBox>
);
}
Show CSS
.image-item {
display: flex;
height: 50px;
gap: 10px;
}
.image-item img {
height: 100%;
object-fit: contain;
}
.image-item span {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}.image-item {
display: flex;
height: 50px;
gap: 10px;
}
.image-item img {
height: 100%;
object-fit: contain;
}
.image-item span {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}.image-item {
display: flex;
height: 50px;
gap: 10px;
}
.image-item img {
height: 100%;
object-fit: contain;
}
.image-item span {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}Directories#
A DirectoryItem references the contents of a directory on the user's device. It includes the name of the directory, as well as a method to iterate through the files and folders within the directory. The contents of any folders within the directory can be accessed recursively.
The getEntries method returns an async iterable object, which can be used in a for await...of loop. This provides each item in teh directory as either a FileItem or DirectoryItem, and you can access the contents of each file as discussed above.
This example accepts directory drops over the whole collection, and renders the contents as items in the list.
import File from '@spectrum-icons/workflow/FileTxt';
import Folder from '@spectrum-icons/workflow/Folder';
function Example() {
let [files, setFiles] = React.useState([]);
let onDrop = async (e) => {
// Find the first dropped item that is a directory.
let dir = e.items.find((item) => item.kind === 'directory');
if (dir) {
// Read entries in directory and update state with relevant info.
let files = [];
for await (let entry of dir.getEntries()) {
files.push({
name: entry.name,
kind: entry.kind
});
}
setFiles(files); }
};
return (
<ListBox
aria-label="Directory contents"
items={files}
getDropOperation={(target) => target.type === 'root' ? 'copy' : 'cancel'}
onDrop={onDrop}
>
{(item) => (
<Item key={item.name}>
<div className="dir-item">
{item.kind === 'directory' ? <Folder /> : <File />}
<span>{item.name}</span>
</div>
</Item>
)}
</ListBox>
);
}
import File from '@spectrum-icons/workflow/FileTxt';
import Folder from '@spectrum-icons/workflow/Folder';
function Example() {
let [files, setFiles] = React.useState([]);
let onDrop = async (e) => {
// Find the first dropped item that is a directory.
let dir = e.items.find((item) =>
item.kind === 'directory'
);
if (dir) {
// Read entries in directory and update state with relevant info.
let files = [];
for await (let entry of dir.getEntries()) {
files.push({
name: entry.name,
kind: entry.kind
});
}
setFiles(files); }
};
return (
<ListBox
aria-label="Directory contents"
items={files}
getDropOperation={(target) =>
target.type === 'root' ? 'copy' : 'cancel'}
onDrop={onDrop}
>
{(item) => (
<Item key={item.name}>
<div className="dir-item">
{item.kind === 'directory'
? <Folder />
: <File />}
<span>{item.name}</span>
</div>
</Item>
)}
</ListBox>
);
}
import File from '@spectrum-icons/workflow/FileTxt';
import Folder from '@spectrum-icons/workflow/Folder';
function Example() {
let [files, setFiles] =
React.useState([]);
let onDrop =
async (e) => {
// Find the first dropped item that is a directory.
let dir = e.items
.find((item) =>
item.kind ===
'directory'
);
if (dir) {
// Read entries in directory and update state with relevant info.
let files = [];
for await (
let entry
of dir
.getEntries()
) {
files.push({
name:
entry.name,
kind:
entry.kind
});
}
setFiles(files); }
};
return (
<ListBox
aria-label="Directory contents"
items={files}
getDropOperation={(target) =>
target.type ===
'root'
? 'copy'
: 'cancel'}
onDrop={onDrop}
>
{(item) => (
<Item
key={item.name}
>
<div className="dir-item">
{item
.kind ===
'directory'
? (
<Folder />
)
: <File />}
<span>
{item.name}
</span>
</div>
</Item>
)}
</ListBox>
);
}
Show CSS
.dir-item {
display: flex;
align-items: center;
gap: 8px;
}
.dir-item {
flex: 0 0 auto;
}
.dir-item {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}.dir-item {
display: flex;
align-items: center;
gap: 8px;
}
.dir-item {
flex: 0 0 auto;
}
.dir-item {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}.dir-item {
display: flex;
align-items: center;
gap: 8px;
}
.dir-item {
flex: 0 0 auto;
}
.dir-item {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}Drop operations#
A DropOperation is an indication of what will happen when dragged data is dropped on a particular drop target. These are:
move– indicates that the dragged data will be moved from its source location to the target location.copy– indicates that the dragged data will be copied to the target destination.link– indicates that there will be a relationship established between the source and target locations.cancel– indicates that the drag and drop operation will be canceled, resulting in no changes made to the source or target.
Many operating systems display these in the form of a cursor change, e.g. a plus sign to indicate a copy operation. The user may also be able to use a modifier key to choose which drop operation to perform, such as Option or Alt to switch from move to copy.
The drag source can specify which drop operations are allowed for the dragged data (see the useDrag docs for how to customize this). By default, the first allowed operation is allowed by drop targets, meaning that the drop target accepts data of any type and operation.
getDropOperation#
The getDropOperation function passed to useDroppableCollection can be used to provide appropriate feedback to the user when a drag hovers over the drop target. If a drop target only supports data of specific types (e.g. images, videos, text, etc.), then it should implement getDropOperation and return 'cancel' for types that aren't supported. This will prevent visual feedback indicating that the drop target accepts the dragged data when this is not true.
When the data is supported, either return one of the drop operations in allowedOperation or a specific drop operation if only that drop operation is supported. If the returned operation is not in allowedOperations, then the drop target will act as if 'cancel' was returned.
In the below example, the drop target only supports dropping PNG images. If a PNG is dragged over the target, it will be highlighted and the operating system displays a copy cursor. If another type is dragged over the target, then there is no visual feedback, indicating that a drop is not accepted there. If the user holds a modifier key such as Control while dragging over the drop target in order to change the drop operation, then the drop target does not accept the drop.
function Example() {
// ...
return (
<ListBox
aria-label="Images"
items={list.items}
getDropOperation={(target, types) =>
types.has('image/png') && target.dropPosition !== 'on'
? 'copy'
: 'cancel'} onDrop={onDrop}
>
{(item) => (
<Item>
<div className="image-item">
<img src={item.url} />
<span>{item.name}</span>
</div>
</Item>
)}
</ListBox>
);
}
function Example() {
// ...
return (
<ListBox
aria-label="Images"
items={list.items}
getDropOperation={(target, types) =>
types.has('image/png') &&
target.dropPosition !== 'on'
? 'copy'
: 'cancel'} onDrop={onDrop}
>
{(item) => (
<Item>
<div className="image-item">
<img src={item.url} />
<span>{item.name}</span>
</div>
</Item>
)}
</ListBox>
);
}
function Example() {
// ...
return (
<ListBox
aria-label="Images"
items={list.items}
getDropOperation={(
target,
types
) =>
types.has(
'image/png'
) &&
target
.dropPosition !==
'on'
? 'copy'
: 'cancel'} onDrop={onDrop}
>
{(item) => (
<Item>
<div className="image-item">
<img
src={item
.url}
/>
<span>
{item.name}
</span>
</div>
</Item>
)}
</ListBox>
);
}
onDrop#
The onDrop event also includes the dropOperation. This can be used to perform different actions accordingly, for example, when communicating with a backend API.
let onDrop = async (e) => {
let item = e.items.find((item) =>
item.kind === 'text' && item.types.has('my-app-file')
);
if (!item) {
return;
}
let data = JSON.parse(await item.getText('my-app-file'));
switch (e.dropOperation) {
case 'move':
MyAppFileService.move(data.filePath, props.filePath);
break;
case 'copy':
MyAppFileService.copy(data.filePath, props.filePath);
break;
case 'link':
MyAppFileService.link(data.filePath, props.filePath);
break;
}};
let onDrop = async (e) => {
let item = e.items.find((item) =>
item.kind === 'text' && item.types.has('my-app-file')
);
if (!item) {
return;
}
let data = JSON.parse(await item.getText('my-app-file'));
switch (e.dropOperation) {
case 'move':
MyAppFileService.move(data.filePath, props.filePath);
break;
case 'copy':
MyAppFileService.copy(data.filePath, props.filePath);
break;
case 'link':
MyAppFileService.link(data.filePath, props.filePath);
break;
}};
let onDrop = async (
e
) => {
let item = e.items
.find((item) =>
item.kind ===
'text' &&
item.types.has(
'my-app-file'
)
);
if (!item) {
return;
}
let data = JSON.parse(
await item.getText(
'my-app-file'
)
);
switch (
e.dropOperation
) {
case 'move':
MyAppFileService
.move(
data.filePath,
props.filePath
);
break;
case 'copy':
MyAppFileService
.copy(
data.filePath,
props.filePath
);
break;
case 'link':
MyAppFileService
.link(
data.filePath,
props.filePath
);
break;
}};