2019-12-07 13:28:52 +02:00
|
|
|
import { RouteComponentProps } from 'react-router-dom';
|
2017-10-28 16:38:07 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { withRouter } from 'react-router-dom';
|
|
|
|
import { restoreScroll } from './scroll';
|
|
|
|
|
2020-01-17 23:37:52 +03:00
|
|
|
interface OwnProps {
|
|
|
|
top?: boolean; // don't touch any DOM and simply scroll to top on location change
|
|
|
|
}
|
|
|
|
|
|
|
|
type Props = RouteComponentProps & OwnProps;
|
|
|
|
|
|
|
|
class ScrollIntoView extends React.PureComponent<Props> {
|
2019-11-27 11:03:32 +02:00
|
|
|
componentDidMount() {
|
|
|
|
this.onPageUpdate();
|
|
|
|
}
|
2017-10-28 16:38:07 +03:00
|
|
|
|
2020-01-17 23:37:52 +03:00
|
|
|
componentDidUpdate(prevProps: Props) {
|
2019-11-27 11:03:32 +02:00
|
|
|
if (this.props.location !== prevProps.location) {
|
|
|
|
this.onPageUpdate();
|
2017-10-28 16:38:07 +03:00
|
|
|
}
|
2019-11-27 11:03:32 +02:00
|
|
|
}
|
2017-10-28 16:38:07 +03:00
|
|
|
|
2019-11-27 11:03:32 +02:00
|
|
|
onPageUpdate() {
|
|
|
|
if (this.props.top) {
|
|
|
|
restoreScroll();
|
2017-10-28 16:38:07 +03:00
|
|
|
}
|
2019-11-27 11:03:32 +02:00
|
|
|
}
|
2017-10-28 16:38:07 +03:00
|
|
|
|
2019-11-27 11:03:32 +02:00
|
|
|
render() {
|
|
|
|
if (this.props.top) {
|
|
|
|
return null;
|
2017-10-28 16:38:07 +03:00
|
|
|
}
|
2019-11-27 11:03:32 +02:00
|
|
|
|
2020-05-20 19:35:52 +03:00
|
|
|
return <span ref={(el) => el && restoreScroll(el)} />;
|
2019-11-27 11:03:32 +02:00
|
|
|
}
|
2017-10-28 16:38:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(ScrollIntoView);
|