Breadcrumb
The path to the current page or folder, with links or buttons and a collapsible middle.
Demo
Usage
import { Badge, Breadcrumb } from '@tuttn/slant'
import { Link } from 'react-router-dom'
// Links: an entry with href. The last entry is the current page, as plain text.
<Breadcrumb items={[
{ id: 'docs', label: 'Docs', href: '/docs' },
{ id: 'navigation', label: 'Navigation', href: '/docs/navigation' },
{ id: 'breadcrumb', label: 'Breadcrumb' },
]} />
// Buttons: entries without href call onSelect(id). A long path keeps the first
// crumb and the last three, with a "…" button that reveals the rest.
<Breadcrumb label="Folder" items={path} onSelect={openFolder} maxItems={4} />
// Rich crumbs: the compound form; asChild renders a router's Link as the crumb.
<Breadcrumb>
<Breadcrumb.Item asChild><Link to="/projects">Projects</Link></Breadcrumb.Item>
<Breadcrumb.Item href="/projects/tuttn">TUTTN</Breadcrumb.Item>
<Breadcrumb.Item current>Settings</Breadcrumb.Item>
</Breadcrumb>
// A status after the last crumb, announced as it changes. Keep it defined,
// '' while there's nothing to say, so the live region is there first.
<Breadcrumb items={path} status={saving ? 'Saving…' : saved ? 'Saved' : ''} />
<Breadcrumb status={<Badge tone="success">Published</Badge>}>…</Breadcrumb>Props
| Prop | Type | Default | Description |
|---|---|---|---|
items | readonly BreadcrumbEntry[] | — | The path, from the root to the current page, for uniform crumbs. The last entry is the current one: plain text with `aria-current="page"`. Leave it out and compose `Breadcrumb.Item`s as children instead. |
onSelect | (id: string) => void | — | Called with an entry's id when its button is pressed (an entry without `href`). |
label | string | messages.breadcrumb ("Breadcrumb") | The nav's accessible name. |
maxItems | number | — | With `items`, the most crumbs to show. A longer path keeps the first and the last `maxItems - 1`, with a "…" button between them that reveals the rest. At least 2. |
status | ReactNode | — | A segment after the last crumb that says how the page stands: "Saved", "Saving…", a Badge. It's a polite live region (`role="status"`), so a change is announced; it renders whenever the prop isn't undefined, so keep it defined (an empty string) while there's nothing to say, and the region exists before its first message. Muted, behind the same drawn slash as a crumb, and never truncated. |
children | ReactNode | — | `Breadcrumb.Item`s, for the compound form. |
Breadcrumb.Item href | string | — | Makes the crumb a link to this URL. |
Breadcrumb.Item current | boolean | — | Marks the current page (aria-current="page"). Without href the crumb is plain text. |
Breadcrumb.Item asChild | boolean | — | Renders the single child (a router's Link, a button) as the crumb, with the crumb's class and aria-current merged onto it. |
…rest | HTMLAttributes | — | className, style, other attributes and the ref go on the <nav>; on Breadcrumb.Item, on its <li>. |
An entry is { id, label, href?, textValue? }. With href it renders a link; without, a button that calls onSelect(id), which is how a file browser moves between folders. The last entry is the current one: plain text with aria-current="page". Breadcrumb is a <nav> landmark named label (“Breadcrumb” by default), so give a second one on the same page a different name.
There are no arrow keys: every crumb is a link or a button in the tab order. The “…” button, named “Show 2 more”, reveals every crumb and moves focus to the first one it revealed. A new path collapses again. maxItems applies to items only.
The separators are drawn by the stylesheet, so they are never focused or read, and they lean the other way under right-to-left text. Long labels truncate with an ellipsis (a string label, or textValue, becomes the tooltip); the current crumb keeps its full text and wraps instead. When a crumb’s button removes itself from the path, move focus somewhere useful, such as the folder’s contents.
status adds a segment after the last crumb for the state of the page, such as “Saving…”, “Saved” or a Badge, in both the items and the compound form. It is a span.sl-breadcrumb-status inside the nav, after the list rather than in it, since it isn't a step on the path. It has role="status", a polite live region, so a screen reader announces each change without moving focus. A live region only speaks about changes made after it exists, so the span renders whenever status isn't undefined: keep the prop defined, an empty string while there's nothing to say, and an empty status draws no slash. It sits behind the same drawn slash as a crumb, muted, and is never cut off with an ellipsis: in a narrow nav it wraps onto a line of its own. Keep it short, and don't put controls in it.