### Install @ameshkin/nextcrumbs Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/gitlab.md Install the package using npm. ```bash npm install @ameshkin/nextcrumbs ``` -------------------------------- ### Install dependencies and run checks Source: https://github.com/ameshkin/nextcrumbs/blob/main/README.md Run these commands in order to install dependencies, typecheck, build, and run tests for the project. ```bash npm ci npm run typecheck npm run build npm test -- --run ``` -------------------------------- ### Install nextcrumbs and peer dependencies Source: https://github.com/ameshkin/nextcrumbs/blob/main/README.md Install the package and its peer dependencies. The optional react-router-dom dependency is only needed when using the useReactRouterBreadcrumbs hook. ```bash npm i @ameshkin/nextcrumbs # peer deps npm i @mui/material @mui/icons-material react react-dom # optional: only needed if using useReactRouterBreadcrumbs hook npm i react-router-dom ``` -------------------------------- ### Check installed dependencies with npm ls Source: https://github.com/ameshkin/nextcrumbs/blob/main/README.md Lists installed versions of MUI and React packages. The second command checks for react-router-dom, which is optional and only needed when using useReactRouterBreadcrumbs. ```bash npm ls @mui/material @mui/icons-material react react-dom # optional router (only needed if using useReactRouterBreadcrumbs) npm ls react-router-dom ``` -------------------------------- ### Testing with Vitest/RTL Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/react-router.md Testing tip: wrap the component with MemoryRouter when testing with Vitest/RTL. This example renders a trail and asserts that the "Home" crumb is present. ```tsx import { render, screen } from "@testing-library/react" import { MemoryRouter, Routes, Route } from "react-router-dom" import { Breadcrumbs, useReactRouterBreadcrumbs } from "@ameshkin/nextcrumbs" function Trail() { const items = useReactRouterBreadcrumbs({ rootLabel: "Home" }) return } it("renders trail", () => { render( } /> ) expect(screen.getByText("Home")).toBeInTheDocument() }) ``` -------------------------------- ### Install react-router-dom as dev dependency Source: https://github.com/ameshkin/nextcrumbs/blob/main/README.md Installs react-router-dom as a dev dependency to resolve webpack errors when not using the React Router hook. ```bash npm install --save-dev react-router-dom ``` -------------------------------- ### Customize labels, base, and excludes with usePathBreadcrumbs options Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/next-router.md Pass options to `usePathBreadcrumbs` to set `baseHref`, `labelMap` for segment overrides, `exclude` for skipping segments, and `decode` for URI decoding. The example maps `sku` to "SKU" and `new` to "Create", excludes `_private`, and decodes labels. ```tsx "use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { Breadcrumbs, usePathBreadcrumbs } from "@ameshkin/nextcrumbs"; export default function CustomTrail() { const pathname = usePathname(); const items = usePathBreadcrumbs(pathname, { baseHref: "/", labelMap: { sku: "SKU", new: "Create" }, exclude: ["_private"], decode: true }); return ; } ``` -------------------------------- ### Next.js quick start with NextCrumb Source: https://github.com/ameshkin/nextcrumbs/blob/main/README.md A client component using NextCrumb with next/link as the LinkComponent. The items array defines breadcrumb links; the last item is typically the current page. ```tsx "use client"; import Link from "next/link"; import { NextCrumb } from "@ameshkin/nextcrumbs"; // Also available as: import { Breadcrumbs } from "@ameshkin/nextcrumbs"; export default function HeaderTrail() { return ( ); } ``` -------------------------------- ### Test Breadcrumbs with mocked Link Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/next-router.md Example test using `@testing-library/react` to render `Breadcrumbs` with the mocked `Link` and assert that item labels appear. Requires the mock component from `test/NextLinkMock`. ```tsx // Breadcrumbs.test.tsx import { render, screen } from "@testing-library/react"; import Link from "../test/NextLinkMock"; import { Breadcrumbs } from "@ameshkin/nextcrumbs"; it("renders items", () => { render( ); expect(screen.getByText("Home")).toBeInTheDocument(); }); ``` -------------------------------- ### Combine JSON-LD and visual breadcrumbs Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/vanilla-json.md Usage example combining the JSON-LD component and the visual Breadcrumbs on a page. Passes the same crumb data to both, with origin for absolute URLs. ```tsx import { Breadcrumbs } from "@ameshkin/nextcrumbs"; import { BreadcrumbJsonLd } from "./BreadcrumbJsonLd"; export default function DocsPage() { const crumbs = [ { name: "Home", href: "/" }, { name: "Docs", href: "/docs" }, { name: "API" } ]; return ( <> ); } ``` -------------------------------- ### Configure webpack to ignore react-router-dom in next.config.js Source: https://github.com/ameshkin/nextcrumbs/blob/main/README.md Adds react-router-dom to webpack externals in Next.js to prevent resolution errors. This is an alternative to installing the package. ```js /** @type {import('next').NextConfig} */ const nextConfig = { webpack: (config) => { config.externals = config.externals || {} config.externals['react-router-dom'] = 'commonjs react-router-dom' return config } } module.exports = nextConfig ``` -------------------------------- ### Exclude private routes and prettify labels Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/react-router.md Excludes private routes (segments starting with underscore) using the exclude option and prettifies labels with mapSegmentLabel, replacing hyphens/underscores with spaces and capitalizing words. ```tsx import { Link as RouterLink } from "react-router-dom" import { Breadcrumbs, useReactRouterBreadcrumbs } from "@ameshkin/nextcrumbs" export default function Trail() { const items = useReactRouterBreadcrumbs({ rootLabel: "Home", exclude: [/^_/], mapSegmentLabel: (seg) => seg.replace(/[-_]+/g, " ").replace(/\b\w/g, c => c.toUpperCase()) }) return } ``` -------------------------------- ### Top-level imports from @ameshkin/nextcrumbs Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/gitlab.md Backward-compatible imports that continue to work and are the recommended default. ```ts import { Breadcrumbs, NextCrumb, // alias of Breadcrumbs usePathBreadcrumbs, useReactRouterBreadcrumbs, toJsonLd, BreadcrumbJsonLd, breadcrumbsToJsonLd, } from "@ameshkin/nextcrumbs"; ``` -------------------------------- ### Quick demo with MemoryRouter Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/react-router.md Quick demo of useReactRouterBreadcrumbs with MemoryRouter. The hook derives breadcrumb items from the current location and renders them with . The rootLabel option prepends a crumb linking to "/". ```tsx import { MemoryRouter, Routes, Route } from "react-router-dom" import { Breadcrumbs, useReactRouterBreadcrumbs } from "@ameshkin/nextcrumbs" function Trail() { const items = useReactRouterBreadcrumbs({ rootLabel: "Home" }) return } export default function Demo() { return ( } /> } /> ) } ``` -------------------------------- ### Mock next/link for Vitest and jsdom Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/next-router.md Create a mock component for `next/link` to render as a plain anchor in jsdom tests. The mock handles string or object hrefs and spreads remaining props. ```tsx // test/NextLinkMock.tsx import * as React from "react"; export default function NextLinkMock(props: any) { const { href, children, ...rest } = props; return {children}; } ``` -------------------------------- ### With SPA navigation (Router ) Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/react-router.md Pass React Router's as LinkComponent to enable client-side navigation for breadcrumb links. Without it, links render as plain anchors. ```tsx import { Link as RouterLink, BrowserRouter, Routes, Route } from "react-router-dom" import { Breadcrumbs, useReactRouterBreadcrumbs } from "@ameshkin/nextcrumbs" function Trail() { const items = useReactRouterBreadcrumbs({ rootLabel: "Home" }) return } export default function App() { return ( } /> } /> ) } ``` -------------------------------- ### Mount under a base path Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/react-router.md Mounts breadcrumbs under a base path by setting basePath to "/app", which strips the leading base path before building crumbs. ```tsx const items = useReactRouterBreadcrumbs({ rootLabel: "Home", basePath: "/app" }) ``` -------------------------------- ### Auto breadcrumbs from URL with usePathBreadcrumbs Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/next-router.md Use `usePathBreadcrumbs` with `usePathname` to automatically generate breadcrumb items from the current URL. Requires `"use client"` and passing `Link` from `next/link` as `LinkComponent`. ```tsx "use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { Breadcrumbs, usePathBreadcrumbs } from "@ameshkin/nextcrumbs"; export default function AutoTrail() { const pathname = usePathname(); const items = usePathBreadcrumbs(pathname); return ; } ``` -------------------------------- ### Pass MUI props via muiProps Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/vanilla-json.md Passes MUI Breadcrumbs props through muiProps to control density and collapse behavior. Here maxItems, itemsBeforeCollapse, and itemsAfterCollapse are set to limit displayed items. ```tsx ``` -------------------------------- ### Custom separator and icons with Breadcrumbs Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/next-router.md Use `separatorIcon` to replace the default separator and `icon` on individual items to add leading icons. Requires importing MUI icons. ```tsx "use client"; import Link from "next/link"; import NavigateNextIcon from "@mui/icons-material/NavigateNext"; import HomeOutlinedIcon from "@mui/icons-material/HomeOutlined"; import { Breadcrumbs } from "@ameshkin/nextcrumbs"; export default function StyledTrail() { return ( } items={[ { label: "Home", href: "/", icon: }, { label: "Products", href: "/products" }, { label: "Gadgets" } ]} /> ); } ``` -------------------------------- ### PathOptions type for usePathBreadcrumbs Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/next-router.md Type definition for the options object accepted by `usePathBreadcrumbs`. `baseHref` defaults to `"/"`, `labelMap` to `{}`, `exclude` to `[]`, and `decode` to `true`. ```ts type PathOptions = { baseHref?: string labelMap?: Record exclude?: string[] decode?: boolean } ``` -------------------------------- ### Tree-shakable subpath imports from @ameshkin/nextcrumbs Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/gitlab.md Explicit per-feature imports using subpath exports, pure ESM and safe for tree-shaking. ```ts // Component only import { Breadcrumbs } from "@ameshkin/nextcrumbs/breadcrumbs"; // Next.js App Router hook import { usePathBreadcrumbs } from "@ameshkin/nextcrumbs/hooks/usePathBreadcrumbs"; // React Router hook import useReactRouterBreadcrumbs, { ReactRouterBreadcrumbsOptions, } from "@ameshkin/nextcrumbs/hooks/useReactRouterBreadcrumbs"; // JSON-LD helpers import { toJsonLd, BreadcrumbJsonLd, breadcrumbsToJsonLd, } from "@ameshkin/nextcrumbs/utils/jsonld"; ``` -------------------------------- ### usePathBreadcrumbs(pathname, options?) Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/next-router.md A hook that derives breadcrumb items from the current pathname using next/navigation. It accepts an optional options object to customize base href, label overrides, exclusions, and decoding. ```APIDOC ## usePathBreadcrumbs(pathname, options?) ### Description Derives breadcrumb items from the given pathname. Accepts an optional options object to customize the base href, label overrides, excluded segments, and decoding behavior. ### Parameters - **pathname** (string) - Required - The current pathname from `usePathname()`. - **options** (PathOptions) - Optional - Configuration object with the following properties: - **baseHref** (string) - Optional - Default: `"/"` - Root href for the first crumb. - **labelMap** (Record) - Optional - Default: `{}` - Override labels by URL segment. - **exclude** (string[]) - Optional - Default: `[]` - Skip specific segments. - **decode** (boolean) - Optional - Default: `true` - Apply `decodeURIComponent` to labels. ### Returns An array of breadcrumb items, each with `label` and `href` properties. ### Example ```tsx const items = usePathBreadcrumbs(pathname, { baseHref: "/", labelMap: { sku: "SKU", new: "Create" }, exclude: ["_private"], decode: true }); ``` ``` -------------------------------- ### Breadcrumbs with custom LinkComponent, separator icon, and item icons Source: https://github.com/ameshkin/nextcrumbs/blob/main/README.md Use this snippet to render breadcrumbs with a custom Next.js Link component, a separator icon (NavigateNext), and optional icons for each breadcrumb item. The last item (Gadgets) has no href, making it the current page. ```tsx import Link from "next/link"; import NavigateNextIcon from "@mui/icons-material/NavigateNext"; import HomeOutlinedIcon from "@mui/icons-material/HomeOutlined"; import { Breadcrumbs } from "@ameshkin/nextcrumbs"; } items={[ { label: "Home", href: "/", icon: }, { label: "Products", href: "/products" }, { label: "Gadgets" } ]} />; ``` -------------------------------- ### Page-level JSON-LD BreadcrumbList component Source: https://github.com/ameshkin/nextcrumbs/blob/main/docs/vanilla-json.md Creates a BreadcrumbJsonLd component that outputs a JSON-LD script tag for static SEO. It builds a BreadcrumbList schema with positions and optional item URLs resolved against the origin; independent of the visual breadcrumbs component. ```tsx import * as React from "react"; type Crumb = { name: string; href?: string }; function toJsonLd(crumbs: Crumb[], origin = "https://example.com") { return { "@context": "https://schema.org", "@type": "BreadcrumbList", itemListElement: crumbs.map((c, i) => ({ "@type": "ListItem", position: i + 1, name: c.name, ...(c.href ? { item: new URL(c.href, origin).toString() } : {}) })) }; } export function BreadcrumbJsonLd({ crumbs, origin }: { crumbs: Crumb[]; origin?: string }) { const json = React.useMemo(() => toJsonLd(crumbs, origin), [crumbs, origin]); return