@mk-drag-and-drop/dom 0.5.1 → 0.5.3

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Matthias Kramer
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Matthias Kramer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -253,17 +253,53 @@ and deduplicate the scheduled work to prevent loops.
253
253
 
254
254
  ## Pointer and keyboard handles
255
255
 
256
+ ### Mobile pointer ownership
257
+
258
+ Dedicated drag handles must declare touch gesture ownership before a gesture
259
+ begins. `createDragHandle` adds the stable `data-dnd-drag-handle` attribute;
260
+ apply the following CSS in every document or shadow root that renders handles:
261
+
262
+ ```css
263
+ [data-dnd-drag-handle] {
264
+ touch-action: none;
265
+ user-select: none;
266
+ -webkit-user-select: none;
267
+ }
268
+ ```
269
+
270
+ `touch-action` cannot be applied from `pointerdown`: the browser decides native
271
+ pan/zoom ownership before the handler runs. The runtime uses pointer capture
272
+ after activation and suppresses standard and WebKit text selection during both
273
+ pending and active pointer input, but neither mechanism replaces the CSS
274
+ contract.
275
+
276
+ For a draggable without a dedicated handle, apply `touch-action` to the
277
+ draggable surface only when that surface should own touch movement. `none`
278
+ prevents ordinary page scrolling from gestures that begin there. An
279
+ axis-specific value can preserve orthogonal native panning, but a vertical drag
280
+ and vertical page scroll cannot own the same gesture simultaneously. Scrolling
281
+ outside dedicated handles remains unaffected. If the draggable is an inline
282
+ anchor, use a display value such as `inline-block` so `touch-action` applies.
283
+
256
284
  `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.
285
+ activates. Without a dedicated handle, pointer drags can start on links
286
+ (`a[href]` and `role="link"`), including when the draggable is itself a link.
287
+ Buttons, inputs, selects, textareas, options, elements with `role="button"` or
288
+ `role="textbox"`, and content-editable elements are protected from pointer drag
289
+ starts. When a draggable has a dedicated handle, pointer drags start only from
290
+ that handle.
291
+
292
+ Native browser dragging is suppressed in the draggable subtree while pointer
293
+ activation is pending or active. A quick release before activation follows the
294
+ link as usual. After activation, the generated click on the initiating draggable
295
+ subtree is suppressed. Later unrelated, programmatic, and keyboard clicks remain
296
+ available.
297
+
298
+ Keyboard drag starts protect links as well as the controls and editable elements
299
+ listed above. Enter on a focused link follows it. A button marked with
300
+ `createDragHandle({ element })` can start a pointer drag and still uses Space and
301
+ Enter for its button/menu action. Use a noninteractive or separately focusable
302
+ handle, or explicit commands, for keyboard sorting.
267
303
 
268
304
  ### Active pointer cursor state
269
305
 
@@ -4,7 +4,9 @@ export type DomDraggableRuntime = {
4
4
  draggableId: string;
5
5
  group: string;
6
6
  element: HTMLElement;
7
+ pointerCaptureTarget?: Element;
7
8
  pointerId: number;
9
+ pointerType?: string;
8
10
  pointerPosition: DragPoint;
9
11
  }) => void;
10
12
  isKeyboardDragEnabled: () => boolean;
@@ -27,6 +29,7 @@ export type DomDraggablePointerDownEvent = {
27
29
  preventDefault: () => void;
28
30
  stopPropagation: () => void;
29
31
  pointerId: number;
32
+ pointerType?: string;
30
33
  button?: number;
31
34
  isPrimary?: boolean;
32
35
  clientX: number;
@@ -1,4 +1,4 @@
1
- import { shouldHandleKeyboardDragFromTarget, shouldStartDragFromTarget, } from "../input/drag-handle.js";
1
+ import { getPointerDragStartTarget, shouldHandleKeyboardDragFromTarget, } from "../input/drag-handle.js";
2
2
  export function createDomDraggable(input) {
3
3
  return {
4
4
  onPointerDown: (event) => {
@@ -6,23 +6,29 @@ export function createDomDraggable(input) {
6
6
  if (!element) {
7
7
  return;
8
8
  }
9
- if (!shouldStartDragFromTarget({
9
+ const dragStartTarget = getPointerDragStartTarget({
10
10
  draggableElement: element,
11
11
  eventTarget: event.target,
12
- })) {
12
+ });
13
+ if (!dragStartTarget) {
13
14
  return;
14
15
  }
15
16
  if (event.isPrimary === false ||
16
17
  (event.button !== undefined && event.button !== 0)) {
17
18
  return;
18
19
  }
19
- event.preventDefault();
20
+ // A canceled pointerdown can prevent a touch tap from clicking the link.
21
+ if (!dragStartTarget.preservePointerDownDefault) {
22
+ event.preventDefault();
23
+ }
20
24
  event.stopPropagation();
21
25
  input.runtime.requestDragStart({
22
26
  draggableId: input.draggableId,
23
27
  group: input.group,
24
28
  element,
29
+ pointerCaptureTarget: dragStartTarget.captureTarget,
25
30
  pointerId: event.pointerId,
31
+ pointerType: event.pointerType,
26
32
  pointerPosition: {
27
33
  x: event.clientX,
28
34
  y: event.clientY,
@@ -50,7 +56,7 @@ export function createDomDraggable(input) {
50
56
  event.preventDefault();
51
57
  event.stopPropagation();
52
58
  },
53
- tabIndex: (input.keyboardDragEnabled ?? input.runtime.isKeyboardDragEnabled())
59
+ tabIndex: input.keyboardDragEnabled ?? input.runtime.isKeyboardDragEnabled()
54
60
  ? 0
55
61
  : undefined,
56
62
  };
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export { createDropContainer, type CreateDropContainerInput, } from "./droppable
5
5
  export { createSortable, type CreateSortableInput, } from "./sortable/create-sortable-binding.js";
6
6
  export type { SortableAxis, SortablePlacementBoundary, } from "./sortable/sortable-options.js";
7
7
  export { createDragHandle, type CreateDragHandleInput, } from "./input/create-drag-handle.js";
8
+ export { domDragHandleAttribute } from "./input/drag-handle.js";
8
9
  export { domPointerDragActiveAttribute } from "./input/pointer-drag-state.js";
9
10
  export type { DragEndResult, DragEndEvent, DragLifecycleCallbacks, DragLifecycleHelpers, DragSource, DragStartEvent, DragUpdateEvent, DropEvent, ActiveDragResetEvent, SortablePreview, } from "./runtime/lifecycle.js";
10
11
  export type { RemeasureDropTargetsInput, SortableDropPlacement, } from "./runtime/drop-target-registry.js";
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ export { createDroppable, } from "./droppable/create-droppable-binding.js";
4
4
  export { createDropContainer, } from "./droppable/create-drop-container-binding.js";
5
5
  export { createSortable, } from "./sortable/create-sortable-binding.js";
6
6
  export { createDragHandle, } from "./input/create-drag-handle.js";
7
+ export { domDragHandleAttribute } from "./input/drag-handle.js";
7
8
  export { domPointerDragActiveAttribute } from "./input/pointer-drag-state.js";
8
9
  export { centerToCenter, pointerToCenter, pointerToRectDistance, } from "./targeting/algorithms.js";
9
10
  export { getDistanceToRect, maxOverlayCenterDistanceToRect, maxPointerDistanceToRect, } from "./targeting/constraints.js";
@@ -0,0 +1 @@
1
+ export declare function acquireDocumentSelectionSuppression(sourceDocument: Document): () => void;
@@ -0,0 +1,89 @@
1
+ const selectionPropertyNames = ["user-select", "-webkit-user-select"];
2
+ const documentSelectionStates = new WeakMap();
3
+ export function acquireDocumentSelectionSuppression(sourceDocument) {
4
+ const owner = {};
5
+ let state = documentSelectionStates.get(sourceDocument);
6
+ if (!state) {
7
+ const elements = [
8
+ sourceDocument.documentElement,
9
+ ...(sourceDocument.body ? [sourceDocument.body] : []),
10
+ ];
11
+ const snapshots = elements.map(createElementSelectionSnapshot);
12
+ const handleSelectStart = (event) => {
13
+ event.preventDefault();
14
+ };
15
+ state = {
16
+ owners: new Set(),
17
+ snapshots,
18
+ handleSelectStart,
19
+ };
20
+ documentSelectionStates.set(sourceDocument, state);
21
+ for (const element of elements) {
22
+ for (const propertyName of selectionPropertyNames) {
23
+ setSelectionProperty(element.style, propertyName, "none");
24
+ }
25
+ }
26
+ sourceDocument.addEventListener("selectstart", handleSelectStart, true);
27
+ }
28
+ state.owners.add(owner);
29
+ let released = false;
30
+ return () => {
31
+ if (released) {
32
+ return;
33
+ }
34
+ released = true;
35
+ if (!state?.owners.delete(owner) || state.owners.size > 0) {
36
+ return;
37
+ }
38
+ documentSelectionStates.delete(sourceDocument);
39
+ sourceDocument.removeEventListener("selectstart", state.handleSelectStart, true);
40
+ for (const snapshot of state.snapshots) {
41
+ restoreElementSelectionSnapshot(snapshot);
42
+ }
43
+ };
44
+ }
45
+ function createElementSelectionSnapshot(element) {
46
+ return {
47
+ element,
48
+ properties: selectionPropertyNames.map((name) => ({
49
+ name,
50
+ present: hasInlineProperty(element.style, name) ||
51
+ getSelectionProperty(element.style, name) !== "",
52
+ value: getSelectionProperty(element.style, name),
53
+ priority: element.style.getPropertyPriority(name),
54
+ })),
55
+ };
56
+ }
57
+ function restoreElementSelectionSnapshot(snapshot) {
58
+ for (const property of snapshot.properties) {
59
+ if (property.present) {
60
+ setSelectionProperty(snapshot.element.style, property.name, property.value, property.priority);
61
+ }
62
+ else {
63
+ snapshot.element.style.removeProperty(property.name);
64
+ if (property.name === "-webkit-user-select") {
65
+ snapshot.element.style.webkitUserSelect = "";
66
+ }
67
+ }
68
+ }
69
+ }
70
+ function getSelectionProperty(style, name) {
71
+ const cssValue = style.getPropertyValue(name);
72
+ return name === "-webkit-user-select" && cssValue === ""
73
+ ? style.webkitUserSelect ?? ""
74
+ : cssValue;
75
+ }
76
+ function setSelectionProperty(style, name, value, priority = "") {
77
+ style.setProperty(name, value, priority);
78
+ if (name === "-webkit-user-select" && style.webkitUserSelect !== value) {
79
+ style.webkitUserSelect = value;
80
+ }
81
+ }
82
+ function hasInlineProperty(style, name) {
83
+ for (let index = 0; index < style.length; index += 1) {
84
+ if (style.item(index) === name) {
85
+ return true;
86
+ }
87
+ }
88
+ return false;
89
+ }
@@ -4,6 +4,13 @@ export declare function shouldStartDragFromTarget(input: {
4
4
  draggableElement: HTMLElement;
5
5
  eventTarget: EventTarget | null;
6
6
  }): boolean;
7
+ export declare function getPointerDragStartTarget(input: {
8
+ draggableElement: HTMLElement;
9
+ eventTarget: EventTarget | null;
10
+ }): {
11
+ captureTarget: Element;
12
+ preservePointerDownDefault: boolean;
13
+ } | null;
7
14
  export declare function shouldHandleKeyboardDragFromTarget(input: {
8
15
  draggableElement: HTMLElement;
9
16
  eventTarget: EventTarget | null;
@@ -1,31 +1,46 @@
1
1
  export const domDragHandleAttribute = "data-dnd-drag-handle";
2
2
  export const domDragHandleSelector = `[${domDragHandleAttribute}]`;
3
- const interactiveElementSelector = [
4
- "a[href]",
3
+ const pointerInteractiveElementSelector = [
5
4
  "button",
6
5
  "input",
7
6
  "option",
8
7
  "select",
9
8
  "textarea",
10
9
  "[role='button']",
11
- "[role='link']",
12
10
  "[role='textbox']",
13
11
  ].join(",");
12
+ const linkElementSelector = "a[href],[role='link']";
13
+ const keyboardInteractiveElementSelector = [
14
+ pointerInteractiveElementSelector,
15
+ linkElementSelector,
16
+ ].join(",");
14
17
  export function shouldStartDragFromTarget(input) {
18
+ return getPointerDragStartTarget(input) !== null;
19
+ }
20
+ export function getPointerDragStartTarget(input) {
15
21
  const eventTarget = getEventTargetElement(input.eventTarget);
16
22
  if (!eventTarget) {
17
- return false;
23
+ return null;
18
24
  }
19
25
  const hasDragHandle = input.draggableElement.querySelector(domDragHandleSelector) !== null;
20
26
  if (!hasDragHandle) {
21
- return !isInteractiveDragTarget({
27
+ if (isInteractiveDragTarget({
22
28
  draggableElement: input.draggableElement,
23
29
  eventTarget,
24
- });
30
+ selector: pointerInteractiveElementSelector,
31
+ })) {
32
+ return null;
33
+ }
34
+ return {
35
+ captureTarget: input.draggableElement,
36
+ preservePointerDownDefault: closestWithin(eventTarget, linkElementSelector, input.draggableElement) !== null,
37
+ };
25
38
  }
26
39
  const closestDragHandle = eventTarget.closest(domDragHandleSelector);
27
- return (closestDragHandle !== null &&
28
- input.draggableElement.contains(closestDragHandle));
40
+ return closestDragHandle !== null &&
41
+ input.draggableElement.contains(closestDragHandle)
42
+ ? { captureTarget: closestDragHandle, preservePointerDownDefault: false }
43
+ : null;
29
44
  }
30
45
  export function shouldHandleKeyboardDragFromTarget(input) {
31
46
  const eventTarget = getEventTargetElement(input.eventTarget);
@@ -35,6 +50,7 @@ export function shouldHandleKeyboardDragFromTarget(input) {
35
50
  if (isInteractiveDragTarget({
36
51
  draggableElement: input.draggableElement,
37
52
  eventTarget,
53
+ selector: keyboardInteractiveElementSelector,
38
54
  })) {
39
55
  return false;
40
56
  }
@@ -49,7 +65,8 @@ function getEventTargetElement(target) {
49
65
  return target instanceof Element ? target : null;
50
66
  }
51
67
  function isInteractiveDragTarget(input) {
52
- return (closestWithin(input.eventTarget, interactiveElementSelector, input.draggableElement) !== null ||
68
+ return (closestWithin(input.eventTarget, input.selector, input.draggableElement) !==
69
+ null ||
53
70
  getEditableElement(input.eventTarget, input.draggableElement) !== null);
54
71
  }
55
72
  function closestWithin(element, selector, boundary) {
@@ -0,0 +1 @@
1
+ export declare function suppressNativeDragStart(element: HTMLElement): () => void;
@@ -0,0 +1,5 @@
1
+ export function suppressNativeDragStart(element) {
2
+ const preventNativeDrag = (event) => event.preventDefault();
3
+ element.addEventListener("dragstart", preventNativeDrag, true);
4
+ return () => element.removeEventListener("dragstart", preventNativeDrag, true);
5
+ }
@@ -4,20 +4,25 @@ export type PointerDragActivationRequest = {
4
4
  draggableId: string;
5
5
  group: string;
6
6
  element: HTMLElement;
7
+ pointerCaptureTarget?: Element;
7
8
  pointerId: number;
9
+ pointerType?: string;
8
10
  pointerPosition: DragPoint;
9
11
  };
10
12
  export type ActivatedPointerDrag = {
11
13
  draggableId: string;
12
14
  group: string;
13
15
  element: HTMLElement;
16
+ pointerCaptureTarget: Element;
14
17
  pointerId: number;
18
+ pointerType: string;
15
19
  initialPointerPosition: DragPoint;
16
20
  latestPointerPosition: DragPoint;
17
21
  };
18
22
  export type PointerActivationControllerOptions = {
19
23
  getConfiguration: () => NormalizedPointerConfiguration;
20
24
  isDragging: () => boolean;
25
+ acquirePendingResources?: (request: PointerDragActivationRequest) => () => void;
21
26
  startImmediately: (request: PointerDragActivationRequest) => void;
22
27
  activate: (activation: ActivatedPointerDrag) => void;
23
28
  };
@@ -24,12 +24,15 @@ export class PointerActivationController {
24
24
  draggableId: request.draggableId,
25
25
  group: request.group,
26
26
  element: request.element,
27
+ pointerCaptureTarget: request.pointerCaptureTarget ?? request.element,
27
28
  pointerId: request.pointerId,
29
+ pointerType: request.pointerType ?? "",
28
30
  initialPointerPosition,
29
31
  latestPointerPosition: initialPointerPosition,
30
32
  targetWindow,
31
33
  activationTimerId: null,
32
34
  releasePendingPointerListeners: null,
35
+ releasePendingResources: this.options.acquirePendingResources?.(request) ?? null,
33
36
  };
34
37
  const handlePointerMove = (event) => {
35
38
  if (event.pointerId !== pendingActivation.pointerId) {
@@ -87,6 +90,9 @@ export class PointerActivationController {
87
90
  const releasePendingPointerListeners = pendingActivation.releasePendingPointerListeners;
88
91
  pendingActivation.releasePendingPointerListeners = null;
89
92
  releasePendingPointerListeners?.();
93
+ const releasePendingResources = pendingActivation.releasePendingResources;
94
+ pendingActivation.releasePendingResources = null;
95
+ releasePendingResources?.();
90
96
  }
91
97
  clearPendingActivationTimer(pendingActivation) {
92
98
  if (pendingActivation.activationTimerId === null) {
@@ -99,14 +105,23 @@ export class PointerActivationController {
99
105
  if (this.pendingActivation !== pendingActivation) {
100
106
  return;
101
107
  }
108
+ const releasePendingResources = pendingActivation.releasePendingResources;
109
+ pendingActivation.releasePendingResources = null;
102
110
  this.clearPendingActivation(pendingActivation);
103
- this.options.activate({
104
- draggableId: pendingActivation.draggableId,
105
- group: pendingActivation.group,
106
- element: pendingActivation.element,
107
- pointerId: pendingActivation.pointerId,
108
- initialPointerPosition: pendingActivation.initialPointerPosition,
109
- latestPointerPosition: pendingActivation.latestPointerPosition,
110
- });
111
+ try {
112
+ this.options.activate({
113
+ draggableId: pendingActivation.draggableId,
114
+ group: pendingActivation.group,
115
+ element: pendingActivation.element,
116
+ pointerCaptureTarget: pendingActivation.pointerCaptureTarget,
117
+ pointerId: pendingActivation.pointerId,
118
+ pointerType: pendingActivation.pointerType,
119
+ initialPointerPosition: pendingActivation.initialPointerPosition,
120
+ latestPointerPosition: pendingActivation.latestPointerPosition,
121
+ });
122
+ }
123
+ finally {
124
+ releasePendingResources?.();
125
+ }
111
126
  }
112
127
  }
@@ -17,6 +17,7 @@ export declare class DragRuntime {
17
17
  private modifiers;
18
18
  private activeDragModifiers;
19
19
  private releaseActiveInputListeners;
20
+ private ensureActivePointerCapture;
20
21
  private restoreTextSelectionSuppression;
21
22
  private releasePointerDragState;
22
23
  private releasePostDragClickSuppression;
@@ -3,6 +3,8 @@ import { getOverlayMeasurement, getOverlayRect, } from "../geometry/overlay.js";
3
3
  import { defaultKeyboardConfiguration, normalizeKeyboardConfiguration, normalizePointerConfiguration, } from "../input/config.js";
4
4
  import { KeyboardDragController, } from "../input/keyboard-drag.js";
5
5
  import { globalPointerListenerCapture } from "../input/global-pointer-listeners.js";
6
+ import { acquireDocumentSelectionSuppression } from "../input/document-selection-state.js";
7
+ import { suppressNativeDragStart } from "../input/native-drag-suppression.js";
6
8
  import { PointerActivationController } from "../input/pointer-activation.js";
7
9
  import { acquireDocumentPointerDragState } from "../input/pointer-drag-state.js";
8
10
  import { applyDragModifiers, createActiveDragModifiers, } from "../modifiers/pipeline.js";
@@ -20,6 +22,7 @@ export class DragRuntime {
20
22
  modifiers = [];
21
23
  activeDragModifiers = [];
22
24
  releaseActiveInputListeners = null;
25
+ ensureActivePointerCapture = null;
23
26
  restoreTextSelectionSuppression = null;
24
27
  releasePointerDragState = null;
25
28
  releasePostDragClickSuppression = null;
@@ -48,6 +51,14 @@ export class DragRuntime {
48
51
  pointerActivation = new PointerActivationController({
49
52
  getConfiguration: () => this.pointerConfiguration,
50
53
  isDragging: () => this.isDragging,
54
+ acquirePendingResources: ({ element }) => {
55
+ const restoreTextSelection = acquireDocumentSelectionSuppression(element.ownerDocument);
56
+ const releaseNativeDragSuppression = suppressNativeDragStart(element);
57
+ return () => {
58
+ releaseNativeDragSuppression();
59
+ restoreTextSelection();
60
+ };
61
+ },
51
62
  startImmediately: (request) => {
52
63
  if (!request.element.isConnected) {
53
64
  return;
@@ -57,6 +68,8 @@ export class DragRuntime {
57
68
  group: request.group,
58
69
  inputType: "pointer",
59
70
  pointerId: request.pointerId,
71
+ pointerCaptureTarget: request.pointerCaptureTarget ?? request.element,
72
+ pointerType: request.pointerType,
60
73
  element: request.element,
61
74
  pointerPosition: request.pointerPosition,
62
75
  sourceRect: measureDomElement(request.element),
@@ -71,6 +84,8 @@ export class DragRuntime {
71
84
  group: activation.group,
72
85
  inputType: "pointer",
73
86
  pointerId: activation.pointerId,
87
+ pointerCaptureTarget: activation.pointerCaptureTarget,
88
+ pointerType: activation.pointerType,
74
89
  element: activation.element,
75
90
  pointerPosition: activation.initialPointerPosition,
76
91
  sourceRect: measureDomElement(activation.element),
@@ -248,6 +263,7 @@ export class DragRuntime {
248
263
  placementPosition: sortablePlacementPosition,
249
264
  movementPosition: rawPointerPosition,
250
265
  });
266
+ this.ensureActivePointerCapture?.();
251
267
  const sortablePreview = this.getSortablePreview(nextSession);
252
268
  const completedSession = {
253
269
  ...nextSession,
@@ -528,7 +544,7 @@ export class DragRuntime {
528
544
  });
529
545
  this.suppressTextSelection(input.element.ownerDocument);
530
546
  if (input.inputType === "pointer") {
531
- this.bindPointerWindowListeners(input.pointerId, input.element);
547
+ this.bindPointerWindowListeners(input.pointerId, input.element, input.pointerCaptureTarget ?? input.element);
532
548
  }
533
549
  else {
534
550
  this.bindKeyboardWindowListeners(this.activeDragWindow);
@@ -665,10 +681,41 @@ export class DragRuntime {
665
681
  }
666
682
  this.clearOverlayHost();
667
683
  }
668
- bindPointerWindowListeners(pointerId, initiatingElement) {
684
+ bindPointerWindowListeners(pointerId, initiatingElement, pointerCaptureTarget) {
669
685
  this.releaseActiveInputListeners?.();
670
686
  const targetWindow = initiatingElement.ownerDocument.defaultView ?? window;
687
+ const releaseNativeDragSuppression = suppressNativeDragStart(initiatingElement);
671
688
  let listenersReleased = false;
689
+ const ensurePointerCapture = () => {
690
+ if (!pointerCaptureTarget.isConnected ||
691
+ typeof pointerCaptureTarget.setPointerCapture !== "function") {
692
+ return;
693
+ }
694
+ try {
695
+ if (typeof pointerCaptureTarget.hasPointerCapture === "function" &&
696
+ pointerCaptureTarget.hasPointerCapture(pointerId)) {
697
+ return;
698
+ }
699
+ pointerCaptureTarget.setPointerCapture(pointerId);
700
+ }
701
+ catch {
702
+ // Window listeners remain the fallback when capture is unavailable.
703
+ }
704
+ };
705
+ const releasePointerCapture = () => {
706
+ if (typeof pointerCaptureTarget.releasePointerCapture !== "function") {
707
+ return;
708
+ }
709
+ try {
710
+ if (typeof pointerCaptureTarget.hasPointerCapture !== "function" ||
711
+ pointerCaptureTarget.hasPointerCapture(pointerId)) {
712
+ pointerCaptureTarget.releasePointerCapture(pointerId);
713
+ }
714
+ }
715
+ catch {
716
+ // Pointerup/cancel may have already released capture implicitly.
717
+ }
718
+ };
672
719
  const handlePointerMove = (event) => {
673
720
  if (event.pointerId !== pointerId) {
674
721
  return;
@@ -701,11 +748,18 @@ export class DragRuntime {
701
748
  targetWindow.addEventListener("pointermove", handlePointerMove, globalPointerListenerCapture);
702
749
  targetWindow.addEventListener("pointerup", handlePointerUp, globalPointerListenerCapture);
703
750
  targetWindow.addEventListener("pointercancel", handlePointerCancel, globalPointerListenerCapture);
751
+ this.ensureActivePointerCapture = ensurePointerCapture;
752
+ ensurePointerCapture();
704
753
  this.releaseActiveInputListeners = () => {
705
754
  if (listenersReleased) {
706
755
  return;
707
756
  }
708
757
  listenersReleased = true;
758
+ if (this.ensureActivePointerCapture === ensurePointerCapture) {
759
+ this.ensureActivePointerCapture = null;
760
+ }
761
+ releaseNativeDragSuppression();
762
+ releasePointerCapture();
709
763
  targetWindow.removeEventListener("pointermove", handlePointerMove, globalPointerListenerCapture);
710
764
  targetWindow.removeEventListener("pointerup", handlePointerUp, globalPointerListenerCapture);
711
765
  targetWindow.removeEventListener("pointercancel", handlePointerCancel, globalPointerListenerCapture);
@@ -751,20 +805,8 @@ export class DragRuntime {
751
805
  }
752
806
  suppressTextSelection(sourceDocument) {
753
807
  this.restoreTextSelectionSuppression?.();
754
- const root = sourceDocument.documentElement;
755
- const body = sourceDocument.body;
756
- const previousRootUserSelect = root.style.userSelect;
757
- const previousBodyUserSelect = body?.style.userSelect;
758
- root.style.userSelect = "none";
759
- if (body) {
760
- body.style.userSelect = "none";
761
- }
762
- this.restoreTextSelectionSuppression = () => {
763
- root.style.userSelect = previousRootUserSelect;
764
- if (body) {
765
- body.style.userSelect = previousBodyUserSelect ?? "";
766
- }
767
- };
808
+ this.restoreTextSelectionSuppression =
809
+ acquireDocumentSelectionSuppression(sourceDocument);
768
810
  }
769
811
  resetActiveDragState() {
770
812
  this.cancelQueuedPointerUpdate();
@@ -775,6 +817,7 @@ export class DragRuntime {
775
817
  releaseActiveInputResources() {
776
818
  const releaseActiveInputListeners = this.releaseActiveInputListeners;
777
819
  this.releaseActiveInputListeners = null;
820
+ this.ensureActivePointerCapture = null;
778
821
  const restoreTextSelectionSuppression = this.restoreTextSelectionSuppression;
779
822
  this.restoreTextSelectionSuppression = null;
780
823
  const releasePointerDragState = this.releasePointerDragState;
@@ -43,6 +43,8 @@ type BaseStartDragInput = {
43
43
  export type StartDragInput = (BaseStartDragInput & {
44
44
  inputType: "pointer";
45
45
  pointerId: number;
46
+ pointerCaptureTarget?: Element;
47
+ pointerType?: string;
46
48
  }) | (BaseStartDragInput & {
47
49
  inputType: "keyboard";
48
50
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mk-drag-and-drop/dom",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "Headless DOM drag-and-drop primitives for draggable, droppable, sortable, and grouped interactions.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://mkramer.dev",
@@ -47,8 +47,8 @@
47
47
  "jsdom": "^24.1.3",
48
48
  "typescript": "5.5.4",
49
49
  "vitest": "^2.1.9",
50
- "@repo/typescript-config": "0.0.0",
51
- "@repo/eslint-config": "0.0.0"
50
+ "@repo/eslint-config": "0.0.0",
51
+ "@repo/typescript-config": "0.0.0"
52
52
  },
53
53
  "scripts": {
54
54
  "clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"",