Create WebPaths.cs to define URL paths. Unlike other frameworks, FluentUI's Icon is defined as a class rather than a string, so you can't specify it directly. Therefore, specify the icon class in a generic format. Use FluentUI Icons for icon definitions. For detailed configuration, refer to Menu Item Customization.
C#
1 2 3 4 5 6 7 8 9101112131415161718192021222324
usingBlazorPathHelper;// Use static to simplify icon definitionsusingstaticMicrosoft.FluentUI.AspNetCore.Components.Icons.Regular.Size20;[BlazorPath]publicpartialclassWebPaths{[Item<Home>("Home")]publicconststringHome="/";[Item<TextHeader1>("Sample1")]publicconststringSample1="/sample1";[Item<AddCircle>("Sample1C1")]publicconststringSample1C1=$"{Sample1}/child1";[Item<AddCircle>("Sample1C2")]publicconststringSample1C2=$"{Sample1}/child2";[Item<CheckmarkCircle>("Sample1C2C1")]publicconststringSample1C2C1=$"{Sample1}/child2/child1";[Item<TextHeader2>("Sample2")]publicconststringSample2="/sample2";[Item<Star>("Sample2C1")]publicconststringSample2C1=$"{Sample2}/child1";[Item<TextHeader3>("Sample3")]publicconststringSample3="/sample3";}
@using BlazorPathHelper
@foreach(var menuItem in MenuItems)
{
<!-- Handle cases with and without child elements separately -->
@if(menuItem.HasChildren)
{
<!-- Use menuItem.Key for defining the key attribute -->
<!-- menuItem.Path represents the URL path of the menu item -->
<!-- Cast Icon to use it since it's of type object? -->
<FluentNavGroup @key=@menuItem.Key Href="@menuItem.Path"
Title="@menuItem.Name" Icon="@((Icon?)menuItem.Icon)">
<!-- Recursively call to display child elements -->
<NavMenuItem MenuItems="menuItem.Children"/>
</FluentNavGroup>
}
else
{
<!-- Apply NavLinkMatch.All only to the homepage, use Prefix otherwise -->
<FluentNavLink @key=@menuItem.Key Href="@menuItem.Path"
Match="@(menuItem.IsHome ? NavLinkMatch.All : NavLinkMatch.Prefix)"
Icon="@((Icon?)menuItem.Icon)" IconColor="Color.Accent">
@menuItem.Name
</FluentNavLink>
}
}
@code {
// Receive an array of menu items
[Parameter, EditorRequired]
public BlazorPathMenuItem[] MenuItems { get; set; } = [];
}