import React, { Component } from "react";import React, { Component } from "react";import PropTypes from "prop-types";class ScrollTo extends Component { constructor(props) { super(props); this.scrollArea = []; this.handleScroll = this.handleScroll.bind(this);}
getChildContext() { return { addScrollArea: ref => { this.scrollArea = this.scrollArea.concat(ref);},
removeScrollArea: ref => { this.scrollArea = this.scrollArea.filter(container => { return container !== ref;});
}
};
}
handleScroll(x, y) { if (this.scrollArea.length === 0) {scrollWindow(x, y);
} else { this.scrollArea.forEach(container => {container.scrollLeft = x;
container.scrollTop = y;
});
}
}
render() { return this.props.children && this.props.children(this.handleScroll);}
}
ScrollTo.childContextTypes = {addScrollArea: PropTypes.func.isRequired,
removeScrollArea: PropTypes.func.isRequired
};
class ScrollArea extends Component { componentDidMount() { this.context.addScrollArea(this.node);}
componentWillUnmount() { this.context.removeScrollArea(this.node);}
render() { const { children, ...props } = this.props; return ( <div {...props} ref={node => (this.node = node)}> {children} </div>);
}
}
ScrollArea.contextTypes = {addScrollArea: PropTypes.func.isRequired,
removeScrollArea: PropTypes.func.isRequired
};
function scrollWindow(x = 0, y = 0) { window.scroll(x, y);}