suiss/ui · Pattern
Trail
A navigation trail that shows breadcrumbs on desktop and a back link with history on mobile.
Trail is an open-source pattern from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.
Trail shows where the current page sits in a hierarchy. On desktop it renders a suiss/ui Breadcrumb that links every ancestor and marks the last item as the current page. On mobile it renders a single back link to the parent page, with a menu of all ancestors.
Use it on documentation, support and settings pages with nested structure.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
-
Add the pieces it builds on: Breadcrumb,
use-mobile. -
Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Examples
Collapsed
Set maxItems to cap the items shown on desktop. The trail keeps the root and the nearest ancestors and folds the rest into an ellipsis.
Back label
Set backLabel to label the mobile back link Back instead of the parent's name.
Two levels
Shows a trail with one parent and the current page.
Accessibility
- On desktop the trail is a navigation landmark named breadcrumb, and the current page has
aria-current="page". - On mobile it is a navigation landmark named Back. When a history menu is available, the back link has
aria-haspopup="menu"andaria-expanded. - The history menu opens from the keyboard with the Context Menu key or Shift+F10.
API Reference
Trail
The navigation element that renders the breadcrumbs or the mobile back link.
| Prop | Type | Default |
|---|---|---|
items | TrailItem[] | Required |
maxItems | number | - |
backLabel | string | - |
ellipsisLabel | string | "More" |
| Prop | Type | Default |
|---|---|---|
render | React.ReactElement | A plain a |
| Prop | Type | Default |
|---|---|---|
render | (props: React.ComponentProps<"a">) => React.ReactNode | A plain a |
Also accepts the props of React.ComponentProps<"nav">.
Pass items in order from the root to the current page. Each TrailItem has a label and an optional href; the last item is the current page. ellipsisLabel is the accessible label of the collapsed ellipsis.
On mobile (below 48rem, via useIsMobile) the trail renders a back link to the parent page, and nothing when there is only one item. When there is more than one ancestor, a long press, right-click, the Context Menu key or Shift+F10 opens a menu of all ancestors, nearest first. A long press opens it only for touch and pen input, cancels if the pointer moves more than 10 pixels, and does not also follow the back link.
Use render to output links through your router, for example a framework Link element. It applies to breadcrumb links, the mobile back link and the history menu items.
Pass a render function to output links through your router. It receives the link props, including href, and applies to breadcrumb links, the mobile back link and the history menu items.
Each part sets a data-slot, such as trail, trail-back or trail-history-item, for styling.