This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/app/components/ScrollToAnchor.js
2018-08-05 22:08:46 -07:00

24 lines
638 B
JavaScript

// @flow
import * as React from 'react';
import { withRouter } from 'react-router-dom';
class ScrollToAnchor extends React.Component<*> {
componentDidUpdate(prevProps) {
if (this.props.location.hash === prevProps.location.hash) return;
if (window.location.hash === '') return;
// Delay on timeout to ensure that the DOM is updated first
setImmediate(() => {
const id = window.location.hash.replace('#', '');
const element = document.getElementById(id);
if (element) element.scrollIntoView();
});
}
render() {
return this.props.children;
}
}
export default withRouter(ScrollToAnchor);