CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-measure

React component library for computing and tracking DOM element measurements using ResizeObserver

Pending
Overview
Eval results
Files

measure-component.mddocs/

Measure Component

The Measure component is the primary interface for measuring React components. It uses the render props pattern to provide measurement data to child components and handles ResizeObserver lifecycle management.

Capabilities

Measure Component

Wraps any child component and calculates its dimensions using ResizeObserver.

/**
 * React component that provides element measurement capabilities via render props
 * @param {Object} props - Component props
 * @param {boolean} [props.client] - Include client measurements (clientTop, clientLeft, clientWidth, clientHeight)
 * @param {boolean} [props.offset] - Include offset measurements (offsetTop, offsetLeft, offsetWidth, offsetHeight)
 * @param {boolean} [props.scroll] - Include scroll measurements (scrollTop, scrollLeft, scrollWidth, scrollHeight)
 * @param {boolean} [props.bounds] - Include bounds measurements via getBoundingClientRect
 * @param {boolean} [props.margin] - Include margin measurements via getComputedStyle
 * @param {Object|Function} [props.innerRef] - Ref to access the internal component
 * @param {Function} [props.onResize] - Callback invoked when element dimensions change
 * @param {Function} props.children - Render prop function that receives measurement data
 * @returns {React.ReactElement}
 */
function Measure(props);

// Children render prop function receives object with:
// {
//   measureRef: function - Must be passed to the element you want to measure as its ref
//   measure: function - Function to programmatically trigger measurement
//   contentRect: object - Object containing the current measurements
// }

Usage Examples:

import React, { Component } from 'react';
import Measure from 'react-measure';

// Basic bounds measurement
class BasicExample extends Component {
  state = { dimensions: { width: -1, height: -1 } };

  render() {
    return (
      <Measure
        bounds
        onResize={contentRect => {
          this.setState({ dimensions: contentRect.bounds });
        }}
      >
        {({ measureRef }) => (
          <div ref={measureRef}>
            Size: {this.state.dimensions.width} x {this.state.dimensions.height}
          </div>
        )}
      </Measure>
    );
  }
}

// Multiple measurement types
class MultiMeasureExample extends Component {
  render() {
    return (
      <Measure bounds scroll margin>
        {({ measureRef, contentRect }) => (
          <div ref={measureRef} style={{ padding: '20px', overflow: 'auto' }}>
            <pre>{JSON.stringify(contentRect, null, 2)}</pre>
          </div>
        )}
      </Measure>
    );
  }
}

// With innerRef for external access
class InnerRefExample extends Component {
  elementRef = React.createRef();

  handleClick = () => {
    if (this.elementRef.current) {
      console.log('Direct access to element:', this.elementRef.current);
    }
  };

  render() {
    return (
      <Measure bounds innerRef={this.elementRef}>
        {({ measureRef }) => (
          <div ref={measureRef} onClick={this.handleClick}>
            Click me to log element to console
          </div>
        )}
      </Measure>
    );
  }
}

Measurement Types

Each measurement type enables different DOM properties to be included in the contentRect.

Client Measurements

/**
 * Include client measurements in contentRect.client
 * @param {boolean} client - When true, includes clientTop, clientLeft, clientWidth, clientHeight
 */
client: true

Includes: clientTop, clientLeft, clientWidth, clientHeight

Offset Measurements

/**
 * Include offset measurements in contentRect.offset
 * @param {boolean} offset - When true, includes offsetTop, offsetLeft, offsetWidth, offsetHeight
 */
offset: true

Includes: offsetTop, offsetLeft, offsetWidth, offsetHeight

Scroll Measurements

/**
 * Include scroll measurements in contentRect.scroll
 * @param {boolean} scroll - When true, includes scrollTop, scrollLeft, scrollWidth, scrollHeight
 */
scroll: true

Includes: scrollTop, scrollLeft, scrollWidth, scrollHeight

Bounds Measurements

/**
 * Include bounds measurements in contentRect.bounds
 * @param {boolean} bounds - When true, uses getBoundingClientRect()
 */
bounds: true

Uses getBoundingClientRect() to include: top, right, bottom, left, width, height

Margin Measurements

/**
 * Include margin measurements in contentRect.margin
 * @param {boolean} margin - When true, uses getComputedStyle() for margins
 */
margin: true

Uses getComputedStyle() to include computed margins: top, right, bottom, left

Lifecycle and Performance

The Measure component automatically:

  • Creates a ResizeObserver instance on mount
  • Observes the measured element when measureRef is attached
  • Calls onResize twice on mount (once from componentDidMount, once from ResizeObserver)
  • Uses requestAnimationFrame to batch DOM updates for performance
  • Cleans up ResizeObserver on unmount
  • Handles ref changes by unobserving old elements and observing new ones

Install with Tessl CLI

npx tessl i tessl/npm-react-measure

docs

index.md

measure-component.md

with-content-rect.md

tile.json