docs: fix weird scroll behavior (#11668)

This commit is contained in:
Shahed Nasser authored and GitHub committed 2025-02-28 17:34:22 +02:00
1 parent bdf9723239
commit 358400c38c
6 files changed
+31 -10

No files matched your search

@@ -30,7 +30,9 @@ export const usePageScrollManager = () => {
? document.getElementById(location.hash.replace("#", ""))
: undefined
targetElm?.scrollIntoView()
scrollableElement?.scrollTo({
top: targetElm ? targetElm.offsetTop : 0,
})
}
}
@@ -21,6 +21,7 @@ import React, {
} from "react"
import { getScrolledTop as getScrolledTopUtil, isElmWindow } from "../../utils"
import { useKeyboardShortcut } from "../use-keyboard-shortcut"
import { useLayout } from "../../providers"
type EventFunc = (...args: never[]) => unknown
@@ -73,6 +74,7 @@ function useScrollControllerContextValue({
restoreScrollOnReload?: boolean
}): ScrollController {
const scrollEventsEnabledRef = useRef(true)
const { showCollapsedNavbar } = useLayout()
const [scrollableElement, setScrollableElement] = useState<
Element | Window | undefined
@@ -92,6 +94,11 @@ function useScrollControllerContextValue({
scrollToTop(elm.offsetTop)
}
const topMargin = useMemo(() => {
// might need a better way to set these static values
return showCollapsedNavbar ? 112 : 56
}, [showCollapsedNavbar])
const scrollToTop = (top: number, parentTop?: number) => {
const parentOffsetTop =
parentTop !== undefined
@@ -103,9 +110,7 @@ function useScrollControllerContextValue({
: 0
scrollableElement?.scrollTo({
// 56 is the height of the navbar
// might need a better way to determine it.
top: top - parentOffsetTop - 56,
top: top - parentOffsetTop - topMargin,
behavior: "instant",
})
}