Simple Floating Edges
This is a simplified version of the Floating Edges example. It’s not as flexible as the floating edges example, but the edges stick to the top, right, bottom or left side of the nodes. You can find the implementation details in the utils.js file.
import React, { useCallback } from 'react';
import {
ReactFlow,
addEdge,
Background,
useNodesState,
useEdgesState,
MarkerType,
ConnectionMode,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
import SimpleFloatingEdge from './SimpleFloatingEdge';
import CustomNode from './CustomNode';
import './index.css';
const nodeTypes = {
custom: CustomNode,
};
const edgeTypes = {
floating: SimpleFloatingEdge,
};
const initialNodes = [
{
id: '1',
label: '1',
position: { x: 0, y: 0 },
data: { label: 'drag me around 😎' },
type: 'custom',
},
{
id: '2',
label: '2',
position: { x: 0, y: 150 },
data: { label: '...or me' },
type: 'custom',
},
];
const initialEdges = [
{
id: '1-2',
source: '1',
target: '2',
sourceHandle: 'c',
targetHandle: 'a',
type: 'floating',
markerEnd: { type: MarkerType.ArrowClosed },
},
];
const fitViewOptions = { padding: 4 };
const NodeAsHandleFlow = () => {
const [nodes, , onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = useCallback(
(params) =>
setEdges((eds) =>
addEdge(
{
...params,
type: 'floating',
markerEnd: { type: MarkerType.Arrow },
},
eds,
),
),
[],
);
return (
<div className="simple-floatingedges">
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
edgeTypes={edgeTypes}
nodeTypes={nodeTypes}
fitView
fitViewOptions={fitViewOptions}
connectionMode={ConnectionMode.Loose}
style={{ backgroundColor: "#F7F9FB" }}
>
<Background />
</ReactFlow>
</div>
);
};
export default NodeAsHandleFlow;