diff --git a/.changeset/blue-shirts-beg.md b/.changeset/blue-shirts-beg.md new file mode 100644 index 0000000000..21713fb514 --- /dev/null +++ b/.changeset/blue-shirts-beg.md @@ -0,0 +1,6 @@ +--- +'@xyflow/react': patch +'@xyflow/svelte': patch +--- + +Keep `onConnectEnd` and `isValidConnection` up to date in an ongoing connection diff --git a/.changeset/tender-ways-attend.md b/.changeset/tender-ways-attend.md new file mode 100644 index 0000000000..5973788497 --- /dev/null +++ b/.changeset/tender-ways-attend.md @@ -0,0 +1,5 @@ +--- +'@xyflow/react': patch +--- + +Consolidate drag handler effects in useDrag to fix programmatic selection issues diff --git a/examples/react/src/App/routes.ts b/examples/react/src/App/routes.ts index 055dc9e149..d5bbfe0150 100644 --- a/examples/react/src/App/routes.ts +++ b/examples/react/src/App/routes.ts @@ -62,6 +62,7 @@ import MovingHandles from '../examples/MovingHandles'; import DetachedHandle from '../examples/DetachedHandle'; import ZIndexMode from '../examples/ZIndexMode'; import Middlewares from '../examples/Middlewares'; +import NodeSelectionBug from '../examples/NodeSelectionBug'; export interface IRoute { name: string; @@ -390,6 +391,11 @@ const routes: IRoute[] = [ path: 'z-index-mode', component: ZIndexMode, }, + { + name: 'Node Selection Bug', + path: 'node-selection-bug', + component: NodeSelectionBug, + }, ]; export default routes; diff --git a/examples/react/src/examples/NodeSelectionBug/index.tsx b/examples/react/src/examples/NodeSelectionBug/index.tsx new file mode 100644 index 0000000000..a587cab392 --- /dev/null +++ b/examples/react/src/examples/NodeSelectionBug/index.tsx @@ -0,0 +1,41 @@ +import { Node, ReactFlow, useNodesState } from '@xyflow/react'; + +import '@xyflow/react/dist/style.css'; +import { useRef } from 'react'; + +export default function App() { + const [nodes, setNodes, onNodesChange] = useNodesState([ + { + id: '0', + position: { x: 0, y: 0 }, + data: { label: 'Rectangle Select Me First' }, + }, + ]); + const id = useRef(0); + return ( + <> + + + + ); +} diff --git a/packages/react/src/components/EdgeWrapper/EdgeUpdateAnchors.tsx b/packages/react/src/components/EdgeWrapper/EdgeUpdateAnchors.tsx index 36a983aa09..87504702cb 100644 --- a/packages/react/src/components/EdgeWrapper/EdgeUpdateAnchors.tsx +++ b/packages/react/src/components/EdgeWrapper/EdgeUpdateAnchors.tsx @@ -53,12 +53,10 @@ export function EdgeUpdateAnchors({ const { autoPanOnConnect, domNode, - isValidConnection, connectionMode, connectionRadius, lib, onConnectStart, - onConnectEnd, cancelConnection, nodeLookup, rfId: flowId, @@ -93,10 +91,10 @@ export function EdgeUpdateAnchors({ flowId, cancelConnection, panBy, - isValidConnection, + isValidConnection: (...args) => store.getState().isValidConnection?.(...args) ?? true, onConnect: onConnectEdge, onConnectStart: _onConnectStart, - onConnectEnd, + onConnectEnd: (...args) => store.getState().onConnectEnd?.(...args), onReconnectEnd: _onReconnectEnd, updateConnection, getTransform: () => store.getState().transform, diff --git a/packages/react/src/components/Handle/index.tsx b/packages/react/src/components/Handle/index.tsx index dec2d242e7..1f0dc83413 100644 --- a/packages/react/src/components/Handle/index.tsx +++ b/packages/react/src/components/Handle/index.tsx @@ -142,10 +142,10 @@ function HandleComponent( panBy: currentStore.panBy, cancelConnection: currentStore.cancelConnection, onConnectStart: currentStore.onConnectStart, - onConnectEnd: currentStore.onConnectEnd, + onConnectEnd: (...args) => store.getState().onConnectEnd?.(...args), updateConnection: currentStore.updateConnection, onConnect: onConnectExtended, - isValidConnection: isValidConnection || currentStore.isValidConnection, + isValidConnection: isValidConnection || ((...args) => store.getState().isValidConnection?.(...args) ?? true), getTransform: () => store.getState().transform, getFromHandle: () => store.getState().connection.fromHandle, autoPanSpeed: currentStore.autoPanSpeed, diff --git a/packages/react/src/components/NodesSelection/index.tsx b/packages/react/src/components/NodesSelection/index.tsx index ecaef17b0e..de118a4495 100644 --- a/packages/react/src/components/NodesSelection/index.tsx +++ b/packages/react/src/components/NodesSelection/index.tsx @@ -51,11 +51,14 @@ export function NodesSelection({ } }, [disableKeyboardA11y]); + const shouldRender = !userSelectionActive && width !== null && height !== null; + useDrag({ nodeRef, + disabled: !shouldRender, }); - if (userSelectionActive || !width || !height) { + if (!shouldRender) { return null; } diff --git a/packages/react/src/hooks/useDrag.ts b/packages/react/src/hooks/useDrag.ts index f7943f9871..6b4c7642bd 100644 --- a/packages/react/src/hooks/useDrag.ts +++ b/packages/react/src/hooks/useDrag.ts @@ -52,22 +52,23 @@ export function useDrag({ }, []); useEffect(() => { - if (disabled) { - xyDrag.current?.destroy(); - } else if (nodeRef.current) { - xyDrag.current?.update({ - noDragClassName, - handleSelector, - domNode: nodeRef.current, - isSelectable, - nodeId, - nodeClickDistance, - }); - return () => { - xyDrag.current?.destroy(); - }; + if (disabled || !nodeRef.current || !xyDrag.current) { + return; } - }, [noDragClassName, handleSelector, disabled, isSelectable, nodeRef, nodeId]); + + xyDrag.current.update({ + noDragClassName, + handleSelector, + domNode: nodeRef.current, + isSelectable, + nodeId, + nodeClickDistance, + }); + + return () => { + xyDrag.current?.destroy(); + }; + }, [noDragClassName, handleSelector, disabled, isSelectable, nodeRef, nodeId, nodeClickDistance]); return dragging; } diff --git a/packages/svelte/src/lib/components/EdgeReconnectAnchor/EdgeReconnectAnchor.svelte b/packages/svelte/src/lib/components/EdgeReconnectAnchor/EdgeReconnectAnchor.svelte index 2a4975df3b..dbca1acf17 100644 --- a/packages/svelte/src/lib/components/EdgeReconnectAnchor/EdgeReconnectAnchor.svelte +++ b/packages/svelte/src/lib/components/EdgeReconnectAnchor/EdgeReconnectAnchor.svelte @@ -79,9 +79,9 @@ flowId, cancelConnection, panBy, - isValidConnection, + isValidConnection: (...args) => store.isValidConnection?.(...args) ?? true, onConnectStart: _onConnectStart, - onConnectEnd: onconnectend, + onConnectEnd: (...args) => store.onconnectend?.(...args), onConnect: (connection) => { const reconnectedEdge = { ...edge, ...connection }; const newEdge = onbeforereconnect diff --git a/packages/svelte/src/lib/components/Handle/Handle.svelte b/packages/svelte/src/lib/components/Handle/Handle.svelte index 15f811b1a5..e77562891d 100644 --- a/packages/svelte/src/lib/components/Handle/Handle.svelte +++ b/packages/svelte/src/lib/components/Handle/Handle.svelte @@ -125,21 +125,14 @@ autoPanOnConnect: store.autoPanOnConnect, autoPanSpeed: store.autoPanSpeed, flowId: store.flowId, - isValidConnection: isValidConnection ?? store.isValidConnection, + isValidConnection: + isValidConnection || ((...args) => store.isValidConnection?.(...args) ?? true), updateConnection: store.updateConnection, cancelConnection: store.cancelConnection, panBy: store.panBy, onConnect: onConnectExtended, - onConnectStart: (event, startParams) => { - store.onconnectstart?.(event, { - nodeId: startParams.nodeId, - handleId: startParams.handleId, - handleType: startParams.handleType - }); - }, - onConnectEnd: (event, connectionState) => { - store.onconnectend?.(event, connectionState); - }, + onConnectStart: store.onconnectstart, + onConnectEnd: (...args) => store.onconnectend?.(...args), getTransform: () => [store.viewport.x, store.viewport.y, store.viewport.zoom], getFromHandle: () => store.connection.fromHandle, dragThreshold: store.connectionDragThreshold,