I want to disconnect nodes by drag handle from target to empty, like litegraph.js editor and blender's node editor.
Is there any to do this in ReactFlow ?
I want to disconnect nodes by drag handle from target to empty, like litegraph.js editor and blender's node editor.
Is there any to do this in ReactFlow ?
There is a demo about this in the official documentation. You can do this by examining the example and codes there. Operations are performed using onReconnect, onReconnectStart and onReconnectEnd. The following codes are the codes shared in the demo.
index.css
html,
body {
margin: 0;
font-family: sans-serif;
box-sizing: border-box;
}
#app {
width: 100vw;
height: 100vh;
}
App.tsx
import React, { useCallback, useRef } from 'react';
import {
ReactFlow,
useNodesState,
useEdgesState,
Controls,
reconnectEdge,
addEdge,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
const initialNodes = [
{
id: '1',
type: 'input',
data: { label: 'Node A' },
position: { x: 250, y: 0 },
},
{
id: '2',
data: { label: 'Node B' },
position: { x: 100, y: 200 },
},
{
id: '3',
data: { label: 'Node C' },
position: { x: 350, y: 200 },
},
];
const initialEdges = [
{ id: 'e1-2', source: '1', target: '2', label: 'reconnectable edge' },
];
const DeleteEdgeDrop = () => {
const edgeReconnectSuccessful = useRef(true);
const [nodes, , onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = useCallback(
(params) => setEdges((els) => addEdge(params, els)),
[],
);
const onReconnectStart = useCallback(() => {
edgeReconnectSuccessful.current = false;
}, []);
const onReconnect = useCallback((oldEdge, newConnection) => {
edgeReconnectSuccessful.current = true;
setEdges((els) => reconnectEdge(oldEdge, newConnection, els));
}, []);
const onReconnectEnd = useCallback((_, edge) => {
if (!edgeReconnectSuccessful.current) {
setEdges((eds) => eds.filter((e) => e.id !== edge.id));
}
edgeReconnectSuccessful.current = true;
}, []);
return (
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
snapToGrid
onReconnect={onReconnect}
onReconnectStart={onReconnectStart}
onReconnectEnd={onReconnectEnd}
onConnect={onConnect}
fitView
attributionPosition="top-right"
>
<Controls />
</ReactFlow>
);
};