Skip to content

Commit 7816bf8

Browse files
committed
📖 documentation
1 parent 7abbe57 commit 7816bf8

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
# react-automatic-width
22

3-
Reacts on window.onResize and sets its current width as property on its children. Use this to keep components such as [fixed-data-table](https://facebook.github.io/fixed-data-table/) or [react-d3-components](https://github.com/codesuki/react-d3-components) responsive.
3+
So you found those cool components that do what you want, but they work on fixed with ([fixed-data-table](https://facebook.github.io/fixed-data-table/) or [react-d3-components](https://github.com/codesuki/react-d3-components) for instance).
44

5-
# Usage
5+
~~~ jsx
6+
import D3 from 'react-d3-components';
67
7-
**TODO**
8+
<D3.BarChart
9+
width={500} /> // :(
10+
~~~
11+
12+
You care about responsiveness and different display sizes! You want variable width! HULK SMASH!
13+
14+
Solution: Just wrap it in `AutoWidth`.
15+
16+
~~~ jsx
17+
import D3 from 'react-d3-components';
18+
import AutoWidth from 'react-automatic-width';
19+
20+
<AutoWidth>
21+
<D3.BarChart /> {/* ^_^ */}
22+
</AutoWidth>
23+
~~~
24+
25+
# How?
26+
27+
It attaches a listener to `resize` event of `window`. In it, the component reads the current width of its DOM node and sets this as `width` property on its children.
28+
29+
# Issues
30+
31+
* Uses `addEventListener`, so no IE8 currently. PRs welcome.
32+
* Uses `clientWidth` because that worked on my current Chrome. Might be funky in your browser. PRs welcome.
33+
* Not clear what should happen if window is resized while container is invisible. Currently zero-widths just get ignored.
834

935
# License
1036

src/react-automatic-width.jsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import React from 'react';
22

33
class AutomaticWidth extends React.Component {
4-
constructor(props) {
4+
constructor() {
55
super();
66
this.state = {
77
listener: null,
8-
width: props.initialWidth || 1000
8+
width: 0
99
};
1010
}
1111

1212
_resizeHandler() {
13-
let dom = this.refs.autowidthWrapper.getDOMNode();
14-
if (dom.clientWidth !== this.state.width) {
13+
let dom = this.refs.autowidthWrapper.getDOMNode(),
14+
{clientWidth} = dom;
15+
if (clientWidth !== this.state.width && clientWidth > 0) {
1516
this.setState({
16-
width: dom.clientWidth
17+
width: clientWidth
1718
});
1819
}
1920
}
@@ -41,6 +42,11 @@ class AutomaticWidth extends React.Component {
4142
</div>;
4243
}
4344
}
44-
AutomaticWidth.displayName='Automatic Width';
45+
AutomaticWidth.displayName = 'AutomaticWidth';
46+
AutomaticWidth.propTypes = {
47+
children: React.PropTypes.oneOf([
48+
React.PropTypes.array,
49+
React.PropTypes.object
50+
])
51+
};
4552
export default AutomaticWidth;
46-

0 commit comments

Comments
 (0)