Custom Nodes
Creating your own nodes is as easy as creating a regular React component and passing them to nodeTypes
. Being just regular components, you can essentially display any content and implement any functionality you like. Inside, you have access to a number of props that let you implement and extend default node behaviour.
import React, { useState, useEffect, useCallback } from 'react';
import {
ReactFlow,
useNodesState,
useEdgesState,
addEdge,
MiniMap,
Controls,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
import ColorSelectorNode from './ColorSelectorNode';
const initBgColor = '#c9f1dd';
const snapGrid = [20, 20];
const nodeTypes = {
selectorNode: ColorSelectorNode,
};
const defaultViewport = { x: 0, y: 0, zoom: 1.5 };
const CustomNodeFlow = () => {
const [nodes, setNodes, onNodesChange] = useNodesState([]);
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
const [bgColor, setBgColor] = useState(initBgColor);
useEffect(() => {
const onChange = (event) => {
setNodes((nds) =>
nds.map((node) => {
if (node.id !== '2') {
return node;
}
const color = event.target.value;
setBgColor(color);
return {
...node,
data: {
...node.data,
color,
},
};
}),
);
};
setNodes([
{
id: '1',
type: 'input',
data: { label: 'An input node' },
position: { x: 0, y: 50 },
sourcePosition: 'right',
},
{
id: '2',
type: 'selectorNode',
data: { onChange: onChange, color: initBgColor },
position: { x: 300, y: 50 },
},
{
id: '3',
type: 'output',
data: { label: 'Output A' },
position: { x: 650, y: 25 },
targetPosition: 'left',
},
{
id: '4',
type: 'output',
data: { label: 'Output B' },
position: { x: 650, y: 100 },
targetPosition: 'left',
},
]);
setEdges([
{
id: 'e1-2',
source: '1',
target: '2',
animated: true,
},
{
id: 'e2a-3',
source: '2',
target: '3',
sourceHandle: 'a',
animated: true,
},
{
id: 'e2b-4',
source: '2',
target: '4',
sourceHandle: 'b',
animated: true,
},
]);
}, []);
const onConnect = useCallback(
(params) =>
setEdges((eds) =>
addEdge({ ...params, animated: true }, eds),
),
[],
);
return (
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
style={{ background: bgColor }}
nodeTypes={nodeTypes}
snapToGrid={true}
snapGrid={snapGrid}
defaultViewport={defaultViewport}
fitView
attributionPosition="bottom-left"
>
<MiniMap
nodeStrokeColor={(n) => {
if (n.type === 'input') return '#0041d0';
if (n.type === 'selectorNode') return bgColor;
if (n.type === 'output') return '#ff0072';
}}
nodeColor={(n) => {
if (n.type === 'selectorNode') return bgColor;
return '#fff';
}}
/>
<Controls />
</ReactFlow>
);
};
export default CustomNodeFlow;