Breadcrumb
Breadcrumbs is a navigation pattern that helps users understand the hierarchy of a website.
Import
Chakra UI exports 4 breadcrumb-related components:
Breadcrumb: The parent container for breadcrumbs.BreadcrumbItem: Individual breadcrumb element containing a link and a divider.BreadcrumbLink: The breadcrumb link.BreadcrumbSeparator: The visual separator between each breadcrumb.
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbSeparator,
} from "@chakra-ui/react";
Usage
Add isCurrentPage prop to the BreadcrumbItem that matches the current path.
When this prop is present, the BreadcrumbLink renders a span with
aria-current set to page instead of an anchor element.
Separators
Change the separator used in the breadcrumb by passing a string, like - or an
icon.
Using an icon as the separator
Using a separator in last item
If you want to have a BreadcrumbSeparator in the last item of the Breadcrumb
you have different ways to achieve it. But to avoid the 'React does not
recognize the isLastChild prop on a DOM element.' error you can not simply
add the separator directly in the Breadcrumb.
The clean way to achieve it is to put the separator in the last
BreadcrumbItem. Feel free to also inspect the example below with the browsers
dev tools.
Composition
Breadcrumb composes Box so you can pass all Box
props to change the style of the breadcrumbs. Let's say we need to reduce the
fontSize of the breadcrumbs.
Usage with Routing Library
To use the Breadcrumb with a routing Library like Reach Router or React Router,
all you need to do is to pass the as prop to the BreadcrumbLink component.
It'll replace the rendered a tag with Reach's Link.
// import { Link } from "@reach/router"
<Breadcrumb>
<BreadcrumbItem>
<BreadcrumbLink as={Link} to="#">
Home
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbItem>
<BreadcrumbLink as={Link} to="#">
About
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbItem isCurrentPage>
<BreadcrumbLink>Contact</BreadcrumbLink>
</BreadcrumbItem>
</Breadcrumb>
Accessibility
- The Breadcrumbs are rendered in a
navto denote that it is a navigation landmark. - The Breadcrumb
navhasaria-labelset tobreadcrumb. - The BreadcrumbItem with
isCurrentPageprop adds thearia-current=pageto the BreadcrumbLink. - The separator has
roleset topresentationto denote that its for presentation purposes.
Props
Breadcrumb Props
colorScheme
colorSchemeColor Schemes for Breadcrumb are not implemented in the default theme. You can extend the theme to implement them.
stringlistProps
listPropsHTMLChakraProps<"ol">separator
separatorThe visual separator between each breadcrumb item
string | React.ReactElement"/"size
sizeSizes for Breadcrumb are not implemented in the default theme. You can extend the theme to implement them.
stringspacing
spacingThe left and right margin applied to the separator
SystemStyleObject["mx"]"0.5rem"variant
variantVariants for Breadcrumb are not implemented in the default theme. You can extend the theme to implement them.
stringBreadcrumbItem Props
isCurrentPage
isCurrentPagebooleanisLastChild
isLastChildbooleanseparator
separatorThe visual separator between each breadcrumb item
string | React.ReactElement"/"spacing
spacingThe left and right margin applied to the separator
SystemStyleObject["mx"]"0.5rem"BreadcrumbLink Props
The BreadcrumbLink composes the Link component so you can use all Link props.
isCurrentPage
isCurrentPagebooleanBreadcrumbSeparator Props
The BreadcrumbSeparator composes the Box component so you can use all Box props.