2.5.2 ā€¢ Published 4 years ago

react-measure v2.5.2

Weekly downloads
317,986
License
MIT
Repository
github
Last release
4 years ago

šŸ“ 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

@yattho/gatsby-theme-rigitus@gooddata/react-components@gooddata/sdk-ui-charts@gooddata/sdk-ui-dashboard@gooddata/sdk-ui-vis-commons@gooddata/sdk-ui-ext@gooddata/sdk-ui-geo@gooddata/sdk-ui-kit@patternfly/react-topologyflowbuilder-testpopular-in-the-communitymozaik-ext-time-testuser-management-uiinsync-stageplayermarin-stageplayerlyrahealth-uicoresimple-widgetshaper-reactbeedeez-webapp-promobx-antd-admin2faccos-react-components@infinitebrahmanuniverse/nolb-react-med3chartwithtablewrapperassisted-ui-lib@everything-registry/sub-chunk-2567@wprdc-viz/vega@allganize/sdk@collinbrewer/react-navigation-view@contexture/react@ctl-bluesquad-components/ctl-bluesquad-ui-components@barbershopio/react-sprucebot@descarteslabs/cycle-widgets@bibletags/bibletags-widget@blumenkraft/nivo-core@byhuz/react-items-carousel@bridgetool/gatsby-theme-dev@canvas-panel/core@camberi/firecms@canva/app-ui-kit@geoffcox/react-splitter@gisce/react-ooui@github1/react-redux-common-modulesreact-cam-recorderreact-column-wrapreact-cube-image-revealreact-d3-hexbinreact-customizable-carouselreact-component-queryreact-clamp-textreact-manhattan-plotreact-measure-boxreact-fancy-tab-panelreact-fluid-containerreact-fluid-container-typescriptreact-expandablereact-flexi-tablereact-flex-tabsreact-forge-viewerreact-forge-viewer-eureact-content-sliderreact-imgixreact-google-publisher-tagreact-github-contribution-calendarreact-graph-editorreact-ludum-darereact-motion-gridreact-node-diagramsreact-reflexreact-popover-selectorreact-sketch-gridreact-chopreact-bottom-sheetreact-delightful-scrollerreact-dynamic-sheetreact-admin-fixed-layoutreact-items-carouselselectlyuccps-assisted-ui-libvoxeet-react-components-edotverioustip-toptextalive-react-apiwex-adminwex-admin-antdwebcmp-topo-chart-horizontalweb-modules-corewix-rich-content-previewwix-rich-content-plugin-social-pollswix-rich-content-editorwix-rich-content-plugin-commonsyuho.reacttg-react-reflextrinny-londonts-react-splittersynapse-react-clienttakeizi-cmssome-react-chartsstick-tree-gh-pagessportywide-webrs-components
2.5.2

4 years ago

2.5.1

4 years ago

2.5.0

4 years ago

2.4.0

4 years ago

2.3.0

5 years ago

2.2.6

5 years ago

2.2.5

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.3

5 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

3.0.0-rc.3

6 years ago

3.0.0-rc.2

6 years ago

3.0.0-rc.1

6 years ago

3.0.0-rc.0

6 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.4.7

7 years ago

1.4.6

7 years ago

1.4.5

7 years ago

1.4.4

7 years ago

1.4.3

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.5.1

7 years ago

0.5.0

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.5

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago