https://github.com/bootstarted/react-resize-observer
Component for giving you `onResize`.
https://github.com/bootstarted/react-resize-observer
bootstart react
Last synced: 8 months ago
JSON representation
Component for giving you `onResize`.
- Host: GitHub
- URL: https://github.com/bootstarted/react-resize-observer
- Owner: bootstarted
- License: mit
- Created: 2016-05-13T00:02:22.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-11-29T16:46:29.000Z (about 7 years ago)
- Last Synced: 2025-05-31T03:51:23.718Z (8 months ago)
- Topics: bootstart, react
- Language: JavaScript
- Size: 348 KB
- Stars: 48
- Watchers: 18
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# react-resize-observer
Component for giving you `onResize`.





## 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](#caveats) section.
```jsx
import ResizeObserver from 'react-resize-observer';
const MyComponent = () => (
Hello World
{
console.log('Resized. New bounds:', rect.width, 'x', rect.height);
}}
onPosition={(rect) => {
console.log('Moved. New position:', rect.left, 'x', rect.top);
}}
/>
);
```
## 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.
[this work]: https://github.com/marcj/css-element-queries/blob/master/src/ResizeSensor.js
[`DOMRect`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMRect