Skip to content

Commit

Permalink
docs: fix weird scroll behavior (#11668)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser authored Feb 28, 2025
1 parent bdf9723 commit 358400c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion www/packages/docs-ui/src/components/Heading/H1/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const H1 = ({ className, ...props }: H1Props) => {
<h1
className={clsx(
"h1-docs [&_code]:!h1-docs [&_code]:!font-mono mb-docs_1 text-medusa-fg-base",
props.id && "scroll-m-56",
props.id && "scroll-m-docs_7",
className
)}
{...props}
Expand Down
10 changes: 8 additions & 2 deletions www/packages/docs-ui/src/components/Heading/H2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@
import clsx from "clsx"
import React from "react"
import { CopyButton, Link } from "@/components"
import { useHeadingUrl } from "../../.."
import { useHeadingUrl, useLayout } from "../../.."

type H2Props = React.HTMLAttributes<HTMLHeadingElement> & {
id?: string
passRef?: React.RefObject<HTMLHeadingElement | null>
}

export const H2 = ({ className, children, passRef, ...props }: H2Props) => {
const { showCollapsedNavbar } = useLayout()

const copyText = useHeadingUrl({
id: props.id || "",
})
return (
<h2
className={clsx(
"h2-docs [&_code]:!h2-docs [&_code]:!font-mono mb-docs_1 mt-docs_2 text-medusa-fg-base",
props.id && "group/h2 scroll-m-56",
props.id && [
"group/h2",
showCollapsedNavbar && "scroll-m-docs_7",
!showCollapsedNavbar && "scroll-m-56",
],
className
)}
{...props}
Expand Down
9 changes: 7 additions & 2 deletions www/packages/docs-ui/src/components/Heading/H3/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
import clsx from "clsx"
import React from "react"
import { CopyButton, Link } from "@/components"
import { useHeadingUrl } from "../../.."
import { useHeadingUrl, useLayout } from "../../.."

type H3Props = React.HTMLAttributes<HTMLHeadingElement> & {
id?: string
}

export const H3 = ({ className, children, ...props }: H3Props) => {
const { showCollapsedNavbar } = useLayout()
const copyText = useHeadingUrl({ id: props.id || "" })
return (
<h3
className={clsx(
"h3-docs [&_code]:!h3-docs [&_code]:!font-mono my-docs_1 text-medusa-fg-base",
props.id && "group/h3 scroll-m-56",
props.id && [
"group/h3",
showCollapsedNavbar && "scroll-m-docs_7",
!showCollapsedNavbar && "scroll-m-56",
],
className
)}
{...props}
Expand Down
5 changes: 4 additions & 1 deletion www/packages/docs-ui/src/components/Toc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isElmWindow,
useActiveOnScroll,
useIsBrowser,
useLayout,
useScrollController,
} from "../.."
import { TocList } from "./List"
Expand All @@ -20,6 +21,7 @@ export const Toc = () => {
const { items: headingItems, activeItemId } = useActiveOnScroll({})
const [maxHeight, setMaxHeight] = useState(0)
const { scrollableElement } = useScrollController()
const { showCollapsedNavbar } = useLayout()

const formatHeadingContent = (content: string | null): string => {
return content?.replaceAll(/#$/g, "") || ""
Expand All @@ -44,10 +46,11 @@ export const Toc = () => {
}, [headingItems])

const handleResize = () => {
const extraMargin = showCollapsedNavbar ? 112 : 56
const offset =
(scrollableElement instanceof HTMLElement
? scrollableElement.offsetTop
: 0) + 56
: 0) + extraMargin

setMaxHeight(
(isElmWindow(scrollableElement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export const usePageScrollManager = () => {
? document.getElementById(location.hash.replace("#", ""))
: undefined

targetElm?.scrollIntoView()
scrollableElement?.scrollTo({
top: targetElm ? targetElm.offsetTop : 0,
})
}
}

Expand Down
11 changes: 8 additions & 3 deletions www/packages/docs-ui/src/hooks/use-scroll-utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -73,6 +74,7 @@ function useScrollControllerContextValue({
restoreScrollOnReload?: boolean
}): ScrollController {
const scrollEventsEnabledRef = useRef(true)
const { showCollapsedNavbar } = useLayout()

const [scrollableElement, setScrollableElement] = useState<
Element | Window | undefined
Expand All @@ -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
Expand All @@ -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",
})
}
Expand Down

0 comments on commit 358400c

Please sign in to comment.