@rehooks/component-size
React hook for determining the size of a component
Note: This is using the new React Hooks API Proposal which is subject to change until React 16.7 final.
You'll need to install
react
,react-dom
, etc at^16.7.0-alpha.0
Install
yarn add @rehooks/component-size
Usage
{ let ref = let size = // size == { width: 100, height: 200 } let width height = size let imgUrl = `https://via.placeholder.com/x` return <div style= width: '100%' height: '100%' > <img ref=ref src=imgUrl /> </div> }
ResizeObserver
Resize Observer is the API is used to determine if an element is resized. Browser support is pretty good in Chrome, but is still missing support in other major browsers.
Polyfill
You can import the polyfill directly from here
yarn add resize-observer-polyfill
Then import it in your app:
If you are using Webpack (or similar) you could use dynamic imports, to load the Polyfill only if needed. A basic implementation could look something like this:
/*** Do feature detection, to figure out which polyfills needs to be imported.**/ { const polyfills = if ! polyfills return Promiseallpolyfills} { return 'ResizeObserver' in global && 'ResizeObserverEntry' in global && 'contentRect' in ResizeObserverEntryprototype }