Files
medusa-store/www/apps/api-reference/components/MDXComponents/H2/index.tsx
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

38 lines
1.1 KiB
TypeScript

"use client"
import React, { useEffect, useMemo, useRef, useState } from "react"
import { useScrollController, useSidebar, H2 as UiH2 } from "docs-ui"
import { getSectionId } from "docs-utils"
type H2Props = React.HTMLAttributes<HTMLHeadingElement>
const H2 = ({ children, ...props }: H2Props) => {
const headingRef = useRef<HTMLHeadingElement>(null)
const { activePath } = useSidebar()
const { scrollableElement, scrollToElement } = useScrollController()
const [scrolledFirstTime, setScrolledFirstTime] = useState(false)
const id = useMemo(() => getSectionId([children as string]), [children])
useEffect(() => {
if (!scrollableElement || !headingRef.current || scrolledFirstTime) {
return
}
if (id === (activePath || location.hash.replace("#", ""))) {
scrollToElement(
(headingRef.current.offsetParent as HTMLElement) || headingRef.current
)
}
setScrolledFirstTime(scrolledFirstTime)
}, [scrollableElement, headingRef, id])
return (
<UiH2 {...props} id={id} passRef={headingRef}>
{children}
</UiH2>
)
}
export default H2