accounts-frontend/packages/app/components/ui/scroll/ScrollIntoView.tsx

39 lines
893 B
TypeScript
Raw Normal View History

2019-12-07 16:58:52 +05:30
import { RouteComponentProps } from 'react-router-dom';
import React from 'react';
import { withRouter } from 'react-router-dom';
import { restoreScroll } from './scroll';
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
}
type Props = RouteComponentProps & OwnProps;
class ScrollIntoView extends React.PureComponent<Props> {
2020-05-24 04:38:24 +05:30
componentDidMount() {
this.onPageUpdate();
}
2020-05-24 04:38:24 +05:30
componentDidUpdate(prevProps: Props) {
if (this.props.location !== prevProps.location) {
this.onPageUpdate();
}
}
2020-05-24 04:38:24 +05:30
onPageUpdate() {
if (this.props.top) {
restoreScroll();
}
}
2020-05-24 04:38:24 +05:30
render() {
if (this.props.top) {
return null;
}
return <span ref={(el) => el && restoreScroll(el)} />;
}
}
export default withRouter(ScrollIntoView);