Skip to content

Commit ecf6e5f

Browse files
committed
Default size feature refactors
1 parent e1018b5 commit ecf6e5f

File tree

7 files changed

+43
-20
lines changed

7 files changed

+43
-20
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ addons:
66
script:
77
- yarn build
88
- yarn test:build
9-
- yarn test
9+
- yarn test --singleRun
1010
deploy:
1111
provider: npm
1212

CHANGELOG.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@
22

33
## 3.2.0
44

5-
- Added option to pass default width and height. This is useful when using this plugin on the SSR side.
6-
7-
Demo:
8-
9-
```js
10-
const DEFAULT_WIDTH = 1024;
11-
const DEFAULT_HEIGHT = 600;
12-
13-
const [ref, width, height] = useResizeObserver(DEFAULT_WIDTH, DEFAULT_HEIGHT);
14-
```
5+
- Added option to pass default width and height. (Useful when using this plugin
6+
on the SSR side.)
157

168
## 3.1.0
179

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ const App = () => {
3434
};
3535
```
3636

37+
## SSR, Default Size
38+
39+
You can set the default custom size, which is useful for SSR.
40+
41+
```js
42+
const [ref, width, height] = useResizeObserver({
43+
defaultWidth: 100,
44+
defaultHeight: 50
45+
});
46+
47+
// width / height will be 100 and 50 respectively, until the ResizeObserver kicks in.
48+
```
49+
3750
## Notes
3851

3952
- Uses [resize-observer-polyfill](https://github.com/que-etc/resize-observer-polyfill)

karma.conf.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ module.exports = function(config) {
1010
browsers,
1111
reporters: ["spec"],
1212

13-
singleRun: true,
14-
1513
// Max concurrency for SauceLabs OS plan
1614
concurrency: 5,
1715

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
"license": "MIT",
1010
"scripts": {
1111
"build": "rollup -c",
12+
"watch": "rollup -c -w",
1213
"test": "karma start",
1314
"test:build": "parcel build tests/index.js --out-dir tests/dist",
15+
"test:watch": "parcel watch tests/index.js --out-dir tests/dist",
1416
"prepublish": "yarn build"
1517
},
1618
"husky": {

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useEffect, useState, useRef } from "react";
22
import ResizeObserver from "resize-observer-polyfill";
33

4-
export default function(defaultWidth = 1, defaultHeight = 1) {
5-
const ref = useRef();
4+
export default function({ defaultWidth = 1, defaultHeight = 1 } = {}) {
5+
const ref = useRef(null);
66
const [width, changeWidth] = useState(defaultWidth);
77
const [height, changeHeight] = useState(defaultHeight);
88

tests/index.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import delay from "delay";
88
// works while it might actually not, if you use the lib without babel-polyfill.
99
import "babel-regenerator-runtime";
1010

11-
const Observed = () => {
12-
const [ref, width, height] = useResizeObserver();
11+
const Observed = ({ defaultWidth, defaultHeight, ...props }) => {
12+
const [ref, width, height] = useResizeObserver({
13+
defaultWidth: defaultWidth || 1,
14+
defaultHeight: defaultHeight || 1
15+
});
1316

1417
return (
1518
<div
19+
{...props}
1620
ref={ref}
17-
id="observed"
1821
style={{
1922
position: "absolute",
2023
left: 0,
@@ -38,20 +41,35 @@ beforeAll(() => {
3841
app.style.height = "300px";
3942
document.body.appendChild(app);
4043

41-
ReactDOM.render(<Observed />, app);
44+
ReactDOM.render(
45+
<React.Fragment>
46+
<Observed id="observed" />
47+
<Observed
48+
id="observed-with-defaults"
49+
defaultWidth={24}
50+
defaultHeight={42}
51+
/>
52+
</React.Fragment>,
53+
app
54+
);
4255

4356
global.app = app;
4457
global.observed = document.querySelector("#observed");
58+
global.observedWithDefaults = document.querySelector(
59+
"#observed-with-defaults"
60+
);
4561
});
4662

47-
it("should render with 1x1 initially, before the ResizeObserver is triggered", async () => {
63+
it("should render with the right defaults, before the ResizeObserver is triggered", async () => {
4864
expect(observed.textContent).toBe("1x1");
65+
expect(observedWithDefaults.textContent).toBe("24x42");
4966
});
5067

5168
it("should report the correct size after the size is reported by the ResizeObserver", async () => {
5269
await delay(100);
5370

5471
expect(observed.textContent).toBe("200x300");
72+
expect(observedWithDefaults.textContent).toBe("200x300");
5573
});
5674

5775
it("should report following size changes", async () => {

0 commit comments

Comments
 (0)