Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(navbar-menu): menu element cannot be found in the DOM after navigation #96

Merged
merged 3 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/LumexUI.Docs.Client/Components/Header.razor
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<LumexNavbarMenuToggle Class="sm:hidden" />
</LumexNavbarContent>

<LumexNavbarMenu>
<LumexNavbarMenu Class="pt-0 pb-10">
<NavMenu />
</LumexNavbarMenu>
</LumexNavbar>
Expand Down
5 changes: 2 additions & 3 deletions src/LumexUI/Components/Navbar/LumexNavbarMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<LumexCollapse As="@As"
Class="@RootClass"
Style="@RootStyle"
Expanded="@Context.IsMenuExpanded"
@attributes="@AdditionalAttributes"
@ref="@_collapse">
Expanded="@Expanded"
@attributes="@AdditionalAttributes">
@ChildContent
</LumexCollapse>
31 changes: 18 additions & 13 deletions src/LumexUI/Components/Navbar/LumexNavbarMenu.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
// See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE

using LumexUI.Common;
using LumexUI.Extensions;
using LumexUI.Styles;
using LumexUI.Utilities;

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components.Routing;

namespace LumexUI;

/// <summary>
/// A component representing a collapsible menu for the <see cref="LumexNavbar"/>.
/// </summary>
[CompositionComponent( typeof( LumexNavbar ) )]
public partial class LumexNavbarMenu : LumexComponentBase
public partial class LumexNavbarMenu : LumexComponentBase, IDisposable
{
/// <summary>
/// Gets or sets content to be rendered inside the navbar menu.
Expand All @@ -25,7 +24,9 @@ public partial class LumexNavbarMenu : LumexComponentBase

[CascadingParameter] internal NavbarContext Context { get; set; } = default!;

[Inject] private IJSRuntime JS { get; set; } = default!;
[Inject] private NavigationManager NavigationManager { get; set; } = default!;

internal bool Expanded { get; private set; }

private protected override string? RootClass =>
TwMerge.Merge( Navbar.GetMenuStyles( this ) );
Expand All @@ -36,8 +37,6 @@ public partial class LumexNavbarMenu : LumexComponentBase
.Add( base.RootStyle )
.ToString();

private LumexCollapse? _collapse;

/// <summary>
/// Initializes a new instance of the <see cref="LumexNavbarMenu"/>.
/// </summary>
Expand All @@ -51,16 +50,22 @@ protected override void OnInitialized()
{
ContextNullException.ThrowIfNull( Context, nameof( LumexNavbarMenu ) );

Context.Register( this );
Context.OnMenuToggle += StateHasChanged;
Context.RegisterMenu( this );
NavigationManager.LocationChanged += HandleLocationChanged;
}

internal void Toggle()
{
Expanded = !Expanded;
StateHasChanged();
}

private void HandleLocationChanged( object? sender, LocationChangedEventArgs e )
=> Toggle();

/// <inheritdoc />
protected override async Task OnAfterRenderAsync( bool firstRender )
public void Dispose()
{
if( firstRender )
{
await _collapse!.ElementReference.PortalToAsync();
}
NavigationManager.LocationChanged -= HandleLocationChanged;
}
}
4 changes: 2 additions & 2 deletions src/LumexUI/Components/Navbar/LumexNavbarMenuToggle.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Class="@RootClass"
Style="@RootStyle"
type="button"
data-expanded="@Context.IsMenuExpanded"
data-expanded="@Context.Menu?.Expanded"
@attributes="@AdditionalAttributes"
@onclick="@Context.ToggleMenu">
@onclick="@Toggle">
<span class="@ToggleIconClass" />
</LumexComponent>
21 changes: 20 additions & 1 deletion src/LumexUI/Components/Navbar/LumexNavbarMenuToggle.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
using LumexUI.Styles;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;

namespace LumexUI;

