Merge pull request #5593 from xyflow/fix/selection-pointerup

Fix/selection pointerup
This commit is contained in:
Moritz Klack
2025-10-30 15:28:45 +01:00
committed by GitHub
4 changed files with 17 additions and 30 deletions

View File

@@ -0,0 +1,5 @@
---
'@xyflow/react': patch
---
Reset selection box when user selects a node

View File

@@ -39,6 +39,7 @@
bind:edges bind:edges
fitView fitView
selectionMode={SelectionMode.Partial} selectionMode={SelectionMode.Partial}
multiSelectionKey={['Meta', 'Shift']}
selectionOnDrag selectionOnDrag
panOnScroll panOnScroll
paneClickDistance={100} paneClickDistance={100}

View File

@@ -121,7 +121,7 @@ export function Pane({
// We are using capture here in order to prevent other pointer events // We are using capture here in order to prevent other pointer events
// to be able to create a selection above a node or an edge // to be able to create a selection above a node or an edge
const onPointerDownCapture = (event: ReactPointerEvent): void => { const onPointerDownCapture = (event: ReactPointerEvent): void => {
const { resetSelectedElements, domNode } = store.getState(); const { domNode } = store.getState();
containerBounds.current = domNode?.getBoundingClientRect(); containerBounds.current = domNode?.getBoundingClientRect();
if (!containerBounds.current) return; if (!containerBounds.current) return;
@@ -155,13 +155,6 @@ export function Pane({
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
} }
if (paneClickDistance === 0 || selectionKeyPressed) {
resetSelectedElements();
onSelectionStart?.(event);
selectionInProgress.current = true;
}
}; };
const onPointerMove = (event: ReactPointerEvent): void => { const onPointerMove = (event: ReactPointerEvent): void => {
@@ -184,14 +177,10 @@ export function Pane({
const { x: mouseX, y: mouseY } = getEventPosition(event.nativeEvent, containerBounds.current); const { x: mouseX, y: mouseY } = getEventPosition(event.nativeEvent, containerBounds.current);
const { startX, startY } = userSelectionRect; const { startX, startY } = userSelectionRect;
if ( if (!selectionInProgress.current) {
!selectionInProgress.current && const requiredDistance = selectionKeyPressed ? 0 : paneClickDistance;
event.target === container.current &&
!selectionKeyPressed &&
paneClickDistance > 0
) {
const distance = Math.hypot(mouseX - startX, mouseY - startY); const distance = Math.hypot(mouseX - startX, mouseY - startY);
if (distance <= paneClickDistance) { if (distance <= requiredDistance) {
return; return;
} }
resetSelectedElements(); resetSelectedElements();
@@ -268,11 +257,14 @@ export function Pane({
store.setState({ store.setState({
userSelectionActive: false, userSelectionActive: false,
userSelectionRect: null, userSelectionRect: null,
nodesSelectionActive: selectedNodeIds.current.size > 0,
}); });
if (selectionInProgress.current) { if (selectionInProgress.current) {
onSelectionEnd?.(event); onSelectionEnd?.(event);
store.setState({
nodesSelectionActive: selectedNodeIds.current.size > 0,
});
} }
}; };

View File

@@ -120,13 +120,6 @@
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
} }
if (paneClickDistance === 0 || store.selectionKeyPressed) {
store.unselectNodesAndEdges();
onselectionstart?.(event);
selectionInProgress = true;
}
} }
function onPointerMove(event: PointerEvent) { function onPointerMove(event: PointerEvent) {
@@ -137,14 +130,10 @@
const mousePos = getEventPosition(event, containerBounds); const mousePos = getEventPosition(event, containerBounds);
const { startX = 0, startY = 0 } = store.selectionRect; const { startX = 0, startY = 0 } = store.selectionRect;
if ( if (!selectionInProgress) {
!selectionInProgress && const requiredDistance = store.selectionKeyPressed ? 0 : paneClickDistance;
event.target === container &&
!store.selectionKeyPressed &&
paneClickDistance > 0
) {
const distance = Math.hypot(mousePos.x - startX, mousePos.y - startY); const distance = Math.hypot(mousePos.x - startX, mousePos.y - startY);
if (distance <= paneClickDistance) { if (distance <= requiredDistance) {
return; return;
} }
store.unselectNodesAndEdges(); store.unselectNodesAndEdges();