Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 5b49665

Browse files
authored
Merge pull request #1 from ZeeCoder/v2
v2
2 parents aea81bc + 5a017da commit 5b49665

11 files changed

+6685
-28
lines changed

.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": [["@babel/env"]]
2+
"presets": [["@babel/preset-env"]]
33
}

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
node_modules
22
.idea
3-
yarn.lock
43
yarn-error.log
54
dist
5+
.cache

.npmignore

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ rollup.config.js
66
yarn.lock
77
yarn-error.log
88
browserslist
9+
karma.conf.js
10+
.cache
11+
.travis.yml
12+
tests

.travis.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: node_js
2+
node_js:
3+
- 10
4+
addons:
5+
chrome: stable
6+
script:
7+
- yarn build
8+
- yarn test:build
9+
- yarn test
10+
deploy:
11+
provider: npm
12+
13+
api_key:
14+
secure: AVhIhFozrf45OjXuFjpL734kSr3Ar/VGqaBzmggF0VWFqOa3stMt6t4gYLRJKrXfr9alMPhB4IdQ7fTjO0EIZh51pT7uRkjAJ8ZUXyDB6fohAFw2Q2mowufOMt2Pu0peC0SDWROosXk0asRAgWhjsMNcNiwgctjr0Pw2DHswq13QBcdL0/sTtTMRnawrjZJBbXtdDb0Q6nfygQjDav6NzpK1uT3elsNKlyf+MiOqNguwc1lgoIqfp2Twvj9a7TmbMf8TFiQjsXZgFZxPYwXZyi62gbcvySEZvEQnYGzUCGsgF+aM3GYsluFmOjMUaUnm9++48gDmKF9+aigowhu5vvVl8XdoI5RIjg/5ibwh04SlHhUab/0JqQy9HmfCkFXDHnzwDyRZMjNlXMfbX+XXSrY3R+WXIfeepdressTHJr3DoPdpkJmic5LN+i8zPjJH/+TU/MMzSot7lTQLD5q5hmxpZi1fCPxAzdVwn7s3uKDtJiUU0i0s864wu7VzRLNBxTLi5uR4mToFNtfQAzuOqOvhK7mGNqSYTIAeCnf67VLHWRLZcqR3bbRCJVt3PT+LKg51nDor/jBnuZTrph1DNo7vGfxIydyQ4EbY3QqDt1gcZoB5imGUc2+4pSzXA0xX9ijwCrNPuh7Vtyg+rV2/R6e9ecdWE5CSz6FzvG5k5TQ=
15+
on:
16+
tags: true
17+
skip_cleanup: true

CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# CHANGELOG
22

3+
## 2.0.0
4+
5+
- [BREAKING] ResizeObserver now always expects a function as a child, to which
6+
it passes in a reference as an argument that can be added to the element to be
7+
observed.
8+
- [BREAKING] Returning width and height as the second and third parameters
9+
instead of as part of an object
10+
- [BREAKING] Now reports 1x1 size on before the observer is triggered instead of
11+
0x0
12+
- Added tests using Karma
13+
14+
## 1.0.1
15+
16+
- Relaxed the prop-types dep
17+
318
## 1.0.0
419

520
- Initial release

README.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# @zeecoder/react-resize-observer
22

