Skip to content

Commit c7a0492

Browse files
author
Federico Zivolo
committed
v2.1.0
new onResize prop
1 parent f9fdd55 commit c7a0492

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ function MyComponent({width, height}) {
3434

3535
function App() {
3636
return (
37-
<ResizeAware>
37+
<ResizeAware onResize={({width, height}) => console.log(width, height)}>
3838
<MyComponent />
3939
</ResizeAware>
4040
);
4141
}
4242

4343
```
4444

45+
The child component of `ResizeAware` will have two new properties (`width` and `height`)
46+
always up to date with the real size of the element.
47+
48+
Also, you can define an optional `onResize` property to make `ResizeAware` call
49+
it on each resize and get the updated sizes.
50+
4551

4652
# License
4753

docs/example.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ function MyComponent({width, height}) {
1111
function App() {
1212
return React.createElement(
1313
ReactResizeAware,
14-
{style: {position: 'relative'}},
15-
React.createElement(MyComponent)
14+
{
15+
style: {position: 'relative'},
16+
onResize(sizes) {
17+
console.log(sizes);
18+
},
19+
},
20+
React.createElement(MyComponent, null)
1621
);
1722
}
1823

docs/react-resize-aware.js

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-resize-aware",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "A resize aware component used to detect sizes changes on your components",
55
"main": "dist/index.js",
66
"scripts": {

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ export default class ResizeAware extends Component {
5151

5252
// Function called on component resize
5353
handleResize = () => {
54-
this.setState({
54+
const sizes = {
5555
width: this.container.offsetWidth,
5656
height: this.container.offsetHeight,
57-
});
57+
};
58+
this.setState(sizes);
59+
this.props.onResize && this.props.onResize(sizes);
5860
};
5961

6062
render() {
61-
const {children, ...props} = this.props;
63+
const {children, onResize, ...props} = this.props;
6264
const {width, height} = this.state;
6365

6466
return createElement(

0 commit comments

Comments
 (0)