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';
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface OwnProps {
|
2020-05-24 04:38:24 +05:30
|
|
|
top?: boolean; // don't touch any DOM and simply scroll to top on location change
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type Props = RouteComponentProps & OwnProps;
|
|
|
|
|
|
|
|
class ScrollIntoView extends React.PureComponent<Props> {
|
2020-05-24 04:38:24 +05:30
|
|
|
componentDidMount() {
|
|
|
|
this.onPageUpdate();
|
2017-10-28 19:08:07 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
componentDidUpdate(prevProps: Props) {
|
|
|
|
if (this.props.location !== prevProps.location) {
|
|
|
|
this.onPageUpdate();
|
|
|
|
}
|
2017-10-28 19:08:07 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
onPageUpdate() {
|
|
|
|
if (this.props.top) {
|
|
|
|
restoreScroll();
|
|
|
|
}
|
2017-10-28 19:08:07 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
render() {
|
|
|
|
if (this.props.top) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <span ref={(el) => el && restoreScroll(el)} />;
|
|
|
|
}
|
2017-10-28 19:08:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(ScrollIntoView);
|