import { memo, useRef, useState, useEffect } from 'react'; import type { FC, PropsWithChildren } from 'react'; import cc from 'classcat'; import { EdgeTextProps, Rect } from '../../types'; const EdgeText: FC> = ({ x, y, label, labelStyle = {}, labelShowBg = true, labelBgStyle = {}, labelBgPadding = [2, 4], labelBgBorderRadius = 2, children, className, ...rest }) => { const edgeRef = useRef(null); const [edgeTextBbox, setEdgeTextBbox] = useState({ x: 0, y: 0, width: 0, height: 0 }); const edgeTextClasses = cc(['react-flow__edge-textwrapper', className]); useEffect(() => { if (edgeRef.current) { const textBbox = edgeRef.current const body = document.body.getBoundingClientRect() setEdgeTextBbox({ x: body.left - textBbox.getBoundingClientRect().left, y: body.top - textBbox.getBoundingClientRect().top, width: textBbox.offsetWidth, height: textBbox.offsetHeight, }); } }, [label]); if (typeof label === 'undefined' || !label) { return null; } return ( {labelShowBg && ( )}
{label}
{children}
); }; export default memo(EdgeText);