"use client" import { ChevronDown, ChevronUp, PanelRight, MoreVertical } from "lucide-react" import { Sheet, SheetClose, SheetContent, SheetHeader, SheetTrigger } from "@/components/ui/sheet" import DocsMenu from "@/components/DocsMenu" import { ModeToggle } from "@/components/ThemeToggle" import { DialogTitle, DialogDescription } from "@/components/ui/dialog" import ContextPopover from "@/components/ContextPopover" import TocObserver from "./TocObserver" import * as React from "react" import { useRef, useMemo } from "react" import { usePathname } from "next/navigation" import { Button } from "./ui/button" import { motion, AnimatePresence } from "framer-motion" import { useActiveSection } from "@/hooks" import { TocItem } from "@/lib/toc" import Search from "@/components/SearchBox" import { NavMenu } from "@/components/navbar" import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" interface MobTocProps { tocs: TocItem[] title?: string } const useClickOutside = (ref: React.RefObject, callback: () => void) => { const handleClick = React.useCallback( (event: MouseEvent) => { if (ref.current && !ref.current.contains(event.target as Node)) { callback() } }, [ref, callback] ) React.useEffect(() => { document.addEventListener("mousedown", handleClick) return () => { document.removeEventListener("mousedown", handleClick) } }, [handleClick]) } export default function MobToc({ tocs, title }: MobTocProps) { const pathname = usePathname() const [isExpanded, setIsExpanded] = React.useState(false) const tocRef = useRef(null) const contentRef = useRef(null) // Use custom hooks const { activeId, setActiveId } = useActiveSection(tocs) // Only show on /docs pages const isDocsPage = useMemo(() => pathname?.startsWith("/docs"), [pathname]) // Get title from active section if available, otherwise document title const activeSection = useMemo(() => { return tocs.find((toc) => toc.href.slice(1) === activeId) }, [tocs, activeId]) const displayTitle = activeSection?.text || title || "On this page" const [mounted, setMounted] = React.useState(false) React.useEffect(() => { setMounted(true) }, []) // Toggle expanded state const toggleExpanded = React.useCallback((e: React.MouseEvent) => { e.stopPropagation() setIsExpanded((prev) => !prev) }, []) // Close TOC when clicking outside useClickOutside(tocRef, () => { if (isExpanded) { setIsExpanded(false) } }) // Handle body overflow when TOC is expanded React.useEffect(() => { if (isExpanded) { document.body.style.overflow = "hidden" } else { document.body.style.overflow = "" } return () => { document.body.style.overflow = "" } }, [isExpanded]) // Don't render anything if not on docs page if (!isDocsPage || !mounted) return null const chevronIcon = isExpanded ? ( ) : ( ) return (
Navigation Menu Main navigation menu with links to different sections
{isExpanded && ( {tocs?.length ? ( ) : (

No headings

)}
)}
) }