mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-30 08:04:18 +00:00
205 lines
7.0 KiB
TypeScript
205 lines
7.0 KiB
TypeScript
"use client"
|
|
|
|
import Link from "next/link"
|
|
import { usePathname } from "next/navigation"
|
|
|
|
import { PAGES_NEW } from "@/lib/docs"
|
|
import { showMcpDocs } from "@/lib/flags"
|
|
import type { source } from "@/lib/source"
|
|
import {
|
|
Sidebar,
|
|
SidebarContent,
|
|
SidebarGroup,
|
|
SidebarGroupContent,
|
|
SidebarGroupLabel,
|
|
SidebarMenu,
|
|
SidebarMenuButton,
|
|
SidebarMenuItem,
|
|
} from "@/registry/new-york-v4/ui/sidebar"
|
|
|
|
type PageTreeNode = (typeof source.pageTree)["children"][number]
|
|
type PageTreeFolder = Extract<PageTreeNode, { type: "folder" }>
|
|
type PageTreePage = Extract<PageTreeNode, { type: "page" }>
|
|
|
|
const TOP_LEVEL_SECTIONS = [
|
|
{ name: "Get Started", href: "/docs" },
|
|
{
|
|
name: "Components",
|
|
href: "/docs/components",
|
|
},
|
|
{
|
|
name: "Directory",
|
|
href: "/docs/directory",
|
|
},
|
|
{
|
|
name: "MCP Server",
|
|
href: "/docs/mcp",
|
|
},
|
|
{
|
|
name: "Forms",
|
|
href: "/docs/forms",
|
|
},
|
|
{
|
|
name: "Changelog",
|
|
href: "/docs/changelog",
|
|
},
|
|
]
|
|
const EXCLUDED_SECTIONS = ["installation", "dark-mode"]
|
|
const EXCLUDED_PAGES = ["/docs", "/docs/changelog"]
|
|
|
|
// Recursively find all pages in a folder tree.
|
|
function getAllPagesFromFolder(folder: PageTreeFolder): PageTreePage[] {
|
|
const pages: PageTreePage[] = []
|
|
|
|
for (const child of folder.children) {
|
|
if (child.type === "page") {
|
|
pages.push(child)
|
|
} else if (child.type === "folder") {
|
|
pages.push(...getAllPagesFromFolder(child))
|
|
}
|
|
}
|
|
|
|
return pages
|
|
}
|
|
|
|
// Get the pages from a folder, handling nested base folders (radix/base).
|
|
function getPagesFromFolder(
|
|
folder: PageTreeFolder,
|
|
currentBase: string
|
|
): PageTreePage[] {
|
|
// For the components folder, find the base subfolder.
|
|
if (folder.$id === "components" || folder.name === "Components") {
|
|
for (const child of folder.children) {
|
|
if (child.type === "folder") {
|
|
// Match by $id or by name.
|
|
const isRadix = child.$id === "radix" || child.name === "Radix UI"
|
|
const isBase = child.$id === "base" || child.name === "Base UI"
|
|
|
|
if (
|
|
(currentBase === "radix" && isRadix) ||
|
|
(currentBase === "base" && isBase)
|
|
) {
|
|
return child.children.filter(
|
|
(c): c is PageTreePage => c.type === "page"
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fallback: return all pages from nested folders.
|
|
return getAllPagesFromFolder(folder).filter(
|
|
(page) => !page.url.endsWith("/components")
|
|
)
|
|
}
|
|
|
|
// For other folders, return direct page children.
|
|
return folder.children.filter(
|
|
(child): child is PageTreePage => child.type === "page"
|
|
)
|
|
}
|
|
|
|
export function DocsSidebar({
|
|
tree,
|
|
...props
|
|
}: React.ComponentProps<typeof Sidebar> & { tree: typeof source.pageTree }) {
|
|
const pathname = usePathname()
|
|
|
|
// Detect current base from URL (radix or base).
|
|
const baseMatch = pathname.match(/\/docs\/components\/(radix|base)\//)
|
|
const currentBase = baseMatch ? baseMatch[1] : "radix" // Default to radix.
|
|
|
|
return (
|
|
<Sidebar
|
|
className="sticky top-[calc(var(--header-height)+1px)] z-30 hidden h-[calc(100svh-var(--footer-height)-4rem)] overscroll-none bg-transparent lg:flex"
|
|
collapsible="none"
|
|
{...props}
|
|
>
|
|
<SidebarContent className="no-scrollbar overflow-x-hidden px-2">
|
|
<div className="from-background via-background/80 to-background/50 sticky -top-1 z-10 h-8 shrink-0 bg-gradient-to-b blur-xs" />
|
|
<SidebarGroup>
|
|
<SidebarGroupLabel className="text-muted-foreground font-medium">
|
|
Sections
|
|
</SidebarGroupLabel>
|
|
<SidebarGroupContent>
|
|
<SidebarMenu>
|
|
{TOP_LEVEL_SECTIONS.map(({ name, href }) => {
|
|
if (!showMcpDocs && href.includes("/mcp")) {
|
|
return null
|
|
}
|
|
return (
|
|
<SidebarMenuItem key={name}>
|
|
<SidebarMenuButton
|
|
asChild
|
|
isActive={
|
|
href === "/docs"
|
|
? pathname === href
|
|
: pathname.startsWith(href)
|
|
}
|
|
className="data-[active=true]:bg-accent data-[active=true]:border-accent 3xl:fixed:w-full 3xl:fixed:max-w-48 relative h-[30px] w-fit overflow-visible border border-transparent text-[0.8rem] font-medium after:absolute after:inset-x-0 after:-inset-y-1 after:z-0 after:rounded-md"
|
|
>
|
|
<Link href={href}>
|
|
<span className="absolute inset-0 flex w-(--sidebar-width) bg-transparent" />
|
|
{name}
|
|
</Link>
|
|
</SidebarMenuButton>
|
|
</SidebarMenuItem>
|
|
)
|
|
})}
|
|
</SidebarMenu>
|
|
</SidebarGroupContent>
|
|
</SidebarGroup>
|
|
{tree.children.map((item) => {
|
|
if (EXCLUDED_SECTIONS.includes(item.$id ?? "")) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<SidebarGroup key={item.$id}>
|
|
<SidebarGroupLabel className="text-muted-foreground font-medium">
|
|
{item.name}
|
|
</SidebarGroupLabel>
|
|
<SidebarGroupContent>
|
|
{item.type === "folder" && (
|
|
<SidebarMenu className="gap-0.5">
|
|
{getPagesFromFolder(item, currentBase).map((page) => {
|
|
if (!showMcpDocs && page.url.includes("/mcp")) {
|
|
return null
|
|
}
|
|
|
|
if (EXCLUDED_PAGES.includes(page.url)) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<SidebarMenuItem key={page.url}>
|
|
<SidebarMenuButton
|
|
asChild
|
|
isActive={page.url === pathname}
|
|
className="data-[active=true]:bg-accent data-[active=true]:border-accent 3xl:fixed:w-full 3xl:fixed:max-w-48 relative h-[30px] w-fit overflow-visible border border-transparent text-[0.8rem] font-medium after:absolute after:inset-x-0 after:-inset-y-1 after:z-0 after:rounded-md"
|
|
>
|
|
<Link href={page.url}>
|
|
<span className="absolute inset-0 flex w-(--sidebar-width) bg-transparent" />
|
|
{page.name}
|
|
{PAGES_NEW.includes(page.url) && (
|
|
<span
|
|
className="flex size-2 rounded-full bg-blue-500"
|
|
title="New"
|
|
/>
|
|
)}
|
|
</Link>
|
|
</SidebarMenuButton>
|
|
</SidebarMenuItem>
|
|
)
|
|
})}
|
|
</SidebarMenu>
|
|
)}
|
|
</SidebarGroupContent>
|
|
</SidebarGroup>
|
|
)
|
|
})}
|
|
<div className="from-background via-background/80 to-background/50 sticky -bottom-1 z-10 h-16 shrink-0 bg-gradient-to-t blur-xs" />
|
|
</SidebarContent>
|
|
</Sidebar>
|
|
)
|
|
}
|