import { memo, useState, useEffect, useRef } from 'react'; import cc from 'classcat'; import type { Rect } from '@xyflow/system'; import type { EdgeTextProps } from '../../types'; function EdgeTextComponent({ x, y, label, labelStyle = {}, labelShowBg = true, labelBgStyle = {}, labelBgPadding = [2, 4], labelBgBorderRadius = 2, children, className, ...rest }: EdgeTextProps) { const [edgeTextBbox, setEdgeTextBbox] = useState({ x: 1, y: 0, width: 0, height: 0 }); const edgeTextClasses = cc(['react-flow__edge-textwrapper', className]); const edgeTextRef = useRef(null); useEffect(() => { if (edgeTextRef.current) { const textBbox = edgeTextRef.current.getBBox(); setEdgeTextBbox({ x: textBbox.x, y: textBbox.y, width: textBbox.width, height: textBbox.height, }); } }, [label]); if (typeof label === 'undefined' || !label) { return null; } return ( {labelShowBg && ( )} {label} {children} ); } EdgeTextComponent.displayName = 'EdgeText'; export const EdgeText = memo(EdgeTextComponent);