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

Commit 5296e8e

Browse files
committed
initial
0 parents  commit 5296e8e

File tree

11 files changed

+282
-0
lines changed

11 files changed

+282
-0
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": [["@babel/env"]]
3+
}

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
tab_width = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.idea
3+
yarn.lock
4+
yarn-error.log
5+
dist

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
src
2+
.babelrc
3+
rollup.config.js
4+
.editorconfig

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# CHANGELOG
2+
3+
## 1.0.0
4+
5+
- Initial release

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import React, { Component } from "react";
2+
import ReactDOM from "react-dom";
3+
import PropTypes from "prop-types";
4+
import ResizeObserver from "resize-observer-polyfill";
5+
import WeakMap from "es6-weak-map";
6+
7+
const registry = new WeakMap();
8+
9+
const resizeObserver = new ResizeObserver(entries => {
10+
entries.forEach(entry => {
11+
const instance = registry.get(entry.target);
12+
if (!instance) {
13+
return;
14+
}
15+
16+
instance.handleResize(entry.contentRect);
17+
18+
});
19+
});
20+
21+
export default class ResizeObserverComponent extends Component {
22+
constructor(props) {
23+
super(props);
24+
25+
this.state = { size: null };
26+
27+
this.lastRegisteredElement = null;
28+
29+
}
30+
31+
componentDidMount() {
32+
const element = ReactDOM.findDOMNode(this);
33+
34+
if (element) {
35+
registry.set(element, this);
36+
resizeObserver.observe(element);
37+
this.lastRegisteredElement = element;
38+
}
39+
40+
}
41+
42+
handleResize(size) {
43+
this.setState({ size });
44+
}
45+
46+
componentWillUnmount() {
47+
if (this.lastRegisteredElement) {
48+
registry.delete(this.lastRegisteredElement);
49+
resizeObserver.unobserve(this.lastRegisteredElement);
50+
}
51+
}
52+
53+
componentDidUpdate() {
54+
const element = ReactDOM.findDOMNode(this);
55+
if (this.lastRegisteredElement === element) {
56+
return;
57+
}
58+
59+
if (this.lastRegisteredElement !== null) {
60+
registry.delete(this.lastRegisteredElement);
61+
resizeObserver.unobserve(this.lastRegisteredElement);
62+
this.lastRegisteredElement = null;
63+
}
64+
65+
if (element) {
66+
registry.set(element, this);
67+
resizeObserver.observe(element);
68+
this.lastRegisteredElement = element;
69+
}
70+
71+
}
72+
73+
render() {
74+
if (this.props.children) {
75+
return this.props.children(this.state.size);
76+
}
77+
78+
return this.props.render(this.state.size);
79+
80+
}
81+
}
82+
83+
ResizeObserverComponent.defaultProps = {
84+
render: size => (
85+
<div>My size is: {size ? `${size.width}x${size.height}` : "?"}</div>
86+
)
87+
};
88+
89+
ResizeObserverComponent.propTypes = {
90+
render: PropTypes.func,
91+
children: PropTypes.func
92+
};

browserslist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
last 1 version
2+
> 1%
3+
not dead

package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "@zeecoder/react-resize-observer",
3+
"version": "1.0.0",
4+
"main": "dist/bundle.cjs.js",
5+
"module": "dist/bundle.esm.js",
6+
"repository": "[email protected]:ZeeCoder/use-resize-observer.git",
7+
"description": "A React component to use ResizeObserver.",
8+
"author": "Viktor Hubert <[email protected]>",
9+
"license": "MIT",
10+
"scripts": {
11+
"build": "rollup -c",
12+
"prepublish": "yarn build"
13+
},
14+
"husky": {
15+
"hooks": {
16+
"pre-commit": "lint-staged"
17+
}
18+
},
19+
"lint-staged": {
20+
"*.{js,md}": [
21+
"prettier --write",
22+
"git add"
23+
]
24+
},
25+
"dependencies": {
26+
"prop-types": "^15.6.2",
27+
"resize-observer-polyfill": "^1.5.0"
28+
},
29+
"peerDependencies": {
30+
"react": "^16.6.0",
31+
"react-dom": "^16.6.0"
32+
},
33+
"devDependencies": {
34+
"@babel/core": "^7.1.2",
35+
"@babel/preset-env": "^7.1.0",
36+
"husky": "^1.1.2",
37+
"lint-staged": "^7.3.0",
38+
"prettier": "^1.14.3",
39+
"rollup": "^0.66.6",
40+
"rollup-plugin-babel": "^4.0.3"
41+
}
42+
}

rollup.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import babel from "rollup-plugin-babel";
2+
3+
export default {
4+
input: "src/index.js",
5+
output: [
6+
{
7+
file: "dist/bundle.cjs.js",
8+
format: "cjs"
9+
},
10+
{
11+
file: "dist/bundle.esm.js",
12+
format: "esm"
13+
}
14+
],
15+
plugins: [babel()],
16+
external: ["react", "resize-observer-polyfill", "react-dom", "prop-types"]
17+
};

0 commit comments

Comments
 (0)