docs: fix weird scroll behavior (#11668)
This commit is contained in:
@@ -10,7 +10,7 @@ export const H1 = ({ className, ...props }: H1Props) => {
|
|||||||
<h1
|
<h1
|
||||||
className={clsx(
|
className={clsx(
|
||||||
"h1-docs [&_code]:!h1-docs [&_code]:!font-mono mb-docs_1 text-medusa-fg-base",
|
"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
|
className
|
||||||
)}
|
)}
|
||||||
{...props}
|
{...props}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import clsx from "clsx"
|
import clsx from "clsx"
|
||||||
import React from "react"
|
import React from "react"
|
||||||
import { CopyButton, Link } from "@/components"
|
import { CopyButton, Link } from "@/components"
|
||||||
import { useHeadingUrl } from "../../.."
|
import { useHeadingUrl, useLayout } from "../../.."
|
||||||
|
|
||||||
type H2Props = React.HTMLAttributes<HTMLHeadingElement> & {
|
type H2Props = React.HTMLAttributes<HTMLHeadingElement> & {
|
||||||
id?: string
|
id?: string
|
||||||
@@ -11,6 +11,8 @@ type H2Props = React.HTMLAttributes<HTMLHeadingElement> & {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const H2 = ({ className, children, passRef, ...props }: H2Props) => {
|
export const H2 = ({ className, children, passRef, ...props }: H2Props) => {
|
||||||
|
const { showCollapsedNavbar } = useLayout()
|
||||||
|
|
||||||
const copyText = useHeadingUrl({
|
const copyText = useHeadingUrl({
|
||||||
id: props.id || "",
|
id: props.id || "",
|
||||||
})
|
})
|
||||||
@@ -18,7 +20,11 @@ export const H2 = ({ className, children, passRef, ...props }: H2Props) => {
|
|||||||
<h2
|
<h2
|
||||||
className={clsx(
|
className={clsx(
|
||||||
"h2-docs [&_code]:!h2-docs [&_code]:!font-mono mb-docs_1 mt-docs_2 text-medusa-fg-base",
|
"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
|
className
|
||||||
)}
|
)}
|
||||||
{...props}
|
{...props}
|
||||||
|
|||||||
@@ -3,19 +3,24 @@
|
|||||||
import clsx from "clsx"
|
import clsx from "clsx"
|
||||||
import React from "react"
|
import React from "react"
|
||||||
import { CopyButton, Link } from "@/components"
|
import { CopyButton, Link } from "@/components"
|
||||||
import { useHeadingUrl } from "../../.."
|
import { useHeadingUrl, useLayout } from "../../.."
|
||||||
|
|
||||||
type H3Props = React.HTMLAttributes<HTMLHeadingElement> & {
|
type H3Props = React.HTMLAttributes<HTMLHeadingElement> & {
|
||||||
id?: string
|
id?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const H3 = ({ className, children, ...props }: H3Props) => {
|
export const H3 = ({ className, children, ...props }: H3Props) => {
|
||||||
|
const { showCollapsedNavbar } = useLayout()
|
||||||
const copyText = useHeadingUrl({ id: props.id || "" })
|
const copyText = useHeadingUrl({ id: props.id || "" })
|
||||||
return (
|
return (
|
||||||
<h3
|
<h3
|
||||||
className={clsx(
|
className={clsx(
|
||||||
"h3-docs [&_code]:!h3-docs [&_code]:!font-mono my-docs_1 text-medusa-fg-base",
|
"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
|
className
|
||||||
)}
|
)}
|
||||||
{...props}
|
{...props}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
isElmWindow,
|
isElmWindow,
|
||||||
useActiveOnScroll,
|
useActiveOnScroll,
|
||||||
useIsBrowser,
|
useIsBrowser,
|
||||||
|
useLayout,
|
||||||
useScrollController,
|
useScrollController,
|
||||||
} from "../.."
|
} from "../.."
|
||||||
import { TocList } from "./List"
|
import { TocList } from "./List"
|
||||||
@@ -20,6 +21,7 @@ export const Toc = () => {
|
|||||||
const { items: headingItems, activeItemId } = useActiveOnScroll({})
|
const { items: headingItems, activeItemId } = useActiveOnScroll({})
|
||||||
const [maxHeight, setMaxHeight] = useState(0)
|
const [maxHeight, setMaxHeight] = useState(0)
|
||||||
const { scrollableElement } = useScrollController()
|
const { scrollableElement } = useScrollController()
|
||||||
|
const { showCollapsedNavbar } = useLayout()
|
||||||
|
|
||||||
const formatHeadingContent = (content: string | null): string => {
|
const formatHeadingContent = (content: string | null): string => {
|
||||||
return content?.replaceAll(/#$/g, "") || ""
|
return content?.replaceAll(/#$/g, "") || ""
|
||||||
@@ -44,10 +46,11 @@ export const Toc = () => {
|
|||||||
}, [headingItems])
|
}, [headingItems])
|
||||||
|
|
||||||
const handleResize = () => {
|
const handleResize = () => {
|
||||||
|
const extraMargin = showCollapsedNavbar ? 112 : 56
|
||||||
const offset =
|
const offset =
|
||||||
(scrollableElement instanceof HTMLElement
|
(scrollableElement instanceof HTMLElement
|
||||||
? scrollableElement.offsetTop
|
? scrollableElement.offsetTop
|
||||||
: 0) + 56
|
: 0) + extraMargin
|
||||||
|
|
||||||
setMaxHeight(
|
setMaxHeight(
|
||||||
(isElmWindow(scrollableElement)
|
(isElmWindow(scrollableElement)
|
||||||
|
|||||||
@@ -30,7 +30,9 @@ export const usePageScrollManager = () => {
|
|||||||
? document.getElementById(location.hash.replace("#", ""))
|
? document.getElementById(location.hash.replace("#", ""))
|
||||||
: undefined
|
: undefined
|
||||||
|
|
||||||
targetElm?.scrollIntoView()
|
scrollableElement?.scrollTo({
|
||||||
|
top: targetElm ? targetElm.offsetTop : 0,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import React, {
|
|||||||
} from "react"
|
} from "react"
|
||||||
import { getScrolledTop as getScrolledTopUtil, isElmWindow } from "../../utils"
|
import { getScrolledTop as getScrolledTopUtil, isElmWindow } from "../../utils"
|
||||||
import { useKeyboardShortcut } from "../use-keyboard-shortcut"
|
import { useKeyboardShortcut } from "../use-keyboard-shortcut"
|
||||||
|
import { useLayout } from "../../providers"
|
||||||
|
|
||||||
type EventFunc = (...args: never[]) => unknown
|
type EventFunc = (...args: never[]) => unknown
|
||||||
|
|
||||||
@@ -73,6 +74,7 @@ function useScrollControllerContextValue({
|
|||||||
restoreScrollOnReload?: boolean
|
restoreScrollOnReload?: boolean
|
||||||
}): ScrollController {
|
}): ScrollController {
|
||||||
const scrollEventsEnabledRef = useRef(true)
|
const scrollEventsEnabledRef = useRef(true)
|
||||||
|
const { showCollapsedNavbar } = useLayout()
|
||||||
|
|
||||||
const [scrollableElement, setScrollableElement] = useState<
|
const [scrollableElement, setScrollableElement] = useState<
|
||||||
Element | Window | undefined
|
Element | Window | undefined
|
||||||
@@ -92,6 +94,11 @@ function useScrollControllerContextValue({
|
|||||||
scrollToTop(elm.offsetTop)
|
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 scrollToTop = (top: number, parentTop?: number) => {
|
||||||
const parentOffsetTop =
|
const parentOffsetTop =
|
||||||
parentTop !== undefined
|
parentTop !== undefined
|
||||||
@@ -103,9 +110,7 @@ function useScrollControllerContextValue({
|
|||||||
: 0
|
: 0
|
||||||
|
|
||||||
scrollableElement?.scrollTo({
|
scrollableElement?.scrollTo({
|
||||||
// 56 is the height of the navbar
|
top: top - parentOffsetTop - topMargin,
|
||||||
// might need a better way to determine it.
|
|
||||||
top: top - parentOffsetTop - 56,
|
|
||||||
behavior: "instant",
|
behavior: "instant",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user