diff --git a/packages/react/src/components/Edges/BaseEdge.tsx b/packages/react/src/components/Edges/BaseEdge.tsx index 91cfd58a..ae14b472 100644 --- a/packages/react/src/components/Edges/BaseEdge.tsx +++ b/packages/react/src/components/Edges/BaseEdge.tsx @@ -2,6 +2,7 @@ import { isNumeric } from '@xyflow/system'; import type { BaseEdgeProps } from '../../types'; import EdgeText from './EdgeText'; +import classcat from 'classcat'; const BaseEdge = ({ id, @@ -17,6 +18,7 @@ const BaseEdge = ({ style, markerEnd, markerStart, + className, interactionWidth = 20, }: BaseEdgeProps) => { return ( @@ -26,7 +28,7 @@ const BaseEdge = ({ style={style} d={path} fill="none" - className="react-flow__edge-path" + className={classcat(['react-flow__edge-path', className])} markerEnd={markerEnd} markerStart={markerStart} /> diff --git a/packages/react/src/components/Edges/EdgeText.tsx b/packages/react/src/components/Edges/EdgeText.tsx index 7926e81d..8cc5c388 100644 --- a/packages/react/src/components/Edges/EdgeText.tsx +++ b/packages/react/src/components/Edges/EdgeText.tsx @@ -1,4 +1,4 @@ -import { memo, useRef, useState, useEffect, type FC, type PropsWithChildren } from 'react'; +import { memo, useState, type FC, type PropsWithChildren, useCallback } from 'react'; import cc from 'classcat'; import type { Rect } from '@xyflow/system'; @@ -17,22 +17,21 @@ const EdgeText: FC> = ({ className, ...rest }) => { - const edgeRef = useRef(null); - const [edgeTextBbox, setEdgeTextBbox] = useState({ x: 0, y: 0, width: 0, height: 0 }); + const [edgeTextBbox, setEdgeTextBbox] = useState({ x: 1, y: 0, width: 0, height: 0 }); const edgeTextClasses = cc(['react-flow__edge-textwrapper', className]); - useEffect(() => { - if (edgeRef.current) { - const textBbox = edgeRef.current.getBBox(); + const onEdgeTextRefChange = useCallback((edgeRef: SVGTextElement) => { + if (edgeRef === null) return; - setEdgeTextBbox({ - x: textBbox.x, - y: textBbox.y, - width: textBbox.width, - height: textBbox.height, - }); - } - }, [label]); + const textBbox = edgeRef.getBBox(); + + setEdgeTextBbox({ + x: textBbox.x, + y: textBbox.y, + width: textBbox.width, + height: textBbox.height, + }); + }, []); if (typeof label === 'undefined' || !label) { return null; @@ -57,7 +56,13 @@ const EdgeText: FC> = ({ ry={labelBgBorderRadius} /> )} - + {label} {children} diff --git a/packages/react/src/types/edges.ts b/packages/react/src/types/edges.ts index 287c277d..a9daf22c 100644 --- a/packages/react/src/types/edges.ts +++ b/packages/react/src/types/edges.ts @@ -99,8 +99,9 @@ export type EdgeProps = Pick< pathOptions?: any; }; -export type BaseEdgeProps = Pick & - EdgeLabelOptions & { +export type BaseEdgeProps = Pick & { + className?: CSSProperties; +} & EdgeLabelOptions & { id?: string; labelX?: number; labelY?: number;