ContentBreadcrumbs
ContentBreadcrumbs renders a breadcrumb trail for a CMS item: a home link, a link for each directory from the
root down to the item's directory, and the item's title. Use it on article and documentation pages, where the
trail should follow the content tree rather than the URL.
Package Osirion.Blazor.Cms.Web, namespace Osirion.Blazor.Cms.Web.Components. Source on GitHub.
@using Osirion.Blazor.Cms.Domain.Entities
@using Osirion.Blazor.Cms.Domain.Enums
@using Osirion.Blazor.Cms.Domain.Repositories
@using Osirion.Blazor.Cms.Domain.Services
@using Osirion.Blazor.Cms.Web.Components
@inject IContentProviderManager Content
@* The trail follows the directories of a blog post from this site's own content: Home, Blog and the post's title *@
@if (post is not null)
{
<ContentBreadcrumbs Content="@post" />
}
@code {
private ContentItem? post;
protected override async Task OnInitializedAsync()
{
var posts = await Content.GetContentByQueryAsync(new ContentQuery
{
Locale = "en",
Directory = "blog",
SortBy = SortField.Order,
SortDirection = SortDirection.Ascending
});
post = posts.FirstOrDefault(item => !item.Path.EndsWith("_index.md", StringComparison.OrdinalIgnoreCase));
}
}The demo takes a blog post from this site's own content and shows its trail: Home, Blog and the post's title.
Usage
@using Osirion.Blazor.Cms.Web.Components
<ContentBreadcrumbs Content="@Item" />
The component walks up from Item.Directory through each directory's Parent. A documentation page that sets
its own home link and directory URLs:
@using Osirion.Blazor.Cms.Domain.Entities
@using Osirion.Blazor.Cms.Web.Components
<ContentBreadcrumbs Content="@Item"
Directory="@Item?.Directory"
HomeText="Docs"
HomeUrl="/docs"
DirectoryUrlFormatter="@(directory => $"/{directory.Url.TrimStart('/')}")" />
@code {
[Parameter]
public ContentItem? Item { get; set; }
}
A directory's Url is the permalink from its _index.md, and is empty when there is none. Without a formatter,
a link goes to that permalink, or, when the folder declares none, to its folder path without the locale folder
(blog for en/blog). The locale folder itself (en) has no page and is left out of the trail.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
Content |
ContentItem? |
null |
The item being shown. Its title is the last crumb, and its Directory is the deepest directory of the trail when Directory is not set (read once, when the component is first initialized). |
Directory |
DirectoryItem? |
null |
The deepest directory of the trail. The crumbs are its parents up to the root, without a locale folder such as en. |
ShowHome |
bool |
true |
Whether the home link is rendered first. |
HomeText |
string |
"Home" |
The text of the home link. |
HomeUrl |
string |
"/" |
The target of the home link. Defaults to /. |
HideCurrentItem |
bool |
false |
Whether the item's title, the last crumb, is left out, for example when the page heading already shows it. |
DirectoryUrlFormatter |
Func<DirectoryItem, string>? |
null |
A function that builds each directory link. Without it, a link goes to the address the directory's _index.md declares in permalink, otherwise to the directory's path without the locale folder. |
Theme |
ThemeMode |
ThemeMode.System |
The component's color mode. Light or Dark writes it to data-theme (data-bs-theme under Bootstrap) on the root element; System writes nothing. |
Class |
string? |
null |
Extra CSS classes for the component's root element. |
Style |
string? |
null |
An inline style for the component's root element. |
Class goes on the nav. Style and Theme are accepted but not written, and other attributes on the tag are
captured but not rendered.
Behavior
- The component takes
Content.Directoryonly once, when it is first initialized. If the item can change while the component stays on the page (an interactive page that loads another item), passDirectoryas well, as the built-inDocumentPagedoes. - A directory's name is its folder name, or the
titlefrom its_index.md. - It renders
<nav aria-label="breadcrumb">around an ordered list. The current item is plain text in aspan, not a link. It has noaria-currentattribute. - The
/separators are drawn with CSS (::after), not written into the markup. The directory links are written in the component's markup, so its scoped stylesheet reaches them like the home and current crumbs. - Titles and directory names are written as text, so they are HTML-encoded. No script is needed.
Styling
| Class | Element |
|---|---|
osirion-breadcrumbs |
The nav, plus your Class |
osirion-breadcrumbs-list |
The ol |
osirion-breadcrumbs-item |
Each li; the current item's li also has osirion-breadcrumbs-current |
osirion-breadcrumbs-link |
The home and directory links |
osirion-breadcrumbs-text |
The current item's title |
Links use --osirion-text-secondary and turn --osirion-action-primary on hover; separators use
--osirion-text-tertiary.
Related
- OsirionBreadcrumbs, breadcrumbs built from a URL path
- DirectoryNavigation
- CMS navigation components