3-
A React component to use ResizeObserver instances.
3+
A React component that allows you to use a ResizeObserver to measure an element's size.
4+
5+
[![npm version](https://badge.fury.io/js/%40zeecoder%2Freact-resize-observer.svg)](https://npmjs.com/package/@zeecoder/react-resize-observer)
6+
[![build](https://travis-ci.org/ZeeCoder/react-resize-observer.svg?branch=master)](https://travis-ci.org/ZeeCoder/react-resize-observer)
47

58
## Install
69

@@ -15,9 +18,9 @@ npm install --save @zeecoder/react-resize-observer
1518
```js
1619
const App = () => (
1720
<ResizeObserver>
18-
{size => (
19-
<div>
20-
My size is {size.width}x{size.height}
21+
{(ref, width, height) => (
22+
<div ref={ref}>
23+
My size is {width}x{height}
2124
</div>
2225
)}
2326
</ResizeObserver>
@@ -26,17 +29,13 @@ const App = () => (
2629

2730
## Notes
2831

29-
- The children prop must be a function, through which the size changes are
30-
communicated
31-
- If the optional `element` prop is given, the the component will observe the
32-
size changes of that given element, instead of looking for the root DOM node
33-
inside the component. (This avoids calling `ReactDOM.findDOMNode(this)`.)
3432
- Uses [resize-observer-polyfill](https://github.com/que-etc/resize-observer-polyfill)
35-
internally, which falls back to the native ResizeObserver, if available
33+
internally, which falls back to the native ResizeObserver, if available.
3634

3735
## Related
3836

3937
- [use-resize-observer](https://github.com/ZeeCoder/use-resize-observer)
38+
- [@zeecoder/container-query](https://github.com/ZeeCoder/container-query)
4039

4140
## License
4241

karma.conf.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = function(config) {
2+
const singleRun = process.env.KARMA_SINGLE_RUN !== "false";
3+
const browsers = (process.env.KARMA_BROWSERS || "Chrome").split(",");
4+
5+
config.set({
6+
basePath: ".",
7+
frameworks: ["jasmine"],
8+
files: ["tests/dist/index.js"],
9+
autoWatch: true,
10+
11+
browsers,
12+
reporters: ["spec"],
13+
14+
singleRun,
15+
16+
// Max concurrency for SauceLabs OS plan
17+
concurrency: 5,
18+
19+
client: {
20+
jasmine: {
21+
// Order of the tests matter, so don't randomise it
22+
random: false
23+
}
24+
}
25+
});
26+
};

package.json

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "@zeecoder/react-resize-observer",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"main": "dist/bundle.cjs.js",
55
"module": "dist/bundle.esm.js",
66
"repository": "[email protected]:ZeeCoder/react-resize-observer.git",
7-
"description": "A React component to use ResizeObserver.",
7+
"description": "A React component that allows you to use a ResizeObserver to measure an element's size.",
88
"author": "Viktor Hubert <[email protected]>",
99
"license": "MIT",
1010
"scripts": {
1111
"build": "rollup -c",
12+
"test": "karma start",
13+
"test:build": "parcel build tests/index.js --out-dir tests/dist",
1214
"prepublish": "yarn build"
1315
},
1416
"husky": {
@@ -32,11 +34,23 @@
3234
},
3335
"devDependencies": {
3436
"@babel/core": "^7.1.2",
35-
"@babel/preset-env": "^7.1.0",
37+
"@babel/preset-env": "^7.2.3",
38+
"babel-regenerator-runtime": "^6.5.0",
39+
"delay": "^4.1.0",
3640
"husky": "^1.1.2",
41+
"karma": "^3.1.4",
42+
"karma-chrome-launcher": "^2.2.0",
43+
"karma-jasmine": "^2.0.1",
44+
"karma-spec-reporter": "^0.0.32",
3745
"lint-staged": "^7.3.0",
46+
"parcel-bundler": "^1.11.0",
3847
"prettier": "^1.14.3",
48+
"react": "^16.7.0",
49+
"react-dom": "^16.7.0",
3950
"rollup": "^0.66.6",
4051
"rollup-plugin-babel": "^4.0.3"
52+
},
53+
"publishConfig": {
54+
"access": "public"
4155
}
4256
}

src/index.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ class ResizeObserverComponent extends Component {
88
super(props);
99

1010
this.state = {
11-
width: 0,
12-
height: 0
11+
width: 1,
12+
height: 1
1313
};
14+
15+
this.ref = React.createRef();
1416
}
1517

1618
observeIfNeeded() {
17-
const element = this.props.element || ReactDOM.findDOMNode(this);
19+
const element = this.ref.current;
1820

1921
if (element && this.element !== element) {
2022
// clean up after a previous element
@@ -60,24 +62,18 @@ class ResizeObserverComponent extends Component {
6062
}
6163

6264
componentWillUnmount() {
63-
if (this.element) {
64-
this.unobserve(this.element);
65+
if (this.ref.current) {
66+
this.unobserve(this.ref.current);
6567
}
6668
}
6769

6870
render() {
69-
const size = {
70-
width: this.state.width,
71-
height: this.state.height
72-
};
73-
74-
return this.props.children(size);
71+
return this.props.children(this.ref, this.state.width, this.state.height);
7572
}
7673
}
7774

7875
ResizeObserverComponent.propTypes = {
79-
children: PropTypes.func.isRequired,
80-
element: PropTypes.object
76+
children: PropTypes.func.isRequired
8177
};
8278

8379
export default ResizeObserverComponent;

tests/index.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
import ResizeObserver from "../dist/bundle.esm";
4+
import delay from "delay";
5+
// Using the following to support async/await in tests.
6+
// I'm intentionally not using babel/polyfill, as that would introduce polyfills
7+
// the actual lib might not have, giving the false impression that something
8+
// works while it might actually not, if you use the lib without babel-polyfill.
9+
import "babel-regenerator-runtime";
10+
11+
const Observed = () => (
12+
<ResizeObserver>
13+
{(ref, width, height) => (
14+
<div
15+
ref={ref}
16+
id="observed"
17+
style={{
18+
position: "absolute",
19+
left: 0,
20+
top: 0,
21+
width: "100%",
22+
height: "100%",
23+
background: "grey",
24+
color: "white",
25+
fontWeight: "bold"
26+
}}
27+
>
28+
{width}x{height}
29+
</div>
30+
)}
31+
</ResizeObserver>
32+
);
33+
34+
beforeAll(() => {
35+
const app = document.createElement("div");
36+
app.style.position = "relative";
37+
app.style.width = "200px";
38+
app.style.height = "300px";
39+
document.body.appendChild(app);
40+
41+
ReactDOM.render(<Observed />, app);
42+
43+
global.app = app;
44+
global.observed = document.querySelector("#observed");
45+
});
46+
47+
it("should render with 1x1 initially, before the ResizeObserver is triggered", async () => {
48+
expect(observed.textContent).toBe("1x1");
49+
});
50+
51+
it("should report the correct size after the size is reported by the ResizeObserver", async () => {
52+
await delay(100);
53+
54+
expect(observed.textContent).toBe("200x300");
55+
});
56+
57+
it("should report following size changes", async () => {
58+
app.style.width = "100px";
59+
app.style.height = "100px";
60+
61+
await delay(100);
62+
expect(observed.textContent).toBe("100x100");
63+
});

0 commit comments

Comments
 (0)