JSPM

  • Created
  • Published
  • Downloads 545123
  • Score
    100M100P100Q174778F
  • License MIT

Compute measurements of React components.

Package Exports

  • react-measure

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-measure) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

React Measure 0.3.0

Compute measurements of React components.

Install

npm install react-measure --save

bower install react-measure --save

Example Usage

import Measure from 'react-measure';

class ItemToMeasure extends Component {
  state = {
    dimensions: {}
  }

  render() {
    const { width, height } = this.state.dimensions

    return(
      <Measure
        whitelist={['width', 'height']}
        onChange={d => this.setState({dimensions: d})}
      >
        <div>
          I can do cool things with my dimensions now :D
        </div>
      </Measure>
    )
  }
}

Good to knows

To help avoid layout thrashing, use the prop blacklist to ignore specific values and stop firing a render to check the DOM for changes. Likewise you can use whitelist to choose only the ones you need to check.

Margins from hell. If your element is not calculating width or height properly it could be due to a margin hanging outside of its container. To get a true measurement, make sure to not have any hanging margins, in some cases a padding of 1px added to the container will fix this. See the stack overflow answers here for more tricks .

Browser Support

All modern browsers supported. IE 9 & 10 support with a MutationObserver polyfill. I recommend this one

Run Example

clone repo

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

move into folder

cd ~/react-measure

install dependencies

npm install

run dev mode

npm run dev

open your browser and visit: http://localhost:8080/

CHANGELOG

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