This repository contains a Generic Breadcrumb Component for Blazor applications, enabling breadcrumb navigation to enhance user experience and track navigation history. The component uses BreadcrumbService
for managing breadcrumb data and integrates with JSInterop
for cookie-based persistence.
- Dynamic Breadcrumb Management: Tracks navigation changes and updates breadcrumb items dynamically.
- Persistence with Cookies: Stores breadcrumbs in cookies for state retention across sessions.
- Customizable UI: Easily styled breadcrumb interface.
- Lightweight Integration: Requires minimal setup to integrate into Blazor projects.
- Core service that:
- Manages breadcrumb states and updates based on navigation events.
- Persists breadcrumb data in cookies via
JSInterop
. - Handles breadcrumb truncation for maintaining relevance.
- UI component for rendering the breadcrumb trail.
- Integrates with
BreadcrumbService
for dynamic updates. - Supports customizable breadcrumb items and design.
- JavaScript utilities for:
- Managing cookies (
setCookie
,getCookie
,deleteCookie
). - Handling breadcrumb click navigation.
- Observing DOM events for the breadcrumb component.
- Managing cookies (
App.razor
- Entry point to the Blazor application, ensuring proper rendering.
MainLayout.razor
- Includes the JavaScript file (
blazorExtensions.js
) and manages layout-level URL updates.
- Includes the JavaScript file (
-
Add the
BreadcrumbService
to the DI Container:builder.Services.AddScoped<BreadcrumbService>();
-
Include the JavaScript File in Your Layout:
<script src="js/blazorExtensions.js"></script>
-
Inject the Breadcrumb Component in Your Page:
@inject BreadcrumbService BreadcrumbService <div id="breadcrumbs"> <Breadcrumbs /> </div>
-
Add Breadcrumb Items in Your Page Logic:
protected override async Task OnInitializedAsync() { await BreadcrumbService.AddBreadcrumb("Breadcrumb Title", NavigationManager.Uri); }
The breadcrumb component is styled using the provided CSS within Breadcrumbs.razor
. Customize it as needed:
.breadcrumb {
padding: 0.75rem 1rem;
background-color: transparent;
}
.breadcrumb-item a {
text-decoration: none;
}
.breadcrumb-item.active {
font-weight: bold;
}
The JavaScript utilities in blazorExtensions.js
provide cookie management and event handling. Key functions:
- Set a Cookie:
blazorExtensions.setCookie("key", "value", 365);
- Get a Cookie:
blazorExtensions.getCookie("key");
- Delete a Cookie:
blazorExtensions.deleteCookie("key");
@inject BreadcrumbService BreadcrumbService
@if (ShowBreadcrumbs)
{
<Breadcrumbs />
}
protected override async Task OnInitializedAsync()
{
await BreadcrumbService.AddBreadcrumb("Home", "/");
await BreadcrumbService.AddBreadcrumb("Dashboard", "/dashboard");
}
- Add support for custom separators between breadcrumb items.
- Improve accessibility with ARIA roles and attributes.
- Provide advanced customization options for the breadcrumb UI.
Feel free to open issues and submit pull requests for improvements or bug fixes. Contributions are welcome!
Thanks to the Blazor and .NET community for their resources and contributions, enabling seamless development of this component.