Breadcrumbs
Breadcrumbs display a heirarchy of links to the current page or resource in an application.
install | yarn add react-aria-components |
---|---|
version | 1.0.0-alpha.0 |
usage | import {Breadcrumbs} from 'react-aria-components' |
Example#
import {Breadcrumbs, Item, Link} from 'react-aria-components';
<Breadcrumbs>
<Item><Link><a href="/">Home</a></Link></Item>
<Item><Link><a href="/react-aria">React Aria</a></Link></Item>
<Item><Link>useBreadcrumbs</Link></Item>
</Breadcrumbs>
import {
Breadcrumbs,
Item,
Link
} from 'react-aria-components';
<Breadcrumbs>
<Item>
<Link>
<a href="/">Home</a>
</Link>
</Item>
<Item>
<Link>
<a href="/react-aria">React Aria</a>
</Link>
</Item>
<Item>
<Link>useBreadcrumbs</Link>
</Item>
</Breadcrumbs>
import {
Breadcrumbs,
Item,
Link
} from 'react-aria-components';
<Breadcrumbs>
<Item>
<Link>
<a href="/">
Home
</a>
</Link>
</Item>
<Item>
<Link>
<a href="/react-aria">
React Aria
</a>
</Link>
</Item>
<Item>
<Link>
useBreadcrumbs
</Link>
</Item>
</Breadcrumbs>
Show CSS
.react-aria-Breadcrumbs {
& ol {
display: flex;
align-items: center;
list-style: none;
margin: 0;
padding: 0;
font-size: 18px;
}
.react-aria-Heading {
margin: 0;
font-size: 1em;
}
.react-aria-Item:not(:last-child)::after {
content: '›';
content: '›' / '';
alt: ' ';
padding: 0 5px;
}
}
.react-aria-Link {
--focus-ring-color: slateblue;
--text-color: var(--spectrum-global-color-gray-800);
--text-color-current: var(--spectrum-global-color-gray-900);
--text-color-disabled: gray;
color: var(--text-color);
outline: none;
position: relative;
text-decoration: none;
cursor: pointer;
&[data-hovered] {
text-decoration: underline;
}
&[aria-current] {
color: var(--text-color-current);
font-weight: bold;
}
&[data-focus-visible]:after {
content: '';
position: absolute;
inset: -2px -4px;
border-radius: 6px;
border: 2px solid var(--focus-ring-color);
}
&[aria-disabled] {
cursor: default;
&:not([aria-current]) {
color: var(--text-color-disabled);
}
}
}
@media (forced-colors: active) {
.react-aria-Link {
--focus-ring-color: Highlight;
--text-color: LinkText;
--text-color-current: CanvasText;
--text-color-disabled: GrayText;
}
}
.react-aria-Breadcrumbs {
& ol {
display: flex;
align-items: center;
list-style: none;
margin: 0;
padding: 0;
font-size: 18px;
}
.react-aria-Heading {
margin: 0;
font-size: 1em;
}
.react-aria-Item:not(:last-child)::after {
content: '›';
content: '›' / '';
alt: ' ';
padding: 0 5px;
}
}
.react-aria-Link {
--focus-ring-color: slateblue;
--text-color: var(--spectrum-global-color-gray-800);
--text-color-current: var(--spectrum-global-color-gray-900);
--text-color-disabled: gray;
color: var(--text-color);
outline: none;
position: relative;
text-decoration: none;
cursor: pointer;
&[data-hovered] {
text-decoration: underline;
}
&[aria-current] {
color: var(--text-color-current);
font-weight: bold;
}
&[data-focus-visible]:after {
content: '';
position: absolute;
inset: -2px -4px;
border-radius: 6px;
border: 2px solid var(--focus-ring-color);
}
&[aria-disabled] {
cursor: default;
&:not([aria-current]) {
color: var(--text-color-disabled);
}
}
}
@media (forced-colors: active) {
.react-aria-Link {
--focus-ring-color: Highlight;
--text-color: LinkText;
--text-color-current: CanvasText;
--text-color-disabled: GrayText;
}
}
.react-aria-Breadcrumbs {
& ol {
display: flex;
align-items: center;
list-style: none;
margin: 0;
padding: 0;
font-size: 18px;
}
.react-aria-Heading {
margin: 0;
font-size: 1em;
}
.react-aria-Item:not(:last-child)::after {
content: '›';
content: '›' / '';
alt: ' ';
padding: 0 5px;
}
}
.react-aria-Link {
--focus-ring-color: slateblue;
--text-color: var(--spectrum-global-color-gray-800);
--text-color-current: var(--spectrum-global-color-gray-900);
--text-color-disabled: gray;
color: var(--text-color);
outline: none;
position: relative;
text-decoration: none;
cursor: pointer;
&[data-hovered] {
text-decoration: underline;
}
&[aria-current] {
color: var(--text-color-current);
font-weight: bold;
}
&[data-focus-visible]:after {
content: '';
position: absolute;
inset: -2px -4px;
border-radius: 6px;
border: 2px solid var(--focus-ring-color);
}
&[aria-disabled] {
cursor: default;
&:not([aria-current]) {
color: var(--text-color-disabled);
}
}
}
@media (forced-colors: active) {
.react-aria-Link {
--focus-ring-color: Highlight;
--text-color: LinkText;
--text-color-current: CanvasText;
--text-color-disabled: GrayText;
}
}
Features#
Breadcrumbs provide a list of links to parent pages of the current page in hierarchical order.
Breadcrumbs
helps implement these in an accessible way.
- Flexible – Support for navigation links, JavaScript handled links, or custom element types (e.g. router links).
- Accessible – Implemented as a navigation landmark with an ordered list of links. Optional support for using breadcrumbs as a heading is available as well.
- Styleable – Hover, press, and keyboard focus states are provided for easy styling. These states only apply when interacting with an appropriate input device, unlike CSS pseudo classes.
Anatomy#
Breadcrumbs consist of a navigation landmark element and a list of links, typically with a visual separator icon between each item. The last link represents the current page in the hierarchy, with the previous links representing the parent pages of the current page. Each of these parent links can be clicked, tapped, or triggered via the Enter key to navigate to that page. Optionally, breadcrumbs can be used in place of a page title, in which case the last breadcrumb acts as a heading.
Concepts#
Breadcrumbs
makes use of the following concepts:
Composed components#
Breadcrumbs
uses the following components, which may also be used standalone or reused in other components.
Props#
Name | Type | Default | Description |
isDisabled | boolean | — | Whether the breadcrumbs are disabled. |
children | ReactNode | (
(item: T
)) => ReactElement | — | The contents of the collection. |
items | Iterable<T> | — | Item objects in the collection. |
className | string | — | The CSS className for the element. |
style | CSSProperties | — | The inline style for the element. |
Layout
Name | Type | Default | Description |
slot | string | — | A slot name for the component. Slots allow the component to receive props from a parent component. |
Accessibility
Name | Type | Default | Description |
id | string | — | The element's unique identifier. See MDN. |
aria-label | string | — | Defines a string value that labels the current element. |
aria-labelledby | string | — | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | — | Identifies the element (or elements) that describes the object. |
aria-details | string | — | Identifies the element (or elements) that provide a detailed, extended description for the object. |
Styling#
React Aria components can be styled in many ways, including using CSS classes, inline styles, utility classes (e.g. Tailwind), CSS-in-JS (e.g. Styled Components), etc. By default, all components include a builtin className
attribute which can be targeted using CSS selectors. These follow the react-aria-ComponentName
naming convention.
.react-aria-Breadcrumbs {
/* ... */
}
.react-aria-Breadcrumbs {
/* ... */
}
.react-aria-Breadcrumbs {
/* ... */
}
A custom className
can also be specified on any component. This overrides the default className
provided by React Aria with your own.
<Breadcrumbs className="my-breadcrumbs">
{/* ... */}
</Breadcrumbs>
<Breadcrumbs className="my-breadcrumbs">
{/* ... */}
</Breadcrumbs>
<Breadcrumbs className="my-breadcrumbs">
{/* ... */}
</Breadcrumbs>
In addition, some components support multiple UI states (e.g. focused, placeholder, readonly, etc.). React Aria components expose states using DOM attributes, which you can target in CSS selectors. These are ARIA attributes wherever possible, or data attributes when a relevant ARIA attribute does not exist. For example:
.react-aria-Link[aria-current] {
/* ... */
}
.react-aria-Link[aria-current] {
/* ... */
}
.react-aria-Link[aria-current] {
/* ... */
}
The className
and style
props also accept functions which receive states for styling. This lets you dynamically determine the classes or styles to apply, which is useful when using utility CSS libraries like Tailwind.
<Link
className={({ isCurrent }) => isCurrent ? 'bg-gray-700' : 'bg-gray-600'}
/>
<Link
className={({ isCurrent }) =>
isCurrent ? 'bg-gray-700' : 'bg-gray-600'}
/>
<Link
className={(
{ isCurrent }
) =>
isCurrent
? 'bg-gray-700'
: 'bg-gray-600'}
/>
The states, selectors, and render props for all components used in Breadcrumbs
are documented below.
Breadcrumbs#
Breadcrumbs
can be targed with the .react-aria-Breadcrumbs
CSS selector, or by overriding with a custom className
. It is rendered as a <nav>
element, and contains an <ol>
element representing the list of items. This additional element can be targed with the ol
selector.
.react-aria-Breadcrumbs {
& ol {
/* ... */
}
}
.react-aria-Breadcrumbs {
& ol {
/* ... */
}
}
.react-aria-Breadcrumbs {
& ol {
/* ... */
}
}
Item#
An Item
can be targeted with the .react-aria-Item
CSS selector, or by overriding with a custom className
. It is rendered as an <li>
element, and should contain a <Link>
or <Heading>
. It may also include another element such as a separator icon.
Link#
A Link
can be targeted with the .react-aria-Link
CSS selector, or by overriding with a custom className
. It clones its only child element (e.g. <a>
) and adds additional props, which should be passed through to the underlying DOM node. If only text is provided, it is rendered as a <span>
. Links support the following states and render props:
Name | CSS Selector | Description |
isCurrent | [aria-current] | Whether the link is the current item within a list. |
isHovered | [data-hovered] | Whether the link is currently hovered with a mouse. |
isPressed | [data-pressed] | Whether the link is currently in a pressed state. |
isFocused | :focus | Whether the link is focused, either via a mouse or keyboard. |
isFocusVisible | [data-focus-visible] | Whether the link is keyboard focused. |
isDisabled | [aria-disabled] | Whether the link is disabled. |
Usage#
Dynamic collections#
Breadcrumbs
follows the Collection Components API, accepting both static and dynamic collections.
The examples above show static collections, which can be used when the full list of options is known ahead of time. Dynamic collections,
as shown below, can be used when the options come from an external data source such as an API call, or update over time.
As seen below, an iterable list of options is passed to the Breadcrumbs using the items
prop. A function provided as children
of the <Breadcrumbs>
is called to render each item. When a breadcrumb is pressed, the onPress
event is triggered and the breadcrumbs array is updated.
function Example() {
let [breadcrumbs, setBreadcrumbs] = React.useState([
{ id: 1, label: 'Home' },
{ id: 2, label: 'Trendy' },
{ id: 3, label: 'March 2022 Assets' }
]);
let navigate = (item: typeof breadcrumbs[0]) => {
setBreadcrumbs(breadcrumbs.slice(0, breadcrumbs.indexOf(item) + 1));
};
return (
<Breadcrumbs items={breadcrumbs}>
{(item) => (
<Item>
<Link onPress={() => navigate(item)}>{item.label}</Link>
</Item>
)}
</Breadcrumbs>
);
}
function Example() {
let [breadcrumbs, setBreadcrumbs] = React.useState([
{ id: 1, label: 'Home' },
{ id: 2, label: 'Trendy' },
{ id: 3, label: 'March 2022 Assets' }
]);
let navigate = (item: typeof breadcrumbs[0]) => {
setBreadcrumbs(
breadcrumbs.slice(0, breadcrumbs.indexOf(item) + 1)
);
};
return (
<Breadcrumbs items={breadcrumbs}>
{(item) => (
<Item>
<Link onPress={() => navigate(item)}>
{item.label}
</Link>
</Item>
)}
</Breadcrumbs>
);
}
function Example() {
let [
breadcrumbs,
setBreadcrumbs
] = React.useState([
{
id: 1,
label: 'Home'
},
{
id: 2,
label: 'Trendy'
},
{
id: 3,
label:
'March 2022 Assets'
}
]);
let navigate = (
item:
typeof breadcrumbs[
0
]
) => {
setBreadcrumbs(
breadcrumbs.slice(
0,
breadcrumbs
.indexOf(
item
) + 1
)
);
};
return (
<Breadcrumbs
items={breadcrumbs}
>
{(item) => (
<Item>
<Link
onPress={() =>
navigate(
item
)}
>
{item.label}
</Link>
</Item>
)}
</Breadcrumbs>
);
}
Heading#
The last breadcrumb may be used as a heading for the page by placing a <Heading>
element within the <Item>
instead of a <Link>
. By default, this is an h3
element, but this can be changed with the level
prop on the Heading
.
import {Heading} from 'react-aria-components';
<Breadcrumbs>
<Item><Link><a href="/">Home</a></Link></Item>
<Item><Link><a href="/react-aria">React Aria</a></Link></Item>
<Item><Heading>useBreadcrumbs</Heading></Item>
</Breadcrumbs>
import {Heading} from 'react-aria-components';
<Breadcrumbs>
<Item>
<Link>
<a href="/">Home</a>
</Link>
</Item>
<Item>
<Link>
<a href="/react-aria">React Aria</a>
</Link>
</Item>
<Item>
<Heading>useBreadcrumbs</Heading>
</Item>
</Breadcrumbs>
import {Heading} from 'react-aria-components';
<Breadcrumbs>
<Item>
<Link>
<a href="/">
Home
</a>
</Link>
</Item>
<Item>
<Link>
<a href="/react-aria">
React Aria
</a>
</Link>
</Item>
<Item>
<Heading>
useBreadcrumbs
</Heading>
</Item>
</Breadcrumbs>
Router links#
The <Link>
component can wrap a custom link element provided by a router like React Router.
import {Link as RouterLink} from 'react-router-dom';
<Breadcrumbs>
<Item><Link><RouterLink to="/foo">Foo</RouterLink></Link></Item>
<Item><Link>Bar</Link></Item>
</Breadcrumbs>
import {Link as RouterLink} from 'react-router-dom';
<Breadcrumbs>
<Item>
<Link>
<RouterLink to="/foo">Foo</RouterLink>
</Link>
</Item>
<Item>
<Link>Bar</Link>
</Item>
</Breadcrumbs>
import {Link as RouterLink} from 'react-router-dom';
<Breadcrumbs>
<Item>
<Link>
<RouterLink to="/foo">
Foo
</RouterLink>
</Link>
</Item>
<Item>
<Link>Bar</Link>
</Item>
</Breadcrumbs>
Separator icons#
The above examples use the CSS :after
pseudo class to add separators between each item. These may also be DOM elements instead, e.g. SVG icons. Be sure that they have aria-hidden="true"
so they are hidden from assistive technologies.
import ChevronIcon from '@spectrum-icons/workflow/ChevronDoubleRight';
<Breadcrumbs>
<Item className="my-item">
<Link><a href="/">Home</a></Link>
<ChevronIcon size="S" />
</Item>
<Item><Link>React Aria</Link></Item>
</Breadcrumbs>
import ChevronIcon from '@spectrum-icons/workflow/ChevronDoubleRight';
<Breadcrumbs>
<Item className="my-item">
<Link>
<a href="/">Home</a>
</Link>
<ChevronIcon size="S" />
</Item>
<Item>
<Link>React Aria</Link>
</Item>
</Breadcrumbs>
import ChevronIcon from '@spectrum-icons/workflow/ChevronDoubleRight';
<Breadcrumbs>
<Item className="my-item">
<Link>
<a href="/">
Home
</a>
</Link>
<ChevronIcon size="S" />
</Item>
<Item>
<Link>
React Aria
</Link>
</Item>
</Breadcrumbs>
Show CSS
.my-item svg {
vertical-align: middle;
padding: 0 5px;
margin-top: -2px;
}
.my-item svg {
vertical-align: middle;
padding: 0 5px;
margin-top: -2px;
}
.my-item svg {
vertical-align: middle;
padding: 0 5px;
margin-top: -2px;
}
Disabled#
Breadcrumbs can be disabled using the isDisabled
prop. This indicates that navigation is not currently available. When a breadcrumb is disabled, onPress
will not be triggered, navigation will not occur, and links will be marked as aria-disabled
for assistive technologies.
<Breadcrumbs isDisabled>
<Item><Link><a href="/">Home</a></Link></Item>
<Item><Link><a href="/react-aria">React Aria</a></Link></Item>
<Item><Link>useBreadcrumbs</Link></Item>
</Breadcrumbs>
<Breadcrumbs isDisabled>
<Item>
<Link>
<a href="/">Home</a>
</Link>
</Item>
<Item>
<Link>
<a href="/react-aria">React Aria</a>
</Link>
</Item>
<Item>
<Link>useBreadcrumbs</Link>
</Item>
</Breadcrumbs>
<Breadcrumbs
isDisabled
>
<Item>
<Link>
<a href="/">
Home
</a>
</Link>
</Item>
<Item>
<Link>
<a href="/react-aria">
React Aria
</a>
</Link>
</Item>
<Item>
<Link>
useBreadcrumbs
</Link>
</Item>
</Breadcrumbs>
Individual breadcrumbs can also be disabled by passing the isDisabled
prop to the <Link>
element:
<Breadcrumbs>
<Item><Link><a href="/">Home</a></Link></Item>
<Item><Link isDisabled><a href="/react-aria">React Aria</a></Link></Item>
<Item><Link>useBreadcrumbs</Link></Item>
</Breadcrumbs>
<Breadcrumbs>
<Item>
<Link>
<a href="/">Home</a>
</Link>
</Item>
<Item>
<Link isDisabled>
<a href="/react-aria">React Aria</a>
</Link>
</Item>
<Item>
<Link>useBreadcrumbs</Link>
</Item>
</Breadcrumbs>
<Breadcrumbs>
<Item>
<Link>
<a href="/">
Home
</a>
</Link>
</Item>
<Item>
<Link isDisabled>
<a href="/react-aria">
React Aria
</a>
</Link>
</Item>
<Item>
<Link>
useBreadcrumbs
</Link>
</Item>
</Breadcrumbs>
Advanced customization#
Hooks#
If you need to customize things further, such as customizing the DOM structure, you can drop down to the lower level Hook-based API. See useBreadcrumbs for more details.