Skip to content

Commit 44e2ce1

Browse files
committed
add collapsible static nav group
1 parent fff4f56 commit 44e2ce1

4 files changed

Lines changed: 96 additions & 9 deletions

File tree

demo/StaticSample/StaticSample.Client/Layout/NavMenu.razor

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
<MudNavLink Href="counter" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Add">Counter</MudNavLink>
44
<MudNavLink Href="weather" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Weather</MudNavLink>
55

6-
<AuthorizeView>
7-
<Authorized>
8-
<MudNavLink Href="Account/Manage" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">@context.User.Identity?.Name</MudNavLink>
9-
</Authorized>
10-
<NotAuthorized>
11-
<MudNavLink Href="Account/Register" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Register</MudNavLink>
12-
<MudNavLink Href="Account/Login" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Login</MudNavLink>
13-
</NotAuthorized>
14-
</AuthorizeView>
6+
<MudStaticNavGroup Icon="@Icons.Material.Filled.AdminPanelSettings" Title="Account" Expanded="true">
7+
<AuthorizeView>
8+
<Authorized>
9+
<MudNavLink Href="Account/Manage" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">@context.User.Identity?.Name</MudNavLink>
10+
</Authorized>
11+
<NotAuthorized>
12+
<MudNavLink Href="Account/Register" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Register</MudNavLink>
13+
<MudNavLink Href="Account/Login" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Login</MudNavLink>
14+
</NotAuthorized>
15+
</AuthorizeView>
16+
</MudStaticNavGroup>
1517
</MudNavMenu>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
@namespace MudBlazor.StaticInput
2+
3+
@inherits MudNavGroup
4+
5+
@{
6+
base.BuildRenderTree(__builder);
7+
}
8+
9+
@code {
10+
private string _elementId = string.Empty;
11+
12+
protected override void OnParametersSet()
13+
{
14+
UserAttributes["data-static-component"] = true;
15+
UserAttributes["data-static-id"] = _elementId;
16+
17+
base.OnParametersSet();
18+
}
19+
20+
protected override void OnInitialized()
21+
{
22+
_elementId = Guid.NewGuid().ToString()[..8];
23+
24+
base.OnInitialized();
25+
}
26+
}
27+
28+
@if (IsStatic()) {
29+
<script>
30+
(function () {
31+
document.addEventListener('DOMContentLoaded', () => {
32+
const button = document.querySelector('[data-static-id="@_elementId"] button');
33+
34+
if (button) {
35+
button.addEventListener('click', (event) => {
36+
const navElement = event.target.closest('nav');
37+
const collapseContainer = navElement.querySelector('.mud-collapse-container');
38+
const expandIcon = navElement.querySelector('.mud-nav-link-expand-icon');
39+
40+
if (!collapseContainer || !expandIcon) {
41+
console.warn("Missing required elements for toggling");
42+
return;
43+
}
44+
45+
// Check current state
46+
const isExpanded = button.getAttribute('aria-expanded') === "true";
47+
48+
// Toggle classes for collapse container
49+
collapseContainer.classList.toggle('mud-collapse-entered', !isExpanded);
50+
collapseContainer.classList.toggle('mud-navgroup-collapse', true);
51+
collapseContainer.classList.remove('mud-collapse-entering');
52+
collapseContainer.setAttribute('aria-hidden', isExpanded);
53+
54+
// Toggle classes for expand icon
55+
expandIcon.classList.toggle('mud-transform', !isExpanded);
56+
57+
// Update aria-expanded on the button
58+
button.setAttribute('aria-expanded', !isExpanded);
59+
});
60+
}
61+
});
62+
})();
63+
</script>
64+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Components;
2+
using Microsoft.AspNetCore.Http;
3+
4+
namespace MudBlazor.StaticInput;
5+
6+
public partial class MudStaticNavGroup : MudNavGroup
7+
{
8+
[CascadingParameter]
9+
private HttpContext HttpContext { get; set; } = default!;
10+
11+
private bool IsStatic()
12+
{
13+
#if NET9_0_OR_GREATER
14+
return !RendererInfo.IsInteractive;
15+
#else
16+
return HttpContext != null;
17+
#endif
18+
}
19+
}
20+

src/MudBlazor.StaticInput.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</PropertyGroup>
2222

2323
<ItemGroup>
24+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
2425
<PackageReference Include="MudBlazor" Version="8.0.0-preview.5" />
2526
</ItemGroup>
2627

0 commit comments

Comments
 (0)