Loading...
Please waitLoading...
Please waitManage breadcrumb navigation rules with push/pop/replace operations, auto-generation from URL, and customizable labels.
pnpm dlx uselab@latest add use-breadcrumbs
import * as React from "react"
import { useBreadcrumbs } from "@/hooks/use-breadcrumbs"
export function BreadcrumbExample() {
const { items, push, pop, replace, navigate } = useBreadcrumbs({
autoGenerate: true,
})
return (
<nav>
{items.map((item, index) => (
<a
key={item.href}
href={item.href}
onClick={(e) => {
e.preventDefault()
navigate(item.href)
}}
>
{item.label}
</a>
))}
</nav>
)
}import * as React from "react"
import Link from "next/link"
import { usePathname, useRouter } from "next/navigation"
import { useBreadcrumbs } from "@/hooks/use-breadcrumbs"
export function NextBreadcrumbExample() {
const router = useRouter()
const pathname = usePathname()
const { items } = useBreadcrumbs({
autoGenerate: true,
navigate: (href) => router.push(href),
getPathname: () => pathname,
})
return (
<nav>
{items.map((item) => (
<Link key={item.href} href={item.href}>
{item.label}
</Link>
))}
</nav>
)
}import * as React from "react"
import { useLocation, useNavigate } from "react-router-dom"
import { useBreadcrumbs } from "@/hooks/use-breadcrumbs"
export function ReactRouterBreadcrumbExample() {
const navigate = useNavigate()
const location = useLocation()
const { items } = useBreadcrumbs({
autoGenerate: true,
navigate: (href) => navigate(href),
getPathname: () => location.pathname,
})
return (
<nav>
{items.map((item) => (
<a
key={item.href}
href={item.href}
onClick={(e) => {
e.preventDefault()
navigate(item.href)
}}
>
{item.label}
</a>
))}
</nav>
)
}import * as React from "react"
import { useBreadcrumbs } from "@/hooks/use-breadcrumbs"
export function CustomLabelExample() {
const { items } = useBreadcrumbs({
autoGenerate: true,
generateLabel: (segment, index, segments) => {
const labels: Record<string, string> = {
dashboard: "Dashboard",
settings: "Settings",
profile: "User Profile",
}
return labels[segment] || segment
},
})
return (
<nav>
{items.map((item) => (
<span key={item.href}>{item.label}</span>
))}
</nav>
)
}import * as React from "react"
import { useBreadcrumbs } from "@/hooks/use-breadcrumbs"
export function ManualBreadcrumbExample() {
const { items, push, pop, replace, setItems, reset } = useBreadcrumbs({
autoGenerate: false,
items: [
{ label: "Home", href: "/" },
{ label: "Products", href: "/products" },
],
})
const handleAddCategory = () => {
push({ label: "Electronics", href: "/products/electronics" })
}
const handleRemoveLast = () => {
pop()
}
const handleUpdateLast = () => {
replace({ label: "Updated", href: "/products/updated" })
}
return (
<div>
<nav>
{items.map((item) => (
<span key={item.href}>{item.label} / </span>
))}
</nav>
<button onClick={handleAddCategory}>Add Category</button>
<button onClick={handleRemoveLast}>Remove Last</button>
<button onClick={handleUpdateLast}>Update Last</button>
<button onClick={reset}>Reset</button>
</div>
)
}import * as React from "react"
import { useBreadcrumbs } from "@/hooks/use-breadcrumbs"
export function BasePathExample() {
const { items } = useBreadcrumbs({
autoGenerate: true,
basePath: "/app",
maxDepth: 5,
})
return (
<nav>
{items.map((item) => (
<a key={item.href} href={item.href}>
{item.label}
</a>
))}
</nav>
)
}useBreadcrumbs(options)Manage breadcrumb navigation with push/pop/replace operations, auto-generation from URL, and customizable labels.
interface UseBreadcrumbsOptions {
items?: BreadcrumbItem[]
generateLabel?: (segment: string, index: number, segments: string[]) => string
autoGenerate?: boolean
basePath?: string
maxDepth?: number
}| Name | Type | Description | Default |
|---|---|---|---|
items | BreadcrumbItem[] | Initial breadcrumb items. If not provided, will auto-generate from current pathname. | – |
generateLabel | (segment: string, index: number, segments: string[]) => string | Custom label generator function for auto-generated breadcrumbs. | defaultGenerateLabel |
autoGenerate | boolean | Whether to auto-generate breadcrumbs from the current URL pathname. | true |
basePath | string | Base path to exclude from auto-generated breadcrumbs (e.g., "/app"). | – |
maxDepth | number | Maximum depth for auto-generated breadcrumbs. | 10 |
navigate | (href: string) => void | Custom navigation function. If not provided, uses window.location.href. | – |
getPathname | () => string | Custom pathname getter. If not provided, uses window.location.pathname. | – |
interface UseBreadcrumbsReturn {
items: BreadcrumbItem[]
push: (item: BreadcrumbItem) => void
pop: () => void
replace: (item: BreadcrumbItem) => void
setItems: (items: BreadcrumbItem[]) => void
reset: () => void
navigate: (href: string) => void
}| Name | Type | Description |
|---|---|---|
items | BreadcrumbItem[] | Current breadcrumb items. |
push | (item: BreadcrumbItem) => void | Push a new breadcrumb item to the stack. |
pop | () => void | Pop the last breadcrumb item from the stack. |
replace | (item: BreadcrumbItem) => void | Replace the last breadcrumb item with a new one. |
setItems | (items: BreadcrumbItem[]) => void | Replace all breadcrumb items. |
reset | () => void | Reset breadcrumbs to initial state or auto-generate from current pathname. |
navigate | (href: string) => void | Navigate to a breadcrumb item's href using Next.js router. |
BreadcrumbIteminterface BreadcrumbItem {
label: string
href: string
}| Name | Type | Description |
|---|---|---|
label | string | Display label for the breadcrumb. |
href | string | URL path for navigation. |
autoGenerate is true.basePath, the base path is excluded from auto-generated breadcrumbs but included in hrefs.window.location.pathname and window.location.href.navigate: (href) => router.push(href) and getPathname: () => usePathname().navigate: (href) => navigate(href) and getPathname: () => location.pathname.pop() will not remove the last item if there's only one breadcrumb remaining (typically "Home").reset() restores breadcrumbs to their initial state or regenerates them from the current pathname.Total items: 1
[
{
"label": "Home",
"href": "/"
}
]