You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
782 B
34 lines
782 B
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*/
|
|
|
|
let offsetY = 0;
|
|
|
|
const getTargetOffset = hash => {
|
|
const id = window.decodeURI(hash.replace(`#`, ``));
|
|
if (id !== ``) {
|
|
const element = document.getElementById(id);
|
|
if (element) {
|
|
return element.offsetTop - offsetY;
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
|
|
exports.onInitialClientRender = (_, pluginOptions) => {
|
|
if (pluginOptions.offsetY) {
|
|
offsetY = pluginOptions.offsetY;
|
|
}
|
|
|
|
requestAnimationFrame(() => {
|
|
const offset = getTargetOffset(window.location.hash);
|
|
if (offset !== null) {
|
|
window.scrollTo(0, offset);
|
|
}
|
|
});
|
|
};
|
|
|
|
exports.shouldUpdateScroll = ({routerProps: {location}}) => {
|
|
const offset = getTargetOffset(location.hash);
|
|
return offset !== null ? [0, offset] : true;
|
|
};
|
|
|