Files
Shahed Nasser 4d632e7a5d docs: added tests for components in api-reference project (#14428)
* add tests (WIP)

* added test for h2

* finished adding tests

* fixes

* fixes

* fixes
2026-01-05 10:56:56 +02:00

55 lines
1.2 KiB
TypeScript

"use client"
import React from "react"
import clsx from "clsx"
import { useActiveOnScroll, useSidebar } from "docs-ui"
import { useRouter } from "next/navigation"
import { useEffect, useRef } from "react"
export type SectionProps = {
checkActiveOnScroll?: boolean
} & React.AllHTMLAttributes<HTMLDivElement>
const Section = ({
children,
className,
checkActiveOnScroll = false,
}: SectionProps) => {
const sectionRef = useRef<HTMLDivElement>(null)
const { activeItemId } = useActiveOnScroll({
rootElm: sectionRef.current || undefined,
enable: checkActiveOnScroll,
useDefaultIfNoActive: false,
maxLevel: 2,
})
const { setActivePath } = useSidebar()
const router = useRouter()
useEffect(() => {
if ("scrollRestoration" in history) {
// disable scroll on refresh
history.scrollRestoration = "manual"
}
}, [])
useEffect(() => {
if (activeItemId.length) {
router.push(`#${activeItemId}`, {
scroll: false,
})
setActivePath(activeItemId)
}
}, [activeItemId])
return (
<div
ref={sectionRef}
className={clsx("[&_ul]:list-disc [&_ul]:px-1", "[&_h2]:pt-7", className)}
>
{children}
</div>
)
}
export default Section