Merge pull request #5428 from Karl255/#5273-handle-click-target-fix

Allow detached handles
This commit is contained in:
Moritz Klack
2025-08-12 14:18:20 +02:00
committed by GitHub
13 changed files with 183 additions and 16 deletions

View File

@@ -1,11 +1,11 @@
// Reconnectable edges have a anchors around their handles to reconnect the edge.
import {
XYHandle,
type EdgePosition,
type FinalConnectionState,
type HandleType,
type OnConnectStart,
type Connection,
EdgePosition,
FinalConnectionState,
HandleType,
OnConnectStart,
} from '@xyflow/system';
import { EdgeAnchor } from '../Edges/EdgeAnchor';
@@ -102,6 +102,7 @@ export function EdgeUpdateAnchors<EdgeType extends Edge = Edge>({
getTransform: () => store.getState().transform,
getFromHandle: () => store.getState().connection.fromHandle,
dragThreshold: store.getState().connectionDragThreshold,
handleDomNode: event.currentTarget,
});
};

View File

@@ -114,7 +114,6 @@ function HandleComponent(
onConnectAction?.(edgeParams);
onConnect?.(edgeParams);
};
const onPointerDown = (event: ReactMouseEvent<HTMLDivElement> | ReactTouchEvent<HTMLDivElement>) => {
if (!nodeId) {
return;
@@ -129,6 +128,7 @@ function HandleComponent(
const currentStore = store.getState();
XYHandle.onPointerDown(event.nativeEvent, {
handleDomNode: event.currentTarget,
autoPanOnConnect: currentStore.autoPanOnConnect,
connectionMode: currentStore.connectionMode,
connectionRadius: currentStore.connectionRadius,

View File

@@ -102,7 +102,8 @@
updateConnection,
getTransform: () => [store.viewport.x, store.viewport.y, store.viewport.zoom],
getFromHandle: () => store.connection.fromHandle,
dragThreshold: dragThreshold ?? store.connectionDragThreshold
dragThreshold: dragThreshold ?? store.connectionDragThreshold,
handleDomNode: event.currentTarget as HTMLElement
});
};
</script>

View File

@@ -111,7 +111,7 @@
function onpointerdown(event: MouseEvent | TouchEvent) {
const isMouseTriggered = isMouseEvent(event);
if ((isMouseTriggered && event.button === 0) || !isMouseTriggered) {
if (event.currentTarget && ((isMouseTriggered && event.button === 0) || !isMouseTriggered)) {
XYHandle.onPointerDown(event, {
handleId,
nodeId,
@@ -140,7 +140,8 @@
},
getTransform: () => [store.viewport.x, store.viewport.y, store.viewport.zoom],
getFromHandle: () => store.connection.fromHandle,
dragThreshold: store.connectionDragThreshold
dragThreshold: store.connectionDragThreshold,
handleDomNode: event.currentTarget as HTMLElement
});
}
}

View File

@@ -46,6 +46,7 @@ function onPointerDown(
getFromHandle,
autoPanSpeed,
dragThreshold = 1,
handleDomNode,
}: OnPointerDownParams
) {
// when xyflow is used inside a shadow root we can't use document
@@ -54,8 +55,7 @@ function onPointerDown(
let closestHandle: Handle | null;
const { x, y } = getEventPosition(event);
const clickedHandle = doc?.elementFromPoint(x, y);
const handleType = getHandleType(edgeUpdaterType, clickedHandle);
const handleType = getHandleType(edgeUpdaterType, handleDomNode);
const containerBounds = domNode?.getBoundingClientRect();
let connectionStarted = false;
@@ -72,7 +72,7 @@ function onPointerDown(
let autoPanStarted = false;
let connection: Connection | null = null;
let isValid: boolean | null = false;
let handleDomNode: Element | null = null;
let resultHandleDomNode: Element | null = null;
// when the user is moving the mouse close to the edge of the canvas while connecting we move the canvas
function autoPan(): void {
@@ -167,7 +167,7 @@ function onPointerDown(
nodeLookup,
});
handleDomNode = result.handleDomNode;
resultHandleDomNode = result.handleDomNode;
connection = result.connection;
isValid = isConnectionValid(!!closestHandle, result.isValid);
@@ -208,7 +208,7 @@ function onPointerDown(
function onPointerUp(event: MouseEvent | TouchEvent) {
if (connectionStarted) {
if ((closestHandle || handleDomNode) && connection && isValid) {
if ((closestHandle || resultHandleDomNode) && connection && isValid) {
onConnect?.(connection);
}
@@ -235,7 +235,7 @@ function onPointerDown(
autoPanStarted = false;
isValid = false;
connection = null;
handleDomNode = null;
resultHandleDomNode = null;
doc.removeEventListener('mousemove', onPointerMove as EventListener);
doc.removeEventListener('mouseup', onPointerUp as EventListener);

View File

@@ -38,6 +38,7 @@ export type OnPointerDownParams = {
getFromHandle: () => Handle | null;
autoPanSpeed?: number;
dragThreshold?: number;
handleDomNode: Element;
};
export type IsValidParams = {