Skip to content

Commit

Permalink
Merge pull request #365 from hshoff/harry-responsive-fix
Browse files Browse the repository at this point in the history
[responsive] remove debounceTime prop from restProps
  • Loading branch information
hshoff authored Sep 28, 2018
2 parents d994f95 + b807edf commit c6ce840
Showing 1 changed file with 85 additions and 67 deletions.
152 changes: 85 additions & 67 deletions packages/vx-responsive/src/components/ParentSize.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,85 @@
import debounce from 'lodash/debounce';
import PropTypes from 'prop-types';
import React from 'react';
import ResizeObserver from 'resize-observer-polyfill';

export default class ParentSize extends React.Component {
constructor(props) {
super(props);
this.state = { width: 0, height: 0, top: 0, left: 0 };
this.resize = debounce(this.resize.bind(this), props.debounceTime);
this.setTarget = this.setTarget.bind(this);
this.animationFrameID = null;
}
componentDidMount() {
this.ro = new ResizeObserver((entries, observer) => {
for (const entry of entries) {
const { left, top, width, height } = entry.contentRect;
this.animationFrameID = window.requestAnimationFrame(() => {
this.resize({
width,
height,
top,
left
});
});
}
});
this.ro.observe(this.target);
}
componentWillUnmount() {
window.cancelAnimationFrame(this.animationFrameID);
this.ro.disconnect();
}
resize({ width, height, top, left }) {
this.setState(() => ({
width,
height,
top,
left
}));
}
setTarget(ref) {
this.target = ref;
}
render() {
const { className, children, ...restProps } = this.props;
return (
<div style={{ width: '100%', height: '100%' }} ref={this.setTarget} className={className} {...restProps}>
{children({
...this.state,
ref: this.target,
resize: this.resize
})}
</div>
);
}
}

ParentSize.defaultProps = {
debounceTime: 300
};

ParentSize.propTypes = {
className: PropTypes.string,
children: PropTypes.func.isRequired,
debounceTime: PropTypes.number
};
import debounce from 'lodash/debounce';
import PropTypes from 'prop-types';
import React from 'react';
import ResizeObserver from 'resize-observer-polyfill';

export default class ParentSize extends React.Component {
constructor(props) {
super(props);
this.state = {
width: 0, height: 0, top: 0, left: 0,
};
this.resize = debounce(this.resize.bind(this), props.debounceTime);
this.setTarget = this.setTarget.bind(this);
this.animationFrameID = null;
}

componentDidMount() {
this.ro = new ResizeObserver((entries, observer) => {
for (const entry of entries) {
const {
left, top, width, height,
} = entry.contentRect;
this.animationFrameID = window.requestAnimationFrame(() => {
this.resize({
width,
height,
top,
left,
});
});
}
});
this.ro.observe(this.target);
}

componentWillUnmount() {
window.cancelAnimationFrame(this.animationFrameID);
this.ro.disconnect();
}

resize({
width, height, top, left,
}) {
this.setState(() => ({
width,
height,
top,
left,
}));
}

setTarget(ref) {
this.target = ref;
}

render() {
const {
className, children, debounceTime, ...restProps
} = this.props;
return (
<div
style={{ width: '100%', height: '100%' }}
ref={this.setTarget}
className={className}
{...restProps}
>
{children({
...this.state,
ref: this.target,
resize: this.resize,
})}
</div>
);
}
}

ParentSize.defaultProps = {
debounceTime: 300,
};

ParentSize.propTypes = {
className: PropTypes.string,
children: PropTypes.func.isRequired,
debounceTime: PropTypes.number,
};

0 comments on commit c6ce840

Please sign in to comment.