Open History to see the last commit
1import { type PointerEvent as ReactPointerEvent } from "react";
2import { type SensorProps } from "@dnd-kit/core";
3import { getOwnerDocument, getWindow } from "@dnd-kit/utilities";
4
5type Options = {
6 distance: number;
7 onAttach: (sensor: SidebarPointerSensor) => void;
8 onFinish: (started: boolean) => void;
9 /** Return true to claim the move and hide this position from sorting. */
10 onMove?: (point: { x: number; y: number }) => boolean;
11 /** Return true to consume the release and cancel sorting. */
12 onDrop?: (point: { x: number; y: number }) => boolean;
13};
14
15export class SidebarPointerSensor {
16 static activators = [
17 {
18 eventName: "onPointerDown" as const,
19 handler: ({ nativeEvent }: ReactPointerEvent) =>
20 nativeEvent.isPrimary && nativeEvent.button === 0,
21 },
22 ];
23 autoScrollEnabled = true;
24 private phase: "pending" | "dragging" | "finished" = "pending";
25 private readonly pointer: PointerEvent;
26 private readonly document: Document;
27 private readonly window: Window;
28
29 constructor(private readonly props: Omit<SensorProps<Options>, "activeNode" | "context">) {
30 this.pointer = props.event as PointerEvent;
31 this.document = getOwnerDocument(this.pointer.target);
32 this.window = getWindow(this.pointer.target);
33 this.document.addEventListener("pointermove", this.move, { passive: false, capture: true });
34 this.document.addEventListener("pointerup", this.end, { capture: true });
35 this.document.addEventListener("pointercancel", this.pointerCancel, { capture: true });
36 this.document.addEventListener("keydown", this.keydown, { capture: true });
37 this.document.addEventListener("visibilitychange", this.visibilityChange);
38 this.window.addEventListener("blur", this.cancel);
39 this.window.addEventListener("pagehide", this.cancel);
40 this.window.addEventListener("resize", this.cancel);
41 this.document.addEventListener("dragstart", this.preventDefault);
42 this.document.addEventListener("contextmenu", this.preventDefault);
43 props.options.onAttach(this);
44 props.onPending(props.active, { distance: props.options.distance }, this.coordinates());
45 }
46
47 private coordinates = () => ({ x: this.pointer.clientX, y: this.pointer.clientY });
48 private preventDefault = (event: Event) => event.preventDefault();
49 private clearClickSuppression = () => {
50 this.document.removeEventListener("click", this.suppressClick, { capture: true });
51 this.document.removeEventListener("pointerdown", this.clearClickSuppression, { capture: true });
52 };
53 private suppressClick = (event: Event) => {
54 event.stopPropagation();
55 this.clearClickSuppression();
56 };
57 private clearSelection = () => this.document.getSelection()?.removeAllRanges();
58
59 private move = (event: PointerEvent) => {
60 if (this.phase === "finished" || event.pointerId !== this.pointer.pointerId) return;
61 const coordinates = { x: event.clientX, y: event.clientY };
62 // Desktop may report buttons=0 before pointerup; drop active context drags before cancelling sort.
63 if ((event.buttons & 1) === 0) {
64 if (this.phase === "dragging") this.props.options.onDrop?.(coordinates);
65 return this.cancel();
66 }
67 if (this.phase === "pending") {
68 const offset = {
69 x: event.clientX - this.pointer.clientX,
70 y: event.clientY - this.pointer.clientY,
71 };
72 if (Math.hypot(offset.x, offset.y) <= this.props.options.distance) {
73 this.props.onPending(
74 this.props.active,
75 { distance: this.props.options.distance },
76 this.coordinates(),
77 offset,
78 );
79 return;
80 }
81 this.phase = "dragging";
82 this.document.addEventListener("click", this.suppressClick, { capture: true });
83 this.document.addEventListener("selectionchange", this.clearSelection);
84 this.clearSelection();
85 this.props.onStart(this.coordinates());
86 }
87 if (this.phase === "dragging") {
88 if (event.cancelable) event.preventDefault();
89 if (this.props.options.onMove?.(coordinates) === true) return;
90 this.props.onMove(coordinates);
91 }
92 };
93
94 private end = (event: PointerEvent) => {
95 if (event.pointerId !== this.pointer.pointerId) return;
96 const dropped =
97 this.phase === "dragging" &&
98 this.props.options.onDrop?.({ x: event.clientX, y: event.clientY }) === true;
99 this.finish(dropped);
100 };
101 private pointerCancel = (event: PointerEvent) => {
102 if (event.pointerId === this.pointer.pointerId) this.cancel();
103 };
104 private keydown = (event: KeyboardEvent) => {
105 if (event.code === "Escape") this.cancel();
106 };
107 private visibilityChange = () => {
108 if (this.document.hidden) this.cancel();
109 };
110 cancel = () => this.finish(true);
111
112 private finish(cancelled: boolean) {
113 if (this.phase === "finished") return;
114 const aborted = this.phase === "pending";
115 this.phase = "finished";
116 this.document.removeEventListener("pointermove", this.move, { capture: true });
117 this.document.removeEventListener("pointerup", this.end, { capture: true });
118 this.document.removeEventListener("pointercancel", this.pointerCancel, { capture: true });
119 this.document.removeEventListener("keydown", this.keydown, { capture: true });
120 this.document.removeEventListener("visibilitychange", this.visibilityChange);
121 this.window.removeEventListener("blur", this.cancel);
122 this.window.removeEventListener("pagehide", this.cancel);
123 this.window.removeEventListener("resize", this.cancel);
124 this.document.removeEventListener("dragstart", this.preventDefault);
125 this.document.removeEventListener("contextmenu", this.preventDefault);
126 this.document.removeEventListener("selectionchange", this.clearSelection);
127 // Suppress the eventual click after cancellation; a fresh pointerdown clears suppression after
128 // out-of-document release.
129 if (!aborted || cancelled) {
130 this.document.addEventListener("click", this.suppressClick, { capture: true });
131 this.document.addEventListener("pointerdown", this.clearClickSuppression, { capture: true });
132 }
133 try {
134 // Release the preview before dnd-kit clears transforms; end/cancel events may be omitted before first
135 // layout.
136 this.props.options.onFinish(!aborted);
137 } finally {
138 if (aborted) this.props.onAbort(this.props.active);
139 if (cancelled) this.props.onCancel();
140 else this.props.onEnd();
141 }
142 }
143}