Fumadocs

Breadcrumb

屏幕顶部的导航组件

一个用于在您的文档中实现 Breadcrumb 的钩子。它根据给定的页面树为页面返回面包屑项。

如果存在,文件夹的索引页面将被用作该项。

用法

它导出一个 useBreadcrumb 钩子:

import {  } from 'fumadocs-core/breadcrumb';

// obtain `pathname` using the hook provided by your React framework.
const  = ();
const items = (, );
const items: BreadcrumbItem[]

示例

Next.js 的一个样式化示例。

'use client';
import { usePathname } from 'next/navigation';
import { useBreadcrumb } from 'fumadocs-core/breadcrumb';
import type { PageTree } from 'fumadocs-core/server';
import { Fragment } from 'react';
import { ChevronRight } from 'lucide-react';
import Link from 'next/link';

export function Breadcrumb({ tree }: { tree: PageTree.Root }) {
  const pathname = usePathname();
  const items = useBreadcrumb(pathname, tree);

  if (items.length === 0) return null;

  return (
    <div className="-mb-3 flex flex-row items-center gap-1 text-sm font-medium text-fd-muted-foreground">
      {items.map((item, i) => (
        <Fragment key={i}>
          {i !== 0 && (
            <ChevronRight className="size-4 shrink-0 rtl:rotate-180" />
          )}
          {item.url ? (
            <Link
              href={item.url}
              className="truncate hover:text-fd-accent-foreground"
            >
              {item.name}
            </Link>
          ) : (
            <span className="truncate">{item.name}</span>
          )}
        </Fragment>
      ))}
    </div>
  );
}

您可以通过在服务器组件中通过 tree prop 传递页面树来使用它。

Prop

Type

How is this guide?

Last updated on