Skip to content

Instantly share code, notes, and snippets.

@Ell
Created April 16, 2017 18:14
Show Gist options
  • Save Ell/29ac75d973804c9af47d9be66f33c85f to your computer and use it in GitHub Desktop.
Save Ell/29ac75d973804c9af47d9be66f33c85f to your computer and use it in GitHub Desktop.
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