Open History to see the last commit
1import { useLayoutEffect, type PointerEvent as ReactPointerEvent } from "react";
2import { type SensorProps } from "@dnd-kit/core";
3import { getOwnerDocument, getWindow } from "@dnd-kit/utilities";
4
5// Search unmounts the drag context while its owning Sidebar remains mounted.
6export function SidebarDragLifecycle({ onUnmount }: { onUnmount: () => void }) {
7 useLayoutEffect(() => onUnmount, [onUnmount]);
8 return null;
9}
10
11type Options = {
12 distance: number;
13 onAttach: (sensor: SidebarPointerSensor) => void;
14 onFinish: (started: boolean) => void;
15 /** Return true to claim the move: the sort gesture then ignores this pointer position. */
16 onMove?: (point: { x: number; y: number }) => boolean;
17 /** Return true when the release was consumed elsewhere, so the sort is cancelled. */
18 onDrop?: (point: { x: number; y: number }) => boolean;
19};
20
21/** A sidebar gesture ends on release, cancellation, or loss of its window.
22 * Own the listeners so unmounting the list can cancel the sensor too. */
23export class SidebarPointerSensor {
24 static activators = [
25 {
26 eventName: "onPointerDown" as const,
27 handler: ({ nativeEvent }: ReactPointerEvent) =>
28 nativeEvent.isPrimary && nativeEvent.button === 0,
29 },
30 ];
31 autoScrollEnabled = true;
32 private phase: "pending" | "dragging" | "finished" = "pending";
33 private readonly pointer: PointerEvent;
34 private readonly document: Document;
35 private readonly window: Window;
36
37 // The settle sweep constructs this sensor directly, outside dnd-kit, so it
38 // takes only the props the gesture reads.
39 constructor(private readonly props: Omit<SensorProps<Options>, "activeNode" | "context">) {
40 this.pointer = props.event as PointerEvent;
41 this.document = getOwnerDocument(this.pointer.target);
42 this.window = getWindow(this.pointer.target);
43 this.document.addEventListener("pointermove", this.move, { passive: false, capture: true });
44 this.document.addEventListener("pointerup", this.end, { capture: true });
45 this.document.addEventListener("pointercancel", this.pointerCancel, { capture: true });
46 this.document.addEventListener("keydown", this.keydown, { capture: true });
47 this.document.addEventListener("visibilitychange", this.visibilityChange);
48 this.window.addEventListener("blur", this.cancel);
49 this.window.addEventListener("pagehide", this.cancel);
50 this.window.addEventListener("resize", this.cancel);
51 this.document.addEventListener("dragstart", this.preventDefault);
52 this.document.addEventListener("contextmenu", this.preventDefault);
53 props.options.onAttach(this);
54 props.onPending(props.active, { distance: props.options.distance }, this.coordinates());
55 }
56
57 private coordinates = () => ({ x: this.pointer.clientX, y: this.pointer.clientY });
58 private preventDefault = (event: Event) => event.preventDefault();
59 private clearClickSuppression = () => {
60 this.document.removeEventListener("click", this.suppressClick, { capture: true });
61 this.document.removeEventListener("pointerdown", this.clearClickSuppression, { capture: true });
62 };
63 private suppressClick = (event: Event) => {
64 event.stopPropagation();
65 this.clearClickSuppression();
66 };
67 private clearSelection = () => this.document.getSelection()?.removeAllRanges();
68
69 private move = (event: PointerEvent) => {
70 if (this.phase === "finished" || event.pointerId !== this.pointer.pointerId) return;
71 // A release outside the window can be missed. Never activate or continue
72 // a drag when the initiating button is no longer held.
73 if ((event.buttons & 1) === 0) return this.cancel();
74 const coordinates = { x: event.clientX, y: event.clientY };
75 if (this.phase === "pending") {
76 const offset = {
77 x: event.clientX - this.pointer.clientX,
78 y: event.clientY - this.pointer.clientY,
79 };
80 if (Math.hypot(offset.x, offset.y) <= this.props.options.distance) {
81 this.props.onPending(
82 this.props.active,
83 { distance: this.props.options.distance },
84 this.coordinates(),
85 offset,
86 );
87 return;
88 }
89 this.phase = "dragging";
90 this.document.addEventListener("click", this.suppressClick, { capture: true });
91 this.document.addEventListener("selectionchange", this.clearSelection);
92 this.clearSelection();
93 this.props.onStart(this.coordinates());
94 }
95 // The move that starts a drag also moves it, so a release before the
96 // next pointermove still lands where the pointer is.
97 if (this.phase === "dragging") {
98 if (event.cancelable) event.preventDefault();
99 if (this.props.options.onMove?.(coordinates) === true) return;
100 this.props.onMove(coordinates);
101 }
102 };
103
104 private end = (event: PointerEvent) => {
105 if (event.pointerId !== this.pointer.pointerId) return;
106 const dropped =
107 this.phase === "dragging" &&
108 this.props.options.onDrop?.({ x: event.clientX, y: event.clientY }) === true;
109 this.finish(dropped);
110 };
111 private pointerCancel = (event: PointerEvent) => {
112 if (event.pointerId === this.pointer.pointerId) this.cancel();
113 };
114 private keydown = (event: KeyboardEvent) => {
115 if (event.code === "Escape") this.cancel();
116 };
117 private visibilityChange = () => {
118 if (this.document.hidden) this.cancel();
119 };
120 cancel = () => this.finish(true);
121
122 private finish(cancelled: boolean) {
123 if (this.phase === "finished") return;
124 const aborted = this.phase === "pending";
125 this.phase = "finished";
126 this.document.removeEventListener("pointermove", this.move, { capture: true });
127 this.document.removeEventListener("pointerup", this.end, { capture: true });
128 this.document.removeEventListener("pointercancel", this.pointerCancel, { capture: true });
129 this.document.removeEventListener("keydown", this.keydown, { capture: true });
130 this.document.removeEventListener("visibilitychange", this.visibilityChange);
131 this.window.removeEventListener("blur", this.cancel);
132 this.window.removeEventListener("pagehide", this.cancel);
133 this.window.removeEventListener("resize", this.cancel);
134 this.document.removeEventListener("dragstart", this.preventDefault);
135 this.document.removeEventListener("contextmenu", this.preventDefault);
136 this.document.removeEventListener("selectionchange", this.clearSelection);
137 // Cancellation can precede release by an arbitrary amount of time. Consume
138 // that release click, or let a fresh pointerdown end suppression if release
139 // happened outside the document. Ordinary clicks never install this guard.
140 if (!aborted || cancelled) {
141 this.document.addEventListener("click", this.suppressClick, { capture: true });
142 this.document.addEventListener("pointerdown", this.clearClickSuppression, { capture: true });
143 }
144 try {
145 // Release the sidebar preview before dnd-kit clears its transforms.
146 // Its public end/cancel event can be omitted before its first layout.
147 this.props.options.onFinish(!aborted);
148 } finally {
149 if (aborted) this.props.onAbort(this.props.active);
150 if (cancelled) this.props.onCancel();
151 else this.props.onEnd();
152 }
153 }
154}