0.3.1 • Published 3 years ago

react-observe-size v0.3.1

Weekly downloads
152
License
MIT
Repository
github
Last release
3 years ago

react-observe-size

Observe element size in React. Get the size of an element and updates whenever it changes.

Motivation

Components need to be able to respond to their render width, rendering differently based on the available space and updating when it changes. Layout changes can have many causes, such as window resizing, device orientation changes, CSS state changes or JavaScript events.

The Resize Observer spec will solve this problem. react-observe-size uses resize-observer-polyfill and provides an easy to use interface for React components.

Installation

npm install react-observe-size

or

yarn add react-observe-size

Usage

import React from 'react';
import ObserveSize from 'react-observe-size';

const MyComponent = () =>
    <ObserveSize>
        {({ width }) =>
            <div style={width < 600 ? styles.small : styles.large}>...</div>
        }
    </ObserveSize>

const styles = {
    small: {
        // ...
    },
    large: {
        // ...
    }
}

OR if you need the dimensions outside of your render method:

import React from 'react';
import ObserveSize from 'react-observe-size';

class MyComponent extends Component {
    updateDimensions = (width, height) => {
        // ...
    };
    
    render() {
        const { renderWidth } = this.state
        return (
            <ObserveSize observerFn={(contentRect) => { this.updateDimensions(contentRect.width, contentRect.height); }}>
                <div>...</div>
            </ObserveSize>
        )
    }
}

You can also combine the two examples above if you need both use cases.

Note: on the first render there is no layout yet so the contentRect will have its values set to 0. You can define defaults using the defaults prop to set values for the first render or you can choose to not render the first frame by checking the values for 0:

Use defaults for first frame:

<ObserveSize defaults={{width: 1000}}>
    {({ width }) =>
        <div>...</div>
    }
</ObserveSize>

Don't render first frame:

<ObserveSize>
    {({ width }) => width > 0 &&
        <div>...</div>
    }
</ObserveSize>

API

<ObserveSize />

Renders a div element that gets a resize observer. Will unbind observer on unmount.

PropertyTypeRequiredDescription
observerFnObserverFnnoCallback function that gets called on first render and on every layout change
childrenNode or function (contentRect: ContentRect) => NodenoThe children of the div element that gets monitored
defaultsPartial<ContentRect>noDefine defaults for first render. Can be useful for server-side rendering or to prevent flashes of first frame

ObserverFn

function (contentRect:ContentRect)

Callback function for the observer.

ContentRect

PropertyType
widthnumber
heightnumber
bottomnumber
topnumber
leftnumber
rightnumber
0.3.1

3 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.0

7 years ago