javascriptreactjsreact-hookszustandreact-flow

In React Flow how to Make Paths lines that connects nodes moveable just like draw.io diagrams


In React Flow diagrams how to Make Paths lines moveable and selectable that connects nodes just like draw.io diagrams where we can change position of path lines and they are selectable and moveable below is my code of Path line. Any guidance appreciated thanks.

`

import { getBezierPath, Position, Edge } from 'reactflow'

// type copied from library, they aren't exporting it for some reason
interface GetBezierPathParams {
  sourceX: number
  sourceY: number
  sourcePosition?: Position
  targetX: number
  targetY: number
  targetPosition?: Position
  curvature?: number
}

export default function BendyEdge({
  id,
  sourceX,
  sourceY,
  targetX,
  targetY,
  sourcePosition,
  targetPosition,
  style,
}: Edge & GetBezierPathParams) {
   const midPoint = (targetY - sourceY) / 2 + sourceY
   // values here are hardcoded for the sake of the example, you can calculate them dynamically later on, no time for that now
   const [edgePath] = getBezierPath({
   sourceX,
   sourceY,
   sourcePosition,
   targetX: targetX + 150,
   targetY: midPoint,
   targetPosition,
 })
 const [edgePath2] = getBezierPath({
   sourceX: targetX + 150,
   sourceY: midPoint,
   sourcePosition,
   targetX,
   targetY,
   targetPosition,
 })

 return (
   <>
     <path id={id} style={style} className="react-flow__edge-path" d={edgePath + edgePath2} />
   </>
 )
}

` enter image description here


Solution

  • Finally I found the solution for this, you can make use of custom edges to achieve this behaviour and now Paths lines moveable and selectable that connects nodes just like draw.io diagrams where we can change position of path lines and they are selectable and moveable below is the code.

    code: https://codesandbox.io/p/sandbox/elated-fog-wzj9c4