fix: Scroll does not reset when navigating shared docs on mobile (#7037)

closes #6968
This commit is contained in:
Tom Moor
2024-06-14 19:36:36 -04:00
committed by GitHub
parent f35676f347
commit f8a9c18650
5 changed files with 61 additions and 32 deletions

View File

@@ -2,6 +2,7 @@
import * as React from "react";
import { useLocation } from "react-router-dom";
import usePrevious from "~/hooks/usePrevious";
import { useScrollContext } from "./ScrollContext";
type Props = {
children: JSX.Element;
@@ -10,6 +11,7 @@ type Props = {
export default function ScrollToTop({ children }: Props) {
const location = useLocation<{ retainScrollPosition?: boolean }>();
const previousLocationPathname = usePrevious(location.pathname);
const scrollContainerRef = useScrollContext();
React.useEffect(() => {
if (
@@ -25,8 +27,9 @@ export default function ScrollToTop({ children }: Props) {
) {
return;
}
window.scrollTo(0, 0);
(scrollContainerRef?.current || window).scrollTo(0, 0);
}, [
scrollContainerRef,
location.pathname,
previousLocationPathname,
location.state?.retainScrollPosition,