diff --git a/examples/react/src/examples/EasyConnect/CustomNode.tsx b/examples/react/src/examples/EasyConnect/CustomNode.tsx index 7d3d6d67..03726da2 100644 --- a/examples/react/src/examples/EasyConnect/CustomNode.tsx +++ b/examples/react/src/examples/EasyConnect/CustomNode.tsx @@ -1,13 +1,10 @@ -import { Handle, NodeProps, Position, ReactFlowState, useStore } from '@xyflow/react'; - -const connectionNodeIdSelector = (state: ReactFlowState) => state.connectionStartHandle?.nodeId; +import { Handle, NodeProps, Position, useConnection } from '@xyflow/react'; export default function CustomNode({ id }: NodeProps) { - const connectionNodeId = useStore(connectionNodeIdSelector); - const isConnecting = !!connectionNodeId; - const isTarget = connectionNodeId && connectionNodeId !== id; + const connection = useConnection(); + + const isTarget = connection.inProgress && connection.fromNode.id !== id; - const targetHandleStyle = { zIndex: isTarget ? 3 : 1 }; const label = isTarget ? 'Drop here' : 'Drag to connect'; return ( @@ -19,17 +16,13 @@ export default function CustomNode({ id }: NodeProps) { backgroundColor: isTarget ? '#ffcce3' : '#ccd9f6', }} > - {!isConnecting && ( - - )} + {/* If handles are conditionally rendered and not present initially, you need to update the node internals https://reactflow.dev/docs/api/hooks/use-update-node-internals/ */} + {/* In this case we don't need to use useUpdateNodeInternals, since !connection.inProgress is true at the beginning and all handles are rendered initially. */} + {!connection.inProgress && } - + {/* We want to disable the target handle, if the connection was started from this node */} + {/* {(!connection.inProgress || isTarget) && ( */} + {label} diff --git a/examples/react/src/examples/EasyConnect/FloatingEdge.tsx b/examples/react/src/examples/EasyConnect/FloatingEdge.tsx index 48d621e7..a35398bc 100644 --- a/examples/react/src/examples/EasyConnect/FloatingEdge.tsx +++ b/examples/react/src/examples/EasyConnect/FloatingEdge.tsx @@ -1,5 +1,4 @@ -import { useCallback } from 'react'; -import { useStore, getStraightPath, EdgeProps, useInternalNode } from '@xyflow/react'; +import { EdgeProps, getStraightPath, useInternalNode } from '@xyflow/react'; import { getEdgeParams } from './utils.js'; diff --git a/examples/react/src/examples/EasyConnect/index.tsx b/examples/react/src/examples/EasyConnect/index.tsx index 11e347f1..a770b778 100644 --- a/examples/react/src/examples/EasyConnect/index.tsx +++ b/examples/react/src/examples/EasyConnect/index.tsx @@ -1,5 +1,15 @@ import { useCallback } from 'react'; -import { ReactFlow, Node, Edge, addEdge, useNodesState, useEdgesState, MarkerType, OnConnect } from '@xyflow/react'; +import { + ReactFlow, + Node, + Edge, + addEdge, + useNodesState, + useEdgesState, + MarkerType, + OnConnect, + ConnectionMode, +} from '@xyflow/react'; import CustomNode from './CustomNode'; import FloatingEdge from './FloatingEdge'; @@ -64,6 +74,8 @@ const EasyConnectExample = () => { const onConnect: OnConnect = useCallback((params) => setEdges((eds) => addEdge(params, eds)), [setEdges]); + console.log(edges); + return (