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};
16
17/** A sidebar gesture ends on release, cancellation, or loss of its window.
18 * Own the listeners so unmounting the list can cancel the sensor too. */
19export class SidebarPointerSensor {
20 static activators = [
21 {
22 eventName: "onPointerDown" as const,
23 handler: ({ nativeEvent }: ReactPointerEvent) =>
24 nativeEvent.isPrimary && nativeEvent.button === 0,
25 },
26 ];
27 autoScrollEnabled = true;
28 private phase: "pending" | "dragging" | "finished" = "pending";
29 private readonly pointer: PointerEvent;
30 private readonly document: Document;
31 private readonly window: Window;
32
33 constructor(private readonly props: SensorProps<Options>) {
34 this.pointer = props.event as PointerEvent;
35 this.document = getOwnerDocument(this.pointer.target);
36 this.window = getWindow(this.pointer.target);
37 this.document.addEventListener("pointermove", this.move, { passive: false, capture: true });
38 this.document.addEventListener("pointerup", this.end, { capture: true });
39 this.document.addEventListener("pointercancel", this.pointerCancel, { capture: true });
40 this.document.addEventListener("keydown", this.keydown, { capture: true });
41 this.document.addEventListener("visibilitychange", this.visibilityChange);
42 this.window.addEventListener("blur", this.cancel);
43 this.window.addEventListener("pagehide", this.cancel);
44 this.window.addEventListener("resize", this.cancel);
45 this.document.addEventListener("dragstart", this.preventDefault);
46 this.document.addEventListener("contextmenu", this.preventDefault);
47 props.options.onAttach(this);
48 props.onPending(props.active, { distance: props.options.distance }, this.coordinates());
49 }
50
51 private coordinates = () => ({ x: this.pointer.clientX, y: this.pointer.clientY });
52 private preventDefault = (event: Event) => event.preventDefault();
53 private clearClickSuppression = () => {
54 this.document.removeEventListener("click", this.suppressClick, { capture: true });
55 this.document.removeEventListener("pointerdown", this.clearClickSuppression, { capture: true });
56 };
57 private suppressClick = (event: Event) => {
58 event.stopPropagation();
59 this.clearClickSuppression();
60 };
61 private clearSelection = () => this.document.getSelection()?.removeAllRanges();
62
63 private move = (event: PointerEvent) => {
64 if (this.phase === "finished" || event.pointerId !== this.pointer.pointerId) return;
65 // A release outside the window can be missed. Never activate or continue
66 // a drag when the initiating button is no longer held.
67 if ((event.buttons & 1) === 0) return this.cancel();
68 const coordinates = { x: event.clientX, y: event.clientY };
69 if (this.phase === "pending") {
70 const offset = {
71 x: event.clientX - this.pointer.clientX,
72 y: event.clientY - this.pointer.clientY,
73 };
74 if (Math.hypot(offset.x, offset.y) <= this.props.options.distance) {
75 this.props.onPending(
76 this.props.active,
77 { distance: this.props.options.distance },
78 this.coordinates(),
79 offset,
80 );
81 return;
82 }
83 this.phase = "dragging";
84 this.document.addEventListener("click", this.suppressClick, { capture: true });
85 this.document.addEventListener("selectionchange", this.clearSelection);
86 this.clearSelection();
87 this.props.onStart(this.coordinates());
88 return;
89 }
90 if (this.phase === "dragging") {
91 if (event.cancelable) event.preventDefault();
92 this.props.onMove(coordinates);
93 }
94 };
95
96 private end = (event: PointerEvent) => {
97 if (event.pointerId === this.pointer.pointerId) this.finish(false);
98 };
99 private pointerCancel = (event: PointerEvent) => {
100 if (event.pointerId === this.pointer.pointerId) this.cancel();
101 };
102 private keydown = (event: KeyboardEvent) => {
103 if (event.code === "Escape") this.cancel();
104 };
105 private visibilityChange = () => {
106 if (this.document.hidden) this.cancel();
107 };
108 cancel = () => this.finish(true);
109
110 private finish(cancelled: boolean) {
111 if (this.phase === "finished") return;
112 const aborted = this.phase === "pending";
113 this.phase = "finished";
114 this.document.removeEventListener("pointermove", this.move, { capture: true });
115 this.document.removeEventListener("pointerup", this.end, { capture: true });
116 this.document.removeEventListener("pointercancel", this.pointerCancel, { capture: true });
117 this.document.removeEventListener("keydown", this.keydown, { capture: true });
118 this.document.removeEventListener("visibilitychange", this.visibilityChange);
119 this.window.removeEventListener("blur", this.cancel);
120 this.window.removeEventListener("pagehide", this.cancel);
121 this.window.removeEventListener("resize", this.cancel);
122 this.document.removeEventListener("dragstart", this.preventDefault);
123 this.document.removeEventListener("contextmenu", this.preventDefault);
124 this.document.removeEventListener("selectionchange", this.clearSelection);
125 // Cancellation can precede release by an arbitrary amount of time. Consume
126 // that release click, or let a fresh pointerdown end suppression if release
127 // happened outside the document. Ordinary clicks never install this guard.
128 if (!aborted) {
129 this.document.addEventListener("pointerdown", this.clearClickSuppression, { capture: true });
130 }
131 try {
132 // Release the sidebar preview before dnd-kit clears its transforms.
133 // Its public end/cancel event can be omitted before its first layout.
134 this.props.options.onFinish(!aborted);
135 } finally {
136 if (aborted) this.props.onAbort(this.props.active);
137 if (cancelled) this.props.onCancel();
138 else this.props.onEnd();
139 }
140 }
141}