Socket
Socket
Sign inDemoInstall

react-resize-observer

Package Overview
Dependencies
3
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-resize-observer

Component for giving you `onResize`.


Version published
Weekly downloads
33K
decreased by-9.04%
Maintainers
2
Install size
32.5 kB
Created
Weekly downloads
 

Readme

Source

react-resize-observer

Component for giving you onResize.

build status coverage license version downloads

Overview

Primarily based on this work by Marc J. Schmidt.

Usage

npm install --save react react-dom react-resize-observer

Add ResizeObserver to the element whose size or position you want to measure. The only requirement is that your component must not have a position of static (see Caveats section.

import ResizeObserver from 'react-resize-observer';

const MyComponent = () => (
  <div style={{position: 'relative'}}>
    Hello World
    <ResizeObserver
      onResize={(rect) => {
        console.log('Resized. New bounds:', rect.width, 'x', rect.height);
      }}
      onPosition={(rect) => {
        console.log('Moved. New position:', rect.left, 'x', rect.top);
      }}
    />
  </div>
);

Component Props

onResize: function

optional

Called with a single DOMRect argument when a size change is detected.

onPosition: function

optional

Called with a single DOMRect argument when a position change is detected.

onReflow: function

optional

Called with a single DOMRect argument when either a position or size change is detected.

Caveats

Target Element Style

ResizeObserver will detect changes in the size or position of the closest containing block (an element with a position other than static) - so use either fixed, absolute, or relative on the element you are measuring.

The mechanism used to detect element size changes relies on the behavior of nested, absolutely positioned elements and their ability to trigger scroll events on their parent element. This is the reason this library is implemented as a rendered child element, and not as component enhancer.

Position Detection

The onPosition (an onReflow) callbacks will detect when the measured element's position in the viewport changes, but only when the change is caused by a scroll event of the window or an ancestor element with overflow: scroll. Position changes caused by other factors (i.e. transform, margin, top/left etc.) will not be immediately detected - although these changes will be observed and returned the next time a scroll event is captured.

If absolutely you need to capture position changes caused by style updates, calling document.body.dispatchEvent(new UIEvent('scroll')) will cause any mounted ResizeObserver instances to update.

Callback Result

This component returns raw DOMRect instances as the callback argument. DOMRect instances return {} when serialized to JSON (which will cause them to appear empty in Redux DevTools). DOMRect instances may crash the React Developer Tools extension if you try to inspect them as part of component state.

If any of these quirks become an issue, the solution is to map the values you need onto a plain object: https://stackoverflow.com/questions/39417566.

Keywords

FAQs

Last updated on 29 Nov 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc