2019-12-07 16:58:52 +05:30
|
|
|
import { RouteComponentProps } 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-12-07 16:58:52 +05:30
|
|
|
class ScrollIntoView extends React.PureComponent<
|
|
|
|
RouteComponentProps & {
|
|
|
|
top?: boolean; // do not touch any DOM and simply scroll to top on location change
|
|
|
|
}
|
|
|
|
> {
|
2019-11-27 14:33:32 +05:30
|
|
|
componentDidMount() {
|
|
|
|
this.onPageUpdate();
|
|
|
|
}
|
2017-10-28 19:08:07 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.location !== prevProps.location) {
|
|
|
|
this.onPageUpdate();
|
2017-10-28 19:08:07 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2017-10-28 19:08:07 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onPageUpdate() {
|
|
|
|
if (this.props.top) {
|
|
|
|
restoreScroll();
|
2017-10-28 19:08:07 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2017-10-28 19:08:07 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
render() {
|
|
|
|
if (this.props.top) {
|
|
|
|
return null;
|
2017-10-28 19:08:07 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
return <span ref={el => el && restoreScroll(el)} />;
|
|
|
|
}
|
2017-10-28 19:08:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(ScrollIntoView);
|