docs: fix sidebar navigation in API reference (#11179)
This commit is contained in:
1 parent
86b16b9049
commit
a37a9c8023
4 files changed
+42
-32
No files matched your search
@@ -30,10 +30,8 @@ export type TagSectionSchemaProps = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const TagSectionSchema = ({ schema, tagName }: TagSectionSchemaProps) => {
|
const TagSectionSchema = ({ schema, tagName }: TagSectionSchemaProps) => {
|
||||||
const paramsRef = useRef<HTMLDivElement>(null)
|
|
||||||
const { addItems, setActivePath, activePath } = useSidebar()
|
const { addItems, setActivePath, activePath } = useSidebar()
|
||||||
const { displayedArea } = useArea()
|
const { displayedArea } = useArea()
|
||||||
const tagSlugName = useMemo(() => getSectionId([tagName]), [tagName])
|
|
||||||
const formattedName = useMemo(
|
const formattedName = useMemo(
|
||||||
() => singular(tagName).replaceAll(" ", ""),
|
() => singular(tagName).replaceAll(" ", ""),
|
||||||
[tagName]
|
[tagName]
|
||||||
@@ -129,7 +127,7 @@ const TagSectionSchema = ({ schema, tagName }: TagSectionSchemaProps) => {
|
|||||||
root={root}
|
root={root}
|
||||||
threshold={0.1}
|
threshold={0.1}
|
||||||
>
|
>
|
||||||
<SectionContainer ref={paramsRef}>
|
<SectionContainer>
|
||||||
<DividedLayout
|
<DividedLayout
|
||||||
mainContent={
|
mainContent={
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import clsx from "clsx"
|
|||||||
import { Feedback, Loading, Link } from "docs-ui"
|
import { Feedback, Loading, Link } from "docs-ui"
|
||||||
import { usePathname, useRouter } from "next/navigation"
|
import { usePathname, useRouter } from "next/navigation"
|
||||||
import { PathsObject, SchemaObject, TagObject } from "@/types/openapi"
|
import { PathsObject, SchemaObject, TagObject } from "@/types/openapi"
|
||||||
import { TagSectionSchemaProps } from "./Schema"
|
import TagSectionSchema from "./Schema"
|
||||||
import checkElementInViewport from "../../../utils/check-element-in-viewport"
|
import checkElementInViewport from "../../../utils/check-element-in-viewport"
|
||||||
import TagPaths from "../Paths"
|
import TagPaths from "../Paths"
|
||||||
import useSWR from "swr"
|
import useSWR from "swr"
|
||||||
@@ -37,10 +37,6 @@ const Section = dynamic<SectionProps>(
|
|||||||
async () => import("../../Section")
|
async () => import("../../Section")
|
||||||
) as React.FC<SectionProps>
|
) as React.FC<SectionProps>
|
||||||
|
|
||||||
const TagSectionSchema = dynamic<TagSectionSchemaProps>(
|
|
||||||
async () => import("./Schema")
|
|
||||||
) as React.FC<TagSectionSchemaProps>
|
|
||||||
|
|
||||||
const MDXContentClient = dynamic<MDXContentClientProps>(
|
const MDXContentClient = dynamic<MDXContentClientProps>(
|
||||||
async () => import("../../MDXContent/Client"),
|
async () => import("../../MDXContent/Client"),
|
||||||
{
|
{
|
||||||
@@ -117,21 +113,22 @@ const TagSectionComponent = ({ tag }: TagSectionProps) => {
|
|||||||
rootMargin={`112px 0px 112px 0px`}
|
rootMargin={`112px 0px 112px 0px`}
|
||||||
root={root}
|
root={root}
|
||||||
onChange={(inView) => {
|
onChange={(inView) => {
|
||||||
if (inView) {
|
if (!inView) {
|
||||||
if (!loadData) {
|
return
|
||||||
setLoadData(true)
|
}
|
||||||
|
if (!loadData) {
|
||||||
|
setLoadData(true)
|
||||||
|
}
|
||||||
|
// ensure that the hash link doesn't change if it links to an inner path
|
||||||
|
const currentHashArr = location.hash.replace("#", "").split("_")
|
||||||
|
if (currentHashArr.length < 2 || currentHashArr[0] !== slugTagName) {
|
||||||
|
if (location.hash !== slugTagName) {
|
||||||
|
router.push(`#${slugTagName}`, {
|
||||||
|
scroll: false,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
// ensure that the hash link doesn't change if it links to an inner path
|
if (activePath !== slugTagName) {
|
||||||
const currentHashArr = location.hash.replace("#", "").split("_")
|
setActivePath(slugTagName)
|
||||||
if (currentHashArr.length < 2 || currentHashArr[0] !== slugTagName) {
|
|
||||||
if (location.hash !== slugTagName) {
|
|
||||||
router.push(`#${slugTagName}`, {
|
|
||||||
scroll: false,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (activePath !== slugTagName) {
|
|
||||||
setActivePath(slugTagName)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -98,6 +98,18 @@ export const useActiveOnScroll = ({
|
|||||||
if (!enable) {
|
if (!enable) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
const rootBoundingRectElm =
|
||||||
|
root && "getBoundingClientRect" in root
|
||||||
|
? root.getBoundingClientRect()
|
||||||
|
: root?.body.getBoundingClientRect()
|
||||||
|
|
||||||
|
if (
|
||||||
|
rootBoundingRectElm === undefined ||
|
||||||
|
(rootBoundingRectElm.top < 0 && rootBoundingRectElm.bottom < 0)
|
||||||
|
) {
|
||||||
|
setActiveItemId("")
|
||||||
|
return
|
||||||
|
}
|
||||||
const headings = getHeadingsInElm()
|
const headings = getHeadingsInElm()
|
||||||
let selectedHeadingByHash: HTMLHeadingElement | undefined = undefined
|
let selectedHeadingByHash: HTMLHeadingElement | undefined = undefined
|
||||||
const hash = location.hash.replace("#", "")
|
const hash = location.hash.replace("#", "")
|
||||||
@@ -129,17 +141,19 @@ export const useActiveOnScroll = ({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const negativeDistanceToHalfway = Math.abs(
|
const negativeDistanceToHalfway = closestNegativeDistance
|
||||||
halfway + closestNegativeDistance
|
? Math.abs(halfway + closestNegativeDistance)
|
||||||
)
|
: 0
|
||||||
const positiveDistanceToHalfway = Math.abs(
|
const positiveDistanceToHalfway = closestPositiveDistance
|
||||||
halfway - closestPositiveDistance
|
? Math.abs(halfway - closestPositiveDistance)
|
||||||
)
|
: 0
|
||||||
|
|
||||||
const chosenClosest =
|
const chosenClosest =
|
||||||
negativeDistanceToHalfway > positiveDistanceToHalfway
|
!negativeDistanceToHalfway && !positiveDistanceToHalfway
|
||||||
? closestNegativeHeading
|
? undefined
|
||||||
: closestPositiveHeading
|
: negativeDistanceToHalfway > positiveDistanceToHalfway
|
||||||
|
? closestNegativeHeading
|
||||||
|
: closestPositiveHeading
|
||||||
|
|
||||||
setActiveItemId(
|
setActiveItemId(
|
||||||
chosenClosest
|
chosenClosest
|
||||||
@@ -152,7 +166,7 @@ export const useActiveOnScroll = ({
|
|||||||
: ""
|
: ""
|
||||||
: ""
|
: ""
|
||||||
)
|
)
|
||||||
}, [getHeadingsInElm, items, enable])
|
}, [getHeadingsInElm, items, enable, root])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!scrollableElement || !enable) {
|
if (!scrollableElement || !enable) {
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ function useScrollControllerContextValue({
|
|||||||
// 56 is the height of the navbar
|
// 56 is the height of the navbar
|
||||||
// might need a better way to determine it.
|
// might need a better way to determine it.
|
||||||
top: top - parentOffsetTop - 56,
|
top: top - parentOffsetTop - 56,
|
||||||
|
behavior: "instant",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user