alpha

Drag and Drop

This page describes how to enable drag and drop functionality for the various React Spectrum components that support it.

Introduction#


Drag and drop is a common UI interaction that allows users to transfer data between two locations by directly moving a visual representation on screen. It is a flexible, efficient, and intuitive way for users to perform a variety of tasks, and is widely supported across both desktop and mobile operating systems. In addition to the standard mouse and touch interactions, React Spectrum also implements keyboard and screen reader accessible alternatives for drag and drop to enable all users to perform these tasks.

Drag and Drop Concepts#


Before we dive into how to enable drag and drop in React Spectrum, let's touch briefly on the terminology and concepts of drag and drop. In a drag and drop operation, there is a drag source and a drop target. The drag source is the starting location of your dragged data and the drop target is its intended destination. The dragged data is made up of one or more drag items, each of which contains information specific to their original item within the drag source.

A drag item contains several pieces of information: the type of the data, the item's kind, and the actual data itself. The type of a drag item can be one of the common mime types or a custom type specific to your application. Multiple types can be attached to a single drag item so that the item's data can be provided in different formats for interoperability with various drop targets. For example, an image could be represented by an image/jpeg type and thus be recognized as a JPEG by a file upload drop target but also have a plain/text type that allows the image's file name to be communicated to a text input drop target. In addition, there are two kinds of items: string items include inline data in the form of a Unicode string, and file items include a reference to a file from the user's computer.

There are several drop operations that can be performed in a drag and drop operation: "move", "copy", "link", and "cancel". A "move" operation indicates that the dragged data will be moved from its source location to the target location. A "copy" operation indicates that the dragged data will be copied to the target destination. A "link" operation indicates that there will be a relationship established between the source and target locations. Finally, a "cancel" operation indicates that the drag and drop operation will be canceled, resulting in no changes made to the source or target. The drag source can specify what drop operations are allowed for its data, allowing the drop source to decide what operation to perform on drop by using the restrictions set by the drag source as a guideline.

Collection components, such as ListView, support multiple drop positions. The component may support a "root" drop position, allowing items to be dropped on the collection as a whole. It may also support "on" drop positions, such as when dropping into a folder in a list. If the collection allows reordering of its items, it could support "between" drop positions, allowing the user to insert or move items between other items. Any number of these drop positions can be allowed at the same time and the component can use the types of the dragged items to selectively allow or disallow certain positions.

Interaction modes#

There are several interaction modes that need to be considered for drag and drop. When using a mouse, you can click an item and drag by holding the mouse button down and moving the pointer. A drop can be performed by releasing the mouse button or canceled by the Esc key. A similar interaction can be performed via touch, with a drag initiated via a long press and a drop performed by removing your finger from the screen. In both cases, selecting and dragging an item is often accompanied by a drag preview. The drag preview is a smaller version of the dragged item that follows the cursor or touch point. When multiple items are dragged at once, the drag preview displays a stack of items instead, accompanied by a badge reflecting the total number of dragged items. Drop targets are visually highlighted when dragged over, and the desired drop operation can be controlled via modifier key presses or drop activations via hovering over the drop target for a period of time.

For keyboards, copy and paste shortcuts have traditionally been the alternative method to drag and drop. This comes with many limitations as it is often hard to know where pasting is allowed and difficult to control the exact positioning of the pasted items. Touch screen readers are even more limited in their ability to perform these operations since they often lack access to a keyboard and thus cannot copy paste in the same manner.

React Spectrum attempts to resolve the above limitations by providing interactive drag affordances that bring a user into drag and drop mode when triggered via keyboard or screen reader virtual click. To perform a drag and drop operation via a keyboard, first select the items to be dragged by focusing the row and pressing Space. You can then start the drag operation by moving focus to the drag handle on any of the selected rows via the arrow keys and hitting Enter or Space. Once a drag operation is started, you will be automatically brought to the first valid drop target. Tab can then be used to cycle through other valid drop targets. For collection components like the ListView above, Tab will move you on or off the overall component itself whereas ArrowUp and ArrowDown will cycle through the valid drop targets within the component itself. Hitting Enter will then confirm the drop operation on the focused drop target. To cancel a drag operation, you can hit Esc at any time.

