button-edge.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import React from 'react';
  2. import { BaseEdge, EdgeProps, getBezierPath, useReactFlow } from 'reactflow';
  3. const ButtonEdge: React.FC<EdgeProps> = ({
  4. id,
  5. sourceX,
  6. sourceY,
  7. targetX,
  8. targetY,
  9. sourcePosition,
  10. targetPosition,
  11. style = {},
  12. markerEnd,
  13. }) => {
  14. const [edgePath, edgeCenterX, edgeCenterY] = getBezierPath({
  15. sourceX,
  16. sourceY,
  17. sourcePosition,
  18. targetX,
  19. targetY,
  20. targetPosition,
  21. });
  22. const reactFlow = useReactFlow();
  23. function onEdgeClick(event: React.MouseEvent, id: string) {
  24. event.stopPropagation();
  25. reactFlow.setEdges(reactFlow.getEdges().filter(edge => edge.id !== id));
  26. }
  27. return (
  28. <>
  29. <BaseEdge id={id} style={style} path={edgePath} markerEnd={markerEnd} />
  30. <foreignObject
  31. width={40}
  32. height={40}
  33. x={edgeCenterX - 40 / 2}
  34. y={edgeCenterY - 40 / 2}
  35. className='bg-transparent w-10 h-10 relative'
  36. requiredExtensions='http://www.w3.org/1999/xhtml'
  37. >
  38. <button
  39. className='absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-5 h-5 rounded-full bg-stone-400 dark:bg-zinc-700 cursor-pointer text-sm'
  40. onClick={event => onEdgeClick(event, id)}
  41. >
  42. ×
  43. </button>
  44. </foreignObject>
  45. </>
  46. );
  47. };
  48. export default ButtonEdge;