Breadcrumb
Animated React breadcrumbs for shadcn/ui and Tailwind CSS: the drop-in Breadcrumb adds growing hover underlines and an accessible current page — plus a Motion block that folds middle items into an animated “…” menu whenever the trail doesn't fit, without wrapping or layout shift.
- Drop-in shadcn API
- Responsive collapse
- Radix Dropdown Menu
- aria-current
- Server Component friendly
"use client";
import * as React from "react";
import { House } from "lucide-react";
import { CollapsibleBreadcrumb } from "@/components/dusk/breadcrumb/collapsible-breadcrumb";
const trail = [
{ label: "Home", href: "#home", icon: <House aria-hidden /> },
{ label: "Acme Inc.", href: "#acme" },
{ label: "Projects", href: "#projects" },
{ label: "Night Market", href: "#night-market" },
{ label: "Design system", href: "#design-system" },
{ label: "Components", href: "#components" },
{ label: "Breadcrumb.tsx" },
];
export default function CollapsibleBreadcrumbDemo() {
const [width, setWidth] = React.useState(100);
const id = React.useId();
return (
<div className="flex w-full max-w-2xl flex-col gap-6">
<div
className="rounded-xl border border-white/10 bg-card/60 px-4 py-3"
style={{ width: `${width}%` }}
>
<CollapsibleBreadcrumb items={trail} />
</div>
<div className="flex items-center gap-3 text-sm text-muted-foreground">
<label htmlFor={id} className="shrink-0">
Container width
</label>
<input
id={id}
type="range"
min={30}
max={100}
value={width}
onChange={(e) => setWidth(Number(e.target.value))}
className="w-full accent-(--color-primary)"
/>
<span className="w-10 text-right tabular-nums">{width}%</span>
</div>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/breadcrumb.json1. Install the dependencies.
npm install @radix-ui/react-slot lucide-react2. Copy the source into
components/ui/breadcrumb.tsx.breadcrumb.tsx import * as React from "react"; import { Slot } from "@radix-ui/react-slot"; import { ChevronRight, MoreHorizontal } from "lucide-react"; import { cn } from "@/lib/utils"; /** * DuskUI Breadcrumb — a drop-in upgrade of the shadcn/ui Breadcrumb. * * Same parts, props and data-slot attributes as shadcn (Breadcrumb, * BreadcrumbList, BreadcrumbItem, BreadcrumbLink, BreadcrumbPage, * BreadcrumbSeparator, BreadcrumbEllipsis) plus: * - Links brighten on hover and get a focus-visible ring. * - The current page is bright and carries aria-current="page" without the * misleading role="link". * - The ellipsis is announced as "More pages" instead of being hidden. * CSS only — no client JavaScript, so it works in Server Components. */ function Breadcrumb({ ...props }: React.ComponentProps<"nav">) { return <nav aria-label="breadcrumb" data-slot="breadcrumb" {...props} />; } function BreadcrumbList({ className, ...props }: React.ComponentProps<"ol">) { return ( <ol data-slot="breadcrumb-list" className={cn( "flex flex-wrap items-center gap-1.5 text-sm break-words text-muted-foreground sm:gap-2.5", className )} {...props} /> ); } function BreadcrumbItem({ className, ...props }: React.ComponentProps<"li">) { return ( <li data-slot="breadcrumb-item" className={cn("inline-flex items-center gap-1.5", className)} {...props} /> ); } function BreadcrumbLink({ asChild, className, ...props }: React.ComponentProps<"a"> & { /** Render the child element (e.g. Next.js `Link`) instead of an `<a>`. */ asChild?: boolean; }) { const Comp = asChild ? Slot : "a"; return ( <Comp data-slot="breadcrumb-link" className={cn( "inline-flex items-center gap-1.5 rounded-sm outline-none transition-colors duration-150 ease-out hover:text-foreground motion-reduce:transition-none", "focus-visible:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50", "[&_svg]:size-3.5 [&_svg]:shrink-0", className )} {...props} /> ); } function BreadcrumbPage({ className, ...props }: React.ComponentProps<"span">) { return ( <span data-slot="breadcrumb-page" aria-current="page" className={cn("inline-flex items-center gap-1.5 font-medium text-foreground [&_svg]:size-3.5", className)} {...props} /> ); } function BreadcrumbSeparator({ children, className, ...props }: React.ComponentProps<"li">) { return ( <li data-slot="breadcrumb-separator" role="presentation" aria-hidden="true" className={cn("text-muted-foreground/60 [&>svg]:size-3.5", className)} {...props} > {children ?? <ChevronRight />} </li> ); } function BreadcrumbEllipsis({ className, ...props }: React.ComponentProps<"span">) { return ( <span data-slot="breadcrumb-ellipsis" className={cn("flex size-9 items-center justify-center", className)} {...props} > <MoreHorizontal className="size-4" aria-hidden /> <span className="sr-only">More pages</span> </span> ); } export { Breadcrumb, BreadcrumbList, BreadcrumbItem, BreadcrumbLink, BreadcrumbPage, BreadcrumbSeparator, BreadcrumbEllipsis, };
Replaces components/ui/breadcrumb.tsx with the same parts and data-slot attributes. The base Breadcrumb is CSS only, so it renders in Server Components.
Usage
import Link from "next/link"
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
} from "@/components/ui/breadcrumb"
export function PageBreadcrumb() {
return (
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbItem>
<BreadcrumbLink asChild>
<Link href="/">Home</Link>
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator />
<BreadcrumbItem>
<BreadcrumbPage>Settings</BreadcrumbPage>
</BreadcrumbItem>
</BreadcrumbList>
</Breadcrumb>
)
}Examples
Default
Links brighten on hover; the current page is bright and marked with aria-current.
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
} from "@/components/ui/breadcrumb";
export default function BreadcrumbBasicDemo() {
return (
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbItem>
<BreadcrumbLink href="/">Home</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator />
<BreadcrumbItem>
<BreadcrumbLink href="/components">Components</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator />
<BreadcrumbItem>
<BreadcrumbPage>Breadcrumb</BreadcrumbPage>
</BreadcrumbItem>
</BreadcrumbList>
</Breadcrumb>
);
}Icons, separator & ellipsis
Pass any separator as children of BreadcrumbSeparator, put icons inside links (they size themselves) and use BreadcrumbEllipsis for a static collapsed trail.
import { FolderOpen, House } from "lucide-react";
import {
Breadcrumb,
BreadcrumbEllipsis,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
} from "@/components/ui/breadcrumb";
export default function BreadcrumbCustomDemo() {
return (
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbItem>
<BreadcrumbLink href="#home">
<House aria-hidden />
Home
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator>/</BreadcrumbSeparator>
<BreadcrumbItem>
<BreadcrumbEllipsis className="size-5" />
</BreadcrumbItem>
<BreadcrumbSeparator>/</BreadcrumbSeparator>
<BreadcrumbItem>
<BreadcrumbLink href="#projects">
<FolderOpen aria-hidden />
Projects
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator>/</BreadcrumbSeparator>
<BreadcrumbItem>
<BreadcrumbPage>Night Market</BreadcrumbPage>
</BreadcrumbItem>
</BreadcrumbList>
</Breadcrumb>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Collapsible Breadcrumb
Give it the trail as items. It never wraps: when the trail is wider than its container, middle items slide out one by one into a “…” button, which opens an animated menu of the hidden pages. Widths are measured off-screen before paint, so there's no flash, and it re-fits on resize — drag the slider to try it.
"use client";
import * as React from "react";
import { House } from "lucide-react";
import { CollapsibleBreadcrumb } from "@/components/dusk/breadcrumb/collapsible-breadcrumb";
const trail = [
{ label: "Home", href: "#home", icon: <House aria-hidden /> },
{ label: "Acme Inc.", href: "#acme" },
{ label: "Projects", href: "#projects" },
{ label: "Night Market", href: "#night-market" },
{ label: "Design system", href: "#design-system" },
{ label: "Components", href: "#components" },
{ label: "Breadcrumb.tsx" },
];
export default function CollapsibleBreadcrumbDemo() {
const [width, setWidth] = React.useState(100);
const id = React.useId();
return (
<div className="flex w-full max-w-2xl flex-col gap-6">
<div
className="rounded-xl border border-white/10 bg-card/60 px-4 py-3"
style={{ width: `${width}%` }}
>
<CollapsibleBreadcrumb items={trail} />
</div>
<div className="flex items-center gap-3 text-sm text-muted-foreground">
<label htmlFor={id} className="shrink-0">
Container width
</label>
<input
id={id}
type="range"
min={30}
max={100}
value={width}
onChange={(e) => setWidth(Number(e.target.value))}
className="w-full accent-(--color-primary)"
/>
<span className="w-10 text-right tabular-nums">{width}%</span>
</div>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/collapsible-breadcrumb.json1. Install the dependencies.
npm install motion @radix-ui/react-dropdown-menu lucide-react2. Copy the source into
components/dusk/breadcrumb/collapsible-breadcrumb.tsx.collapsible-breadcrumb.tsx "use client"; import * as React from "react"; import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu"; import { ChevronRight, MoreHorizontal } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion, type Transition, type Variants, } from "motion/react"; import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator, } from "@/components/ui/breadcrumb"; import { cn } from "@/lib/utils"; export type BreadcrumbEntry = { label: string; /** Omit for the current page (usually the last item). */ href?: string; icon?: React.ReactNode; }; const itemSpring: Transition = { type: "spring", bounce: 0, duration: 0.35 }; export interface CollapsibleBreadcrumbProps extends Omit<React.ComponentProps<"nav">, "children"> { /** The trail, root first. The last item is rendered as the current page. */ items: BreadcrumbEntry[]; /** Always collapse when there are more items than this. Width-based collapsing still applies. */ maxItems?: number; /** Items always shown before the "…" menu. */ itemsBeforeCollapse?: number; /** Items always shown after the "…" menu (including the current page). */ itemsAfterCollapse?: number; /** Component used for links, e.g. Next.js `Link`. Defaults to `<a>`. */ linkAs?: React.ElementType; /** Custom separator. Defaults to a chevron. */ separator?: React.ReactNode; } /** * A breadcrumb that never wraps. When the trail doesn't fit its container (or * is longer than `maxItems`), the middle items fold into a "…" button one by * one, sliding out as they go; the button opens an animated menu with the * hidden pages. Widths are measured off-screen before paint, so there is no * flash or layout shift, and it re-fits on resize. * * Built on the DuskUI Breadcrumb primitive and Radix Dropdown Menu, so the * menu has full keyboard support (arrows, typeahead, Escape). */ export function CollapsibleBreadcrumb({ items, maxItems, itemsBeforeCollapse = 1, itemsAfterCollapse = 1, linkAs, separator, className, ...props }: CollapsibleBreadcrumbProps) { const reduceMotion = useReducedMotion(); const navRef = React.useRef<HTMLElement>(null); const measureRef = React.useRef<HTMLOListElement>(null); const count = items.length; const before = Math.min(Math.max(0, itemsBeforeCollapse), count); const after = Math.min(Math.max(1, itemsAfterCollapse), count - before); const collapsible = Math.max(0, count - before - after); // With maxItems, the "…" takes one of the slots. const minCollapsed = maxItems !== undefined && count > maxItems ? Math.min(collapsible, count - maxItems + 1) : 0; const [collapsed, setCollapsed] = React.useState(minCollapsed); // The first fit happens before paint and must not animate. const [fitted, setFitted] = React.useState(false); const shown = Math.max(collapsed, minCollapsed); React.useLayoutEffect(() => { const nav = navRef.current; const measure = measureRef.current; if (!nav || !measure) return; const fit = () => { const available = nav.clientWidth; const gap = parseFloat(getComputedStyle(measure).columnGap) || 0; const nodes = Array.from(measure.children) as HTMLElement[]; const widths = nodes.slice(0, count).map((n) => n.offsetWidth); const sepWidth = nodes[count]?.offsetWidth ?? 0; const ellipsisWidth = nodes[count + 1]?.offsetWidth ?? 0; const required = (c: number) => { const visible = widths.filter((_, i) => i < before || i >= before + c); const parts = visible.length + (c > 0 ? 1 : 0); const content = visible.reduce((a, b) => a + b, 0) + (c > 0 ? ellipsisWidth : 0); // Each part after the first is preceded by a separator; every node adds a gap. return content + (parts - 1) * sepWidth + Math.max(0, parts * 2 - 2) * gap; }; let next = collapsible; for (let c = minCollapsed; c <= collapsible; c++) { if (required(c) <= available + 0.5) { next = c; break; } } setCollapsed((prev) => (prev === next ? prev : next)); setFitted(true); }; fit(); const observer = new ResizeObserver(fit); observer.observe(nav); observer.observe(measure); return () => observer.disconnect(); }, [count, before, collapsible, minCollapsed]); const hidden = items.slice(before, before + shown); const Link = linkAs ?? "a"; const enter = reduceMotion ? { opacity: 0 } : { opacity: 0, x: -6 }; const visibleState = { opacity: 1, x: 0 }; const renderItem = (item: BreadcrumbEntry, index: number) => index === count - 1 || !item.href ? ( <BreadcrumbPage className={cn(index === count - 1 && "min-w-0")}> {item.icon} <span className="truncate">{item.label}</span> </BreadcrumbPage> ) : ( <BreadcrumbLink asChild> <Link href={item.href}> {item.icon} {item.label} </Link> </BreadcrumbLink> ); type Part = { key: string; node: React.ReactNode; grow?: boolean }; const parts: Part[] = []; items.forEach((item, index) => { if (index === before && shown > 0) { parts.push({ key: "ellipsis", node: <BreadcrumbMenu items={hidden} linkAs={Link} reduceMotion={!!reduceMotion} />, }); } if (index >= before && index < before + shown) return; parts.push({ key: `item-${index}`, node: renderItem(item, index), grow: index === count - 1 }); }); return ( <Breadcrumb ref={navRef} className={cn("relative min-w-0", className)} {...props}> <BreadcrumbList className="relative flex-nowrap whitespace-nowrap"> {/* Remounting once after the first fit drops items without exit animations. */} <AnimatePresence key={fitted ? "fitted" : "initial"} initial={false} mode="popLayout"> {parts.flatMap((part, i) => { const nodes = []; if (i > 0) { nodes.push( <motion.li key={`sep-${part.key}`} data-slot="breadcrumb-separator" layout="position" role="presentation" aria-hidden="true" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={itemSpring} className="text-muted-foreground/60 [&>svg]:size-3.5" > {separator ?? <ChevronRight />} </motion.li> ); } nodes.push( <motion.li key={part.key} data-slot="breadcrumb-item" layout="position" initial={enter} animate={visibleState} exit={enter} transition={itemSpring} className={cn("inline-flex items-center gap-1.5", part.grow && "min-w-0")} > {part.node} </motion.li> ); return nodes; })} </AnimatePresence> </BreadcrumbList> {/* Off-screen copy used only to measure natural widths. */} <BreadcrumbList ref={measureRef} aria-hidden inert className="pointer-events-none invisible absolute top-0 left-0 flex-nowrap whitespace-nowrap" > {items.map((item, index) => ( <BreadcrumbItem key={index}> {index === count - 1 || !item.href ? ( <BreadcrumbPage> {item.icon} {item.label} </BreadcrumbPage> ) : ( <BreadcrumbLink asChild> <span> {item.icon} {item.label} </span> </BreadcrumbLink> )} </BreadcrumbItem> ))} <BreadcrumbSeparator>{separator}</BreadcrumbSeparator> <BreadcrumbItem> <span className="flex size-7 items-center justify-center" /> </BreadcrumbItem> </BreadcrumbList> </Breadcrumb> ); } const menuItemVariants: Variants = { closed: { opacity: 0, x: -4 }, open: { opacity: 1, x: 0 }, }; function BreadcrumbMenu({ items, linkAs: Link, reduceMotion, }: { items: BreadcrumbEntry[]; linkAs: React.ElementType; reduceMotion: boolean; }) { const [open, setOpen] = React.useState(false); const variants: Variants = reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1, transition: { duration: 0.15 } }, } : { closed: { opacity: 0, scale: 0.96, y: -4, transition: { duration: 0.12, ease: "easeIn" } }, open: { opacity: 1, scale: 1, y: 0, transition: { type: "spring", duration: 0.25, bounce: 0, staggerChildren: 0.02, delayChildren: 0.02 }, }, }; return ( <DropdownMenuPrimitive.Root open={open} onOpenChange={setOpen}> <DropdownMenuPrimitive.Trigger aria-label={`Show ${items.length} more ${items.length === 1 ? "page" : "pages"}`} className={cn( "flex size-7 items-center justify-center rounded-md text-muted-foreground outline-none transition-colors duration-200", "hover:bg-accent hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50", "data-[state=open]:bg-accent data-[state=open]:text-foreground" )} > <MoreHorizontal className="size-4" aria-hidden /> </DropdownMenuPrimitive.Trigger> <AnimatePresence> {open && ( // forceMount hands mounting to AnimatePresence so the exit animation can play. <DropdownMenuPrimitive.Portal forceMount> <DropdownMenuPrimitive.Content forceMount asChild align="start" sideOffset={6}> <motion.div variants={variants} initial="closed" animate="open" exit="closed" className={cn( "z-50 min-w-44 overflow-hidden rounded-xl border bg-popover p-1.5 text-popover-foreground outline-none", "origin-(--radix-dropdown-menu-content-transform-origin)", "shadow-lg" )} > {items.map((item) => ( <motion.div key={item.label + item.href} variants={reduceMotion ? undefined : menuItemVariants}> <DropdownMenuPrimitive.Item asChild> {item.href ? ( <Link href={item.href} className="flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5 text-sm text-muted-foreground outline-none transition-colors data-highlighted:bg-accent data-highlighted:text-foreground [&_svg]:size-3.5" > {item.icon} {item.label} </Link> ) : ( <span className="flex items-center gap-2 rounded-md px-2 py-1.5 text-sm text-muted-foreground outline-none data-highlighted:bg-accent [&_svg]:size-3.5"> {item.icon} {item.label} </span> )} </DropdownMenuPrimitive.Item> </motion.div> ))} </motion.div> </DropdownMenuPrimitive.Content> </DropdownMenuPrimitive.Portal> )} </AnimatePresence> </DropdownMenuPrimitive.Root> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| items | { label: string; href?: string; icon?: ReactNode }[] | — | The trail, root first. The last item is the current page. |
| maxItems | number | — | Always collapse when there are more items than this. |
| itemsBeforeCollapse | number | 1 | Items always shown before the “…”. |
| itemsAfterCollapse | number | 1 | Items always shown after the “…” (including the current page). |
| separator | ReactNode | <ChevronRight /> | Custom separator. |
| linkAs | ElementType | "a" | Link component, e.g. Next.js Link. |
Collapsible Breadcrumb — maxItems
maxItems collapses long trails even when they'd fit — here to three slots with a slanted slash separator.
import { Slash } from "lucide-react";
import { CollapsibleBreadcrumb } from "@/components/dusk/breadcrumb/collapsible-breadcrumb";
const trail = [
{ label: "Docs", href: "#docs" },
{ label: "Guides", href: "#guides" },
{ label: "Deployment", href: "#deployment" },
{ label: "Edge", href: "#edge" },
{ label: "Caching" },
];
export default function CollapsibleBreadcrumbMaxDemo() {
return (
<CollapsibleBreadcrumb
items={trail}
maxItems={3}
itemsAfterCollapse={1}
separator={<Slash className="-rotate-12" />}
/>
);
}$ npx shadcn@latest add https://duskui.com/r/collapsible-breadcrumb.json1. Install the dependencies.
npm install motion @radix-ui/react-dropdown-menu lucide-react2. Copy the source into
components/dusk/breadcrumb/collapsible-breadcrumb.tsx.collapsible-breadcrumb.tsx "use client"; import * as React from "react"; import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu"; import { ChevronRight, MoreHorizontal } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion, type Transition, type Variants, } from "motion/react"; import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator, } from "@/components/ui/breadcrumb"; import { cn } from "@/lib/utils"; export type BreadcrumbEntry = { label: string; /** Omit for the current page (usually the last item). */ href?: string; icon?: React.ReactNode; }; const itemSpring: Transition = { type: "spring", bounce: 0, duration: 0.35 }; export interface CollapsibleBreadcrumbProps extends Omit<React.ComponentProps<"nav">, "children"> { /** The trail, root first. The last item is rendered as the current page. */ items: BreadcrumbEntry[]; /** Always collapse when there are more items than this. Width-based collapsing still applies. */ maxItems?: number; /** Items always shown before the "…" menu. */ itemsBeforeCollapse?: number; /** Items always shown after the "…" menu (including the current page). */ itemsAfterCollapse?: number; /** Component used for links, e.g. Next.js `Link`. Defaults to `<a>`. */ linkAs?: React.ElementType; /** Custom separator. Defaults to a chevron. */ separator?: React.ReactNode; } /** * A breadcrumb that never wraps. When the trail doesn't fit its container (or * is longer than `maxItems`), the middle items fold into a "…" button one by * one, sliding out as they go; the button opens an animated menu with the * hidden pages. Widths are measured off-screen before paint, so there is no * flash or layout shift, and it re-fits on resize. * * Built on the DuskUI Breadcrumb primitive and Radix Dropdown Menu, so the * menu has full keyboard support (arrows, typeahead, Escape). */ export function CollapsibleBreadcrumb({ items, maxItems, itemsBeforeCollapse = 1, itemsAfterCollapse = 1, linkAs, separator, className, ...props }: CollapsibleBreadcrumbProps) { const reduceMotion = useReducedMotion(); const navRef = React.useRef<HTMLElement>(null); const measureRef = React.useRef<HTMLOListElement>(null); const count = items.length; const before = Math.min(Math.max(0, itemsBeforeCollapse), count); const after = Math.min(Math.max(1, itemsAfterCollapse), count - before); const collapsible = Math.max(0, count - before - after); // With maxItems, the "…" takes one of the slots. const minCollapsed = maxItems !== undefined && count > maxItems ? Math.min(collapsible, count - maxItems + 1) : 0; const [collapsed, setCollapsed] = React.useState(minCollapsed); // The first fit happens before paint and must not animate. const [fitted, setFitted] = React.useState(false); const shown = Math.max(collapsed, minCollapsed); React.useLayoutEffect(() => { const nav = navRef.current; const measure = measureRef.current; if (!nav || !measure) return; const fit = () => { const available = nav.clientWidth; const gap = parseFloat(getComputedStyle(measure).columnGap) || 0; const nodes = Array.from(measure.children) as HTMLElement[]; const widths = nodes.slice(0, count).map((n) => n.offsetWidth); const sepWidth = nodes[count]?.offsetWidth ?? 0; const ellipsisWidth = nodes[count + 1]?.offsetWidth ?? 0; const required = (c: number) => { const visible = widths.filter((_, i) => i < before || i >= before + c); const parts = visible.length + (c > 0 ? 1 : 0); const content = visible.reduce((a, b) => a + b, 0) + (c > 0 ? ellipsisWidth : 0); // Each part after the first is preceded by a separator; every node adds a gap. return content + (parts - 1) * sepWidth + Math.max(0, parts * 2 - 2) * gap; }; let next = collapsible; for (let c = minCollapsed; c <= collapsible; c++) { if (required(c) <= available + 0.5) { next = c; break; } } setCollapsed((prev) => (prev === next ? prev : next)); setFitted(true); }; fit(); const observer = new ResizeObserver(fit); observer.observe(nav); observer.observe(measure); return () => observer.disconnect(); }, [count, before, collapsible, minCollapsed]); const hidden = items.slice(before, before + shown); const Link = linkAs ?? "a"; const enter = reduceMotion ? { opacity: 0 } : { opacity: 0, x: -6 }; const visibleState = { opacity: 1, x: 0 }; const renderItem = (item: BreadcrumbEntry, index: number) => index === count - 1 || !item.href ? ( <BreadcrumbPage className={cn(index === count - 1 && "min-w-0")}> {item.icon} <span className="truncate">{item.label}</span> </BreadcrumbPage> ) : ( <BreadcrumbLink asChild> <Link href={item.href}> {item.icon} {item.label} </Link> </BreadcrumbLink> ); type Part = { key: string; node: React.ReactNode; grow?: boolean }; const parts: Part[] = []; items.forEach((item, index) => { if (index === before && shown > 0) { parts.push({ key: "ellipsis", node: <BreadcrumbMenu items={hidden} linkAs={Link} reduceMotion={!!reduceMotion} />, }); } if (index >= before && index < before + shown) return; parts.push({ key: `item-${index}`, node: renderItem(item, index), grow: index === count - 1 }); }); return ( <Breadcrumb ref={navRef} className={cn("relative min-w-0", className)} {...props}> <BreadcrumbList className="relative flex-nowrap whitespace-nowrap"> {/* Remounting once after the first fit drops items without exit animations. */} <AnimatePresence key={fitted ? "fitted" : "initial"} initial={false} mode="popLayout"> {parts.flatMap((part, i) => { const nodes = []; if (i > 0) { nodes.push( <motion.li key={`sep-${part.key}`} data-slot="breadcrumb-separator" layout="position" role="presentation" aria-hidden="true" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={itemSpring} className="text-muted-foreground/60 [&>svg]:size-3.5" > {separator ?? <ChevronRight />} </motion.li> ); } nodes.push( <motion.li key={part.key} data-slot="breadcrumb-item" layout="position" initial={enter} animate={visibleState} exit={enter} transition={itemSpring} className={cn("inline-flex items-center gap-1.5", part.grow && "min-w-0")} > {part.node} </motion.li> ); return nodes; })} </AnimatePresence> </BreadcrumbList> {/* Off-screen copy used only to measure natural widths. */} <BreadcrumbList ref={measureRef} aria-hidden inert className="pointer-events-none invisible absolute top-0 left-0 flex-nowrap whitespace-nowrap" > {items.map((item, index) => ( <BreadcrumbItem key={index}> {index === count - 1 || !item.href ? ( <BreadcrumbPage> {item.icon} {item.label} </BreadcrumbPage> ) : ( <BreadcrumbLink asChild> <span> {item.icon} {item.label} </span> </BreadcrumbLink> )} </BreadcrumbItem> ))} <BreadcrumbSeparator>{separator}</BreadcrumbSeparator> <BreadcrumbItem> <span className="flex size-7 items-center justify-center" /> </BreadcrumbItem> </BreadcrumbList> </Breadcrumb> ); } const menuItemVariants: Variants = { closed: { opacity: 0, x: -4 }, open: { opacity: 1, x: 0 }, }; function BreadcrumbMenu({ items, linkAs: Link, reduceMotion, }: { items: BreadcrumbEntry[]; linkAs: React.ElementType; reduceMotion: boolean; }) { const [open, setOpen] = React.useState(false); const variants: Variants = reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1, transition: { duration: 0.15 } }, } : { closed: { opacity: 0, scale: 0.96, y: -4, transition: { duration: 0.12, ease: "easeIn" } }, open: { opacity: 1, scale: 1, y: 0, transition: { type: "spring", duration: 0.25, bounce: 0, staggerChildren: 0.02, delayChildren: 0.02 }, }, }; return ( <DropdownMenuPrimitive.Root open={open} onOpenChange={setOpen}> <DropdownMenuPrimitive.Trigger aria-label={`Show ${items.length} more ${items.length === 1 ? "page" : "pages"}`} className={cn( "flex size-7 items-center justify-center rounded-md text-muted-foreground outline-none transition-colors duration-200", "hover:bg-accent hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50", "data-[state=open]:bg-accent data-[state=open]:text-foreground" )} > <MoreHorizontal className="size-4" aria-hidden /> </DropdownMenuPrimitive.Trigger> <AnimatePresence> {open && ( // forceMount hands mounting to AnimatePresence so the exit animation can play. <DropdownMenuPrimitive.Portal forceMount> <DropdownMenuPrimitive.Content forceMount asChild align="start" sideOffset={6}> <motion.div variants={variants} initial="closed" animate="open" exit="closed" className={cn( "z-50 min-w-44 overflow-hidden rounded-xl border bg-popover p-1.5 text-popover-foreground outline-none", "origin-(--radix-dropdown-menu-content-transform-origin)", "shadow-lg" )} > {items.map((item) => ( <motion.div key={item.label + item.href} variants={reduceMotion ? undefined : menuItemVariants}> <DropdownMenuPrimitive.Item asChild> {item.href ? ( <Link href={item.href} className="flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5 text-sm text-muted-foreground outline-none transition-colors data-highlighted:bg-accent data-highlighted:text-foreground [&_svg]:size-3.5" > {item.icon} {item.label} </Link> ) : ( <span className="flex items-center gap-2 rounded-md px-2 py-1.5 text-sm text-muted-foreground outline-none data-highlighted:bg-accent [&_svg]:size-3.5"> {item.icon} {item.label} </span> )} </DropdownMenuPrimitive.Item> </motion.div> ))} </motion.div> </DropdownMenuPrimitive.Content> </DropdownMenuPrimitive.Portal> )} </AnimatePresence> </DropdownMenuPrimitive.Root> ); }
API Reference
Same parts as shadcn/ui: Breadcrumb, BreadcrumbList, BreadcrumbItem, BreadcrumbLink, BreadcrumbPage, BreadcrumbSeparator and BreadcrumbEllipsis, each accepting its element's props.
| Prop | Type | Default | Description |
|---|---|---|---|
| asChild | boolean | false | On BreadcrumbLink: render your router's Link instead of an <a>. |
| children | ReactNode | <ChevronRight /> | On BreadcrumbSeparator: a custom separator. |
Accessibility
- Breadcrumb renders a nav landmark labelled “breadcrumb” containing an ordered list, as the WAI-ARIA breadcrumb pattern recommends.
- The current page carries aria-current="page" (without the misleading role="link" of the original).
- Separators are presentational and hidden from screen readers.
- The “…” trigger is named “Show 3 more pages” and opens a Radix menu with arrow-key navigation, typeahead and Escape; focus returns to the trigger on close.
- The off-screen copy used for measuring is aria-hidden and inert, so links are never announced or focused twice.
- Under prefers-reduced-motion items and the menu only fade.