For screen readers, please follow the custom instructions announced when focusing the row's drag handle to begin a drag operation. For screen readers on mobile devices, swiping left and right will move you between valid drop targets and double tapping will confirm a drop operation. Go ahead and try out drag and drop in the example below!

Example#


Creating the draggable list#

For the first ListView in the example above, we want to make the rows draggable and have the dragged rows removed from the list upon a successful drop. To accomplish this, we first want to setup the initial list of items for our draggable ListView via useListData so that we have access to some helper methods to modify the list of items on the fly. Note that this is completely optional and is not required to enable drag and drop in React Spectrum. You may substitute useListData with useAsyncList or with any other state management solution.

let list = useListData({
  initialItems: [
    {id: 'a', textValue: 'Adobe Photoshop'},
    {id: 'b', textValue: 'Adobe XD'},
    {id: 'c', textValue: 'Adobe Dreamweaver'},
    {id: 'd', textValue: 'Adobe InDesign'},
    {id: 'e', textValue: 'Adobe Connect'}
  ]
});
let list = useListData({
  initialItems: [
    {id: 'a', textValue: 'Adobe Photoshop'},
    {id: 'b', textValue: 'Adobe XD'},
    {id: 'c', textValue: 'Adobe Dreamweaver'},
    {id: 'd', textValue: 'Adobe InDesign'},
    {id: 'e', textValue: 'Adobe Connect'}
  ]
});
let list = useListData({
  initialItems: [
    {
      id: 'a',
      textValue:
        'Adobe Photoshop'
    },
    {
      id: 'b',
      textValue:
        'Adobe XD'
    },
    {
      id: 'c',
      textValue:
        'Adobe Dreamweaver'
    },
    {
      id: 'd',
      textValue:
        'Adobe InDesign'
    },
    {
      id: 'e',
      textValue:
        'Adobe Connect'
    }
  ]
});

Next, we need to specify the data associated with each dragged item by returning an array from the getItems function. As described above in the concepts section, each item includes a mapping of drag types to serialized data. In this case, we look up the information for each dragged item and serialize it, mapping it to a custom item type. This information will be processed and provided to the drop target's drop handlers on drop.

function getItems(keys) {
  return [...keys].map(key => {
    let item = list.getItem(key);
    return {
      'adobe-app': JSON.stringify(item)
    };
  })
}
function getItems(keys) {
  return [...keys].map(key => {
    let item = list.getItem(key);
    return {
      'adobe-app': JSON.stringify(item)
    };
  })
}
function getItems(keys) {
  return [...keys].map(
    (key) => {
      let item = list
        .getItem(key);
      return {
        'adobe-app': JSON
          .stringify(
            item
          )
      };
    }
  );
}

We also create an onDragEnd event handler for useDnDHooks that handles removing the dragged items from the draggable list upon a successful drop operation. Note how we use the .remove method provided by useListData to remove the dropped items from our list.

function onDragEnd(e) {
  if (e.dropOperation === 'move') {
    list.remove(...e.keys);
  }
}
function onDragEnd(e) {
  if (e.dropOperation === 'move') {
    list.remove(...e.keys);
  }
}
function onDragEnd(e) {
  if (
    e.dropOperation ===
      'move'
  ) {
    list.remove(
      ...e.keys
    );
  }
}

Finally, we provide our getItems and onDragEnd functions as options to useDnDHooks, providing us with a set of dragHooks that we can pass to our ListView directly. Below is what our draggable ListView would look like after combining everything together. For more info on getItems and onDragEnd, see the API section below.

import {Item, ListView} from '@react-spectrum/list';
import {useDnDHooks} from '@react-spectrum/dnd';
import {useListData} from '@react-stately/data';

