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 elementsRefs = useRef([]); const { children, direction = 'row', space = 0, ...resProps } = props; useLayoutEffect(() => { let shift = 0; const elements = elementsRefs.current; if (!elements) { return; } for (const element of elements) { if (element) { 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) { elementsRefs.current[index] = ref; } }} > {child} ); })} ); }