|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { useParams, usePathname } from "next/navigation"; |
| 6 | +import type { PageTree, TOCItemType } from "fumadocs-core/server"; |
| 7 | +import { findNeighbour } from "fumadocs-core/server"; |
| 8 | +import { useTreeContext, useTreePath } from "fumadocs-ui/provider"; |
| 9 | +import * as Base from "fumadocs-core/toc"; |
| 10 | +import { useActiveAnchor } from "fumadocs-core/toc"; |
| 11 | +import { repoDocsPages } from "@/app/source"; |
| 12 | +import { AlignmentLeft } from "../icons/alignment-left"; |
| 13 | +import { ChevronLeft } from "../icons/chevron-left"; |
| 14 | +import { ChevronRight } from "../icons/chevron-right"; |
| 15 | +import { SidebarMenu } from "@/components/ui/sidebar"; |
| 16 | +import { |
| 17 | + SidebarFolder, |
| 18 | + SidebarFolderLink, |
| 19 | + SidebarItem, |
| 20 | + SidebarFolderContent, |
| 21 | + SidebarFolderTrigger, |
| 22 | + SidebarSeparator, |
| 23 | +} from "./sidebar"; |
| 24 | + |
| 25 | +export const LayoutBody = ({ children }: { children: React.ReactNode }) => { |
| 26 | + return ( |
| 27 | + <div |
| 28 | + id="nd-docs-layout" |
| 29 | + className="mx-auto mb-16 grid w-full max-w-screen-xl grid-cols-1 gap-x-6 md:grid-cols-[var(--sidebar-width)_minmax(0,1fr)]" |
| 30 | + > |
| 31 | + {children} |
| 32 | + </div> |
| 33 | + ); |
| 34 | +}; |
| 35 | + |
| 36 | +function renderSidebarList(items: PageTree.Node[]): React.ReactNode[] { |
| 37 | + return items.map((item, i) => { |
| 38 | + const id = `${item.type}_${i}`; |
| 39 | + |
| 40 | + switch (item.type) { |
| 41 | + case "separator": |
| 42 | + return <SidebarSeparator key={id}>{item.name}</SidebarSeparator>; |
| 43 | + case "folder": |
| 44 | + return ( |
| 45 | + <PageTreeFolder key={id} item={item}> |
| 46 | + {item.index ? ( |
| 47 | + <SidebarFolderLink href={item.index.url}> |
| 48 | + {item.name} |
| 49 | + </SidebarFolderLink> |
| 50 | + ) : ( |
| 51 | + <SidebarFolderTrigger>{item.name}</SidebarFolderTrigger> |
| 52 | + )} |
| 53 | + <SidebarFolderContent> |
| 54 | + {renderSidebarList(item.children)} |
| 55 | + </SidebarFolderContent> |
| 56 | + </PageTreeFolder> |
| 57 | + ); |
| 58 | + default: |
| 59 | + return ( |
| 60 | + <SidebarItem key={item.url} href={item.url}> |
| 61 | + {item.name} |
| 62 | + </SidebarItem> |
| 63 | + ); |
| 64 | + } |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +export const SidebarItems = () => { |
| 69 | + const { root } = useTreeContext(); |
| 70 | + return ( |
| 71 | + <SidebarMenu className="flex flex-col gap-y-2.5"> |
| 72 | + {renderSidebarList(root.children)} |
| 73 | + </SidebarMenu> |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +const PageTreeFolder = ({ |
| 78 | + item, |
| 79 | + children, |
| 80 | +}: { |
| 81 | + item: PageTree.Folder; |
| 82 | + children: React.ReactNode; |
| 83 | +}) => { |
| 84 | + const path = useTreePath(); |
| 85 | + return ( |
| 86 | + <SidebarFolder defaultOpen={item.defaultOpen || path.includes(item)}> |
| 87 | + {children} |
| 88 | + </SidebarFolder> |
| 89 | + ); |
| 90 | +}; |
| 91 | + |
| 92 | +export const Footer = () => { |
| 93 | + const { root } = useTreeContext(); |
| 94 | + const pathname = usePathname(); |
| 95 | + const neighbours = findNeighbour(root, pathname); |
| 96 | + return ( |
| 97 | + <div className="grid w-full grid-cols-2 gap-4 md:mt-4 md:pb-6"> |
| 98 | + {neighbours.previous ? ( |
| 99 | + <Link |
| 100 | + className="group flex w-full flex-col items-start gap-2 text-sm no-underline opacity-100 transition-colors [&>*]:hover:text-gray-1000 [&_svg]:hover:fill-gray-1000" |
| 101 | + href={neighbours.previous.url} |
| 102 | + > |
| 103 | + <div className="flex items-center justify-center gap-x-1.5 text-gray-900 text-label-14"> |
| 104 | + <ChevronLeft className="translate-y-px w-[10px] h-[10px]" /> |
| 105 | + Previous |
| 106 | + </div> |
| 107 | + <span className="font-medium text-gray-1000 text-label-16"> |
| 108 | + {neighbours.previous.name} |
| 109 | + </span> |
| 110 | + </Link> |
| 111 | + ) : null} |
| 112 | + {neighbours.next ? ( |
| 113 | + <Link |
| 114 | + className="col-start-2 flex w-full flex-col items-end gap-2 text-sm no-underline opacity-100 transition-colors [&>*]:hover:text-gray-1000 [&_svg]:hover:fill-gray-1000" |
| 115 | + href={neighbours.next.url} |
| 116 | + > |
| 117 | + <div className="flex items-center justify-center gap-x-1.5 text-gray-900 text-label-14"> |
| 118 | + Next <ChevronRight className="translate-y-px w-[10px] h-[10px]" /> |
| 119 | + </div> |
| 120 | + <span className="font-medium text-gray-1000 text-label-16"> |
| 121 | + {neighbours.next.name} |
| 122 | + </span> |
| 123 | + </Link> |
| 124 | + ) : null} |
| 125 | + </div> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +function getDepthClassName(depth: number) { |
| 130 | + switch (depth) { |
| 131 | + case 3: |
| 132 | + return "pl-3"; |
| 133 | + case 4: |
| 134 | + return "pl-6"; |
| 135 | + case 5: |
| 136 | + return "pl-9"; |
| 137 | + default: |
| 138 | + return ""; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +const TOCItem = ({ item }: { item: TOCItemType }) => { |
| 143 | + const activeAnchor = useActiveAnchor(); |
| 144 | + const isActive = item.url.replace("#", "") === activeAnchor; |
| 145 | + |
| 146 | + return ( |
| 147 | + <li className={`text-sm text-gray-900 ${getDepthClassName(item.depth)}`}> |
| 148 | + <Base.TOCItem |
| 149 | + data-active={isActive} |
| 150 | + href={item.url} |
| 151 | + className="data-[active=true]:text-blue-700 dark:data-[active=true]:text-blue-600" |
| 152 | + > |
| 153 | + {item.title} |
| 154 | + </Base.TOCItem> |
| 155 | + </li> |
| 156 | + ); |
| 157 | +}; |
| 158 | + |
| 159 | +export const TableOfContents = () => { |
| 160 | + const params = useParams<{ code: string; slug: string[] }>(); |
| 161 | + const page = repoDocsPages.getPage(params.slug); |
| 162 | + if (!page) return null; |
| 163 | + const { data } = page; |
| 164 | + const ref = React.useRef<HTMLDivElement>(null); |
| 165 | + |
| 166 | + return ( |
| 167 | + <Base.AnchorProvider toc={data.toc}> |
| 168 | + <Base.ScrollProvider containerRef={ref}> |
| 169 | + <span className="-ms-0.5 flex items-center gap-x-1.5 text-sm font-medium text-gray-1000"> |
| 170 | + <AlignmentLeft className="w-3 h-3" /> |
| 171 | + On this page |
| 172 | + </span> |
| 173 | + {/* Fumadocs doesn't include title in the TOC by default, but this is too hack to keep atm */} |
| 174 | + {/* <span className="text-sm text-gray-900">{data.title}</span> */} |
| 175 | + <ul className="flex flex-col gap-y-2.5 text-sm text-gray-900"> |
| 176 | + {data.toc.map((item) => { |
| 177 | + return <TOCItem key={item.url} item={item} />; |
| 178 | + })} |
| 179 | + </ul> |
| 180 | + </Base.ScrollProvider> |
| 181 | + </Base.AnchorProvider> |
| 182 | + ); |
| 183 | +}; |
0 commit comments