import type { ReactElement, SVGAttributes } from 'react'; import { Children, useLayoutEffect, useRef } from 'react'; interface SVGGroupProps extends SVGAttributes { children: ReactElement | ReactElement[]; direction?: 'row' | 'column'; space?: number; } export function SVGGroup(props: SVGGroupProps) { const elementsRef = useRef([]); const { children, direction = 'row', space = 0, ...resProps } = props; useLayoutEffect(() => { const elements = elementsRef.current; if (!elements) { return; } let shift = 0; for (const element of elements) { if (!element) { continue; } const boundary = element.getBBox(); if (direction === 'row') { element.setAttribute('transform', `translate(${shift} 0)`); shift += boundary.width + space; } else { element.setAttribute('transform', `translate(0 ${shift})`); shift += boundary.height + space; } } }); const items = Children.toArray(children); return ( {Children.map(items, (child, index) => { return ( { if (ref) { elementsRef.current[index] = ref; } }} > {child} ); })} ); }