Skip to content

Commit 3fe5c44

Browse files
committed
more testing, and readme updates
1 parent 8a9576a commit 3fe5c44

File tree

3 files changed

+87
-16
lines changed

3 files changed

+87
-16
lines changed

CHANGELOG.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
## 5.0.0
44

5-
- **[BREAKING]** Removed Babel code transpiling and the ResizeObserver polyfill.
6-
- **[BREAKING]** Returning an object instead of an array, so that the returned
7-
values could be omitted when not needed.
5+
- **[BREAKING]** `#14` Removed Babel code transpiling and the ResizeObserver polyfill.
6+
- **[BREAKING]** `#21` Returning an object instead of an array, so that values not
7+
needed could be omitted.
88
- `#18` Added missing copyright notice in the MIT license.
9-
- New `package.json` scripts to ease development
109
- Improved ref handling:
11-
- You can now pass in your own ref
12-
- Ref changes are handled: TODO better explanation
13-
- The hook will no longer break if an invalid ref is passed in.
14-
(Anything other than an object with a `.current` value of an `Element` )
15-
- TODO: Document useDefaults
16-
- TODO: Document throttle / debounce solutions
10+
- `#16` You can now pass in your own ref
11+
- The same hook instance can now be reused with different refs
12+
- The hook will no longer break if the ref is not immediately filled.
13+
(Anything other than an object with a `.current` value of an `Element` will
14+
be ignored.)
15+
- Made defaults optional with the `useDefaults` option.
16+
- New `package.json` scripts to ease development
17+
- Added throttle and debounce guides to the readme
18+
- More tests
1719

1820
## 4.0.0
1921

README.md

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ yarn add use-resize-observer --dev
2424
npm install use-resize-observer --save-dev
2525
```
2626

27-
## Usage
27+
## Basic Usage
2828

2929
```js
3030
import React from "react";
@@ -43,7 +43,28 @@ const App = () => {
4343

4444
## Passing in your own `ref`
4545

46-
TODO
46+
You can pass in your own ref to measure, use now.
47+
This can be useful if you already have a ref from somewhere you want to measure.
48+
49+
```js
50+
const { ref, width, height } = useResizeObserver({
51+
defaultWidth: 100,
52+
defaultHeight: 50
53+
});
54+
```
55+
56+
You can even reuse the same hook instance to measure different elements:
57+
58+
[CodeSandbox Demo](https://codesandbox.io/s/use-resize-observer-reusing-refs-buftd)
59+
60+
## Throttle / Debounce
61+
62+
You might want values less frequently than actually reported.
63+
64+
While this hook does not come its own implementation of throttling / debouncing
65+
the reported values (Issue #19), you can use hook composition to achieve it:
66+
67+
[CodeSandbox Demo](https://codesandbox.io/s/use-resize-observer-throttle-and-debounce-8uvsg)
4768

4869
## SSR, Default Size
4970

@@ -54,17 +75,37 @@ const { ref, width, height } = useResizeObserver({
5475
defaultWidth: 100,
5576
defaultHeight: 50
5677
});
78+
```
79+
80+
Here "width" and "height" will be 100 and 50 respectively, until the
81+
ResizeObserver kicks in and reports the actual size.
82+
83+
## Without Default Measurements
5784

58-
// width / height will be 100 and 50 respectively, until the ResizeObserver
59-
// kicks in and reports the actual size.
85+
If you only want real measurements (only values from the ResizeObserver without
86+
defaults), then you can use the following:
87+
88+
```js
89+
const { ref, width, height } = useResizeObserver({
90+
useDefaults: false
91+
});
6092
```
6193

62-
## Contributing
94+
Here "width" and "height" will be undefined until the ResizeObserver takes its
95+
first measurement.
96+
97+
## Container/Element Query with CSS-in-JS
98+
99+
It's possible to apply styles conditionally based on the width / height of an
100+
element using a CSS-in-JS solution, which is the basic idea behind
101+
container/element queries:
102+
103+
[CodeSandbox Demo](https://codesandbox.io/s/use-resize-observer-container-query-with-css-in-js-iitxl)
63104

64105
## Related
65106

66-
- [@zeecoder/react-resize-observer](https://github.com/ZeeCoder/react-resize-observer)
67107
- [@zeecoder/container-query](https://github.com/ZeeCoder/container-query)
108+
- [@zeecoder/react-resize-observer](https://github.com/ZeeCoder/react-resize-observer)
68109

69110
## License
70111

tests/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,31 @@ it("should keep the same response instance between renders if nothing changed",
335335

336336
assertSameInstance();
337337
});
338+
339+
it("should ignore invalid custom refs", async () => {
340+
const Test = ({ resolveHandler }) => {
341+
const ref = useRef(null);
342+
const { width, height } = useResizeObserver({ ref: null }); // invalid custom ref
343+
344+
useEffect(() => {
345+
resolveHandler({
346+
assertSize: ({ width, height }) => {
347+
expect(ref.current.textContent).toBe(`${width}x${height}`);
348+
}
349+
});
350+
}, []);
351+
352+
return (
353+
<div ref={ref}>
354+
{width}x{height}
355+
</div>
356+
);
357+
};
358+
359+
const { assertSize } = await render(Test);
360+
361+
// Since no refs were passed in with an element to be measured, the hook should
362+
// stay on the defaults
363+
await delay(50);
364+
assertSize({ width: 1, height: 1 });
365+
});

0 commit comments

Comments
 (0)