Coder Social home page Coder Social logo

react-measure's Introduction

React Measure

npm version Dependency Status

Compute measurements of React components. Uses element-resize-detector to detect changes of an element and return the new dimensions.

Install

npm install react-measure --save

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

Codepen Demo

Live Demo

Example Usage w/ state

import Measure from 'react-measure';

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

  render() {
    const { height } = this.state.dimensions
    return (
      <Measure
        onMeasure={(dimensions) => {
          this.setState({dimensions})
        }}
      >
        <div>
          I can do cool things with my dimensions now :D
        </div>
      </Measure>
    )
  }
}

Example Usage w/ child function

import Measure from 'react-measure';

class ItemToMeasure extends Component {
  render() {
    return (
      <Measure>
        { dimensions =>
          <div>
            {Object.keys(dimensions).map((dimension, i) =>
              <div key={i}>{dimension}: {dimensions[dimension]}</div>
            )}
          </div>
        }
      </Measure>
    )
  }
}

Props

whitelist: PropTypes.array

Provide a list of properties to fire a callback for. Accepts any of the following properties ['width', 'height', 'top', 'right', 'bottom', 'left']

blacklist: PropTypes.array

Like above, but will not fire a callback for the specified properties.

useClone: PropTypes.bool

Tries to give the most accurate measure by cloning the element and measuring it. Use if you your item is hidden or you want know to find out what height/width you need to get to.

cloneOptions: PropTypes.Object

Passes clone options to getNodeDimensions.

shouldMeasure: PropTypes.bool

Determines whether or not a measurement should occur. Useful if you only need to measure in certain cases.

onMeasure: PropTypes.func

Callback when the component has been mutated. Receives the new dimensions of your component.

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 .

Run Example

clone repo

git clone [email protected]: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/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.