mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-03 20:27:20 +02:00
fix: avoid flash of page scrolling to top on refresh (#7342)
This commit is contained in:
parent
0cc4fa82c1
commit
dfe743c660
1 changed files with 13 additions and 7 deletions
|
@ -27,6 +27,17 @@ export function dispatchLifecycleAction<K extends keyof ClientModule>(
|
||||||
return () => callbacks.forEach((cb) => cb?.());
|
return () => callbacks.forEach((cb) => cb?.());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function scrollAfterNavigation(location: Location) {
|
||||||
|
const {hash} = location;
|
||||||
|
if (!hash) {
|
||||||
|
window.scrollTo(0, 0);
|
||||||
|
} else {
|
||||||
|
const id = decodeURIComponent(hash.substring(1));
|
||||||
|
const element = document.getElementById(id);
|
||||||
|
element?.scrollIntoView();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function ClientLifecyclesDispatcher({
|
function ClientLifecyclesDispatcher({
|
||||||
children,
|
children,
|
||||||
location,
|
location,
|
||||||
|
@ -38,13 +49,8 @@ function ClientLifecyclesDispatcher({
|
||||||
}): JSX.Element {
|
}): JSX.Element {
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
if (previousLocation !== location) {
|
if (previousLocation !== location) {
|
||||||
const {hash} = location;
|
if (previousLocation) {
|
||||||
if (!hash) {
|
scrollAfterNavigation(location);
|
||||||
window.scrollTo(0, 0);
|
|
||||||
} else {
|
|
||||||
const id = decodeURIComponent(hash.substring(1));
|
|
||||||
const element = document.getElementById(id);
|
|
||||||
element?.scrollIntoView();
|
|
||||||
}
|
}
|
||||||
dispatchLifecycleAction('onRouteDidUpdate', {previousLocation, location});
|
dispatchLifecycleAction('onRouteDidUpdate', {previousLocation, location});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue