2017-10-28 19:08:07 +05:30
|
|
|
// @flow
|
2019-06-30 19:02:50 +05:30
|
|
|
import type { Location } from 'react-router-dom';
|
2017-10-28 19:08:07 +05:30
|
|
|
import React from 'react';
|
|
|
|
import { withRouter } from 'react-router-dom';
|
|
|
|
import { restoreScroll } from './scroll';
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
class ScrollIntoView extends React.PureComponent<{
|
|
|
|
location: Location,
|
2017-10-28 19:08:07 +05:30
|
|
|
top?: bool, // do not touch any DOM and simply scroll to top on location change
|
|
|
|
}> {
|
|
|
|
componentDidMount() {
|
|
|
|
this.onPageUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.location !== prevProps.location) {
|
|
|
|
this.onPageUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onPageUpdate() {
|
|
|
|
if (this.props.top) {
|
|
|
|
restoreScroll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.props.top) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <span ref={(el) => el && restoreScroll(el)} />;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(ScrollIntoView);
|