Uses
getBoundingClientRect
to calculate the element rect and add it to contentRect.bounds returned in the
child function.
margin: PropTypes.bool
Uses
getComputedStyle
to calculate margins and add it to contentRect.margin returned in the child
function.
innerRef: PropTypes.func
Use this to access the internal component ref.
onResize: PropTypes.func
Callback invoked when either element width or height have changed. Note that this will be called twice on mount to get the initial values. The first call will come from componentDidMount while the second call will come from the ResizeObserver.
children: PropTypes.func
Children must be a function. Will receive the following object shape:
measureRef: must be passed down to your component's ref in order to obtain a
proper node to measure
measure: use to programmatically measure your component, calls the internal
measure method in withContentRect
contentRect: this will contain any of the following allowed rects from
above: client, offset, scroll, bounds, or margin. It will also
include entry from the ResizeObserver when available.
A higher-order component that provides dimensions to the wrapped component.
Accepts types, which determines what measurements are returned, similar to
above. Then returns a function to pass the component you want measured.
Pass an array or single value of either client, offset, scroll, bounds,
or margin to calculate and receive those measurements as the prop
contentRect in your wrapped component. You can also use the measure function
passed down to programmatically measure your component if you need to. And
finally, remember to pass down the measureRef to the component you want
measured.
Passes down the same props as the Measure child function above, measureRef,
measure, and contentRect.
Fun fact, the Measure component is a thin wrapper around withContentRect.
Just check
the source.
This means your wrapped component will accept the same props as Measure does
😊
Example
import { withContentRect } from 'react-measure'const ItemToMeasure = withContentRect('bounds')( ({ measureRef, measure, contentRect }) => ( <div ref={measureRef}> Some content here <pre>{JSON.stringify(contentRect, null, 2)}</pre> </div> ))