1.1.1 • Published 5 years ago

react-resize-observer v1.1.1

Weekly downloads
11,854
License
(MIT OR CC0-1.0)
Repository
github
Last release
5 years ago

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.

uf-grydsxmejruf-rfc-dmmmeozhlpreact-flow-chart-zoomablevsnode@unificater/uf-rfc-dmmmeozhlpunificater-react-flow-charttest-reactflow-chartcustom-react-flow-chart@unificater/flow-chartmirador-biblissimaempath-video-player@linkedcamp/flow-chart@artemantcev/react-flow-chartchinese-mirador@everything-registry/sub-chunk-2607ra-mirador@kaliber/react-flow-chart@izumiz-dev/react-flow-chart@rsengo/react-flow-chart@nypl/web-readersimpl-chat@regang/miradorworkflow-container@mursai/react-flow-chartreact-antd-asset-manager@nakamura196/mirador@naveenraj006/react-flow-chartreact-lib-dnd-flowchart-reworked@mohamed-karawia/library@mitter-io/react-scl@mrblenny/react-flow-chart@msteinmn/react-flow-chartreact-flow-chart-updatedreact-flow-chart-zoom@lucho_1312/react-flow-chart@mathewjordan/mirador-garbreact-stack-gallery@notjiam/react-flow-chart@nicholas27/react-flow-chart@sublet/react-flow-chart@soytomasgoldenberg/react-flow-charttamu-mirador@applandstream/streaming-view-sdkflowtipflowtip-react-dommobisoft-react-flow-chartmobx-react-generator@columbia-libraries/mirador@darrenloasby/react-flow-chart@europeana/mirador@far-more/web-uimirador-knaw-huc-mui5mirador-videomiradormirador-annotation-editormirador-annotation-editor-video@bhuma.dev/react-dashboard@auguststurm/react-d3-chartsguima-react-flow-chart@codecademy/codebytescompoliciousboostcrm-widgetbemobirfcautomate-flow-chart@barmola/react-flow-chart@bastinjafari/react-flow-chart@bastinjafari/react-flow-chart-with-tooltips@bastinjafari/react-flow-chart-with-tooltips-and-multi-select@dshlass/react-flow-chart@eyecuelab/react-common@ielijose/react-flow-chart@grantnunneley/react-flow-chart@frdy/web-ui@getflywheel/local-components@georginaso/react-flow-chart@geourjoa/mirador-r18@futureworkshops/react-flow-chart@hxsllc/mirador@hxsllc/mirador-react17@hayaah/react-flow-chart@zpawn/app-buildertrust-kaz-react-flow-chart@ajuhos/react-flow-chart
1.1.1

5 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

7 years ago

0.1.0

8 years ago