/* eslint-disable quote-props */ import React from 'react'; import _ from 'lodash'; export default class SplitPane extends React.PureComponent { constructor(props) { super(props); this.state = {dragging: false}; } componentDidUpdate(prevProps) { if (this.state.dragging && prevProps.sizes !== this.props.sizes) { // recompute positions for ongoing dragging this.dragPanePosition = this.dragTarget.getBoundingClientRect()[this.d2]; } } setupPanes(ev) { this.panes = Array.from(ev.target.parentNode.childNodes); this.paneIndex = this.panes.indexOf(ev.target); this.paneIndex -= Math.ceil(this.paneIndex / 2); } handleAutoResize = ev => { ev.preventDefault(); this.setupPanes(ev); const sizes_ = this.getSizes(); sizes_[this.paneIndex] = 0; sizes_[this.paneIndex + 1] = 0; const availableWidth = 1 - _.sum(sizes_); sizes_[this.paneIndex] = availableWidth / 2; sizes_[this.paneIndex + 1] = availableWidth / 2; this.props.onResize(sizes_); }; handleDragStart = ev => { ev.preventDefault(); this.setState({dragging: true}); window.addEventListener('mousemove', this.onDrag); window.addEventListener('mouseup', this.onDragEnd); // dimensions to consider if (this.props.direction === 'horizontal') { this.d1 = 'height'; this.d2 = 'top'; this.d3 = 'clientY'; } else { this.d1 = 'width'; this.d2 = 'left'; this.d3 = 'clientX'; } this.dragTarget = ev.target; this.dragPanePosition = this.dragTarget.getBoundingClientRect()[this.d2]; this.panesSize = ev.target.parentNode.getBoundingClientRect()[this.d1]; this.setupPanes(ev); }; getSizes() { const {sizes} = this.props; let sizes_; if (sizes) { sizes_ = [].concat(sizes); } else { const total = this.props.children.length; const count = new Array(total).fill(1 / total); sizes_ = count; } return sizes_; } onDrag = ev => { const sizes_ = this.getSizes(); const i = this.paneIndex; const pos = ev[this.d3]; const d = Math.abs(this.dragPanePosition - pos) / this.panesSize; if (pos > this.dragPanePosition) { sizes_[i] += d; sizes_[i + 1] -= d; } else { sizes_[i] -= d; sizes_[i + 1] += d; } this.props.onResize(sizes_); }; onDragEnd = () => { if (this.state.dragging) { window.removeEventListener('mousemove', this.onDrag); window.removeEventListener('mouseup', this.onDragEnd); this.setState({dragging: false}); } }; render() { const children = this.props.children; const {direction, borderColor} = this.props; const sizeProperty = direction === 'horizontal' ? 'height' : 'width'; let {sizes} = this.props; if (!sizes) { // workaround for the fact that if we don't specify // sizes, sometimes flex fails to calculate the // right height for the horizontal panes sizes = new Array(children.length).fill(1 / children.length); } return (
{React.Children.map(children, (child, i) => { const style = { // flexBasis doesn't work for the first horizontal pane, height need to be specified [sizeProperty]: `${sizes[i] * 100}%`, flexBasis: `${sizes[i] * 100}%`, flexGrow: 0 }; return [
{child}
, i < children.length - 1 ? (
) : null ]; })}
); } componentWillUnmount() { // ensure drag end if (this.dragging) { this.onDragEnd(); } } }