-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #365 from hshoff/harry-responsive-fix
[responsive] remove debounceTime prop from restProps
- Loading branch information
Showing
1 changed file
with
85 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |