Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Sidebar.pointer.ts

149 lines6.5 KB

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