# Breadcrumbs

## Introduction

The Breadcrumbs component displays a navigation trail that helps users understand their current location within a hierarchical structure and navigate back to previous levels. Each level in the path is represented as a clickable link except for the current page, which is displayed as plain text.

Breadcrumbs are essential for sites with deep navigation hierarchies -- they provide context at a glance, reduce the number of steps needed to return to higher-level pages, and improve overall wayfinding within an application.

```tsx
<Breadcrumbs
  crumbs={[{
  label: 'Home',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Catalog',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Category',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Subcategory',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Product',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Product Detail',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Owner',
  type: 'text'
}]}
  collapsed
/>
```

| Field           | Description | Default |
| --------------- | ----------- | ------- |
| startCrumbCount | —           | —       |
| endCrumbCount   | —           | —       |
| collapsed       | —           | true    |
| separator       | —           | —       |
| size            | —           | —       |

## Usage

```tsx
import { Breadcrumbs } from '@ceed/cds';

function MyComponent() {
  const crumbs = [
    { label: 'Home', type: 'link', linkHref: '/' },
    { label: 'Products', type: 'link', linkHref: '/products' },
    { label: 'Electronics', type: 'link', linkHref: '/products/electronics' },
    { label: 'Laptops', type: 'text' },
  ];

  return <Breadcrumbs crumbs={crumbs} />;
}
```

## Features

### Basic Breadcrumbs

A simple breadcrumb trail with a few levels of hierarchy. The last item uses `type: 'text'` to indicate the current page.

```
<Canvas of={Breadcrumbs.BasicExample} />
```

### Sizes

Breadcrumbs are available in three sizes -- `sm`, `md`, and `lg` -- allowing you to match the surrounding layout density.

```tsx
<div>
    <Breadcrumbs {...args} size="sm" />
    <Breadcrumbs {...args} size="md" />
    <Breadcrumbs {...args} size="lg" />
  </div>
```

### Custom Separator

The separator character between items can be customized via the `separator` prop. Common choices include `/`, `>`, and `-`.

```
<Canvas of={Breadcrumbs.CustomSeparator} />
```

### Collapsed Breadcrumbs

When paths are deep, the component automatically collapses middle items behind an ellipsis. You can control how many items appear at the start and end using `startCrumbCount` and `endCrumbCount`.

```
<Canvas of={Breadcrumbs.CollapsedVariants} />
```

### Expanded View

Setting `collapsed={false}` renders every item in the path without collapsing.

```tsx
<Breadcrumbs
  crumbs={[{
  label: 'Home',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Catalog',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Category',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Subcategory',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Product',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Product Detail',
  type: 'link',
  linkHref: '/'
}, {
  label: 'Owner',
  type: 'text'
}]}
  collapsed={false}
/>
```

### Single Crumb

The component handles the edge case of a single breadcrumb item gracefully.

```tsx
<Breadcrumbs
  crumbs={[{
  label: 'Home',
  type: 'text'
}]}
  collapsed
/>
```

### With Icons

Breadcrumb items can include icons alongside their labels.

```
<Canvas of={Breadcrumbs.WithIcons} />
```

## Common Use Cases

### Admin Dashboard Navigation

```tsx
function UserDetailPage({ user }) {
  const crumbs = [
    { label: 'Dashboard', type: 'link', linkHref: '/admin' },
    { label: 'Users', type: 'link', linkHref: '/admin/users' },
    { label: user.name, type: 'text' },
  ];

  return (
    <Box>
      <Breadcrumbs crumbs={crumbs} />
      <UserProfile user={user} />
    </Box>
  );
}
```

### E-commerce Product Page

```tsx
function ProductPage({ product, category, subcategory }) {
  const crumbs = [
    { label: 'Home', type: 'link', linkHref: '/' },
    { label: 'Shop', type: 'link', linkHref: '/shop' },
    { label: category.name, type: 'link', linkHref: `/shop/${category.slug}` },
    {
      label: subcategory.name,
      type: 'link',
      linkHref: `/shop/${category.slug}/${subcategory.slug}`,
    },
    { label: product.name, type: 'text' },
  ];

  return (
    <Box>
      <Breadcrumbs crumbs={crumbs} collapsed={true} startCrumbCount={1} endCrumbCount={2} />
      <ProductDetails product={product} />
    </Box>
  );
}
```

### Dynamic Breadcrumbs from Route

```tsx
function DynamicBreadcrumbs() {
  const location = useLocation();
  const pathnames = location.pathname.split('/').filter(Boolean);

  const crumbs = [
    { label: 'Home', type: 'link', linkHref: '/' },
    ...pathnames.map((value, index) => ({
      label: formatLabel(value),
      type: index === pathnames.length - 1 ? 'text' : 'link',
      linkHref: `/${pathnames.slice(0, index + 1).join('/')}`,
    })),
  ];

  return <Breadcrumbs crumbs={crumbs} />;
}
```

## Props and Customization

### Key Props

| Prop              | Type                                                             | Default                          | Description                                          |
| ----------------- | ---------------------------------------------------------------- | -------------------------------- | ---------------------------------------------------- |
| `crumbs`          | `{ type: 'text' \| 'link'; label: string; linkHref?: string }[]` | (required)                       | Breadcrumb items                                     |
| `collapsed`       | `boolean`                                                        | `true`                           | Whether long crumb lists are collapsed with ellipsis |
| `startCrumbCount` | `number`                                                         | `1`                              | Number of crumbs shown at the start when collapsed   |
| `endCrumbCount`   | `number`                                                         | `3`                              | Number of crumbs shown at the end when collapsed     |
| `slots`           | `{ link?: ElementType }`                                         | -                                | Custom component for link crumbs                     |
| `slotProps`       | `{ link?: { color?: string } }`                                  | `{ link: { color: 'neutral' } }` | Props passed to slot components                      |

## Best Practices

- **Always start with a root item.** Begin the trail with "Home" or an equivalent top-level link so users can always navigate back to the starting point.

```tsx
// Good
const crumbs = [
  { label: 'Home', type: 'link', linkHref: '/' },
  { label: 'Settings', type: 'text' },
];

// Bad -- missing root item
const crumbs = [{ label: 'Settings', type: 'text' }];
```

- **Make the current page non-clickable.** The last item in the trail should use `type: 'text'` since clicking the current page serves no purpose.

```tsx
// Good
{ label: 'Current Page', type: 'text' }

// Bad
{ label: 'Current Page', type: 'link', linkHref: '/current' }
```

- **Use collapsing for deep hierarchies.** When paths exceed four or five levels, enable `collapsed` to keep the UI clean and avoid horizontal overflow.

- **Keep labels concise.** Breadcrumb labels should be short but descriptive. Match them to the corresponding page titles for consistency.

- **Do not use breadcrumbs as primary navigation.** They are a supplementary wayfinding aid, not a replacement for the main navigation menu.

## Accessibility

- **Semantic HTML**: The component renders a `<nav>` element with `aria-label="breadcrumb"` and an ordered list (`<ol>`) to convey the sequential nature of the trail.
- **Current page indicator**: The last item is automatically marked with `aria-current="page"` so screen readers announce the user's current location.
- **Keyboard navigation**: All link items are focusable with `Tab` and activated with `Enter`, following standard keyboard interaction patterns.
- **Screen readers**: The navigation landmark is announced as "breadcrumb navigation" and each link communicates its position within the path hierarchy.
