Scroll to previous hashed elements when user clicks back button

This commit is contained in:
Fabio Berger
2018-03-08 16:35:33 +01:00
parent 8057f4a678
commit 2011349eb1

View File

@@ -70,6 +70,12 @@ export class Documentation extends React.Component<DocumentationProps, Documenta
isHoveringSidebar: false,
};
}
public componentDidMount() {
window.addEventListener('hashchange', this._onHashChanged.bind(this), false);
}
public componentWillUnmount() {
window.removeEventListener('hashchange', this._onHashChanged.bind(this), false);
}
public componentDidUpdate(prevProps: DocumentationProps, prevState: DocumentationState) {
if (!_.isEqual(prevProps.docAgnosticFormat, this.props.docAgnosticFormat)) {
const hash = window.location.hash.slice(1);
@@ -362,4 +368,8 @@ export class Documentation extends React.Component<DocumentationProps, Documenta
isHoveringSidebar: false,
});
}
private _onHashChanged(event: any) {
const hash = window.location.hash.slice(1);
sharedUtils.scrollToHash(hash, sharedConstants.SCROLL_CONTAINER_ID);
}
}