mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-29 02:08:36 +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?.());
|
||||
}
|
||||
|
||||
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({
|
||||
children,
|
||||
location,
|
||||
|
@ -38,13 +49,8 @@ function ClientLifecyclesDispatcher({
|
|||
}): JSX.Element {
|
||||
useLayoutEffect(() => {
|
||||
if (previousLocation !== location) {
|
||||
const {hash} = location;
|
||||
if (!hash) {
|
||||
window.scrollTo(0, 0);
|
||||
} else {
|
||||
const id = decodeURIComponent(hash.substring(1));
|
||||
const element = document.getElementById(id);
|
||||
element?.scrollIntoView();
|
||||
if (previousLocation) {
|
||||
scrollAfterNavigation(location);
|
||||
}
|
||||
dispatchLifecycleAction('onRouteDidUpdate', {previousLocation, location});
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue