Breadcrumb
Draws a page's breadcrumb trail from its loader data, with structured data for search engines, unless the merchant turned breadcrumbs off.
import { Breadcrumb } from '@salla.sa/twilight-theme-engine/components/common';In plain words
Breadcrumbs are the "Home › Chairs › Wooden chair" trail at the top of a page. Give Breadcrumb the page object your route's loader returned (the function that fetches a page's data before it shows), and it draws the trail as links.
It also writes a hidden description of the trail that search engines read, and it hides itself when the merchant switched breadcrumbs off in the theme settings.
Signature
const Breadcrumb: LazyExoticComponent<(props: BreadcrumbProps) => JSX.Element | null>
// BreadcrumbProps is not exported
interface BreadcrumbProps {
page?: Page | null; // the page object a route loader returns
className?: string; // replaces 'breadcrumbs w-full py-5' on the <nav>
}Try it live
The JSON-LD it also rendered, for search engines
(nothing: the trail is empty)
import { Breadcrumb } from '@salla.sa/twilight-theme-engine/components/common';
import type { Page } from '@salla.sa/twilight-theme-engine/types';
// `page` is what the route loader returned, e.g.
// { title: 'Breadcrumb', slug: 'page-single',
// breadcrumbs: [{ name: 'Link', url: '/playground/reference/components/link' },
// { name: 'Image', url: '/playground/reference/components/image' },
// { name: 'Breadcrumb', url: '/playground/reference/components/breadcrumb' }] }
export function PageTop({ page }: { page: Page }) {
return <Breadcrumb page={page} />;
}
Example
import { Breadcrumb } from '@salla.sa/twilight-theme-engine/components/common';
import type { Page } from '@salla.sa/twilight-theme-engine/types';
export function PageTop({ page }: { page: Page }) {
return (
<div className="container">
<Breadcrumb page={page} />
<h1>{page.title}</h1>
</div>
);
}
How it behaves
The trail is
useBreadcrumbs(page):page.breadcrumbswhen the loader sent any, otherwise Home plus the page title, and nothing on the home page or without apage.Markup:
<nav aria-label="Breadcrumb">around<ol className="s-breadcrumb-wrapper">, with schema.org microdata. Every crumb but the last is an engineLinkfollowed by a chevron; the last is plain text witharia-current="page".More than 4 crumbs collapse to the first, a "•••" button and the last two. The button expands the trail; a new trail collapses it again.
It also renders a
<script type="application/ld+json">BreadcrumbList. Each URL isstore.urlfollowed by the crumb'surl, unless that already starts withhttp; the last item uses@idinstead ofitem.It renders nothing when the theme setting
is_breadcrumbs_enabledisfalse. A store that never saved the setting shows breadcrumbs.It is lazy (code-split), like
NoContent,RenderWhenVisible,CurrencySymbolandErrorPage: the first render suspends while its code loads.
Gotchas
docs/23-breadcrumb-system.md imports
BreadcrumbTrailandBreadcrumbJsonLdfrom@salla.sa/twilight-theme-engine/components/common. Neither is exported, so that import fails. For custom markup, build onuseBreadcrumbsfrom@salla.sa/twilight-theme-engine/hooks.Rendering it records the page in
sessionStorage(breadcrumb-referrer), and the next product page shows that entry as its middle crumb. ABreadcrumbfor something that is not a real page (a modal, a preview panel) puts a wrong crumb on product pages.The structured data joins text.
store.urlcan end in/(the demo store's ishttps://demostore.salla.sa/ar/dev-vgckq3fssfhjewwi/), and then a relative crumb such as Home (/) becomes…/dev-vgckq3fssfhjewwi//. Crumbs built from APIurlfields are absolute and unaffected.A
pageobject built during render is new every time, and both the trail and the referrer effect depend on that object: each re-render rewritessessionStorageand collapses an expanded trail. Loader data is stable; memoise hand-built pages withuseMemo.
Related
Builds a page's breadcrumb trail from its loader data, with a Home-plus-title fallback and a remembered referrer on product pages.
LinkAn anchor that moves between store pages without reloading, adding the language (and, on localhost or the preview host, the store) to the path.
CustomerLayoutThe account area's frame: a breadcrumb band, the account menu sidebar and the page title around each account page.