function DraggableList(props) {
  let {
    items,
    ...otherProps
  } = props;
  let list = useListData({
    initialItems: items || [
      {id: 'a', textValue: 'Adobe Photoshop'},
      {id: 'b', textValue: 'Adobe XD'},
      {id: 'c', textValue: 'Adobe Dreamweaver'},
      {id: 'd', textValue: 'Adobe InDesign'},
      {id: 'e', textValue: 'Adobe Connect'}
    ]
  });

  let {dragHooks} = useDnDHooks({
    getItems: (keys) => [...keys].map(key => {
      let item = list.getItem(key);
      return {
        'adobe-app': JSON.stringify(item)
      };
    }),
    onDragEnd: (e) => {
      if (e.dropOperation === 'move') {
        list.remove(...e.keys);
      }
    }
  });

  return (
    <ListView
      aria-label="Draggable list view example"
      width="size-3600"
      height="size-3600"
      selectionMode="multiple"
      items={list.items}
      dragHooks={dragHooks}
      {...otherProps}>
      {item => (
        <Item key={item.key}>
          {item.textValue}
        </Item>
      )}
    </ListView>
  );
}
import {Item, ListView} from '@react-spectrum/list';
import {useDnDHooks} from '@react-spectrum/dnd';
import {useListData} from '@react-stately/data';

function DraggableList(props) {
  let {
    items,
    ...otherProps
  } = props;
  let list = useListData({
    initialItems: items || [
      {id: 'a', textValue: 'Adobe Photoshop'},
      {id: 'b', textValue: 'Adobe XD'},
      {id: 'c', textValue: 'Adobe Dreamweaver'},
      {id: 'd', textValue: 'Adobe InDesign'},
      {id: 'e', textValue: 'Adobe Connect'}
    ]
  });

  let {dragHooks} = useDnDHooks({
    getItems: (keys) => [...keys].map(key => {
      let item = list.getItem(key);
      return {
        'adobe-app': JSON.stringify(item)
      };
    }),
    onDragEnd: (e) => {
      if (e.dropOperation === 'move') {
        list.remove(...e.keys);
      }
    }
  });

  return (
    <ListView
      aria-label="Draggable list view example"
      width="size-3600"
      height="size-3600"
      selectionMode="multiple"
      items={list.items}
      dragHooks={dragHooks}
      {...otherProps}>
      {item => (
        <Item key={item.key}>
          {item.textValue}
        </Item>
      )}
    </ListView>
  );
}
import {
  Item,
  ListView
} from '@react-spectrum/list';
import {useDnDHooks} from '@react-spectrum/dnd';
import {useListData} from '@react-stately/data';

function DraggableList(
  props
) {
  let {
    items,
    ...otherProps
  } = props;
  let list = useListData(
    {
      initialItems:
        items || [
          {
            id: 'a',
            textValue:
              'Adobe Photoshop'
          },
          {
            id: 'b',
            textValue:
              'Adobe XD'
          },
          {
            id: 'c',
            textValue:
              'Adobe Dreamweaver'
          },
          {
            id: 'd',
            textValue:
              'Adobe InDesign'
          },
          {
            id: 'e',
            textValue:
              'Adobe Connect'
          }
        ]
    }
  );

  let { dragHooks } =
    useDnDHooks({
      getItems: (keys) =>
        [...keys].map(
          (key) => {
            let item =
              list
                .getItem(
                  key
                );
            return {
              'adobe-app':
                JSON
                  .stringify(
                    item
                  )
            };
          }
        ),
      onDragEnd: (e) => {
        if (
          e.dropOperation ===
            'move'
        ) {
          list.remove(
            ...e.keys
          );
        }
      }
    });

  return (
    <ListView
      aria-label="Draggable list view example"
      width="size-3600"
      height="size-3600"
      selectionMode="multiple"
      items={list.items}
      dragHooks={dragHooks}
      {...otherProps}
    >
      {(item) => (
        <Item
          key={item.key}
        >
          {item
            .textValue}
        </Item>
      )}
    </ListView>
  );
}

Creating the droppable list#

For the second ListView, we want to make it droppable but have it only accept root level drops. Similar to the draggable ListView, we'll start by initializing the default item list via useListData. As a reminder, useListData is completely optional here and can be replaced by useAsyncList or any other state management solution.

