Skip to content

Commit 192df28

Browse files
committed
update docs to use resize-observer-polyfill as a ponyfill
1 parent 25f40f0 commit 192df28

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

README.md

+43-26
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,69 @@ function MyComponent() {
2929

3030
## ResizeObserver
3131

32-
[Resize Observer](https://developers.google.com/web/updates/2016/10/resizeobserver)
33-
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.
32+
If it is present, this library uses the recent [`ResizeObserver` browser
33+
API](https://developers.google.com/web/updates/2016/10/resizeobserver) to
34+
determine if an element's content size has changed.
35+
36+
If a browser does not have the `ResizeObserver` API present, then this library
37+
falls back to listening on window size changes, which is less efficient and does
38+
not listen for changes to the component's size due to other factors like content
39+
changes. If it is not present, you can use pass a `ResizeObserver`
40+
implementation into the `useComponentSize()` hook (see below).
41+
42+
Browser support is pretty good in Chrome, but is still missing support in other
43+
major browsers.
3444

3545
> [Can i use ResizeObserver?](https://caniuse.com/#feat=resizeobserver)
3646
3747
### Polyfill
3848

39-
You can import the
40-
[polyfill](https://github.com/que-etc/resize-observer-polyfill) directly from here
49+
You can import [a ResizeObserver
50+
ponyfill](https://github.com/que-etc/resize-observer-polyfill) with this NPM
51+
library:
4152

4253
```sh
4354
yarn add resize-observer-polyfill
4455
```
4556

46-
Then import it in your app:
57+
Then use it with the `useComponentSize()` hook:
4758

4859
```js
49-
import 'resize-observer-polyfill'
60+
import ResizeObserver from 'resize-observer-polyfill'
61+
// ...
62+
useComponentSize(ref, { ResizeObserver });
5063
```
5164

5265
If you are using Webpack (or similar) you could use [dynamic
53-
imports](https://webpack.js.org/api/module-methods/#import-), to load the
66+
imports](https://webpack.js.org/api/module-methods/#import), to load the
5467
Polyfill only if needed. A basic implementation could look something like this:
5568

5669
```js
57-
loadPolyfills()
58-
.then(() => /* Render React application now that your Polyfills are ready */)
59-
60-
/**
61-
* Do feature detection, to figure out which polyfills needs to be imported.
62-
**/
63-
function loadPolyfills() {
64-
const polyfills = []
65-
66-
if (!supportsResizeObserver()) {
67-
polyfills.push(import('resize-observer-polyfill'))
68-
}
69-
70-
return Promise.all(polyfills)
71-
}
72-
73-
function supportsResizeObserver() {
74-
return (
70+
function getResizeObserver() {
71+
if (
7572
'ResizeObserver' in global &&
7673
'ResizeObserverEntry' in global &&
7774
'contentRect' in ResizeObserverEntry.prototype
78-
)
75+
) {
76+
return Promise.resolve(ResizeObserver);
77+
}
78+
return import('resize-observer-polyfill');
7979
}
8080
```
81+
82+
And in your component:
83+
```js
84+
const [ResizeObserverApi, setResizeObserverApi] = setState();
85+
useEffect(() => {
86+
let cancelled = false;
87+
getResizeObserver().then(observer => {
88+
if (!cancelled) {
89+
setResizeObserverApi(observer);
90+
}
91+
});
92+
return () => {
93+
cancelled = true;
94+
}
95+
}, []);
96+
useComponentSize(ref, { ResizeObserver: ResizeObserverApi });
97+
```

0 commit comments

Comments
 (0)