/// <summary>
/// A component representing a button that toggles the <see cref="LumexNavbarMenu"/>.
/// </summary>
[CompositionComponent( typeof( LumexNavbar ) )]
public partial class LumexNavbarMenuToggle : LumexComponentBase
public partial class LumexNavbarMenuToggle : LumexComponentBase, IDisposable
{
[CascadingParameter] internal NavbarContext Context { get; set; } = default!;

[Inject] private NavigationManager NavigationManager { get; set; } = default!;

private protected override string? RootClass =>
TwMerge.Merge( Navbar.GetToggleStyles( this ) );

Expand All @@ -35,5 +38,21 @@ public LumexNavbarMenuToggle()
protected override void OnInitialized()
{
ContextNullException.ThrowIfNull( Context, nameof( LumexNavbarMenuToggle ) );

NavigationManager.LocationChanged += HandleLocationChanged;
}

private void HandleLocationChanged( object? sender, LocationChangedEventArgs e )
=> StateHasChanged();

private void Toggle()
{
Context.Menu?.Toggle();
}

/// <inheritdoc />
public void Dispose()
{
NavigationManager.LocationChanged -= HandleLocationChanged;
}
}
13 changes: 1 addition & 12 deletions src/LumexUI/Components/Navbar/NavbarContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,9 @@ internal class NavbarContext( LumexNavbar owner ) : IComponentContext<LumexNavba
{
public LumexNavbar Owner { get; } = owner;
public LumexNavbarMenu? Menu { get; private set; }
public bool IsMenuExpanded { get; private set; }

public event Action OnMenuToggle = default!;

public void Register( LumexNavbarMenu menu )
public void RegisterMenu( LumexNavbarMenu menu )
{
Menu = menu;
}

public void ToggleMenu()
{
IsMenuExpanded = !IsMenuExpanded;
NotifyStateChanged();
}

private void NotifyStateChanged() => OnMenuToggle.Invoke();
}
36 changes: 26 additions & 10 deletions src/LumexUI/Styles/Navbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ internal readonly record struct Navbar
.Add( "w-full" )
.Add( "items-center" )
.Add( "justify-center" )
.Add( "bg-background" )
.ToString();

private readonly static string _wrapper = ElementClass.Empty()
Expand Down Expand Up @@ -108,7 +107,6 @@ internal readonly record struct Navbar
.Add( "bottom-0" )
.Add( "inset-x-0" )
.Add( "overflow-y-auto" )
.Add( "bg-background" )
.ToString();

private readonly static string _menuItem = ElementClass.Empty()
Expand All @@ -126,12 +124,6 @@ internal readonly record struct Navbar
.Add( "border-divider" )
.ToString();

private readonly static string _blurred = ElementClass.Empty()
.Add( "backdrop-blur-lg" )
.Add( "backdrop-saturate-150" )
.Add( "bg-background/70" )
.ToString();

private static ElementClass GetMaxWidthStyles( MaxWidth maxWidth )
{
return ElementClass.Empty()
Expand All @@ -149,13 +141,37 @@ private static ElementClass GetAlignStyles( Align? align )
.Add( "ms-auto", when: align is Align.End );
}

private static ElementClass GetBlurredStyles( bool blurred, string slot )
{
return blurred switch
{
false => ElementClass.Empty()
.Add( "bg-background", when: slot is nameof( _base ) )
.Add( "bg-background", when: slot is nameof( _menu ) ),

true => ElementClass.Empty()
// https://stackoverflow.com/questions/60997948/backdrop-filter-not-working-for-nested-elements-in-chrome
.Add( ElementClass.Empty()
.Add( "before:-z-10" )
.Add( "before:absolute" )
.Add( "before:inset-0" )
.Add( "before:backdrop-blur-lg" )
.Add( "before:backdrop-saturate-150" )
.Add( "before:bg-background/70" ), when: slot is nameof( _base ) )
.Add( ElementClass.Empty()
.Add( "backdrop-blur-lg" )
.Add( "backdrop-saturate-150" )
.Add( "bg-background/70" ), when: slot is nameof( _menu ) )
};
}

public static string GetStyles( LumexNavbar navbar )
{
return ElementClass.Empty()
.Add( _base )
.Add( _sticky, when: navbar.Sticky )
.Add( _bordered, when: navbar.Bordered )
.Add( _blurred, when: navbar.Blurred )
.Add( GetBlurredStyles( navbar.Blurred, slot: nameof( _base ) ) )
.Add( navbar.Classes?.Root )
.Add( navbar.Class )
.ToString();
Expand Down Expand Up @@ -210,7 +226,7 @@ public static string GetMenuStyles( LumexNavbarMenu navbarMenu )

return ElementClass.Empty()
.Add( _menu )
.Add( _blurred, when: navbar.Blurred )
.Add( GetBlurredStyles( navbar.Blurred, slot: nameof( _menu ) ) )
.Add( navbar.Classes?.Menu )
.Add( navbarMenu.Class )
.ToString();
Expand Down
Loading