let list = useListData({
  initialItems: [
    {id: 'f', textValue: 'Adobe AfterEffects'},
    {id: 'g', textValue: 'Adobe Illustrator'},
    {id: 'h', textValue: 'Adobe Lightroom'},
    {id: 'i', textValue: 'Adobe Premiere Pro'},
    {id: 'j', textValue: 'Adobe Fresco'}
  ]
});
let list = useListData({
  initialItems: [
    {id: 'f', textValue: 'Adobe AfterEffects'},
    {id: 'g', textValue: 'Adobe Illustrator'},
    {id: 'h', textValue: 'Adobe Lightroom'},
    {id: 'i', textValue: 'Adobe Premiere Pro'},
    {id: 'j', textValue: 'Adobe Fresco'}
  ]
});
let list = useListData({
  initialItems: [
    {
      id: 'f',
      textValue:
        'Adobe AfterEffects'
    },
    {
      id: 'g',
      textValue:
        'Adobe Illustrator'
    },
    {
      id: 'h',
      textValue:
        'Adobe Lightroom'
    },
    {
      id: 'i',
      textValue:
        'Adobe Premiere Pro'
    },
    {
      id: 'j',
      textValue:
        'Adobe Fresco'
    }
  ]
});

To implement root level only drops, we create a getDropOperation function that returns "cancel" for any drop targets other than root or if the dragged item types doesn't include 'adobe-app'.

function getDropOperation(target, types) {
  if (target.type !== 'root' || !types.has('adobe-app')) {
    return 'cancel';
  }

  return 'move';
}
function getDropOperation(target, types) {
  if (target.type !== 'root' || !types.has('adobe-app')) {
    return 'cancel';
  }

  return 'move';
}
function getDropOperation(
  target,
  types
) {
  if (
    target.type !==
      'root' ||
    !types.has(
      'adobe-app'
    )
  ) {
    return 'cancel';
  }

  return 'move';
}

Next, we create an onDrop event handler to process the successfully dropped items. This onDrop handler first checks each dropped item's kind and type, extracting the item's relevant information if it detects it has the expected types. This information is used to construct an array of items to insert to the end of the droppable list via list.append.

async function onDrop(e) {
  let itemsToAdd = await Promise.all(
    e.items
      .filter(item => item.kind === 'text' && item.types.has('adobe-app'))
      .map(async item => JSON.parse(await item.getText('adobe-app')))
  );
  list.append(...itemsToAdd);
}
async function onDrop(e) {
  let itemsToAdd = await Promise.all(
    e.items
      .filter((item) =>
        item.kind === 'text' && item.types.has('adobe-app')
      )
      .map(async (item) =>
        JSON.parse(await item.getText('adobe-app'))
      )
  );
  list.append(...itemsToAdd);
}
async function onDrop(
  e
) {
  let itemsToAdd =
    await Promise.all(
      e.items
        .filter((item) =>
          item.kind ===
            'text' &&
          item.types.has(
            'adobe-app'
          )
        )
        .map(
          async (item) =>
            JSON.parse(
              await item
                .getText(
                  'adobe-app'
                )
            )
        )
    );
  list.append(
    ...itemsToAdd
  );
}

Finally, we provide our getDropOperation and onDrop functions as options to useDnDHooks, providing us with a set of dropHooks that we can pass to our ListView directly. Below is what our droppable ListView would look like after combining everything together. For more info on getDropOperation and onDrop, see the API section below.

import {useDnDHooks} from '@react-spectrum/dnd';

function DroppableList(props) {
  let {
    items,
    ...otherProps
  } = props;
  let list = useListData({
    initialItems: items || [
      {id: 'f', textValue: 'Adobe AfterEffects'},
      {id: 'g', textValue: 'Adobe Illustrator'},
      {id: 'h', textValue: 'Adobe Lightroom'},
      {id: 'i', textValue: 'Adobe Premiere Pro'},
      {id: 'j', textValue: 'Adobe Fresco'}
    ]
  });

  let {dropHooks} = useDnDHooks({
    getDropOperation: (target, types) => {
      if (target.type !== 'root' || !types.has('adobe-app')) {
        return 'cancel';
      }

      return 'move';
    },
    onDrop: async (e) => {
      let itemsToAdd = await Promise.all(
        e.items
          .filter(item => item.kind === 'text' && item.types.has('adobe-app'))
          .map(async item => JSON.parse(await item.getText('adobe-app')))
      );
      list.append(...itemsToAdd);
    }
  });

  return (
    <ListView
      aria-label="Droppable list view example"
      width="size-3600"
      height="size-3600"
      selectionMode="multiple"
      items={list.items}
      dropHooks={dropHooks}
      {...otherProps}>
      {item => (
        <Item key={item.key}>
          {item.textValue}
        </Item>
      )}
    </ListView>
  );
}
import {useDnDHooks} from '@react-spectrum/dnd';

