Skip to content

Commit d71d65f

Browse files
author
Federico Zivolo
committed
v2.2.0
- Added onlyEvent property - Added tag property
1 parent c7a0492 commit d71d65f

File tree

4 files changed

+92
-19
lines changed

4 files changed

+92
-19
lines changed

README.md

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,94 @@ npm install react-resize-aware --save
2323
> **note**: `ResizeAware` needs a position different from `initial` to work!
2424
> Make sure to set it to `relative`, `absolute` or `fixed` using its `style` property or with CSS
2525
26+
## Stateless approach
27+
28+
If your component is stateless or you prefer to follow a functional approach
29+
you can use ResizeAware to wrap your existing component and it will take care
30+
to provide two property (`height` and `width`) that will get updated every time
31+
the component sizes change.
32+
2633
```jsx
2734
import React from 'react';
2835
import ResizeAware from 'react-resize-aware';
2936

30-
// This component will get rerendered everytime its width or height changes
37+
// This component will get re-rendered every time its width or height changes
3138
function MyComponent({width, height}) {
3239
return <div>{width}x{height}</div>;
3340
}
3441

3542
function App() {
3643
return (
37-
<ResizeAware onResize={({width, height}) => console.log(width, height)}>
44+
<ResizeAware style={{ position: 'relative' }}>
3845
<MyComponent />
3946
</ResizeAware>
4047
);
4148
}
49+
```
50+
51+
## Stateful approach
4252

53+
If your component is stateful or you need to use ResizeAware in the middle of one
54+
of your components you can use the `onResize` property of the component to provide
55+
a callback that will be called on each resize of the ResizeAware component and will
56+
provide as first argument an object with `width` and `height` properties.
57+
58+
```jsx
59+
import React, { Component } from 'react';
60+
import ResizeAware from 'react-resize-aware';
61+
62+
function MyComponent({width, height}) {
63+
return <div>{width}x{height}</div>;
64+
}
65+
66+
class MyComponent extend Component {
67+
handleResize = ({ width, height }) => console.log(width, height);
68+
69+
render() {
70+
return (
71+
<div>
72+
My app renders...
73+
<ResizeAware
74+
style={{ position: 'relative' }}
75+
onlyEvent
76+
onResize={this.handleResize}
77+
>
78+
<MyComponent />
79+
</ResizeAware>
80+
</div>
81+
);
82+
}
83+
}
4384
```
4485

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.
86+
## Properties
4787

48-
Also, you can define an optional `onResize` property to make `ResizeAware` call
49-
it on each resize and get the updated sizes.
88+
- The `onlyEvent` property will prevent ResizeAware from passing the `height` and `width`
89+
properties to its child component, in case you don't want to rely on them.
90+
- The `tag` property allows to define the HTML tag used by ResizeAware to render.
91+
- The `onResize` property is an optional callback called on each resize with as first
92+
argument an object containing `height` and `width` properties.
5093

94+
<!-- ## Self containing
95+
96+
If you need to keep your DOM structure clean and you don't want the additional
97+
`div` added by ResizeAware, you can use the component as base for your own one.
98+
99+
```jsx
100+
import React from 'react';
101+
import ResizeAware from 'react-resize-aware';
102+
103+
// This component will get re-rendered every time its width or height changes
104+
function MyComponent({width, height}) {
105+
return <div style={{ position: 'relative' }}>{width}x{height}</div>;
106+
}
107+
108+
function App() {
109+
return (
110+
<ResizeAware component={MyComponent} />
111+
);
112+
}
113+
``` -->
51114

52115
# License
53116

docs/react-resize-aware.js

Lines changed: 12 additions & 6 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.1.0",
3+
"version": "2.2.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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const objectStyle = {
2121
};
2222

2323
export default class ResizeAware extends Component {
24+
static defaultProps = {
25+
tag: 'div',
26+
};
27+
2428
state = {
2529
width: undefined,
2630
height: undefined,
@@ -60,24 +64,24 @@ export default class ResizeAware extends Component {
6064
};
6165

6266
render() {
63-
const {children, onResize, ...props} = this.props;
67+
const {children, onResize, onlyEvent, tag, ...props} = this.props;
6468
const {width, height} = this.state;
6569

6670
return createElement(
67-
'div',
71+
tag,
6872
{
6973
ref: el => (this.container = el),
7074
...props,
7175
},
72-
cloneElement(children, {
73-
width,
74-
height,
75-
}),
7676
createElement('object', {
7777
type: 'text/html',
7878
style: objectStyle,
7979
ref: el => (this.resizeElement = el),
8080
onLoad: this.handleObjectLoad,
81+
}),
82+
cloneElement(children, {
83+
width: onlyEvent ? undefined : width,
84+
height: onlyEvent ? undefined : height,
8185
})
8286
);
8387
}

0 commit comments

Comments
 (0)