1 "use client"
2
3 import React from "react"
4 import clsx from "clsx"
5 import { useActiveOnScroll, useSidebar } from "docs-ui"
6 import { useEffect, useRef } from "react"
7 import { useArea } from "@/providers/area"
8 import basePathUrl from "../../utils/base-path-url"
9
10 export type SectionProps = {
11 checkActiveOnScroll?: boolean
12 } & React.AllHTMLAttributes<HTMLDivElement>
13
14 const Section = ({
15 children,
16 className,
17 checkActiveOnScroll = false,
18 }: SectionProps) => {
19 const sectionRef = useRef<HTMLDivElement>(null)
20 const { activeItemId } = useActiveOnScroll({
21 rootElm: sectionRef.current || undefined,
22 enable: checkActiveOnScroll,
23 useDefaultIfNoActive: false,
24 maxLevel: 2,
25 })
26 const { setActivePath } = useSidebar()
27 const { area } = useArea()
28
29 useEffect(() => {
30 if ("scrollRestoration" in history) {
31 // disable scroll on refresh
32 history.scrollRestoration = "manual"
33 }
34 }, [])
35
36 useEffect(() => {
37 if (!activeItemId.length) {
38 return
39 }
40
41 // map the intro heading in view to its page path (the top "introduction"
42 // heading maps to the area index).
43 const path =
44 activeItemId === "introduction" ? `/${area}` : `/${area}/${activeItemId}`
45 window.history.replaceState(null, "", basePathUrl(path))
46 setActivePath(path)
47 }, [activeItemId, area])
48
49 return (
50 <div
51 ref={sectionRef}
52 className={clsx("[&_ul]:list-disc [&_ul]:px-1", "[&_h2]:pt-7", className)}
53 data-active-on-scroll={checkActiveOnScroll || undefined}
54 >
55 {children}
56 </div>
57 )
58 }
59
60 export default Section