--- name: react-flow description: "Implements and structures node-based UIs with React Flow (@xyflow/react v12/v13), covering custom nodes and edges, handles, viewport control, sub-flows, store subscriptions, and @dagrejs/dagre auto-layout. Use when building flow editors, workflow diagrams, or interactive graphs in React." --- # React Flow (`@xyflow/react`) Implement interactive node-based graphs and workflow editors using `@xyflow/react` (v12 and v13, React 18/19). ## Implementation Gates Run these checks in order before treating any integration as complete: 1. **CSS & container dimensions** — Import `@xyflow/react/style.css` (v13, wrapped in `@layer xyflow` for Tailwind v4 precedence) or `@xyflow/react/dist/style.css` (v12), and give the wrapper element explicit dimensions (`width` and `height`). 2. **Stable `nodeTypes` / `edgeTypes`** — Define `nodeTypes` and `edgeTypes` at module scope or memoize with `useMemo`; never create inline object literals on every render (causes full node/edge remounting). 3. **Provider boundary** — Any component calling `useReactFlow()`, `useNodesData()`, `useNodeConnections()`, or store hooks must be a descendant of `` (or rendered inside ``). 4. **Measured vs SSR dimensions** — Read DOM-measured node sizes from `node.measured?.width` / `node.measured?.height` (falling back to `node.width ?? defaultWidth`). For SSR/SSG, provide static `width`/`height` or `initialWidth`/`initialHeight` on nodes so `fitView` and initial layout work without DOM measurement. ## Quick Start & TypeScript Unions ```tsx import { useCallback } from 'react'; import { ReactFlow, ReactFlowProvider, Background, Controls, useNodesState, useEdgesState, addEdge, type Node, type Edge, type BuiltInNode, type BuiltInEdge, type OnConnect, } from '@xyflow/react'; import '@xyflow/react/style.css'; // v13 (@layer xyflow); use '@xyflow/react/dist/style.css' on v12 export type TaskNode = Node<{ label: string; active: boolean }, 'task'>; export type AppNode = BuiltInNode | TaskNode; export type WeightedEdge = Edge<{ weight: number }, 'weighted'>; export type AppEdge = BuiltInEdge | WeightedEdge; const nodeTypes = { /* task: TaskNodeComponent */ }; const edgeTypes = { /* weighted: WeightedEdgeComponent */ }; const initialNodes: AppNode[] = [ { id: '1', type: 'input', position: { x: 0, y: 0 }, data: { label: 'Start' } }, { id: '2', position: { x: 240, y: 80 }, data: { label: 'Process' } }, ]; const initialEdges: AppEdge[] = [{ id: 'e1-2', source: '1', target: '2' }]; function FlowCanvas() { const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes); const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); const onConnect: OnConnect = useCallback( (connection) => setEdges((eds) => addEdge(connection, eds)), [setEdges] ); return (
); } export default function App() { return ( ); } ``` ## Custom Nodes, Edges & Interaction Classes - **Typed props**: Use `NodeProps` and `EdgeProps`, wrapped in `memo()`. Import `CSSProperties` from `'react'` (not `'@xyflow/react'`). - **Handles**: Always use `Position` enum (`Position.Top`, `Position.Right`, `Position.Bottom`, `Position.Left`). Assign distinct `id` props when a node has multiple handles of the same type, and match them via `sourceHandle` / `targetHandle` on edges. - **Interaction classes**: Prevent accidental graph interactions on embedded controls: - `nodrag` — prevents dragging the node (required on ``, `