Grence

Breadcrumb

The navigation component at the top of screen

A hook for implementing Breadcrumb in your documentation, it returns the path to a page based on the given page tree.

If present, the index page of a folder will be used as the item

Usage

it exports a useBreadcrumb hook:

declare const tree: any;
// ---cut---
import { usePathname } from "next/navigation";
import { useBreadcrumb } from "fumadocs-core/breadcrumb";

const pathname = usePathname();
const items = useBreadcrumb(pathname, tree);
//    ^?

Example

A styled example.

"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>
  );
}

You can use it by passing the page tree via tree in a server component.

PropTypeDefault
name
ReactNode
-
url?
string
-

How is this guide?

On this page