function DroppableList(props) {
  let {
    items,
    ...otherProps
  } = props;
  let list = useListData({
    initialItems: items || [
      { id: 'f', textValue: 'Adobe AfterEffects' },
      { id: 'g', textValue: 'Adobe Illustrator' },
      { id: 'h', textValue: 'Adobe Lightroom' },
      { id: 'i', textValue: 'Adobe Premiere Pro' },
      { id: 'j', textValue: 'Adobe Fresco' }
    ]
  });

  let { dropHooks } = useDnDHooks({
    getDropOperation: (target, types) => {
      if (
        target.type !== 'root' || !types.has('adobe-app')
      ) {
        return 'cancel';
      }

      return 'move';
    },
    onDrop: async (e) => {
      let itemsToAdd = await Promise.all(
        e.items
          .filter((item) =>
            item.kind === 'text' &&
            item.types.has('adobe-app')
          )
          .map(async (item) =>
            JSON.parse(await item.getText('adobe-app'))
          )
      );
      list.append(...itemsToAdd);
    }
  });

  return (
    <ListView
      aria-label="Droppable list view example"
      width="size-3600"
      height="size-3600"
      selectionMode="multiple"
      items={list.items}
      dropHooks={dropHooks}
      {...otherProps}
    >
      {(item) => (
        <Item key={item.key}>
          {item.textValue}
        </Item>
      )}
    </ListView>
  );
}
import {useDnDHooks} from '@react-spectrum/dnd';

function DroppableList(
  props
) {
  let {
    items,
    ...otherProps
  } = props;
  let list = useListData(
    {
      initialItems:
        items || [
          {
            id: 'f',
            textValue:
              'Adobe AfterEffects'
          },
          {
            id: 'g',
            textValue:
              'Adobe Illustrator'
          },
          {
            id: 'h',
            textValue:
              'Adobe Lightroom'
          },
          {
            id: 'i',
            textValue:
              'Adobe Premiere Pro'
          },
          {
            id: 'j',
            textValue:
              'Adobe Fresco'
          }
        ]
    }
  );

  let { dropHooks } =
    useDnDHooks({
      getDropOperation: (
        target,
        types
      ) => {
        if (
          target.type !==
            'root' ||
          !types.has(
            'adobe-app'
          )
        ) {
          return 'cancel';
        }

        return 'move';
      },
      onDrop: async (
        e
      ) => {
        let itemsToAdd =
          await Promise
            .all(
              e.items
                .filter(
                  (item) =>
                    item
                        .kind ===
                      'text' &&
                    item
                      .types
                      .has(
                        'adobe-app'
                      )
                )
                .map(
                  async (item) =>
                    JSON
                      .parse(
                        await item
                          .getText(
                            'adobe-app'
                          )
                      )
                )
            );
        list.append(
          ...itemsToAdd
        );
      }
    });

  return (
    <ListView
      aria-label="Droppable list view example"
      width="size-3600"
      height="size-3600"
      selectionMode="multiple"
      items={list.items}
      dropHooks={dropHooks}
      {...otherProps}
    >
      {(item) => (
        <Item
          key={item.key}
        >
          {item
            .textValue}
        </Item>
      )}
    </ListView>
  );
}

API#


