useBreadcrumbs
Get the breadcrumb trail for the current page, with a fallback when the page has none.
Use useBreadcrumbs to render the trail ("Home / Accessories / Watch") above a page.
Example
import { useBreadcrumbs } from '@salla.sa/twilight-theme-engine/hooks';
import type { Page } from '@salla.sa/twilight-theme-engine/types';
export function Breadcrumbs({ page }: { page: Page }) {
const crumbs = useBreadcrumbs(page);
if (!crumbs.length) return null;
return (
<nav aria-label="Breadcrumb">
{crumbs.map((c, i) =>
i === crumbs.length - 1 ? (
<span key={c.url}>{c.name}</span>
) : (
<a key={c.url} href={c.url}>{c.name} /</a>
)
)}
</nav>
);
}API
function useBreadcrumbs(page?: Page | null): Breadcrumb[];| Prop | Type | Description |
|---|---|---|
page | Page | null (optional) | The page object your route loader returned. Without it the hook returns []. |
It returns a list of { name: string; url: string } items. See Shared types for Breadcrumb and Page.
Where the trail comes from, in order:
page.breadcrumbs, when the route loader provides them. They are returned as they are.- On the home page, an empty list.
- Otherwise a fallback:
Home(translated, linking to/) followed by the page title linking to the current path.
Gotchas
- Import it from
@salla.sa/twilight-theme-engine/hooks, the package root of the hooks. It has no subpath of its own. - The returned list is empty on the home page and when
pageis missing. Render nothing in that case. - On a product page the hook inserts the listing the customer came from (a category, a search, the brands page) between the first crumb and the product. It remembers that listing in the browser's session storage, so the inserted crumb appears after hydration, not in server-rendered HTML.
- The referrer is saved for every page except the home page and product pages, and only lasts for the browser session.
- Crumb
urlvalues from the loader are paths. Render them with yourLinkcomponent for client-side navigation.
Next
- Shared types:
BreadcrumbandPage. - Routes and pages explains the
pageobject. Link