Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-measure

souporserious1.2mMIT2.5.2TypeScript support: definitely-typed

Compute measurements of React components.

react, component, measure, measurements, dimensions, element-queries, container-queries, size

readme

📏 React Measure

npm version Dependency Status

Compute measurements of React components. Uses a ResizeObserver to detect when an element's dimensions have changed.

Includes a polyfill for ResizeObserver in unsupported browsers.

Install

yarn add react-measure

npm install react-measure --save

<script src="https://unpkg.com/react-measure/dist/index.umd.js"></script>
(UMD library exposed as `ReactMeasure`)

Measure Component

Wrap any child component and calculate its client rect.

Props

client: PropTypes.bool

Adds the following to contentRect.client returned in the child function.

clientTop, clientLeft, clientWidth, and clientHeight.

offset: PropTypes.bool

Adds the following to contentRect.offset returned in the child function.

offsetTop, offsetLeft, offsetWidth, and offsetHeight.

scroll: PropTypes.bool

Adds the following to contentRect.scroll returned in the child function.

scrollTop, scrollLeft, scrollWidth, and scrollHeight.

bounds: PropTypes.bool

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.

Example

import Measure from 'react-measure'
import classNames from 'classnames'

class ItemToMeasure extends Component {
  state = {
    dimensions: {
      width: -1,
      height: -1,
    },
  }

  render() {
    const { width, height } = this.state.dimensions
    const className = classNames(width < 400 && 'small-width-modifier')

    return (
      <Measure
        bounds
        onResize={contentRect => {
          this.setState({ dimensions: contentRect.bounds })
        }}
      >
        {({ measureRef }) => (
          <div ref={measureRef} className={className}>
            I can do cool things with my dimensions now :D
            {height > 250 && (
              <div>Render responsive content based on the component size!</div>
            )}
          </div>
        )}
      </Measure>
    )
  }
}

withContentRect(types) HoC

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>
  )
)

Run Example

clone repo

git clone git@github.com:souporserious/react-measure.git

move into folder

cd ~/react-measure

install package dependencies

yarn

move into site folder and install local site dependencies

cd ~/site && yarn

run development mode

yarn gatsby develop

changelog

CHANGELOG

2.5.2

Default to global (possibly polyfilled) ResizeObserver if the local window does not have ResizeObserver #148

2.5.1

Use ResizeObserver of local window object #147

2.5.0

Set innerRef before observing #140

2.4.0

Use the local window object of the observed node #146

2.3.0

Fix regression with contentRect.bounds not getting called properly

Note that onResize will get called twice when first mounting now

2.2.6

Fix initial onResize delay caused by requestAnimationFrame #135

2.2.5

Fix sporadic ResizeObserver loop limit exceeded error when using onResize #133

2.2.4

Only observe one element, add tests #130

2.2.3

Fix not passing ResizeObserver entries to measure method #125

Add support for createRef #126

2.2.2

Add @babel/runtime as a dependency

2.2.1

Fix ResizeObserver callback error

Fix eslint warnings

2.2.0

Remove componentWillMount for React >16 StrictMode compliance #121

Upgrade get-node-dimensions package to 1.2.1

Upgrade prop-types package to 15.6.2

Fixes disconnect being used instead of unobserve for ResizeObserver

2.1.3

Update resize-observer-polyfill #88

Added handling when getComputedStyle returns null #89

Call setState within requestAnimationFrame to prevent infinite loop #118

2.1.2

Move children propType from with-content-rect to Measure #117

2.1.1

Allow children to be any element #78

2.1.0

Disconnect and de-initialize resize observer on unmount #112

Remove babel-plugin-add-module-exports

2.0.2

Disconnect correct node within handleRef #51

2.0.1

Observe and disconnect ResizeObserver in ref callback

2.0.0

Complete rewrite. Check README for new docs.

Most transitions from the old API should be easy. You just need to pass a ref callback down now. If you have any issues please feel free to file an issue.

1.4.7

Update to use separate prop-types package as per React 15.5 deprecation #43

1.4.6

Update to resize-observer-polyfill 1.4.1

1.4.5

Update to resize-observer-polyfill 1.3.1 to fix Webpack 2 issues #29

Remove monkey patch for importing resize-observer-polyfill

1.4.4

Use ResizeObserver.default if available. This fixes older browsers in the local dev environment.

1.4.3

Requiring default export of resize-observer-polyfill #28

1.4.2

Only require ResizeObserver polyfill when window is available

1.4.1

Cleanup old element-resize-detector code #23

1.4.0

Moved away from element-resize-detector in favor of resize-observer-polyfill 🎉

1.3.1

Fixes exception when changing key of rendered child #19

1.3.0

Update get-node-dimensions to 1.2.0

Use includeMargin to account for margins when calculating dimensions now

1.2.2

Fix dist build

1.2.1

Ensure setState is not called after unmounting #18

1.2.0

Provide dimension defaults

1.1.0

Update get-node-dimensions to 1.1.0

1.0.0

Update get-node-dimensions to 1.0.0

accurate renamed to useClone

Added cloneOptions prop that gets passed to getNodeDimensions

Fixed build to not include get-node-dimensions library

Removed bower support

0.5.1

Use properties instead of constructor

When unmounting, call uninstall in addition to removeAllListeners #15

0.5.0

Moved dimension calculations to its own library

Cleaned up build files for NPMCDN

0.4.2

Removed old code from lib folder

Make sure package.json cleans lib folder on each build

0.4.1

Fixed dist build

Updated to latest element-resize-detector

0.4.0

Moved away from MutationObserver's in favor of element-resize-detector

Added a more convenient API by allowing child functions #11

measure is now a public method available on the Measure component

accurate prop now returns both cloned element width and height

shouldMeasure now accepts only a boolean

Removed lodash.debounce dependency

0.3.5

Fixed bug in IE with accurate height calculation when checking for children nodes.

Fixed deprecation notice when calculating SVG dimensions.

Removed react-addons-shallow-compare dependency.

Moved react and react-dom packages into peer dependencies.

0.3.4

Fix server-side rendering

0.3.3

Added public method getDimensions

Clone nodes without any children

Fixed calculating measurements on resize

0.3.2

Patch to fix shallowCompare so bower works.

Added a resize handler to measure component changes on window resize.

0.3.1

Renamed onChange prop to onMeasure

Added shouldMeasure prop, similar to componentShouldUpdate. It determines whether or not the onMeasure callback will fire, useful for perf and not performing measurements if you don't need to.

Fixed updating of config prop to disconnect and reconnect a new MutationObserver with the new configuration

Fixed updaing of whitelist & blacklist props to use new values

0.3.0

Rebuilt from the ground up

No more cloning of elements!

Optimized to touch the DOM as least as possible

clone, forceAutoHeight, collection props removed

config prop added, accepts a MutationObserver configuration

accurate prop added, use to get an accurate measurement, only height supported right now

0.2.0

Upgraded to React 0.14.0

0.1.3

Added forceAutoHeight prop to help with proper height calculation when children heights are animating

0.1.2

Clone prop now exposed to allow optional cloning of component

Defaults to false which could potentially break components relying on cloned calculations

0.1.1

Set width/height to auto on clone no matter what to get a true dimension

Append clone directly after original instead of the end of its parent

Portal now gets destroyed after measurements have been calculated

0.1.0

Rewritten to be more React friendly

Measure component no longer accepts a child function, instead get dimensions by setting state in onChange callback