const React = window.React; const {Fragment, useRef, useState} = React; const colors = [ '#e74c3c', '#3498db', '#2ecc71', '#9b59b6', '#f39c12', '#1abc9c', ]; export default function PrintRectsFragmentContainer({children}) { const fragmentRef = useRef(null); const [rects, setRects] = useState([]); const getRects = () => { const rectsResult = fragmentRef.current.getClientRects(); setRects(Array.from(rectsResult)); }; const getColor = index => colors[index % colors.length]; return (
{rects.length > 0 && ( Found {rects.length} rect{rects.length !== 1 ? 's' : ''} )}
{rects.length === 0 && (
Click button to visualize rects
)} {rects.map(({x, y, width, height}, index) => { const scale = 0.3; const color = getColor(index); return (
); })}
{rects.map(({x, y, width, height}, index) => { const color = getColor(index); return (
#{index}{' '} x: {Math.round(x)}, y: {Math.round(y)}, w: {Math.round(width)} , h: {Math.round(height)}
); })}
{children}
); }