Created
April 16, 2017 18:14
-
-
Save Ell/29ac75d973804c9af47d9be66f33c85f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import './Resizable.scss'; | |
import * as React from 'react'; | |
import { clamp } from 'lodash'; | |
export class Resizable extends React.Component { | |
state = { | |
width: this.props.defaultWidth | |
} | |
onDrag = (event) => { | |
const { minWidth, maxWidth } = this.props; | |
const mouseX = event.nativeEvent.clientX; | |
if (mouseX === 0) { | |
return; | |
} | |
const width = clamp(mouseX, minWidth, maxWidth) | |
this.setState({ width }); | |
} | |
render() { | |
const { width } = this.state; | |
return ( | |
<div className="resizable__container" style={{ width }}> | |
<div | |
className="resizable__dragarea" | |
onDrag={this.onDrag} | |
/> | |
{this.props.children} | |
</div> | |
); | |
} | |
} | |
Resizable.propTypes = { | |
children: React.PropTypes.element.isRequired, | |
defaultWidth: React.PropTypes.number, | |
minWidth: React.PropTypes.number, | |
maxWidth: React.PropTypes.number, | |
visible: React.PropTypes.bool | |
}; | |
Resizable.defaultProps = { | |
defaultWidth: 200, | |
minWidth: 150, | |
maxWidth: 300, | |
visible: true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment