Menu

Components

Breadcrumbs

The path to the current page as a row of links, so people can see where they are and go back up.

Introduction

Breadcrumbs show where the current page sits in the site: Home, then Docs, then Components, then this page. Each step but the last is a link back up. They suit sites and apps with more than two levels of pages, such as documentation, a shop with categories, or a file browser.

  • A real nav with an ordered list. The last item is the current page: plain text marked aria-current="page".
  • Links go through next/link, so navigation happens on the client.
  • Items can carry an icon. The separator is a chevron, a slash, or anything you pass.
  • A long trail folds with maxItems: the first item, a "…" button that opens the items left out, then the last ones. The row wraps and long labels are cut, so it never overflows.

Breadcrumbs show location, not history, and they do not replace the main navigation.

Installation

centerUI has no CLI or registry yet: components are copied into your project. Breadcrumbs uses Button and Popover for its folded items.

# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx

# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx

# npm
npm install react-aria-components tailwind-variants tailwind-merge clsx

Add Button and Popover first (see their Manual installation) if you use maxItems, then follow the steps below.

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles and the context

src/types/breadcrumbs-styles.tsShow code
import { tv } from "tailwind-variants";

// Styles for Breadcrumbs. Every item is a list item that holds the link (or the
// current page) and the separator that comes after it.
export const breadcrumbsStyles = tv({
  slots: {
    list: "flex flex-wrap items-center gap-y-1",
    item: "inline-flex min-w-0 items-center",
    link: [
      "inline-flex min-w-0 max-w-64 items-center gap-1.5 rounded-sm text-muted-fg outline-none transition-colors [&_svg]:shrink-0",
      "hover:text-fg",
      "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-bg",
    ],
    current:
      "inline-flex min-w-0 max-w-64 items-center gap-1.5 font-medium text-fg [&_svg]:shrink-0",
    separator: "flex shrink-0 items-center text-muted-fg/60 select-none",
    ellipsis: "text-muted-fg",
  },
  variants: {
    size: {
      sm: {
        list: "text-xs",
        link: "[&_svg]:size-3.5",
        current: "[&_svg]:size-3.5",
        separator: "mx-1.5 [&_svg]:size-3",
      },
      md: {
        list: "text-sm",
        link: "[&_svg]:size-4",
        current: "[&_svg]:size-4",
        separator: "mx-2 [&_svg]:size-3.5",
      },
      lg: {
        list: "text-base",
        link: "[&_svg]:size-5",
        current: "[&_svg]:size-5",
        separator: "mx-2.5 [&_svg]:size-4",
      },
    },
  },
  defaultVariants: { size: "md" },
});
src/types/breadcrumbs-context.tsShow code
import { createContext, type ReactNode, useContext } from "react";
import type { VariantProps } from "tailwind-variants";
import type { breadcrumbsStyles } from "./breadcrumbs-styles";

/** What Breadcrumbs tells each item about its place in the trail. */
export interface BreadcrumbsContextValue {
  size: VariantProps<typeof breadcrumbsStyles>["size"];
  /** The last item is the current page: it is not a link. */
  isLast: boolean;
  /** Drawn after every item but the last. */
  separator: ReactNode;
}

const BreadcrumbsContext = createContext<BreadcrumbsContextValue | null>(null);

export const BreadcrumbsProvider = BreadcrumbsContext.Provider;

/** The enclosing Breadcrumbs' state, or an error outside of one. */
export function useBreadcrumbs() {
  const context = useContext(BreadcrumbsContext);
  if (!context) {
    throw new Error("BreadcrumbItem must be used inside <Breadcrumbs>.");
  }
  return context;
}

2. Add the Breadcrumbs file

src/components/ui/breadcrumbs/breadcrumbs.tsxShow code
"use client";

import Link from "next/link";
import {
  Children,
  type ComponentProps,
  isValidElement,
  type ReactElement,
  type ReactNode,
} from "react";
import { ChevronRightIcon, EllipsisIcon } from "@/components/icons";
import { Button } from "@/components/ui/button/button";
import { Popover, PopoverContent } from "@/components/ui/popover/popover";
import { cn } from "@/lib/cn";
import {
  BreadcrumbsProvider,
  useBreadcrumbs,
} from "@/types/breadcrumbs-context";
import { breadcrumbsStyles } from "@/types/breadcrumbs-styles";

type Size = "sm" | "md" | "lg";

export interface BreadcrumbsProps {
  /** The `BreadcrumbItem`s, from the top of the site down to the current page. */
  children: ReactNode;
  /** What goes between the items: a chevron, a slash, or anything you pass. */
  separator?: "chevron" | "slash" | ReactNode;
  /**
   * Show at most this many items. The first one stays, then a "…" button
   * that opens the ones left out, then the last ones. Needs at least 2.
   */
  maxItems?: number;
  size?: Size;
  /** Names the navigation for screen readers. Defaults to "Breadcrumbs". */
  "aria-label"?: string;
  className?: string;
}

export interface BreadcrumbItemProps {
  /** Where the item goes. The last item is the current page and is never a link. */
  href?: ComponentProps<typeof Link>["href"];
  /** An icon before the text. */
  icon?: ReactNode;
  children: ReactNode;
  className?: string;
}

const ellipsisSize = { sm: "xxs", md: "xs", lg: "sm" } as const;

function renderSeparator(separator: BreadcrumbsProps["separator"]) {
  if (separator === "slash") return "/";
  if (separator === undefined || separator === "chevron") {
    return <ChevronRightIcon />;
  }
  return separator;
}

/**
 * The path to the current page, as a row of links: Home / Docs / Components /
 * Breadcrumbs. Each item but the last is a link, and the last is the page you
 * are on. Set `maxItems` to fold a long trail into a "…" button that opens the
 * items left out.
 *
 * It renders a `nav` with an ordered list, and marks the last item with
 * `aria-current="page"`.
 *
 * @example
 * <Breadcrumbs>
 *   <BreadcrumbItem href="/">Home</BreadcrumbItem>
 *   <BreadcrumbItem href="/docs">Docs</BreadcrumbItem>
 *   <BreadcrumbItem>Breadcrumbs</BreadcrumbItem>
 * </Breadcrumbs>
 */
export function Breadcrumbs({
  children,
  separator,
  maxItems,
  size = "md",
  className,
  ...props
}: BreadcrumbsProps) {
  const styles = breadcrumbsStyles({ size });
  const sep = renderSeparator(separator);
  const items = Children.toArray(children).filter(
    isValidElement,
  ) as ReactElement<BreadcrumbItemProps>[];

  // Keep the first item and the last ones, and fold the middle.
  const keep = maxItems === undefined ? items.length : Math.max(maxItems, 2);
  const folded = items.length > keep;
  const hidden = folded ? items.slice(1, items.length - (keep - 1)) : [];
  const visible = folded
    ? [items[0], ...items.slice(items.length - (keep - 1))]
    : items;

  const entries = visible.map((item, i) => ({
    item,
    // The last item of the whole trail, not of what is visible.
    isLast: item === items[items.length - 1],
    // The fold sits right after the first item.
    fold: folded && i === 0,
  }));

  return (
    <nav
      aria-label={props["aria-label"] ?? "Breadcrumbs"}
      className={className}
    >
      <ol className={styles.list()}>
        {entries.map(({ item, isLast, fold }) => (
          <BreadcrumbsProvider
            key={item.key}
            value={{ size, isLast, separator: sep }}
          >
            {item}
            {fold ? (
              <BreadcrumbsProvider
                value={{ size, isLast: false, separator: sep }}
              >
                <BreadcrumbFold hidden={hidden} />
              </BreadcrumbsProvider>
            ) : null}
          </BreadcrumbsProvider>
        ))}
      </ol>
    </nav>
  );
}

