Closed michahell closed 2 years ago
I realise this is indeed not how breadcrumbs were meant to be used. I made my own tiny breadcrumb implementation:
type BreadCrumb = { name: string; url: string }
let breadcrumbs: BreadCrumb[] = []
function buildBreadCrumbs(crumbs: string[]): BreadCrumb[] {
return crumbs.map((crumb, index) => {
if (index === 0) {
return {
name: crumb,
url: `/${crumb}`,
}
} else {
const url = crumbs.reduce((acc, currCrumb, currIndex) => {
if (currIndex <= index) {
return acc + `${currCrumb}/`
} else {
return acc
}
}, '/')
return {
name: crumb,
url: url,
}
}
})
}
router.subscribe((currentRoute: TinroRoute) => {
const crumbs = currentRoute.path.split('/').filter(Boolean)
breadcrumbs = buildBreadCrumbs(crumbs)
})
<section class="breadcrumbs">
<Breadcrumb>
<BreadcrumbItem href="/">home</BreadcrumbItem>
{#each breadcrumbs as breadcrumb}
<BreadcrumbItem href={breadcrumb.url}>{breadcrumb.name}</BreadcrumbItem>
{/each}
</Breadcrumb>
</section>
Even nicer is to prepend <BreadcrumbItem href="/">home</BreadcrumbItem>
within the script tag but I'm lazy
Basically, using IBM's Carbon Components, I'd like to create this:
There is probably a more svelte way to do this but I'm still picking up svelte. However, I don't see
currentRoute
containing any breadcrumbs. I feel that the idea is to recreate breadcrumbs on every page that sits within a Route, but I find that a bit... verbose.Instead of this:
I want to do this:
Can this be done?