Drag & Drop
Typed payloads, drop zones that components register, mouse, touch and pen alike.
The kraft-dnd module has a DndManager and a stage that the app mounts by itself, like
the modals. A component starts a drag from a pointer event; other components register drop zones and hear
what happens. The zones keep their own UI state and render from it: the manager writes nothing to their DOM.
The stage draws only the drag preview, above the modals, so a drag works inside a modal too.
kraftApp {
semanticUI() // registers dnd(); without Semantic UI: dnd()
} Starting a drag
From the source component's onPointerDown. The drag begins once the pointer moved past a
threshold (4 px by default), so a click stays a click; the click after a drag is swallowed. Only the primary
button drags; a press in a text field selects text. The browser's own drag of an image or a link inside the
source is held off while the press lasts, and so is its context menu for a touch or pen press and once the drag
runs; a mouse press that is still a click keeps its menu (a ctrl+click on macOS). With nested sources the innermost one drags.
ui.segment {
// For touch: the browser must leave the pointer to the drag, and a long press must not select text
style = "touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; cursor: grab;"
onPointerDown { event ->
dnd.startDrag(
event = event,
payload = look,
source = slot, // where the drag came from, for the zone
preview = { ui.compact.segment { +look.name } },
)
.onDropped { zone -> /* dropped on zone */ }
.onCancelled { /* released outside every zone, Esc, pointercancel */ }
}
} In a list that scrolls, a whole tile with touch-action: none cannot be swiped on touch. Start the
drag from a grip inside the tile instead, and give the touch styles to the grip only:
ui.segment {
span {
style = "touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; cursor: grab;"
onPointerDown { event -> dnd.startDrag(event = event, payload = look) }
icon.grip_vertical()
}
+look.name // a swipe here scrolls the list
} Drop zones
A component registers a zone for a payload type while it is mounted. Every accepting zone hears
onDragStart when a drag starts and onDragStop when it ends; the zone under the
pointer (the innermost one under the topmost element there that is not the preview, also at the release) hears
onEnter, onOver, onLeave and onDrop. Handlers stack: each
call adds one. A handler that throws is logged; the others and the stops still run. Payloads are matched by
class: generic arguments are erased, so give a payload a class of its own rather than a Pair.
class SlotView(ctx: Ctx<Props>) : Component<SlotView.Props>(ctx) {
private var canTake by value(false)
private var hovered by value(false)
init {
dropZone<Look>(accepts = { it.payload.kind == props.slot.kind }) {
onDragStart { canTake = true }
onDragStop { canTake = false; hovered = false }
onEnter { hovered = true }
onLeave { hovered = false }
onDrop { drag ->
when (val from = drag.source) {
is Slot -> swap(drag.payload, from)
else -> equip(drag.payload)
}
}
}
}
override fun VDom.render() {
ui.given(hovered) { green }.given(canTake && !hovered) { secondary }.segment { /* ... */ }
}
} The zone's area is the component's own element by default; dropZone(element = { ... })
names another. A drag carries payload, source, the pointer's position
and its pointerType.
Semantic UI helpers
DropZoneState from kraft-semanticui keeps canTake and
hovered for you and gives Fomantic classes; nothing requires it.
private val zone = DropZoneState()
init {
dropZone<Number>(accepts = { props.accepts(it.payload) }) {
track(zone) { triggerRedraw() }
onDrop { counter += it.payload.toDouble() }
}
}
override fun VDom.render() {
ui.with(*zone.semanticClasses).segment { /* ... */ }
}