Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/components/dnd/config.ts
10799 views
1
// Shared DnD configuration constants for @dnd-kit.
2
// Used by both file explorer DnD and frame editor DnD to ensure
3
// identical activation behavior and visual appearance.
4
5
import type { Modifier } from "@dnd-kit/core";
6
7
// --- Sensor activation constraints ---
8
9
/** Mouse: 300ms hold OR 3px drag distance to activate. */
10
export const MOUSE_SENSOR_OPTIONS = {
11
activationConstraint: { distance: 3, delay: 300, tolerance: 5 },
12
} as const;
13
14
/** Touch: 300ms hold to activate. */
15
export const TOUCH_SENSOR_OPTIONS = {
16
activationConstraint: { delay: 300, tolerance: 5 },
17
} as const;
18
19
// --- Overlay positioning ---
20
21
/** Extract clientX/clientY from mouse, pointer, or touch events. */
22
export function getEventCoords(event: Event): { x: number; y: number } | null {
23
if ("clientX" in event && typeof (event as any).clientX === "number") {
24
return {
25
x: (event as MouseEvent).clientX,
26
y: (event as MouseEvent).clientY,
27
};
28
}
29
const te = event as TouchEvent;
30
const touch = te.touches?.[0] ?? te.changedTouches?.[0];
31
if (touch) {
32
return { x: touch.clientX, y: touch.clientY };
33
}
34
return null;
35
}
36
37
/**
38
* Position DragOverlay at pointer (12px bottom-right offset)
39
* instead of at the original element's origin.
40
*/
41
export const snapToPointerModifier: Modifier = ({
42
activatorEvent,
43
activeNodeRect,
44
transform,
45
}) => {
46
if (!activatorEvent || !activeNodeRect) return transform;
47
const coords = getEventCoords(activatorEvent);
48
if (!coords) return transform;
49
return {
50
...transform,
51
x: transform.x + (coords.x - activeNodeRect.left) + 12,
52
y: transform.y + (coords.y - activeNodeRect.top) + 12,
53
};
54
};
55
56
export const DRAG_OVERLAY_MODIFIERS: Modifier[] = [snapToPointerModifier];
57
58
// --- Overlay styling ---
59
60
export const DRAG_OVERLAY_STYLE = {
61
padding: "4px 10px",
62
borderRadius: 4,
63
fontSize: "12px",
64
whiteSpace: "nowrap" as const,
65
width: "max-content" as const,
66
pointerEvents: "none" as const,
67
} as const;
68
69