Skip to content

Commit 5cda8f1

Browse files
authored
Merge pull request #6 from okonet/all-client-rect-props
Pass all values of getBoundingClientRect
2 parents dd81f95 + 2c97d23 commit 5cda8f1

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.3.0
2+
3+
- Pass all values of getBoundingClientRect, not just `width` and `height`. (#6)
4+
15
# 1.2.0
26

37
- Added React 15 to a semver range for peerDependencies (#3)

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ It is especially useful when you create components with dimensions that change o
99
time and you want to explicitely pass the container dimensions to the children. For example, SVG
1010
visualization needs to be updated in order to fit into container.
1111

12+
It uses [`getBoundingClientRect()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) and passes values for all `top`, `right`, `bottom`, `left`, `width`, `height` CSs attributes down the tree.
13+
1214
## Usage
1315

14-
* Wrap your existing components. Children component will recieve `width` and `height` as props.
16+
* Wrap your existing components. Children component will recieve `top`, `right`, `bottom`, `left`, `width`, `height` as props.
1517

1618
```jsx
1719
<ContainerDimensions>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-container-dimensions",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Wrapper component that detects element resize and passes new dimensions down the tree. Based on [element-resize-detector](https://github.com/wnr/element-resize-detector)",
55
"main": "lib/index.js",
66
"scripts": {

src/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export default class ContainerDimensions extends Component {
99
children: PropTypes.oneOfType([PropTypes.element, PropTypes.func]).isRequired
1010
}
1111

12+
static getDomNodeDimensions(node) {
13+
const { top, right, bottom, left, width, height } = node.getBoundingClientRect()
14+
return { top, right, bottom, left, width, height }
15+
}
16+
1217
constructor() {
1318
super()
1419
this.state = {
@@ -29,11 +34,10 @@ export default class ContainerDimensions extends Component {
2934
}
3035

3136
onResize() {
32-
const clientRect = this.parentNode.getBoundingClientRect()
37+
const clientRect = ContainerDimensions.getDomNodeDimensions(this.parentNode)
3338
this.setState({
3439
initiated: true,
35-
width: clientRect.width,
36-
height: clientRect.height
40+
...clientRect
3741
})
3842
}
3943

tests/index.js

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint no-unused-expressions: 0 */
22
import React from 'react'
33
import { mount } from 'enzyme'
4-
import { spy } from 'sinon'
4+
import { spy, stub } from 'sinon'
55
import chai, { expect } from 'chai'
66
import ContainerDimensions from '../src/index'
77

@@ -61,11 +61,44 @@ describe('react-container-dimensions', () => {
6161
const el = wrapper.render()
6262
el.css('width', 10)
6363
setTimeout(() => {
64-
el.css('width', 100)
64+
el.css('width', 100) // Triggering onResize event
6565
expect(ContainerDimensions.prototype.onResize.calledTwice).to.be.true
6666
ContainerDimensions.prototype.onResize.restore()
6767
done()
68-
}, 0)
68+
}, 10)
69+
})
70+
71+
it('onResize sets state with all keys and values from getBoundingClientRect', () => {
72+
const styles = { top: 100, width: 200 }
73+
stub(ContainerDimensions, 'getDomNodeDimensions', () => ({
74+
top: 0,
75+
right: 0,
76+
bottom: 0,
77+
left: 0,
78+
width: 0,
79+
height: 0,
80+
...styles
81+
}))
82+
const wrapper = mount(
83+
<div style={styles}>
84+
<ContainerDimensions>
85+
<MyComponent />
86+
</ContainerDimensions>
87+
</div>
88+
, { attachTo: document.body })
89+
90+
wrapper.render()
91+
expect(wrapper.find(MyComponent).props()).to.have.keys([
92+
'initiated',
93+
'top',
94+
'right',
95+
'bottom',
96+
'left',
97+
'width',
98+
'height'])
99+
expect(wrapper.find(MyComponent)).to.have.prop('top', 100)
100+
expect(wrapper.find(MyComponent)).to.have.prop('width', 200)
101+
ContainerDimensions.getDomNodeDimensions.restore()
69102
})
70103

71104
it('should initially render an empty placeholder', () => {
@@ -89,34 +122,49 @@ describe('react-container-dimensions', () => {
89122
expect(wrapper.find(MyComponent).length).to.eq(1)
90123
})
91124

92-
it('should pass width and height as props to its children', () => {
125+
it('should pass dimensions as props to its children', () => {
93126
const wrapper = mount(
94127
<ContainerDimensions>
95128
<MyComponent />
96129
</ContainerDimensions>
97130
)
98131
wrapper.setState({
132+
top: 0,
133+
right: 100,
134+
bottom: 300,
135+
left: 200,
99136
width: 100,
100137
height: 200
101138
})
102139
expect(wrapper.find(MyComponent)).to.have.length(1)
140+
expect(wrapper.find(MyComponent)).to.have.prop('top', 0)
141+
expect(wrapper.find(MyComponent)).to.have.prop('right', 100)
142+
expect(wrapper.find(MyComponent)).to.have.prop('bottom', 300)
143+
expect(wrapper.find(MyComponent)).to.have.prop('left', 200)
103144
expect(wrapper.find(MyComponent)).to.have.prop('width', 100)
104145
expect(wrapper.find(MyComponent)).to.have.prop('height', 200)
105146
})
106147

107-
it('should pass width and height as function arguments', () => {
148+
it('should pass dimensions as function arguments', () => {
108149
const wrapper = mount(
109150
<ContainerDimensions>
110151
{
111-
({ width, height }) => <MyComponent width={width + 10} height={height + 10} /> // eslint-disable-line
152+
({ left, width, height }) => // eslint-disable-line
153+
<MyComponent
154+
left={left + 10}
155+
width={width + 10}
156+
height={height + 10}
157+
/>
112158
}
113159
</ContainerDimensions>
114160
)
115161
wrapper.setState({
162+
left: 20,
116163
width: 100,
117164
height: 200
118165
})
119166
expect(wrapper.find(MyComponent)).to.have.length(1)
167+
expect(wrapper.find(MyComponent)).to.have.prop('left', 30)
120168
expect(wrapper.find(MyComponent)).to.have.prop('width', 110)
121169
expect(wrapper.find(MyComponent)).to.have.prop('height', 210)
122170
})

0 commit comments

Comments
 (0)