Drag Overlay
Last updated
Last updated
The <DragOverlay>
component provides a way to render a draggable overlay that is removed from the normal document flow and is positioned relative to the viewport.
Depending on your use-case, you may want to use a drag overlay rather than transforming the original draggable source element that is connected to the useDraggable
hook:
If you'd like to show a preview of where the draggable source will be when dropped, you can update the position of the draggable source while dragging without affecting the drag overlay.
If your item needs to move from one container to another while dragging, we strongly recommend you use the <DragOverlay>
component so the draggable item can unmount from its original container while dragging and mount back into a different container without affecting the drag overlay.
If your draggable item is within a scrollable container, we also recommend you use a <DragOverlay>
, otherwise you'll need to set the draggable element to position: fixed
yourself so the item isn't restricted to the overflow and stacking context of its scroll container, and can move without being affected by the scroll position of its container.
If your useDraggable
items are within a virtualized list, you will absolutely want to use a drag overlay, since the original drag source can unmount while dragging as the virtualized container is scrolled.
If you want smooth drop animations without the effort of building them yourself.
You may render any valid JSX within the children of the <DragOverlay>
.
The <DragOverlay>
component should remain mounted at all times so that it can perform the drop animation. If you conditionally render the <DragOverlay>
component, drop animations will not work.
As a rule of thumb, try to render the <DragOverlay>
outside of your draggable components, and follow the presentational component pattern to maintain a good separation of concerns.
Instead, you should conditionally render the children passed to the <DragOverlay>
:
While this is an optional pattern, we recommend that the components you intend to make draggable be presentational components that are decoupled from @dnd-kit
.
Using this pattern, create a presentational version of your component that you intend on rendering within the drag overlay, and another version that is draggable and renders the presentational component.
As you may have noticed from the example above, we can create small abstract components that render a wrapper node and make any children rendered within draggable:
Using this pattern, we can then render our presentational components within <Draggable>
and within <DragOverlay>
:
Use the ref forwarding pattern to connect your presentational components to the useDraggable
hook:
This way, you can create two versions of your component, one that is presentational, and one that is draggable and renders the presentational component without the need for additional wrapper elements:
The drag overlay is not rendered in a portal by default. Rather, it is rendered in the container where it is rendered.
If you would like to render the <DragOverlay>
in a different container than where it is rendered, import the createPortal
helper from react-dom
:
You may render any valid JSX within the children of the <DragOverlay>
. However, make sure that the components rendered within the drag overlay do not use the useDraggable
hook.
Prefer conditionally rendering the children
of <DragOverlay>
rather than conditionally rendering <DragOverlay>
, otherwise drop animations will not work.
If you'd like to customize the wrapper element that the DragOverlay
's children are rendered into, use the className
and style
props:
Use the dropAnimation
prop to configure the drop animation.
The duration
option should be a number, in milliseconds
. The default value is 250
milliseconds. The easing
option should be a string that represents a valid CSS easing function. The default easing is ease
.
To disable drop animations, set the dropAnimation
prop to null
.
The <DragOverlay>
component should remain mounted at all times so that it can perform the drop animation. If you conditionally render the <DragOverlay>
component, drop animations will not work.
Modifiers let you dynamically modify the movement coordinates that are detected by sensors. They can be used for a wide range of use-cases, which you can learn more about by reading the Modifiers documentation.
For example, you can use modifiers to restrict the movement of the <DragOverlay>
to the bounds of the window:
By default, the <DragOverlay>
component does not have any transitions, unless activated by the Keyboard
sensor. Use the transition
prop to create a function that returns the transition based on the activator event. The default implementation is:
By default, the <DragOverlay>
component renders your elements within a div
element. If your draggable elements are list items, you'll want to update the <DragOverlay>
component to render a ul
wrapper instead, since wrapping a li
item without a parent ul
is invalid HTML:
z-index
The zIndex
prop sets the z-order of the drag overlay. The default value is 999
for compatibility reasons, but we highly recommend you use a lower value.