Intersections
The useReactFlow
hook exports helpers to check intersections of nodes and areas. In this example you can drag a node and get a visual feedback when it intersects with another node.
import React, { useCallback, type MouseEvent } from 'react';
import {
ReactFlow,
Background,
Controls,
ReactFlowProvider,
useReactFlow,
useNodesState,
type Edge,
type Node,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
const initialNodes: Node[] = [
{
id: '1',
data: { label: 'Node 1' },
position: { x: 0, y: 0 },
style: {
width: 200,
height: 100,
},
},
{
id: '2',
data: { label: 'Node 2' },
position: { x: 0, y: 150 },
},
{
id: '3',
data: { label: 'Node 3' },
position: { x: 250, y: 0 },
},
{
id: '4',
data: { label: 'Node' },
position: { x: 350, y: 150 },
style: {
width: 50,
height: 50,
},
},
];
const initialEdges: Edge[] = [];
const BasicFlow = () => {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const { getIntersectingNodes } = useReactFlow();
const onNodeDrag = useCallback((_: MouseEvent, node: Node) => {
const intersections = getIntersectingNodes(node).map((n) => n.id);
setNodes((ns) =>
ns.map((n) => ({
...n,
className: intersections.includes(n.id) ? 'highlight' : '',
})),
);
}, []);
return (
<ReactFlow
style={{ backgroundColor: "#F7F9FB" }}
nodes={nodes}
edges={initialEdges}
onNodesChange={onNodesChange}
onNodeDrag={onNodeDrag}
className="intersection-flow"
minZoom={0.2}
maxZoom={4}
fitView
selectNodesOnDrag={false}
>
<Background />
<Controls />
</ReactFlow>
);
};
export default function App() {
return (
<ReactFlowProvider>
<BasicFlow />
</ReactFlowProvider>
);
}