mirror of
https://github.com/hanzoai/shadcn.git
synced 2026-10-30 18:22:59 +00:00
Squash merge of shadcn-ui/ui main branch into hanzo/ui, bringing in: - [email protected] through [email protected] releases - New apps/v4 directory structure - Registry system improvements and MCP server - Framework support (Vue, Svelte, React Native) - New templates (vite-app, monorepo-next, laravel) - Zod 4 compatibility (scoped override for zod@>4) - Test fixture corrections for alias resolution All 867 merge conflicts resolved preserving Hanzo customizations. 1020/1020 shadcn tests pass.
131 lines
3.1 KiB
TypeScript
131 lines
3.1 KiB
TypeScript
// @ts-nocheck
|
|
"use client"
|
|
|
|
import * as React from "react"
|
|
|
|
import { TableOfContents } from "@/lib/toc"
|
|
import { cn } from "@/lib/utils"
|
|
import { useMounted } from "@/hooks/use-mounted"
|
|
|
|
interface TocProps {
|
|
toc: TableOfContents
|
|
}
|
|
|
|
export function DashboardTableOfContents({ toc }: TocProps) {
|
|
const itemIds = React.useMemo(
|
|
() =>
|
|
toc.items
|
|
? toc.items
|
|
.flatMap((item) => [item.url, item?.items?.map((item) => item.url)])
|
|
.flat()
|
|
.filter(Boolean)
|
|
.map((id) => id?.split("#")[1])
|
|
: [],
|
|
[toc]
|
|
)
|
|
const activeHeading = useActiveItem(itemIds)
|
|
const mounted = useMounted()
|
|
|
|
if (!toc?.items?.length) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-2">
|
|
<p className="font-medium">On This Page</p>
|
|
<Tree tree={toc} activeItem={activeHeading} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
// DocsTableOfContents for Fumadocs MDX pages
|
|
export function DocsTableOfContents({ toc }: { toc: any[] }) {
|
|
const mounted = useMounted()
|
|
|
|
if (!toc || toc.length === 0 || !mounted) {
|
|
return null
|
|
}
|
|
|
|
// Convert Fumadocs TOC format to our format if needed
|
|
const tocItems = toc.map((item) => ({
|
|
title: item.title,
|
|
url: `#${item.url || item.id || ""}`,
|
|
items: item.children?.map((child: any) => ({
|
|
title: child.title,
|
|
url: `#${child.url || child.id || ""}`,
|
|
})),
|
|
}))
|
|
|
|
const tocData = { items: tocItems }
|
|
|
|
return <DashboardTableOfContents toc={tocData} />
|
|
}
|
|
|
|
function useActiveItem(itemIds: string[]) {
|
|
const [activeId, setActiveId] = React.useState(null)
|
|
|
|
React.useEffect(() => {
|
|
const observer = new IntersectionObserver(
|
|
(entries) => {
|
|
entries.forEach((entry) => {
|
|
if (entry.isIntersecting) {
|
|
setActiveId(entry.target.id)
|
|
}
|
|
})
|
|
},
|
|
{ rootMargin: `0% 0% -80% 0%` }
|
|
)
|
|
|
|
itemIds?.forEach((id) => {
|
|
const element = document.getElementById(id)
|
|
if (element) {
|
|
observer.observe(element)
|
|
}
|
|
})
|
|
|
|
return () => {
|
|
itemIds?.forEach((id) => {
|
|
const element = document.getElementById(id)
|
|
if (element) {
|
|
observer.unobserve(element)
|
|
}
|
|
})
|
|
}
|
|
}, [itemIds])
|
|
|
|
return activeId
|
|
}
|
|
|
|
interface TreeProps {
|
|
tree: TableOfContents
|
|
level?: number
|
|
activeItem?: string
|
|
}
|
|
|
|
function Tree({ tree, level = 1, activeItem }: TreeProps) {
|
|
return tree?.items?.length && level < 3 ? (
|
|
<ul className={cn("m-0 list-none", { "pl-4": level !== 1 })}>
|
|
{tree.items.map((item, index) => {
|
|
return (
|
|
<li key={index} className={cn("mt-0 pt-2")}>
|
|
<a
|
|
href={item.url}
|
|
className={cn(
|
|
"inline-block no-underline transition-colors hover:text-foreground",
|
|
item.url === `#${activeItem}`
|
|
? "font-medium text-foreground"
|
|
: "text-muted-foreground"
|
|
)}
|
|
>
|
|
{item.title}
|
|
</a>
|
|
{item.items?.length ? (
|
|
<Tree tree={item} level={level + 1} activeItem={activeItem} />
|
|
) : null}
|
|
</li>
|
|
)
|
|
})}
|
|
</ul>
|
|
) : null
|
|
}
|