import {ReactNode, useEffect, useRef, useState} from 'react'; import styled from '@emotion/styled'; type Sizes = {height: number; width: number}; type ChildFn = (props: Sizes) => ReactNode; /** * Similar to from 'react-virtualized` but works better with flex & grid parents */ const MeasureSize = styled( ({children, className}: {children: ChildFn; className?: string}) => { const outerRef = useRef(null); const [sizes, setSizes] = useState(); useEffect(() => { if (outerRef.current) { const {height, width} = outerRef.current.getBoundingClientRect(); setSizes({height, width}); } }, []); return (
{sizes ? children(sizes) : null}
); } )` height: 100%; width: 100%; overflow: hidden; `; export default MeasureSize;