// The "…" button and the popover with the items that were folded away.
function BreadcrumbFold({
  hidden,
}: {
  hidden: ReactElement<BreadcrumbItemProps>[];
}) {
  const { size, separator } = useBreadcrumbs();
  const styles = breadcrumbsStyles({ size });

  return (
    <li className={styles.item()}>
      <Popover>
        <Button
          isIcon
          variant="ghost"
          size={ellipsisSize[size ?? "md"]}
          aria-label={`Show ${hidden.length} more`}
          className={styles.ellipsis()}
        >
          <EllipsisIcon />
        </Button>
        <PopoverContent
          size="auto"
          placement="bottom start"
          aria-label="Hidden pages"
          className="p-1"
        >
          {({ close }) => (
            <ul className="flex min-w-40 flex-col">
              {hidden.map((item) => (
                <li key={item.key}>
                  {item.props.href !== undefined ? (
                    <Link
                      href={item.props.href}
                      onClick={close}
                      className="flex items-center gap-2 rounded-md px-2.5 py-1.5 text-sm outline-none hover:bg-muted focus-visible:bg-muted [&_svg]:size-4 [&_svg]:text-muted-fg"
                    >
                      {item.props.icon}
                      {item.props.children}
                    </Link>
                  ) : (
                    <span className="flex items-center gap-2 px-2.5 py-1.5 text-sm text-muted-fg [&_svg]:size-4">
                      {item.props.icon}
                      {item.props.children}
                    </span>
                  )}
                </li>
              ))}
            </ul>
          )}
        </PopoverContent>
      </Popover>
      <span aria-hidden="true" className={styles.separator()}>
        {separator}
      </span>
    </li>
  );
}

/**
 * One step of the trail. Inside `Breadcrumbs`, every item but the last is a
 * link to `href`, and the last is the current page.
 */
export function BreadcrumbItem({
  href,
  icon,
  children,
  className,
}: BreadcrumbItemProps) {
  const { size, isLast, separator } = useBreadcrumbs();
  const styles = breadcrumbsStyles({ size });
  const content = (
    <>
      {icon}
      <span className="truncate">{children}</span>
    </>
  );

  return (
    <li className={styles.item()}>
      {isLast ? (
        <span aria-current="page" className={cn(styles.current(), className)}>
          {content}
        </span>
      ) : href !== undefined ? (
        <Link href={href} className={cn(styles.link(), className)}>
          {content}
        </Link>
      ) : (
        <span className={cn(styles.link(), "hover:text-muted-fg", className)}>
          {content}
        </span>
      )}
      {isLast ? null : (
        <span aria-hidden="true" className={styles.separator()}>
          {separator}
        </span>
      )}
    </li>
  );
}

It imports ChevronRightIcon and EllipsisIcon from @/components/icons. Add those icons, or swap them for your own.

3. Use it

import {
  BreadcrumbItem,
  Breadcrumbs,
} from "@/components/ui/breadcrumbs/breadcrumbs";

Anatomy

A navigation landmark with an ordered list of steps, and a separator after each.

<Breadcrumbs>
  <BreadcrumbItem href="/" icon={<HomeIcon />}>Home</BreadcrumbItem>
  <BreadcrumbItem href="/docs">Docs</BreadcrumbItem>
  <BreadcrumbItem>Breadcrumbs</BreadcrumbItem>
</Breadcrumbs>
PartRendered asNotes
Breadcrumbsnav with an olA navigation landmark named "Breadcrumbs", holding an ordered list of the steps.
BreadcrumbItemli with a linkOne step. A link to its href, with an optional icon before the text.
Current pageli with a spanThe last item. It is not a link, and is marked aria-current="page".
Separatorspan, hidden from screen readersDrawn after every item but the last. A chevron, a slash, or your own.
FoldButton and PopoverWith maxItems, a "…" button after the first item that opens the items left out.

Examples

Links here point at real pages of this site.

Usage

With icons

An icon before the text, often on the first item. With an icon only, keep the name in screen-reader-only text.

Separators

A chevron by default. Pass a name, any text, or any element.

Sizes

Collapsed

With maxItems the trail keeps the first item and the last ones, and the ones between fold into a button that opens them.

Long labels

A label longer than the item allows is cut with an ellipsis, and the row wraps when it still does not fit.

Component API

Breadcrumbs is made of the parts below.

Breadcrumbs

PropTypeDefault
childrenBreadcrumbItem[]- (required)
separator"chevron" | "slash" | ReactNode"chevron"
maxItemsnumber-
size"sm" | "md" | "lg""md"
aria-labelstring"Breadcrumbs"
classNamestring-

BreadcrumbItem

PropTypeDefault
hrefstring | UrlObject-
iconReactNode-
childrenReactNode- (required)
classNamestring-