As seen in the example above, enabling drag and drop for a supported React Spectrum component differs slightly from the typical event handler prop pattern that you may be familiar with. Instead of providing each event handler directly to the component, you must first import useDnDHooks from the @react-spectrum/dnd package and provide this hook with your desired options. useDnDHooks will then provide you with a set of drag hooks that you can pass to the component via its dragHooks prop, thus enabling drag operations for the component. Drop operations are then enabled in the same way by passing the drop hooks from useDnDHooks to the component's dropHooks prop. This approach allows the drag and drop implementation to be included only when used, keeping bundle sizes small when unused by an application.

useDnDHooks#

TODO: Update this section to better separate the util handlers from the lower level props

useDnDHooks( (options: DnDOptions )): DnDHooks
NameTypeDefaultDescription
getItems( (keys: Set<Key> )) => DragItem[]() => []A function that returns the items being dragged. If not specified, we assume that the collection is not draggable.
onDragStart( (e: DraggableCollectionStartEvent )) => voidHandler that is called when a drag operation is started.
onDragMove( (e: DraggableCollectionMoveEvent )) => voidHandler that is called when the dragged element is moved.
onDragEnd( (e: DraggableCollectionEndEvent )) => voidHandler that is called when the drag operation is ended, either as a result of a drop or a cancellation.
getAllowedDropOperations() => DropOperation[]Function that returns the drop operations that are allowed for the dragged items. If not provided, all drop operations are allowed.
getDropOperation( target: DropTarget, types: DragTypes, allowedOperations: DropOperation[] ) => DropOperation

A function returning the drop operation to be performed when items matching the given types are dropped on the drop target.

onDropEnter( (e: DroppableCollectionEnterEvent )) => voidHandler that is called when a valid drag enters the drop target.
onDropMove( (e: DroppableCollectionMoveEvent )) => voidHandler that is called when a valid drag is moved within the drop target.
onDropActivate( (e: DroppableCollectionActivateEvent )) => voidHandler that is called after a valid drag is held over the drop target for a period of time.
onDropExit( (e: DroppableCollectionExitEvent )) => voidHandler that is called when a valid drag exits the drop target.
onDrop( (e: DroppableCollectionDropEvent )) => voidHandler that is called when a valid drag is dropped on the drop target.
onInsert( (e: DroppableCollectionInsertDropEvent )) => voidHandler called when external items are dropped "between" the droppable collection's items.
onRootDrop( (e: DroppableCollectionRootDropEvent )) => voidHandler called when external items are dropped on the droppable collection's root.
onItemDrop( (e: DroppableCollectionOnItemDropEvent )) => voidHandler called when items are dropped "on" a droppable collection's item.
onReorder( (e: DroppableCollectionReorderEvent )) => voidHandler called when items are reordered via drag in the source collection.
acceptedDragTypes'all'Array<string>'all'The drag types that the droppable collection accepts. If directories are accepted, include the DIRECTORY_DRAG_TYPE from @react-aria/dnd in the array of allowed types.
shouldAcceptItemDrop( (target: ItemDropTarget, , types: DragTypes )) => booleanA function returning whether a given target in the droppable collection is a valid "on" drop target for the current drag types.

Of the various hook options above, getAllowedDropOperations and getDropOperation may be of particular interest since it allows you to specify what kinds of drag and drop operations you want to allow. When the dragged items are dropped on a drop target created using the React Aria drag and drop hooks, the allowed drop operations you return in getAllowedDropOperations are provided to the drop target's getDropOperation, giving the drop target extra information to use when deciding what drop operation to execute. This in turn provides the onDragEnd and onDrop handlers with the executed drop operation, allowing you to decide what to do with the dragged items in your original collection and in the dropped collection.

For instance, you may have a draggable collection of items that allows "move" and "copy" operations but you need a way to know whether or not you should be removing the dragged items from the list after a drop operation. A drop target that only allows "copy" operations, such as a file upload drop zone, would be able to return "copy" from its getDropOperation and communicate that to your draggable collection's onDragEnd handler, letting the draggable collection know that it shouldn't remove the dragged items from its list. Alternatively, a drop target that allows "move" operations, like in the example above, would return move from its getDropOperation and thus inform your draggable collection to remove the dragged items from its list.

Supported components#


The following list shows which components currently support drag and drop. Common drag and drop implementations are included in each component's documentation so definitely take a look!