DndContext
Last updated
Last updated
In order for your your and components to interact with each other, you'll need to make sure that the part of your React tree that uses them is nested within a parent <DndContext>
component. The <DndContext>
provider makes use of the to share data between draggable and droppable components and hooks.
React context provides a way to pass data through the component tree without having to pass props down manually at every level.
Therefore, components that use , or will need to be nested within a DndContext
provider.
They don't need to be direct descendants, but, there does need to be a parent <DndContext>
provider somewhere higher up in the tree.
You may also nest <DndContext>
providers within other <DndContext>
providers to achieve nested draggable/droppable interfaces that are independent of one another.
When nesting DndContext
providers, keep in mind that the useDroppable
and useDraggable
hooks will only have access to the other draggable and droppable nodes within that context.
As you can see from the list of props above, there are a number of different events emitted by <DndContext>
that you can listen to and decide how to handle.
The main events you can listen to are:
onDragStart
onDragMove
onDragOver
onDragEnd
Fires after a draggable item is dropped.
This event contains information about the active draggable id
along with information on whether the draggable item was dropped over
.
onDragCancel
Fires if a drag operation is cancelled, for example, if the user presses escape
while dragging a draggable item.
For more details and best practices around accessibility of draggable and droppable components, read the accessibility section:
Use the announcements
prop to customize the screen reader announcements that are announced in the live region when draggable items are picked up, moved over droppable regions, and dropped.
The default announcements are:
While these default announcements are sensible defaults that should cover most simple use cases, you know your application best, and we highly recommend that you customize these to provide a screen reader experience that is more tailored to the use case you are building.
Use the screenReaderInstructions
prop to customize the instructions that are read to screen readers when the focus is moved
Use the optional autoScroll
boolean prop to temporarily or permanently disable auto-scrolling for all sensors used within this DndContext
.
Use the collisionDetection
prop to customize the collision detection algorithm used to detect collisions between draggable nodes and droppable areas within theDndContext
provider.
The built-in collision detection algorithms are:
You may also build custom collision detection algorithms or compose existing ones.
To learn more, read the collision detection guide:
Sensors are an abstraction to detect different input methods in order to initiate drag operations, respond to movement and end or cancel the operation.
To learn how to customize sensors or how to pass different sensors to DndContext
, read the Sensors guide:
Modifiers let you dynamically modify the movement coordinates that are detected by sensors. They can be used for a wide range of use cases, for example:
Restricting motion to a single axis
Restricting motion to the draggable node container's bounding rectangle
Restricting motion to the draggable node's scroll container bounding rectangle
Applying resistance or clamping the motion
To learn more about how to use Modifiers, read the Modifiers guide:
You can configure when and how often DndContext
should measure its droppable elements by using the layoutMeasuring
prop.
The frequency
argument controls how frequently layouts should be measured. By default, layout measuring is set to optimized
, which only measures layouts based on the strategy
.
Specify one of the following strategies:
LayoutMeasuringStrategy.WhileDragging
: Default behavior, only measure droppable elements right after dragging has begun.
LayoutMeasuringStrategy.BeforeDragging
: Measure droppable elements before dragging begins and right after it ends.
LayoutMeasuringStrategy.Always
: Measure droppable elements before dragging begins, right after dragging has begun, and after it ends.
Example usage:
If multiple DndContext
providers are listening for the same event, events will be captured by the first DndContext
that contains a that is activated by that event, similar to how .
Fires when a drag event that meets the for that happens, along with the unique identifier of the draggable element that was picked up.
Fires anytime as the item is moved. Depending on the activated , this could for example be as the is moved or the movement keys are pressed.
Fires when a item is moved over a container, along with the unique identifier of that droppable container.
If there are no when the draggable item is dropped, the over
property will be null
. If a collision is detected, the over
property will contain the id
of the droppable over which it was dropped.
It's important to understand that the onDragEnd
event does not move items into containers.
Auto-scrolling may also be disabled on an individual sensor basis using the static property autoScrollEnabled
of the sensor. For example, the manages scrolling internally, and therefore has the static property autoScrollEnabled
set to false
.
The default collision detection algorithm is the algorithm.
The default sensors used by DndContext
are the and sensors.