@mk-drag-and-drop/dom 0.5.2 → 0.5.4

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 CHANGED
@@ -278,19 +278,28 @@ draggable surface only when that surface should own touch movement. `none`
278
278
  prevents ordinary page scrolling from gestures that begin there. An
279
279
  axis-specific value can preserve orthogonal native panning, but a vertical drag
280
280
  and vertical page scroll cannot own the same gesture simultaneously. Scrolling
281
- outside dedicated handles remains unaffected.
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.
282
283
 
283
284
  `activationDelay` and `activationDistance` use OR semantics: either threshold
284
- activates. Quick release before activation remains an ordinary click. After an
285
- activated pointer drag, the browser click belonging to that interaction and
286
- targeting the initiating draggable subtree is suppressed once; later unrelated,
287
- programmatic, and keyboard clicks remain available.
288
-
289
- `createDragHandle({ element })` marks a descendant handle. A button may be a
290
- pointer handle, but Space and Enter are reserved for its button/menu behavior;
291
- interactive-element protection prevents those keys from starting a drag. Use a
292
- noninteractive or separately focusable handle, or explicit commands, for
293
- 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.
294
303
 
295
304
  ### Active pointer cursor state
296
305
 
@@ -1,4 +1,4 @@
1
- import { getPointerCaptureTarget, shouldHandleKeyboardDragFromTarget, } 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,24 +6,27 @@ export function createDomDraggable(input) {
6
6
  if (!element) {
7
7
  return;
8
8
  }
9
- const pointerCaptureTarget = getPointerCaptureTarget({
9
+ const dragStartTarget = getPointerDragStartTarget({
10
10
  draggableElement: element,
11
11
  eventTarget: event.target,
12
12
  });
13
- if (!pointerCaptureTarget) {
13
+ if (!dragStartTarget) {
14
14
  return;
15
15
  }
16
16
  if (event.isPrimary === false ||
17
17
  (event.button !== undefined && event.button !== 0)) {
18
18
  return;
19
19
  }
20
- event.preventDefault();
20
+ // A canceled pointerdown can prevent a touch tap from clicking the link.
21
+ if (!dragStartTarget.preservePointerDownDefault) {
22
+ event.preventDefault();
23
+ }
21
24
  event.stopPropagation();
22
25
  input.runtime.requestDragStart({
23
26
  draggableId: input.draggableId,
24
27
  group: input.group,
25
28
  element,
26
- pointerCaptureTarget,
29
+ pointerCaptureTarget: dragStartTarget.captureTarget,
27
30
  pointerId: event.pointerId,
28
31
  pointerType: event.pointerType,
29
32
  pointerPosition: {
@@ -4,10 +4,13 @@ export declare function shouldStartDragFromTarget(input: {
4
4
  draggableElement: HTMLElement;
5
5
  eventTarget: EventTarget | null;
6
6
  }): boolean;
7
- export declare function getPointerCaptureTarget(input: {
7
+ export declare function getPointerDragStartTarget(input: {
8
8
  draggableElement: HTMLElement;
9
9
  eventTarget: EventTarget | null;
10
- }): Element | null;
10
+ }): {
11
+ captureTarget: Element;
12
+ preservePointerDownDefault: boolean;
13
+ } | null;
11
14
  export declare function shouldHandleKeyboardDragFromTarget(input: {
12
15
  draggableElement: HTMLElement;
13
16
  eventTarget: EventTarget | null;
@@ -1,37 +1,45 @@
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) {
15
- return getPointerCaptureTarget(input) !== null;
18
+ return getPointerDragStartTarget(input) !== null;
16
19
  }
17
- export function getPointerCaptureTarget(input) {
20
+ export function getPointerDragStartTarget(input) {
18
21
  const eventTarget = getEventTargetElement(input.eventTarget);
19
22
  if (!eventTarget) {
20
23
  return null;
21
24
  }
22
25
  const hasDragHandle = input.draggableElement.querySelector(domDragHandleSelector) !== null;
23
26
  if (!hasDragHandle) {
24
- return isInteractiveDragTarget({
27
+ if (isInteractiveDragTarget({
25
28
  draggableElement: input.draggableElement,
26
29
  eventTarget,
27
- })
28
- ? null
29
- : input.draggableElement;
30
+ selector: pointerInteractiveElementSelector,
31
+ })) {
32
+ return null;
33
+ }
34
+ return {
35
+ captureTarget: input.draggableElement,
36
+ preservePointerDownDefault: closestWithin(eventTarget, linkElementSelector, input.draggableElement) !== null,
37
+ };
30
38
  }
31
39
  const closestDragHandle = eventTarget.closest(domDragHandleSelector);
32
40
  return closestDragHandle !== null &&
33
41
  input.draggableElement.contains(closestDragHandle)
34
- ? closestDragHandle
42
+ ? { captureTarget: closestDragHandle, preservePointerDownDefault: false }
35
43
  : null;
36
44
  }
37
45
  export function shouldHandleKeyboardDragFromTarget(input) {
@@ -42,6 +50,7 @@ export function shouldHandleKeyboardDragFromTarget(input) {
42
50
  if (isInteractiveDragTarget({
43
51
  draggableElement: input.draggableElement,
44
52
  eventTarget,
53
+ selector: keyboardInteractiveElementSelector,
45
54
  })) {
46
55
  return false;
47
56
  }
@@ -56,7 +65,8 @@ function getEventTargetElement(target) {
56
65
  return target instanceof Element ? target : null;
57
66
  }
58
67
  function isInteractiveDragTarget(input) {
59
- return (closestWithin(input.eventTarget, interactiveElementSelector, input.draggableElement) !== null ||
68
+ return (closestWithin(input.eventTarget, input.selector, input.draggableElement) !==
69
+ null ||
60
70
  getEditableElement(input.eventTarget, input.draggableElement) !== null);
61
71
  }
62
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,6 +4,7 @@ import { defaultKeyboardConfiguration, normalizeKeyboardConfiguration, normalize
4
4
  import { KeyboardDragController, } from "../input/keyboard-drag.js";
5
5
  import { globalPointerListenerCapture } from "../input/global-pointer-listeners.js";
6
6
  import { acquireDocumentSelectionSuppression } from "../input/document-selection-state.js";
7
+ import { suppressNativeDragStart } from "../input/native-drag-suppression.js";
7
8
  import { PointerActivationController } from "../input/pointer-activation.js";
8
9
  import { acquireDocumentPointerDragState } from "../input/pointer-drag-state.js";
9
10
  import { applyDragModifiers, createActiveDragModifiers, } from "../modifiers/pipeline.js";
@@ -50,7 +51,14 @@ export class DragRuntime {
50
51
  pointerActivation = new PointerActivationController({
51
52
  getConfiguration: () => this.pointerConfiguration,
52
53
  isDragging: () => this.isDragging,
53
- acquirePendingResources: (request) => acquireDocumentSelectionSuppression(request.element.ownerDocument),
54
+ acquirePendingResources: ({ element }) => {
55
+ const restoreTextSelection = acquireDocumentSelectionSuppression(element.ownerDocument);
56
+ const releaseNativeDragSuppression = suppressNativeDragStart(element);
57
+ return () => {
58
+ releaseNativeDragSuppression();
59
+ restoreTextSelection();
60
+ };
61
+ },
54
62
  startImmediately: (request) => {
55
63
  if (!request.element.isConnected) {
56
64
  return;
@@ -676,6 +684,7 @@ export class DragRuntime {
676
684
  bindPointerWindowListeners(pointerId, initiatingElement, pointerCaptureTarget) {
677
685
  this.releaseActiveInputListeners?.();
678
686
  const targetWindow = initiatingElement.ownerDocument.defaultView ?? window;
687
+ const releaseNativeDragSuppression = suppressNativeDragStart(initiatingElement);
679
688
  let listenersReleased = false;
680
689
  const ensurePointerCapture = () => {
681
690
  if (!pointerCaptureTarget.isConnected ||
@@ -749,6 +758,7 @@ export class DragRuntime {
749
758
  if (this.ensureActivePointerCapture === ensurePointerCapture) {
750
759
  this.ensureActivePointerCapture = null;
751
760
  }
761
+ releaseNativeDragSuppression();
752
762
  releasePointerCapture();
753
763
  targetWindow.removeEventListener("pointermove", handlePointerMove, globalPointerListenerCapture);
754
764
  targetWindow.removeEventListener("pointerup", handlePointerUp, globalPointerListenerCapture);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mk-drag-and-drop/dom",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
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",
@@ -42,14 +42,6 @@
42
42
  "dist"
43
43
  ],
44
44
  "sideEffects": false,
45
- "devDependencies": {
46
- "eslint": "^8.57.0",
47
- "jsdom": "^24.1.3",
48
- "typescript": "5.5.4",
49
- "vitest": "^2.1.9",
50
- "@repo/eslint-config": "0.0.0",
51
- "@repo/typescript-config": "0.0.0"
52
- },
53
45
  "scripts": {
54
46
  "clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"",
55
47
  "build": "pnpm run clean && tsc -p tsconfig.json",
@@ -57,5 +49,13 @@
57
49
  "lint": "eslint \"src/**/*.ts\"",
58
50
  "test": "vitest run",
59
51
  "test:watch": "vitest"
52
+ },
53
+ "devDependencies": {
54
+ "@repo/eslint-config": "workspace:*",
55
+ "@repo/typescript-config": "workspace:*",
56
+ "eslint": "^8.57.0",
57
+ "jsdom": "^24.1.3",
58
+ "typescript": "5.5.4",
59
+ "vitest": "^2.1.9"
60
60
  }
61
- }
61
+ }