import type { SVGAttributes } from 'react'; import { useLayoutEffect, useRef, useState } from 'react'; interface SVGTextProps extends SVGAttributes { rectProps?: SVGAttributes; padding?: number; borderRadius?: number; } type BoxSize = Pick; const defaultTextBBox: BoxSize = { width: 0, height: 0, x: 0, y: 0, }; export function SVGText(props: SVGTextProps) { const textRef = useRef(null); const [boxSize, setBoxSize] = useState(defaultTextBBox); const { rectProps = {}, padding = 0, borderRadius = 0, children, ...otherProps } = props; const { ...otherRectProps } = rectProps; useLayoutEffect(() => { const boundary = textRef?.current?.getBBox(); if (boundary) { setBoxSize(boundary); } }, []); const { width: textWidth, height: textHeight, x, y } = boxSize; const width = textWidth + padding * 2; const height = textHeight + padding * 2; return ( {children} ); }