@mk-drag-and-drop/dom 0.4.6 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +232 -478
- package/dist/controller/create-drag-controller.d.ts +1 -0
- package/dist/controller/create-drag-controller.js +3 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/input/keyboard-drag.d.ts +1 -1
- package/dist/input/keyboard-drag.js +3 -3
- package/dist/input/pointer-activation.js +10 -8
- package/dist/input/pointer-drag-state.d.ts +2 -0
- package/dist/input/pointer-drag-state.js +32 -0
- package/dist/integration/index.d.ts +2 -1
- package/dist/integration/index.js +1 -0
- package/dist/runtime/drag-runtime-scope.d.ts +2 -0
- package/dist/runtime/drag-runtime-scope.js +2 -0
- package/dist/runtime/drag-runtime.d.ts +7 -1
- package/dist/runtime/drag-runtime.js +214 -90
- package/dist/runtime/drop-target-registry.d.ts +3 -10
- package/dist/runtime/drop-target-registry.js +52 -96
- package/dist/runtime/lifecycle.d.ts +15 -0
- package/dist/runtime/sortable-coordinator.d.ts +32 -0
- package/dist/runtime/sortable-coordinator.js +11 -0
- package/dist/runtime/types.d.ts +1 -0
- package/dist/sortable/create-sortable.js +1 -0
- package/dist/sortable/sortable-preview.d.ts +3 -2
- package/dist/sortable/sortable-preview.js +48 -43
- package/dist/sortable/sortable-registry.d.ts +10 -54
- package/dist/sortable/sortable-registry.js +59 -46
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,310 +1,147 @@
|
|
|
1
1
|
# @mk-drag-and-drop/dom
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
Headless DOM drag-and-drop primitives for draggable, droppable, sortable, and
|
|
4
|
+
grouped interactions. The package owns input, target measurement and selection,
|
|
5
|
+
modifiers, overlays, transient sortable DOM preview, normalization, and
|
|
6
|
+
lifecycle sequencing. Applications own their canonical data and commits.
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
persistence, and final commits. The DOM package reports what happened; it does
|
|
10
|
-
not own application data.
|
|
8
|
+
## Imports
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
Use the root package import for application code:
|
|
10
|
+
Use `@mk-drag-and-drop/dom` for application code:
|
|
15
11
|
|
|
16
12
|
```ts
|
|
17
13
|
import {
|
|
18
14
|
createDragController,
|
|
15
|
+
createDragHandle,
|
|
19
16
|
createDraggable,
|
|
17
|
+
createDropContainer,
|
|
20
18
|
createDroppable,
|
|
21
19
|
createSortable,
|
|
20
|
+
domPointerDragActiveAttribute,
|
|
21
|
+
type DragUpdateEvent,
|
|
22
|
+
type SortableDropPlacement,
|
|
23
|
+
type SortablePreview,
|
|
22
24
|
} from "@mk-drag-and-drop/dom";
|
|
23
25
|
```
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
## Public Root API
|
|
30
|
-
|
|
31
|
-
The root export includes:
|
|
32
|
-
|
|
33
|
-
- `createDragController`
|
|
34
|
-
- `createDraggable`
|
|
35
|
-
- `createDroppable`
|
|
36
|
-
- `createDropContainer`
|
|
37
|
-
- `createSortable`
|
|
38
|
-
- `createDragHandle`
|
|
39
|
-
- controller types: `DragController`, `DragControllerOptions`,
|
|
40
|
-
`DragControllerOverlayInput`, `DragControllerAnnouncements`
|
|
41
|
-
- lifecycle types: `DragStartEvent`, `DragUpdateEvent`, `DragEndEvent`,
|
|
42
|
-
`DropEvent`, `DragSource`, `DragEndResult`, `DragLifecycleCallbacks`,
|
|
43
|
-
`DragLifecycleHelpers`
|
|
44
|
-
- placement types: `SortableDropPlacement`, `RemeasureDropTargetsInput`
|
|
45
|
-
- overlay types: `OverlayReleaseMode`
|
|
46
|
-
- targeting helpers and types: `pointerToCenter`, `centerToCenter`,
|
|
47
|
-
`pointerToRectDistance`, `getDistanceToRect`,
|
|
48
|
-
`maxPointerDistanceToRect`, `maxOverlayCenterDistanceToRect`, `DropTarget`,
|
|
49
|
-
`TargetingAlgorithm`, `TargetingAlgorithmInput`,
|
|
50
|
-
`TargetingConstraint`, `TargetingConstraintInput`
|
|
51
|
-
- modifiers and types: `lockToXAxis`, `lockToYAxis`, `restrictToContainer`,
|
|
52
|
-
`RestrictToContainerResolver`, `DragModifier`, `DragModifierInput`,
|
|
53
|
-
`DragModifierSetupInput`, `DragModifierTransformInput`
|
|
54
|
-
- input and geometry types: `KeyboardCommand`, `KeyboardConfiguration`,
|
|
55
|
-
`PointerConfiguration`, `DragPoint`, `DragRect`
|
|
56
|
-
|
|
57
|
-
Internal classes and helpers such as the underlying runtime class, registries,
|
|
58
|
-
input controllers, measurement helpers, sortable preview internals, and modifier
|
|
59
|
-
pipeline internals are not public root APIs.
|
|
60
|
-
|
|
61
|
-
## Integration Subpath
|
|
62
|
-
|
|
63
|
-
`@mk-drag-and-drop/dom/integration` exports:
|
|
64
|
-
|
|
65
|
-
- `createDragRuntimeScope`
|
|
66
|
-
- `DragRuntimeScope`, `DragRuntimeScopeOptions`,
|
|
67
|
-
`DragRuntimeScopeConfigureInput`
|
|
68
|
-
- `DragState`, `DragOverlayPhase`, `DragOverlayRenderState`,
|
|
69
|
-
`OverlayReleaseMode`
|
|
70
|
-
- `DragRuntimeSubscription`
|
|
71
|
-
- `createDomDraggable`, `createDomDroppable`, `createDomDropContainer`,
|
|
72
|
-
`createDomSortable`
|
|
73
|
-
- the related DOM behavior/input/runtime types
|
|
74
|
-
- `domDragHandleAttribute`
|
|
75
|
-
|
|
76
|
-
Use this subpath when building a framework adapter or custom behavior layer. For
|
|
77
|
-
ordinary DOM usage, prefer the root controller and binding helpers.
|
|
78
|
-
|
|
79
|
-
## Controller
|
|
80
|
-
|
|
81
|
-
`createDragController(options?)` creates the public DOM drag-and-drop scope used
|
|
82
|
-
by the root binding helpers. Controller options include:
|
|
83
|
-
|
|
84
|
-
- lifecycle callbacks: `onDragStart`, `onDragUpdate`, `onDragEnd`, `onDrop`
|
|
85
|
-
- `announcements`: lifecycle callbacks that return polite live-region text
|
|
86
|
-
- `dragOverlay(input)`: returns an overlay `HTMLElement` or `null`
|
|
87
|
-
- `overlayRoot`: optional root for the DOM overlay wrapper
|
|
88
|
-
- `overlayRelease`: `"auto"` removes overlays on drag end; `"manual"` keeps a
|
|
89
|
-
released overlay until its `removeOverlay` callback is called
|
|
90
|
-
- `targetingAlgorithm` and `targetingConstraint`
|
|
91
|
-
- `modifiers`
|
|
92
|
-
- `pointerConfiguration`
|
|
93
|
-
- `keyboardConfiguration`
|
|
94
|
-
|
|
95
|
-
The returned controller has:
|
|
96
|
-
|
|
97
|
-
- `remeasureDropTargets(input?)`: remeasures all targets, one target id, an
|
|
98
|
-
array of target ids, or `{ group }`
|
|
99
|
-
- `remeasureOverlay()`: remeasures the currently mounted drag overlay element;
|
|
100
|
-
it is a safe no-op when no overlay is mounted or no drag is active
|
|
101
|
-
- `recomputeActiveDrag()`: reruns active targeting and drag update from the
|
|
102
|
-
last pointer position; it is a safe no-op when no drag is active and does not
|
|
103
|
-
remeasure drop targets
|
|
104
|
-
|
|
105
|
-
The controller does not expose runtime teardown, disposal, update, or broad
|
|
106
|
-
overlay removal methods. Runtime objects are ordinary JavaScript objects; DOM
|
|
107
|
-
bindings and active drag resources are owned by their own lifetimes.
|
|
108
|
-
|
|
109
|
-
## Drag Overlays
|
|
110
|
-
|
|
111
|
-
`dragOverlay(input)` is a creation hook. The controller calls it when overlay
|
|
112
|
-
content is mounted for the `"dragging"` phase. By default, overlays are removed
|
|
113
|
-
immediately when a drag ends. When `overlayRelease: "manual"` is configured, the
|
|
114
|
-
controller calls `dragOverlay` once more for the `"released"` phase and passes
|
|
115
|
-
`removeOverlay`; call that function when app-owned release animation is done.
|
|
116
|
-
Pointer movement does not call `dragOverlay`.
|
|
117
|
-
|
|
118
|
-
The overlay input includes:
|
|
119
|
-
|
|
120
|
-
- `dragState`: item id, group, source rect, start pointer, and current pointer.
|
|
121
|
-
- `phase`: `"dragging"` or `"released"`.
|
|
122
|
-
- `remeasureOverlay`: manually refreshes the cached overlay measurement.
|
|
123
|
-
- `removeOverlay`: present only for `"released"` overlays in manual release
|
|
124
|
-
mode; call it when app-owned release animation should remove the overlay.
|
|
125
|
-
|
|
126
|
-
`removeOverlay` is only present for released overlays in manual release mode.
|
|
127
|
-
Calling it more than once is safe. It removes the overlay host state only; it is
|
|
128
|
-
not a controller or runtime teardown API.
|
|
129
|
-
|
|
130
|
-
Apps own the returned overlay element and any dynamic content inside it. The
|
|
131
|
-
package owns the overlay wrapper, movement, release, and geometry measurement
|
|
132
|
-
used by targeting and modifiers. The wrapper is positioned with `position:
|
|
133
|
-
fixed`, source rect dimensions, `pointer-events: none`, and a transform derived
|
|
134
|
-
from the current pointer delta.
|
|
135
|
-
|
|
136
|
-
The controller measures the returned overlay element on mount/replacement. When
|
|
137
|
-
`ResizeObserver` is available, it also remeasures when that element changes
|
|
138
|
-
size. The runtime derives the current overlay rect from that cached measurement
|
|
139
|
-
plus modified pointer movement, and it does not measure overlay DOM on every
|
|
140
|
-
pointer move.
|
|
141
|
-
|
|
142
|
-
Manual `remeasureOverlay()` is for cases automatic observation cannot cover or
|
|
143
|
-
where the app knows visual geometry changed. CSS transforms can change visual
|
|
144
|
-
bounds without necessarily triggering `ResizeObserver`.
|
|
145
|
-
|
|
146
|
-
```ts
|
|
147
|
-
controller.remeasureOverlay();
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
```ts
|
|
151
|
-
const controller = createDragController({
|
|
152
|
-
dragOverlay({ dragState, remeasureOverlay }) {
|
|
153
|
-
const element = document.createElement("div");
|
|
154
|
-
element.textContent = dragState.draggableId;
|
|
155
|
-
|
|
156
|
-
element.addEventListener("transitionend", () => {
|
|
157
|
-
remeasureOverlay();
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
return element;
|
|
161
|
-
},
|
|
162
|
-
});
|
|
163
|
-
```
|
|
27
|
+
`@mk-drag-and-drop/dom/integration` is the intentional adapter entry point. It
|
|
28
|
+
exports `createDragRuntimeScope`, DOM behavior factories, subscription and
|
|
29
|
+
active-reset types, and related behavior types. Registries, sortable
|
|
30
|
+
coordinators, and deep source/build paths are not public.
|
|
164
31
|
|
|
165
|
-
|
|
166
|
-
lifecycle callbacks or subscriptions:
|
|
32
|
+
## Controller and bindings
|
|
167
33
|
|
|
168
34
|
```ts
|
|
169
|
-
let overlayElement: HTMLElement | null = null;
|
|
170
|
-
|
|
171
35
|
const controller = createDragController({
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
return overlayElement;
|
|
36
|
+
pointerConfiguration: {
|
|
37
|
+
activationDelay: 180,
|
|
38
|
+
activationDistance: 6,
|
|
176
39
|
},
|
|
177
|
-
onDragUpdate(
|
|
178
|
-
|
|
179
|
-
overlayElement.textContent = `${pointerPosition.x}, ${pointerPosition.y}`;
|
|
180
|
-
}
|
|
40
|
+
onDragUpdate(event) {
|
|
41
|
+
routePreview(event.group, event.sortablePreview);
|
|
181
42
|
},
|
|
182
|
-
|
|
183
|
-
|
|
43
|
+
onDrop(event) {
|
|
44
|
+
commitDrop(event);
|
|
184
45
|
},
|
|
185
46
|
});
|
|
47
|
+
|
|
48
|
+
createSortable({
|
|
49
|
+
controller,
|
|
50
|
+
element,
|
|
51
|
+
draggableId: "row-1",
|
|
52
|
+
group: "table-rows",
|
|
53
|
+
axis: "vertical",
|
|
54
|
+
});
|
|
186
55
|
```
|
|
187
56
|
|
|
188
|
-
|
|
57
|
+
The public controller methods are:
|
|
189
58
|
|
|
190
|
-
|
|
59
|
+
- `cancelDrag()`
|
|
60
|
+
- `remeasureDropTargets(input?)`
|
|
61
|
+
- `remeasureOverlay()`
|
|
62
|
+
- `recomputeActiveDrag()`
|
|
191
63
|
|
|
192
|
-
|
|
64
|
+
Bindings return `void`; their connected-element registrations are scope owned.
|
|
65
|
+
Call `cancelDrag()` before a Vanilla owner removes its registered DOM. It is an
|
|
66
|
+
idle-safe no-op, cancels pending activation or an active pointer/keyboard drag,
|
|
67
|
+
restores sortable preview DOM, and never produces a drop.
|
|
68
|
+
|
|
69
|
+
## Lifecycle types
|
|
193
70
|
|
|
194
71
|
```ts
|
|
195
|
-
type
|
|
72
|
+
export type SortablePreview =
|
|
73
|
+
| { status: "inactive" }
|
|
74
|
+
| { status: "source"; placement: SortableDropPlacement }
|
|
75
|
+
| { status: "projected"; placement: SortableDropPlacement };
|
|
76
|
+
|
|
77
|
+
export type DragStartEvent = {
|
|
196
78
|
draggableId: string;
|
|
79
|
+
group: string;
|
|
197
80
|
source: DragSource;
|
|
198
81
|
pointerPosition: DragPoint;
|
|
199
82
|
sourceRect: DragRect;
|
|
200
83
|
};
|
|
201
84
|
|
|
202
|
-
type DragUpdateEvent = {
|
|
85
|
+
export type DragUpdateEvent = {
|
|
203
86
|
draggableId: string;
|
|
87
|
+
group: string;
|
|
204
88
|
source: DragSource;
|
|
205
89
|
pointerPosition: DragPoint;
|
|
206
90
|
overlayRect: DragRect | null;
|
|
207
91
|
activeDropTargetId: string | null;
|
|
208
92
|
previousDropTargetId: string | null;
|
|
93
|
+
sortablePreview: SortablePreview;
|
|
209
94
|
};
|
|
210
95
|
|
|
211
|
-
type
|
|
212
|
-
|
|
213
|
-
type DragEndResult =
|
|
214
|
-
| "dropped"
|
|
215
|
-
| "no-target"
|
|
216
|
-
| "invalid-target"
|
|
217
|
-
| "canceled";
|
|
218
|
-
|
|
219
|
-
type DragEndEvent = {
|
|
96
|
+
export type DragEndEvent = {
|
|
220
97
|
draggableId: string;
|
|
98
|
+
group: string;
|
|
221
99
|
source: DragSource;
|
|
222
100
|
result: DragEndResult;
|
|
223
|
-
dropTargetId: string | null;
|
|
224
101
|
overlayRect: DragRect | null;
|
|
102
|
+
dropTargetId: string | null;
|
|
225
103
|
};
|
|
226
104
|
|
|
227
|
-
type DropEvent = {
|
|
105
|
+
export type DropEvent = {
|
|
228
106
|
draggableId: string;
|
|
107
|
+
group: string;
|
|
229
108
|
source: DragSource;
|
|
230
109
|
dropTargetId: string;
|
|
231
110
|
sortablePlacement?: SortableDropPlacement;
|
|
232
111
|
};
|
|
233
112
|
|
|
234
|
-
type
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
nextDraggableId: string | null;
|
|
239
|
-
targetDraggableId: string | null;
|
|
240
|
-
side: "before" | "after" | null;
|
|
113
|
+
export type ActiveDragResetEvent = {
|
|
114
|
+
draggableId: string;
|
|
115
|
+
group: string;
|
|
116
|
+
source: DragSource;
|
|
241
117
|
};
|
|
242
118
|
```
|
|
243
119
|
|
|
244
|
-
`
|
|
245
|
-
|
|
246
|
-
at the moment the drag ends. Both are `null` when no drag overlay is configured.
|
|
247
|
-
When an overlay is configured, the runtime uses cached overlay measurement plus
|
|
248
|
-
modified pointer movement. Before overlay content has been measured, it uses the
|
|
249
|
-
same translated source-rect fallback as the visual overlay. The drag update and
|
|
250
|
-
drag end paths do not read overlay DOM just to produce this field.
|
|
251
|
-
|
|
252
|
-
Helpers:
|
|
253
|
-
|
|
254
|
-
- `getDropTargetRect(dropTargetId)`
|
|
255
|
-
|
|
256
|
-
`onDrop` only runs for a valid successful drop. `onDragEnd` runs whenever a drag
|
|
257
|
-
ends. `result: "dropped"` means a valid target was accepted and `onDrop` also
|
|
258
|
-
runs. `result: "no-target"` means the user ended normally with no active target.
|
|
259
|
-
`result: "invalid-target"` means an active target candidate was stale by drag
|
|
260
|
-
end. `result: "canceled"` means the drag was canceled, such as Escape,
|
|
261
|
-
pointercancel, or runtime cancellation.
|
|
120
|
+
`DragSource` is `"pointer" | "keyboard"`. `DragEndResult` is `"dropped" |
|
|
121
|
+
"no-target" | "invalid-target" | "canceled"`.
|
|
262
122
|
|
|
263
|
-
|
|
123
|
+
The group is captured when activation succeeds and remains stable through every
|
|
124
|
+
event and announcement. Re-registering the source with another group may
|
|
125
|
+
invalidate it, but cannot change the session group.
|
|
264
126
|
|
|
265
|
-
|
|
266
|
-
Registrations are WeakRef-backed and stale disconnected elements are pruned on
|
|
267
|
-
drag-critical paths and explicit remeasurement.
|
|
127
|
+
## Live sortable preview
|
|
268
128
|
|
|
269
|
-
`
|
|
129
|
+
`sortablePreview` is included in every normal drag update.
|
|
270
130
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
131
|
+
| Status | Exact meaning |
|
|
132
|
+
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
133
|
+
| `inactive` | The source is a plain draggable or is no longer a connected, valid sortable registration in the captured group. It does not merely mean there is no active target. |
|
|
134
|
+
| `source` | The normalized package-owned preview equals the source container and sibling anchors captured at activation. This includes initial position, no target before movement, valid no-op, and a return to the original location. |
|
|
135
|
+
| `projected` | The normalized preview differs from source. This includes same- or cross-container movement, container-only placement, and a moved preview retained after the pointer leaves valid targets. |
|
|
275
136
|
|
|
276
|
-
|
|
137
|
+
A source placement describes the captured source container and adjacent sortable
|
|
138
|
+
siblings; `targetDraggableId` and `side` are `null`. Container-only projected
|
|
139
|
+
placement also has null target/side while its container and neighboring anchors
|
|
140
|
+
describe the location. Siblings from other groups and disconnected or replaced
|
|
141
|
+
registrations are excluded.
|
|
277
142
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
- `dropTargetId`
|
|
281
|
-
- `group`, defaulting to `"default"`
|
|
282
|
-
- `containerId`, defaulting to `null`
|
|
283
|
-
|
|
284
|
-
`createDropContainer(input)` accepts:
|
|
285
|
-
|
|
286
|
-
- `controller`
|
|
287
|
-
- `element`
|
|
288
|
-
- `containerId`
|
|
289
|
-
- `group`, defaulting to `"default"`
|
|
290
|
-
|
|
291
|
-
`createSortable(input)` accepts:
|
|
292
|
-
|
|
293
|
-
- `controller`
|
|
294
|
-
- `element`
|
|
295
|
-
- `draggableId`
|
|
296
|
-
- `group`, defaulting to `"default"`
|
|
297
|
-
- `containerId`, defaulting to `null`
|
|
298
|
-
|
|
299
|
-
`createDragHandle({ element })` marks an element with the drag-handle data
|
|
300
|
-
attribute. When a draggable or sortable contains a handle, pointer and keyboard
|
|
301
|
-
dragging are handled from that handle instead of unrelated children.
|
|
302
|
-
|
|
303
|
-
## Placement Data
|
|
304
|
-
|
|
305
|
-
Plain droppable drops are id-only: `draggableId`, `dropTargetId`, and `source`.
|
|
306
|
-
Sortable behavior may add sortable-specific placement under
|
|
307
|
-
`event.sortablePlacement`:
|
|
143
|
+
`SortableDropPlacement` is the sole placement payload for both live and final
|
|
144
|
+
placement:
|
|
308
145
|
|
|
309
146
|
```ts
|
|
310
147
|
type SortableDropPlacement = {
|
|
@@ -317,274 +154,191 @@ type SortableDropPlacement = {
|
|
|
317
154
|
};
|
|
318
155
|
```
|
|
319
156
|
|
|
320
|
-
|
|
321
|
-
may omit `sortablePlacement`.
|
|
322
|
-
`targetDraggableId` and `side` preserve the active sortable item anchor when a
|
|
323
|
-
drop lands relative to an item. They are `null` for container-only placements.
|
|
324
|
-
|
|
325
|
-
Sortable behavior owns only a transient DOM preview during the drag. It may move
|
|
326
|
-
DOM nodes to show where an item would land, but it does not commit app data. On
|
|
327
|
-
drop, the app reads `event.sortablePlacement` and updates its own data/rendering.
|
|
328
|
-
|
|
329
|
-
## Targeting
|
|
157
|
+
## Sequencing and completion
|
|
330
158
|
|
|
331
|
-
|
|
159
|
+
The public update guarantee is:
|
|
332
160
|
|
|
333
|
-
```
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
161
|
+
```text
|
|
162
|
+
measure/select target
|
|
163
|
+
-> update package-owned sortable preview
|
|
164
|
+
-> normalize current preview
|
|
165
|
+
-> publish DragUpdateEvent
|
|
338
166
|
```
|
|
339
167
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
- `centerToCenter`
|
|
344
|
-
- `pointerToRectDistance`
|
|
345
|
-
|
|
346
|
-
Built-in targeting helpers/constraints:
|
|
347
|
-
|
|
348
|
-
- `getDistanceToRect(point, rect)`
|
|
349
|
-
- `maxPointerDistanceToRect({ maxDistance?, maxXDistance?, maxYDistance? })`
|
|
350
|
-
- `maxOverlayCenterDistanceToRect({ maxDistance?, maxXDistance?, maxYDistance? })`
|
|
351
|
-
|
|
352
|
-
Custom `TargetingAlgorithm` functions receive `pointerPosition`, `overlayRect`,
|
|
353
|
-
and the filtered `dropTargets` list. Algorithms decide which geometry to use:
|
|
354
|
-
`pointerToCenter` and `pointerToRectDistance` are pointer based, while
|
|
355
|
-
`centerToCenter` computes the overlay center from `overlayRect` and returns no
|
|
356
|
-
target when no overlay rect is available. Custom `TargetingConstraint`
|
|
357
|
-
functions receive `pointerPosition`, `overlayRect`, and one `dropTarget`, and
|
|
358
|
-
return whether that candidate is eligible. Use the pointer or overlay-center
|
|
359
|
-
distance helper that matches the intended behavior.
|
|
360
|
-
|
|
361
|
-
Sortable placement is separate from targeting. The configured targeting
|
|
362
|
-
algorithm alone chooses `activeDropTarget`; sortable only decides before/after
|
|
363
|
-
preview placement relative to that active target. For sortable groups, the
|
|
364
|
-
registry may narrow measured candidates to relevant sortable item, neighbor, or
|
|
365
|
-
container entries before the targeting algorithm runs; the algorithm still makes
|
|
366
|
-
the active-target decision from that narrowed measured list.
|
|
367
|
-
|
|
368
|
-
Same-container sortable preview keeps its movement-responsive first-placement
|
|
369
|
-
behavior: forward movement places after a newly active target, backward movement
|
|
370
|
-
places before, and no sortable-axis movement uses the target midpoint. When a
|
|
371
|
-
sortable item first enters a different DOM container, the initial side is based
|
|
372
|
-
on the current sortable placement position against the target midpoint on the
|
|
373
|
-
sortable axis: above or left of midpoint places before, while below or right of
|
|
374
|
-
midpoint places after. Pointer-based targeting uses the pointer as that position;
|
|
375
|
-
rect-based targeting such as `centerToCenter` uses the overlay center.
|
|
376
|
-
|
|
377
|
-
A container-only preview keeps that cross-container entry state through active
|
|
378
|
-
recomputes until the first item target in the destination list receives a
|
|
379
|
-
before/after placement.
|
|
380
|
-
|
|
381
|
-
`placementBoundary` is used after a preview placement already has an established
|
|
382
|
-
movement direction to control same-target reversal and hysteresis thresholds. It
|
|
383
|
-
does not decide the initial side for cross-container sortable entry or delay the
|
|
384
|
-
first movement that follows midpoint-based entry into a list.
|
|
385
|
-
|
|
386
|
-
## Modifiers
|
|
387
|
-
|
|
388
|
-
Modifiers transform pointer movement before drag state updates. Built-ins:
|
|
389
|
-
|
|
390
|
-
- `lockToXAxis()`
|
|
391
|
-
- `lockToYAxis()`
|
|
392
|
-
- `restrictToContainer(resolver)`
|
|
393
|
-
|
|
394
|
-
`restrictToContainer` receives a resolver function with
|
|
395
|
-
`DragModifierSetupInput` and returns an `HTMLElement | null`. The resolver runs
|
|
396
|
-
at drag setup to choose the bounds element; the selected element's rect is
|
|
397
|
-
remeasured during movement so growing or shrinking containers keep constraining
|
|
398
|
-
against current bounds.
|
|
399
|
-
|
|
400
|
-
Custom modifiers can provide:
|
|
401
|
-
|
|
402
|
-
- `setup(input)`: optional setup state for the active drag
|
|
403
|
-
- `transform(input)`: returns the next `DragPoint`
|
|
404
|
-
|
|
405
|
-
Modifier input includes `draggableId`, `group`, source rect, initial pointer
|
|
406
|
-
position, raw pointer position, current pointer position, overlay rect, and
|
|
407
|
-
modifier state.
|
|
408
|
-
|
|
409
|
-
## Measurement
|
|
410
|
-
|
|
411
|
-
Targets are measured when registered and remeasured at drag start. The explicit
|
|
412
|
-
target remeasurement entry point is `controller.remeasureDropTargets(input?)`;
|
|
413
|
-
call it when app-owned layout changes during a drag should affect targeting.
|
|
414
|
-
|
|
415
|
-
Overlay measurement is separate from target measurement. Overlay content is
|
|
416
|
-
measured on mount/replacement and by `ResizeObserver` when available. Use
|
|
417
|
-
`controller.remeasureOverlay()` when the current overlay geometry should be
|
|
418
|
-
refreshed manually, such as after an app-owned transition or transform that
|
|
419
|
-
changes visual bounds without a normal size-observer notification.
|
|
420
|
-
|
|
421
|
-
`RemeasureDropTargetsInput` accepts:
|
|
422
|
-
|
|
423
|
-
- no argument for all targets
|
|
424
|
-
- a target id string
|
|
425
|
-
- an array of target id strings
|
|
426
|
-
- `{ group: string }`
|
|
427
|
-
|
|
428
|
-
Sortable preview movement does not automatically remeasure a group. Targeting
|
|
429
|
-
continues from the runtime's cached target rects until the app explicitly
|
|
430
|
-
remeasures. Final sortable placement is derived from the current preview DOM
|
|
431
|
-
order, so app-owned layout changes such as expanding tree rows or changing
|
|
432
|
-
grouped sections during a drag should call the public remeasure API when those
|
|
433
|
-
changes need to affect targeting.
|
|
434
|
-
|
|
435
|
-
## Active Drag Recompute
|
|
436
|
-
|
|
437
|
-
`controller.recomputeActiveDrag()` reruns the active drag update path from the
|
|
438
|
-
last stored raw pointer position. It reapplies modifiers, derives the overlay
|
|
439
|
-
rect from cached overlay measurement, recomputes the active drop target from
|
|
440
|
-
cached target measurements, updates the overlay host, and sends the normal drag
|
|
441
|
-
update notifications. This is the same update path used by pointer movement.
|
|
442
|
-
Calling it with no active drag is safe and does nothing.
|
|
443
|
-
|
|
444
|
-
Use it when something external changes what is under the pointer during an
|
|
445
|
-
active drag without producing a new pointer event.
|
|
168
|
+
Public callbacks and adapter subscriptions receive a completed event after the
|
|
169
|
+
DOM mutation. Consumers must not inspect carrier DOM to reconstruct placement
|
|
170
|
+
or depend on subscription insertion order.
|
|
446
171
|
|
|
447
|
-
|
|
448
|
-
|
|
172
|
+
The drop guarantee is:
|
|
173
|
+
|
|
174
|
+
```text
|
|
175
|
+
flush final pointer update
|
|
176
|
+
-> validate target
|
|
177
|
+
-> capture normalized placement
|
|
178
|
+
-> restore package preview DOM
|
|
179
|
+
-> publish onDragEnd
|
|
180
|
+
-> publish onDrop for a valid drop
|
|
449
181
|
```
|
|
450
182
|
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
183
|
+
Live and drop placement use the same normalizer, so a projected drop with no
|
|
184
|
+
intervening update equals the last projected placement. A successful source/no-
|
|
185
|
+
op drop omits `sortablePlacement`. Package-owned carrier DOM is restored before
|
|
186
|
+
public end/drop handlers. Cancellation emits no artificial final update;
|
|
187
|
+
`onDragEnd` ends application projection. An invalid target yields
|
|
188
|
+
`invalid-target` and no `onDrop`, and its disconnected id is not retained as a
|
|
189
|
+
normalized anchor.
|
|
454
190
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
191
|
+
## Applying placement
|
|
192
|
+
|
|
193
|
+
Apply the package decision to application ids; do not calculate geometry,
|
|
194
|
+
midpoints, direction, hysteresis, or candidates:
|
|
459
195
|
|
|
460
196
|
```ts
|
|
461
|
-
|
|
462
|
-
|
|
197
|
+
function getProjectedOrder(
|
|
198
|
+
canonicalOrder: readonly string[],
|
|
199
|
+
event: DragUpdateEvent,
|
|
200
|
+
): readonly string[] {
|
|
201
|
+
if (event.sortablePreview.status === "inactive") return canonicalOrder;
|
|
202
|
+
if (event.sortablePreview.status === "source") return canonicalOrder;
|
|
203
|
+
|
|
204
|
+
return applySortablePlacement(
|
|
205
|
+
canonicalOrder,
|
|
206
|
+
event.draggableId,
|
|
207
|
+
event.sortablePreview.placement,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
463
210
|
```
|
|
464
211
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
Commit data in lifecycle callbacks, usually `onDrop`. Rerender or patch the DOM
|
|
471
|
-
using your app's normal rendering path after the commit.
|
|
212
|
+
Canonical state remains unchanged during updates. Commit it once from
|
|
213
|
+
`onDrop`. Since `onDragEnd` runs first, keep a successful projection until
|
|
214
|
+
`onDrop` commits; clear it immediately for cancellation, no-target, and invalid
|
|
215
|
+
target.
|
|
472
216
|
|
|
473
|
-
##
|
|
217
|
+
## Groups
|
|
474
218
|
|
|
475
|
-
|
|
476
|
-
objects are ordinary JavaScript objects and are released by garbage collection
|
|
477
|
-
when unreachable.
|
|
219
|
+
Groups isolate target interaction and route lifecycle work:
|
|
478
220
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
221
|
+
```ts
|
|
222
|
+
const controller = createDragController({
|
|
223
|
+
onDragUpdate(event) {
|
|
224
|
+
switch (event.group) {
|
|
225
|
+
case "document-blocks":
|
|
226
|
+
break;
|
|
227
|
+
case "table-rows":
|
|
228
|
+
break;
|
|
229
|
+
case "table-columns":
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
```
|
|
483
235
|
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
pointer position are canceled by pointerup, pointercancel, activation success,
|
|
487
|
-
activation failure, or active drag reset.
|
|
236
|
+
Use one controller when those domains belong to one scope. Do not encode the
|
|
237
|
+
domain in `draggableId`.
|
|
488
238
|
|
|
489
|
-
|
|
490
|
-
pointer updates, text-selection suppression, active target/session state,
|
|
491
|
-
overlay host state, active modifiers, and sortable preview/snapshot state. They
|
|
492
|
-
are released by active drag lifecycle paths. Runtime disposal is not the cleanup
|
|
493
|
-
mechanism for those resources.
|
|
239
|
+
## Remeasure and recompute
|
|
494
240
|
|
|
495
|
-
|
|
496
|
-
Registered sortable elements, ids, groups, containers, and options remain until
|
|
497
|
-
they are unregistered or pruned as stale DOM. Per-drag sortable snapshots,
|
|
498
|
-
dragged attributes, preview placement, and moved DOM are restored by active drag
|
|
499
|
-
cleanup.
|
|
241
|
+
After scrolling or application layout changes:
|
|
500
242
|
|
|
501
|
-
|
|
243
|
+
```ts
|
|
244
|
+
controller.remeasureDropTargets({ group: "table-columns" });
|
|
245
|
+
controller.recomputeActiveDrag();
|
|
246
|
+
```
|
|
502
247
|
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
248
|
+
Remeasurement refreshes cached geometry and publishes nothing. Recompute reuses
|
|
249
|
+
the last raw pointer position, reapplies modifiers, and runs the same target,
|
|
250
|
+
preview, normalization, and publication path. It does not implicitly
|
|
251
|
+
remeasure. Schedule after visible projection reaches the DOM, compare orders,
|
|
252
|
+
and deduplicate the scheduled work to prevent loops.
|
|
253
|
+
|
|
254
|
+
## Pointer and keyboard handles
|
|
255
|
+
|
|
256
|
+
`activationDelay` and `activationDistance` use OR semantics: either threshold
|
|
257
|
+
activates. Quick release before activation remains an ordinary click. After an
|
|
258
|
+
activated pointer drag, the browser click belonging to that interaction and
|
|
259
|
+
targeting the initiating draggable subtree is suppressed once; later unrelated,
|
|
260
|
+
programmatic, and keyboard clicks remain available.
|
|
261
|
+
|
|
262
|
+
`createDragHandle({ element })` marks a descendant handle. A button may be a
|
|
263
|
+
pointer handle, but Space and Enter are reserved for its button/menu behavior;
|
|
264
|
+
interactive-element protection prevents those keys from starting a drag. Use a
|
|
265
|
+
noninteractive or separately focusable handle, or explicit commands, for
|
|
266
|
+
keyboard sorting. Editable descendants remain protected.
|
|
267
|
+
|
|
268
|
+
### Active pointer cursor state
|
|
269
|
+
|
|
270
|
+
When a pointer drag activates, the runtime sets the stable
|
|
271
|
+
`data-dnd-pointer-drag-active="true"` contract on the initiating document's
|
|
272
|
+
root element. Pending delay or distance activation and keyboard dragging do not
|
|
273
|
+
set it. The marker is removed when the actual drag ends, before lifecycle
|
|
274
|
+
completion and before any manual overlay release animation finishes.
|
|
275
|
+
|
|
276
|
+
The package does not choose or inject a cursor. Applications can opt into one:
|
|
277
|
+
|
|
278
|
+
```css
|
|
279
|
+
:root[data-dnd-pointer-drag-active="true"],
|
|
280
|
+
:root[data-dnd-pointer-drag-active="true"] *,
|
|
281
|
+
:root[data-dnd-pointer-drag-active="true"] *::before,
|
|
282
|
+
:root[data-dnd-pointer-drag-active="true"] *::after {
|
|
283
|
+
cursor: grabbing !important;
|
|
284
|
+
}
|
|
285
|
+
```
|
|
506
286
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
287
|
+
The descendant selectors and `!important` override explicit application
|
|
288
|
+
cursors such as `text`, `pointer`, or `col-resize`. Add equivalent rules inside
|
|
289
|
+
shadow roots when shadow content declares its own cursor. Each iframe document
|
|
290
|
+
also needs its own consumer stylesheet. The exported
|
|
291
|
+
`domPointerDragActiveAttribute` constant contains the supported attribute name
|
|
292
|
+
for JavaScript and CSS-in-JS integrations.
|
|
510
293
|
|
|
511
|
-
|
|
512
|
-
polite live region only when announcement callbacks are provided. The package
|
|
513
|
-
does not claim complete screen-reader drag-and-drop support; apps should provide
|
|
514
|
-
domain-specific instructions and announcements for their interaction model.
|
|
515
|
-
The lifecycle `onDragUpdate` callback runs for drag updates; the announcement
|
|
516
|
-
`onDragUpdate` callback is intended for active-drop-target transitions and
|
|
517
|
-
dedupes repeated messages to avoid live-region spam. Use lifecycle
|
|
518
|
-
`onDragUpdate` for per-frame side effects, not announcements.
|
|
294
|
+
## Targeting and overlays
|
|
519
295
|
|
|
520
|
-
|
|
296
|
+
Pointer-based targeting works without an overlay. `centerToCenter` and other
|
|
297
|
+
rect-based targeting need a measurable overlay rectangle. A configured overlay
|
|
298
|
+
callback receives `dragState.group`, enabling real blank elements sized for row
|
|
299
|
+
or column carriers:
|
|
521
300
|
|
|
522
301
|
```ts
|
|
523
|
-
import {
|
|
524
|
-
createDragController,
|
|
525
|
-
createDraggable,
|
|
526
|
-
createDroppable,
|
|
527
|
-
} from "@mk-drag-and-drop/dom";
|
|
528
|
-
|
|
529
|
-
const state = {
|
|
530
|
-
itemLocation: "todo",
|
|
531
|
-
};
|
|
532
|
-
|
|
533
302
|
const controller = createDragController({
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
303
|
+
dragOverlay({ dragState }) {
|
|
304
|
+
const overlay = document.createElement("div");
|
|
305
|
+
overlay.className =
|
|
306
|
+
dragState.group === "table-rows" ? "rowOverlay" : "columnOverlay";
|
|
307
|
+
return overlay;
|
|
537
308
|
},
|
|
538
309
|
});
|
|
539
|
-
|
|
540
|
-
const item = document.createElement("button");
|
|
541
|
-
item.type = "button";
|
|
542
|
-
item.textContent = "Drag me";
|
|
543
|
-
|
|
544
|
-
createDraggable({
|
|
545
|
-
controller,
|
|
546
|
-
element: item,
|
|
547
|
-
draggableId: "task-1",
|
|
548
|
-
});
|
|
549
|
-
|
|
550
|
-
for (const columnId of ["todo", "done"]) {
|
|
551
|
-
const column = document.createElement("section");
|
|
552
|
-
|
|
553
|
-
createDroppable({
|
|
554
|
-
controller,
|
|
555
|
-
element: column,
|
|
556
|
-
dropTargetId: columnId,
|
|
557
|
-
});
|
|
558
|
-
|
|
559
|
-
document.body.append(column);
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
document.body.append(item);
|
|
563
310
|
```
|
|
564
311
|
|
|
565
|
-
|
|
312
|
+
The wrapper is pointer-inert. The source rect comes from the registered carrier,
|
|
313
|
+
not a thin descendant handle, and is translated until overlay measurement is
|
|
314
|
+
available. Returning `null` from a configured callback is not a measurable blank
|
|
315
|
+
overlay. Sortable behavior itself does not require any overlay.
|
|
566
316
|
|
|
567
|
-
|
|
317
|
+
Built-in targeting algorithms are `pointerToCenter`, `pointerToRectDistance`,
|
|
318
|
+
and `centerToCenter`. Sortable `placementBoundary` controls same-target reversal
|
|
319
|
+
hysteresis; it does not select the active target.
|
|
568
320
|
|
|
569
|
-
|
|
570
|
-
- `dropzoneList.ts`
|
|
571
|
-
- `sortableList.ts`
|
|
572
|
-
- `kanbanExample.ts`
|
|
573
|
-
- `groupedExample.ts`
|
|
574
|
-
- `treeExample.ts`
|
|
321
|
+
## Integration entry point
|
|
575
322
|
|
|
576
|
-
|
|
323
|
+
Adapter code may import:
|
|
577
324
|
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
325
|
+
```ts
|
|
326
|
+
import {
|
|
327
|
+
createDragRuntimeScope,
|
|
328
|
+
createDomSortable,
|
|
329
|
+
type ActiveDragResetEvent,
|
|
330
|
+
type DragRuntimeSubscription,
|
|
331
|
+
} from "@mk-drag-and-drop/dom/integration";
|
|
584
332
|
```
|
|
585
333
|
|
|
586
|
-
|
|
334
|
+
Subscribers observe completed lifecycle events and are not responsible for
|
|
335
|
+
sortable processing. The integration entry point does not expose mutable
|
|
336
|
+
registries or the internal sortable coordinator.
|
|
587
337
|
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
338
|
+
## Carrier table
|
|
339
|
+
|
|
340
|
+
The complete carrier architecture and runnable Vanilla/React examples are
|
|
341
|
+
documented in the repository carrier-table guide. Carriers stay connected,
|
|
342
|
+
contain no table content, match logical row/column geometry, and reflow as
|
|
343
|
+
normal-flow children of absolute overlay lanes. `sortablePreview` drives the
|
|
344
|
+
visible table; `sortablePlacement` performs one canonical drop commit.
|