Drag and Drop
Because UI Toolkit lacks a z-index property and the visual order is determined by the order in the hierarchy, we have to get creative when we want elements to be rendered on top of everything. Drag & Drop is one such situation.
Context
A DragAndDropContext ancestor is necessary for Draggables to work. When an element is being dragged, it will add it at the end of its hierarchy (on top of everything else) and will handle its positioning.
Props
Action<bool> onDrag: Callback that gets called when dragging begins or ends.VisualAttributes visualAttributes: Styling information. Expanded inCreatemethod.Children children: Children.
Draggable
The Draggable element is kind of special: it’s a generic RishElement. The type argument determines the type of information the draggable element holds and it must be a struct type.
Props
Element content: TheElementthat is used by default.Element contentWhileDragging: TheElementthat is used while it’s being dragged. Useful to show a grayed out variation or something similar. If invalid, it will fallback tocontent.Element secondaryContentWhileDragging: TheElementthat is used while it’s being dragged with the right mouse button. Useful to show a grayed out variation or something similar. If invalid, it will fallback tocontentWhileDragging.Element draggedElement: TheElementthat follows the cursor.Element draggedAcceptedElement: TheElementthat follows the cursor when hovering a validDragArea. If invalid, it will fallback todraggedElement.Element draggedRejectedElement: TheElementthat follows the cursor when hovering an invalidDragArea. If invalid, it will fallback todraggedElement.Element secondaryDraggedElement: TheElementthat follows the cursor when dragging with the right mouse button.Element secondaryDraggedAcceptedElement: TheElementthat follows the cursor when hovering a validDragAreawhen dragging with the right mouse button. If invalid, it will fallback tosecondaryDraggedElement.Element secondaryDraggedRejectedElement: TheElementthat follows the cursor when hovering an invalidDragAreawhen dragging with the right mouse button. If invalid, it will fallback tosecondaryDraggedElement.T info: The information the element holds and drops.T? secondaryInfo: The information the element holds and drops when dragging with the right mouse button. If null, it will fallback toinfo.Action onDragAction: Callback that gets called when the drag action begins.Action onDropAction: Callback that gets called when the element is dropped.Action dropNowhereAction: Callback that gets called when the element is dropped over empty space.
DropArea
Draggable elements must be dropped over compatible DragAreas. The DragArea element is also generic and their type argument must match for them to be compatible with each other.
Not only Draggables and DropAreas must be compatible, but the DropArea also has to validate if the information being dragged is accepted or not. You can imagine a scenario where an item is being dragged but it can only be dropped in certain inventories or slots; some DropAreas will accept the item and some will reject it, even though the types match.
Props
Element content: TheElementthat is used by default.Element highlightedContent: TheElementthat is used when a compatible and acceptableDraggableis being dragged. Useful to show a drop call to action. If invalid, it will fallback tocontent.Element hoveredAcceptedContent: TheElementthat is used when a compatible and acceptableDraggableis hovering theDropArea. If invalid, it will fallback tohighlightedContent.Element hoveredRejectedContent: TheElementthat is used when a compatible but rejectedDraggableis hovering theDropArea. If invalid, it will fallback tocontent.Predicate<T> canDropAction: The predicate that is called to determine if theDropAreaaccepts or rejects theDraggableinformation.Action<T> dropAction: The callback that is called when an acceptedDraggableis dropped onto theDropArea.Action<T> hoverStarted: The callback that is called when a compatibleDraggablestarts hovering theDropArea. Useful to play a sound effect or some other kind of feedback.Action hoverEnded: The callback that is called when a compatibleDraggablestops hovering theDropArea. Useful to play a sound effect or some other kind of feedback.