From 82e7f94156e13398b003bcadf9f66ba8cf395111 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sat, 3 Nov 2018 16:14:15 +0800 Subject: [PATCH] feat: basic auto sizer --- src/components/AutoSizer.js | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/components/AutoSizer.js diff --git a/src/components/AutoSizer.js b/src/components/AutoSizer.js new file mode 100644 index 0000000..4535a96 --- /dev/null +++ b/src/components/AutoSizer.js @@ -0,0 +1,38 @@ +import React from 'react' +import PropTypes from 'prop-types' + +export default class AutoSizer extends React.Component { + static propTypes = {} + + static defaultProps = {} + + state = { + size: { + width: 0, + height: 0, + }, + } + + ref = React.createRef() + + componentDidMount() { + const container = this.ref.current + if (container) { + this.setState({ + size: { + width: container.offsetWidth, + height: container.offsetHeight, + }, + }) + } + } + + render() { + const { size } = this.state + return ( +
+ {this.props.children(size)} +
+ ) + } +}