组件 · 任务
TaskDnd
The drag-and-drop parts that let people move task cards between columns and around other cards, with the drop policy and the move left to the caller.
也叫 Drag and drop, DnD, TaskDraggable, TaskDropZone
import { TaskDnd } from "@botiverse/raft-ui";TaskDnd is the drag-and-drop wiring behind the TaskBoard: a provider, the cards people drag, the places they drop them, the gap a card lands in and the copy of the card that follows the pointer. It decides nothing about tasks. The caller says which drops are allowed and what a move does, such as changing #25 from Todo to In Review.
When to use
| Situation | Use |
|---|---|
| Moving a card to another column of a board to change its status | TaskDnd, inside a TaskBoard |
| Reordering a card among the cards of a column | TaskDnd, with a drop target on each card |
| Changing a status without dragging | The status chip on the card, or the property in the task's details (Tasks) |
| Reordering composer attachments | The composer's own sortable attachments (Composer) |
| Resizing a column | Resizable |
Anatomy
TaskDndProvider: wraps the board and its overlay. It holdsisDropAllowed, the caller's policy, callsonMovewhen a card is dropped on an allowed target, and passesaccessibilityto dnd-kit. Required.TaskDropZone: a place a card can be dropped, usually aTaskBoardColumnthroughrender. It reportsdropState:idle,allowedorblocked.TaskDraggable: wraps one TaskCard and makes it draggable. WithdropIdit is also a drop target, so a card can land before or after it.TaskDropPlaceholder: the gap where the card would land, before or after a card, or in an empty column. It shows only while the drop there is allowed.TaskDragOverlay: the copy of the card that follows the pointer. Render the dragged card inside it, and nothing when no card is dragged.
States
| State | Look |
|---|---|
| Dragging | The copy in TaskDragOverlay is the only card with shadow-float (TaskCard) |
| Over an allowed target | TaskDropPlaceholder shows a dashed line-strong box with the height of the dragged card |
| Over a blocked target | No placeholder; the target's dropState is blocked, and an empty column says why (TaskBoard) |
| Dragging turned off | disabled on the provider removes every way to drag; disabled on a TaskDraggable removes it for that card |
Behavior
- With a mouse, a drag starts once the pointer has moved 5px, so a click on the card isn't a drag.
- On a touch screen, hold the card still for 500ms, then move it: the drag starts on that move. Moving before the hold is up scrolls the board instead (Dragging on a touch screen).
- Where the card lands follows the middle of the dragged card: above the middle of the card under it is
before, below isafter. - The card doesn't animate back or into place on drop. The move is the caller's:
onMovemay return a promise, andonMoveErrorreceives its rejection, so the caller can undo the change and say what went wrong.
Accessibility
- The parts wrap dnd-kit, not Base UI.
TaskDraggablecarries dnd-kit's attributes: it is focusable, with a role description and instructions for screen readers. - Keyboard: Tab to a card, then Space picks it up. ArrowDown and ArrowUp move it one slot down or up its column, before or after each card; ArrowLeft and ArrowRight move it to the nearest drop target in the next column, such as an empty column or the card level with it. Space drops it and Escape cancels. The keys step between drop targets, so a card moves the same way whether or not it leaves its column while it is dragged.
- After a keyboard drop, focus stays where dnd-kit leaves it. Moving focus to the moved card, which has a new place in the DOM, is the caller's job: focus it in
onMoveoronDragEndonce the board has rendered the move. - dnd-kit's own announcements name cards and targets by their ID. Pass
accessibilitytoTaskDndProviderto replace them: it goes to dnd-kit'sDndContextas is, withannouncements(what to say on pick-up, move, drop and cancel) andscreenReaderInstructions(how to drag, read with each card). Write them in words, such as "Task #25 moved to In Review", or announce each move in a polite live region of your own; the TaskBoard composition shows how. TaskDropPlaceholderis hidden from assistive technology.
Rules
- Decide in
isDropAllowedwhich drops are allowed, so the target says while the card is over it that it can't move there (TaskBoard). - Offer another way to change a status: dragging is one of three (Tasks).
- Put the dragged card's own
TaskCardinTaskDragOverlay, so the card under the pointer reads the same as the one in the column.
API
TaskDndProvider
| Prop | 类型 | 默认值 |
|---|---|---|
| { announcements?: Announcements; container?: Element; restoreFocus?: boolean; screenReaderInstructions?: ScreenReaderInstructions; } | ||
| ReactNode | ||
| boolean | false | |
| (activeId: UniqueIdentifier, overId: UniqueIdentifier, overData: TaskDropData | undefined) => boolean | allowTaskDrop | |
| (event: DragCancelEvent) => void | ||
| (event: DragEndEvent) => void | ||
| (event: DragMoveEvent) => void | ||
| (event: DragOverEvent) => void | ||
| (event: DragStartEvent) => void | ||
| (details: TaskMoveDetails) => void | Promise<void> | ||
| (error: unknown, details: TaskMoveDetails) => void |
TaskDragOverlay
| Prop | 类型 | 默认值 |
|---|---|---|
| ReactNode |
TaskDraggable
| Prop | 类型 | 默认值 |
|---|---|---|
| boolean | false | |
| TaskDropData | ||
| boolean | false | |
| UniqueIdentifier |
Also takes the attributes of its element.
TaskDropPlaceholder
| Prop | 类型 | 默认值 |
|---|---|---|
| TaskDropPlacement | ||
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
TaskDropZone
| Prop | 类型 | 默认值 |
|---|---|---|
| TaskDropData | ||
| boolean | false | |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
相关组件
改动
- 0.8.1 · Added
TaskDndProvidertakesaccessibility(theTaskDndAccessibilitytype), passed through to dnd-kit, so you can translate the screen-reader announcements and drag instructions. - 0.8.1 · FixedKeyboard dragging in
TaskDndProvidermoves a card between drop targets. Before, Space picked the card up but the arrow keys never moved it.