sdxc

Type to search, or start from one of these:

[ @sdxc/ui/behaviors ]

DragSession

Headless drag-and-drop session.

import { DragSession } from "@sdxc/ui/behaviors";

Usage

Owns one drag-and-drop interaction and dispatches a plain "change" event from every mutating method, so a pointer- or keyboard-driven mixin subscribes once and keeps every view it drives in sync from this state.

Signature

new DragSession<TData = unknown>()

Properties

PropertyTypeDescription
sourceDragSession.Source<TData> | nullItem currently being dragged, or null when no session is active.
targetDragSession.Target | nullDrop candidate currently under the pointer; null while the session is idle or the pointer sits away from every valid target.
activebooleanWhether a drag session is currently in progress.

Methods

MethodDescription
start(source: DragSession.Source<TData>): voidBegins a drag session for the given item, implicitly ending any session already in progress first. Always dispatches "change".
moveOver(target: DragSession.Target): voidRecords the drop candidate currently under the pointer. A no-op outside an active session and when key and position match the last call, so a drop indicator repositions only on actual movement.
clearTarget(): voidClears the current drop candidate and keeps the session running, e.g. when the pointer moves past every valid target mid-drag. A no-op while the target is already clear.
drop(): DragSession.DropDetail<TData> | nullCommits a drop at the current target and ends the session. Returns null and leaves the state untouched while the session is idle or the target is unset.
cancel(): voidEnds the current session and discards the pending drop, e.g. on Escape or a pointer release away from every valid target. A no-op while the session is idle.

Examples

let session = new DragSession<{ index: number }>();
session.addEventListener("change", () => update());
session.start({ key: "row-1", data: { index: 0 } });
session.moveOver({ key: "row-3", position: "after" });
session.drop();

The events, constants and types its module publishes, each imported from @sdxc/ui/behaviors too.

Interface

DragSession.Source

Item that starts a drag session: a stable key identifying it, plus an optional consumer-defined payload carried for the life of the session and read back when the drop commits.

MemberTypeDescription
keystringStable identifier for the dragged item.
data?TDataConsumer-defined payload carried for the life of the session.
Interface

DragSession.Target

Drop candidate currently under the pointer.

MemberTypeDescription
keystringStable identifier for the candidate drop target.
positionPositionPosition of the pending drop relative to this target.
Interface

DragSession.DropDetail

Snapshot returned when a drag session ends in a committed drop.

MemberTypeDescription
sourceSource<TData>Item that was dragged.
targetTargetTarget the item was dropped on.
Interface

DragSession.EventMap

Events dispatched by DragSession as its state changes.

MemberTypeDescription
changeEventDispatched after the drag source, the current target, or its position changes.
Type

DragSession.Position

Position of a pending drop relative to its current target. "before" and "after" insert the dragged item next to the target for reordering; "on" drops onto the target itself, nesting into it or filling a drop zone.

type DragSession.Position = "before" | "after" | "on"

Written by Sergio Xalambrí. Follow @sergiodxa for new packages, or sponsor the work.