Skip to content

Commit d22076f

Browse files
author
Federico Zivolo
committed
v2.3.0
- Allow to define a custom component to use as base of ResizeAware - Added some useless tests
1 parent 8bff18c commit d22076f

File tree

12 files changed

+2464
-130
lines changed

12 files changed

+2464
-130
lines changed

.babelrc

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

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
dist/
33
node_modules/
4+
coverage/

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,7 @@ class MyComponent extend Component {
8080
}
8181
}
8282
```
83-
84-
## Properties
85-
86-
- The `onlyEvent` property will prevent ResizeAware from passing the `height` and `width`
87-
properties to its child component, in case you don't want to rely on them.
88-
- The `tag` property allows to define the HTML tag used by ResizeAware to render.
89-
- The `onResize` property is an optional callback called on each resize with as first
90-
argument an object containing `height` and `width` properties.
91-
92-
<!-- ## Self containing
83+
## Self containing
9384

9485
If you need to keep your DOM structure clean and you don't want the additional
9586
`div` added by ResizeAware, you can use the component as base for your own one.
@@ -99,16 +90,32 @@ import React from 'react';
9990
import ResizeAware from 'react-resize-aware';
10091

10192
// This component will get re-rendered every time its width or height changes
102-
function MyComponent({width, height}) {
103-
return <div style={{ position: 'relative' }}>{width}x{height}</div>;
93+
// It must expose a `getRef` property and must allow its `children` to be rendered
94+
// as direct descendant
95+
// The `getRef` property must be assigned to the `ref` property of the main element
96+
function MyComponent({width, height, getRef, children}) {
97+
return (
98+
<div ref={getRef} style={{ position: 'relative' }}>
99+
<span>{width}x{height}</span>
100+
{children}
101+
</div>
102+
);
104103
}
105104

106105
function App() {
107106
return (
108107
<ResizeAware component={MyComponent} />
109108
);
110109
}
111-
``` -->
110+
```
111+
112+
## Properties
113+
114+
- The `onlyEvent` property will prevent ResizeAware from passing the `height` and `width`
115+
properties to its child component, in case you don't want to rely on them.
116+
- The `component` property allows to define the HTML tag used by ResizeAware to render, or any React component.
117+
- The `onResize` property is an optional callback called on each resize with as first
118+
argument an object containing `height` and `width` properties.
112119

113120
# License
114121

docs/example.js

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,62 @@
1-
function MyComponent({width, height}) {
1+
'use strict';
2+
3+
var _extends =
4+
Object.assign ||
5+
function(target) {
6+
for (var i = 1; i < arguments.length; i++) {
7+
var source = arguments[i];
8+
for (var key in source) {
9+
if (Object.prototype.hasOwnProperty.call(source, key)) {
10+
target[key] = source[key];
11+
}
12+
}
13+
}
14+
return target;
15+
};
16+
17+
function _objectWithoutProperties(obj, keys) {
18+
var target = {};
19+
for (var i in obj) {
20+
if (keys.indexOf(i) >= 0) continue;
21+
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
22+
target[i] = obj[i];
23+
}
24+
return target;
25+
}
26+
27+
function MyComponent(_ref) {
28+
var width = _ref.width,
29+
height = _ref.height,
30+
getRef = _ref.getRef,
31+
children = _ref.children,
32+
props = _objectWithoutProperties(_ref, [
33+
'width',
34+
'height',
35+
'getRef',
36+
'children',
37+
]);
38+
239
return React.createElement(
340
'div',
4-
{className: 'example'},
41+
_extends({className: 'example', ref: getRef}, props),
542
"Hover me! I don't rely on any DOM manipulation, transition event or anything, I use a real resize event!",
643
React.createElement('br', null),
7-
`${width}x${height}`
44+
width,
45+
'x',
46+
height,
47+
children
848
);
949
}
1050

1151
function App() {
12-
return React.createElement(
13-
ReactResizeAware,
14-
{
15-
style: {position: 'relative'},
16-
onResize(sizes) {
17-
console.log(sizes);
18-
},
52+
return React.createElement(ReactResizeAware, {
53+
component: MyComponent,
54+
useBoundingClientRect: true,
55+
style: {position: 'relative'},
56+
onResize: function onResize(sizes) {
57+
return console.log(sizes);
1958
},
20-
React.createElement(MyComponent, null)
21-
);
59+
});
2260
}
2361

2462
ReactDOM.render(

docs/index.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
<head>
44
<title>Example</title>
55
<style>
6+
body {
7+
font-family: Helvetica Neue, Helvetica, Segoe UI, Ubuntu, Arial, sans-serif;
8+
}
69
.example {
710
background-color: rebeccapurple;
811
height: 100px;
12+
width: 100%;
913
color: white;
10-
transition: 1s height ease-in-out
14+
transition: 1s all ease-in-out;
1115
}
1216
.example:hover {
1317
height: 200px;
18+
width: 80vw;
1419
}
1520
</style>
1621
</head>
@@ -22,4 +27,4 @@
2227
<script src="./react-resize-aware.js"></script>
2328
<script src="./example.js"></script>
2429
</body>
25-
</html>
30+
</html>

docs/react-resize-aware.js

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

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "react-resize-aware",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"description": "A resize aware component used to detect sizes changes on your components",
55
"main": "dist/index.js",
66
"scripts": {
7+
"test": "jest --coverage",
78
"prepare": "NODE_ENV=production rollup -c rollup.config.js && cp dist/index.js docs/react-resize-aware.js"
89
},
910
"repository": {
@@ -29,9 +30,19 @@
2930
"homepage": "https://github.com/FezVrasta/react-resize-aware#readme",
3031
"devDependencies": {
3132
"babel-preset-es2015": "^6.24.1",
33+
"babel-preset-react": "^6.24.1",
3234
"babel-preset-stage-2": "^6.24.1",
35+
"enzyme": "^2.8.2",
36+
"enzyme-to-json": "^1.5.1",
37+
"jest": "^19.0.2",
38+
"jest-enzyme": "^3.0.1",
3339
"react": "^15.5.4",
40+
"react-dom": "^15.5.4",
41+
"react-test-renderer": "^15.5.4",
3442
"rollup": "^0.41.6",
3543
"rollup-plugin-babel": "^2.7.1"
44+
},
45+
"jest": {
46+
"setupTestFrameworkScriptFile": "./node_modules/jest-enzyme/lib/index.js"
3647
}
3748
}

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default {
1212
},
1313
plugins: [
1414
babel({
15+
babelrc: false,
1516
presets: [['es2015', {modules: false}], 'stage-2'],
1617
}),
1718
],
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`allows to define a custom component 1`] = `
4+
<DummyComponent
5+
getRef={[Function]}
6+
style={
7+
Object {
8+
"position": "relative",
9+
}
10+
}
11+
>
12+
<object
13+
onLoad={[Function]}
14+
style={
15+
Object {
16+
"display": "block",
17+
"height": "100%",
18+
"left": 0,
19+
"overflow": "hidden",
20+
"pointerEvents": "none",
21+
"position": "absolute",
22+
"top": 0,
23+
"width": "100%",
24+
"zIndex": -1,
25+
}
26+
}
27+
type="text/html"
28+
/>
29+
</DummyComponent>
30+
`;
31+
32+
exports[`allows to define an \`onResize\` property 1`] = `
33+
<ResizeAware
34+
component="div"
35+
onResize={[Function]}
36+
style={
37+
Object {
38+
"position": "relative",
39+
}
40+
}
41+
>
42+
<div
43+
style={
44+
Object {
45+
"position": "relative",
46+
}
47+
}
48+
>
49+
<object
50+
onLoad={[Function]}
51+
style={
52+
Object {
53+
"display": "block",
54+
"height": "100%",
55+
"left": 0,
56+
"overflow": "hidden",
57+
"pointerEvents": "none",
58+
"position": "absolute",
59+
"top": 0,
60+
"width": "100%",
61+
"zIndex": -1,
62+
}
63+
}
64+
type="text/html"
65+
/>
66+
</div>
67+
</ResizeAware>
68+
`;
69+
70+
exports[`allows to use its children as target 1`] = `
71+
<ResizeAware
72+
component="div"
73+
style={
74+
Object {
75+
"position": "relative",
76+
}
77+
}
78+
>
79+
<div
80+
style={
81+
Object {
82+
"position": "relative",
83+
}
84+
}
85+
>
86+
<object
87+
onLoad={[Function]}
88+
style={
89+
Object {
90+
"display": "block",
91+
"height": "100%",
92+
"left": 0,
93+
"overflow": "hidden",
94+
"pointerEvents": "none",
95+
"position": "absolute",
96+
"top": 0,
97+
"width": "100%",
98+
"zIndex": -1,
99+
}
100+
}
101+
type="text/html"
102+
/>
103+
<DummyComponent>
104+
<div>
105+
x
106+
</div>
107+
</DummyComponent>
108+
</div>
109+
</ResizeAware>
110+
`;

0 commit comments

Comments
 (0)