-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): add navlink component (#46)
* feat(components): initial implementation of navlink component * feat(components): add Disabled and Color parameters to the navlink component * chore(components): update the XML docs in the navlink component * chore(components): nits * feat(components): add link base class; inherit link and navlink components from it * chore(components): add navlink styles * feat(components): remove the `ActiveClass` parameter from the navlink component; add active state styles; simplify link styles * test(components): add/update the tests for both `Link` and `NavLink` components
- Loading branch information
1 parent
9675787
commit 65c34cb
Showing
8 changed files
with
207 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using LumexUI.Common; | ||
|
||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace LumexUI; | ||
|
||
/// <summary> | ||
/// A base class for link components. | ||
/// </summary> | ||
public abstract class LumexLinkBase : LumexComponentBase | ||
{ | ||
/// <summary> | ||
/// Gets or sets content to be rendered inside the link. | ||
/// </summary> | ||
[Parameter] public RenderFragment? ChildContent { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value representing the URL of the link. | ||
/// </summary> | ||
[Parameter] public string Href { get; set; } = "#"; | ||
|
||
/// <summary> | ||
/// Gets or sets a color of the link. | ||
/// </summary> | ||
/// <remarks> | ||
/// The default value is <see cref="ThemeColor.Primary"/> | ||
/// </remarks> | ||
[Parameter] public ThemeColor Color { get; set; } = ThemeColor.Primary; | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether the link is disabled. | ||
/// </summary> | ||
[Parameter] public bool Disabled { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@namespace LumexUI | ||
@inherits LumexLinkBase | ||
|
||
<NavLink Match="@Match" | ||
ActiveClass="@string.Empty" | ||
href="@Href" | ||
class="@RootClass" | ||
style="@RootStyle" | ||
data-disabled="@Utils.GetDataAttributeValue( Disabled )" | ||
data-active="@Utils.GetDataAttributeValue( _isActive )" | ||
@attributes="@AdditionalAttributes" | ||
@ref="@_navLink"> | ||
@ChildContent | ||
</NavLink> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) LumexUI 2024 | ||
// LumexUI licenses this file to you under the MIT license | ||
// See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Routing; | ||
|
||
namespace LumexUI; | ||
|
||
/// <summary> | ||
/// A component representing a navigation link within the application. | ||
/// </summary> | ||
public partial class LumexNavLink : LumexLinkBase | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value representing the URL matching behavior. | ||
/// </summary> | ||
/// <remarks> | ||
/// The default value is <see cref="NavLinkMatch.All"/> | ||
/// </remarks> | ||
[Parameter] public NavLinkMatch Match { get; set; } = NavLinkMatch.All; | ||
|
||
private protected override string? RootClass => | ||
TwMerge.Merge( Styles.NavLink.GetStyles( this ) ); | ||
|
||
private NavLink? _navLink; | ||
private bool _isActive; | ||
|
||
/// <inheritdoc /> | ||
protected override void OnAfterRender( bool firstRender ) | ||
{ | ||
if( !_isActive ) | ||
{ | ||
if( GetActiveState( _navLink! ) ) | ||
{ | ||
_isActive = true; | ||
StateHasChanged(); | ||
} | ||
} | ||
} | ||
|
||
[UnsafeAccessor( UnsafeAccessorKind.Field, Name = "_isActive" )] | ||
private static extern ref bool GetActiveState( NavLink navLink ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) LumexUI 2024 | ||
// LumexUI licenses this file to you under the MIT license | ||
// See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE | ||
|
||
using Bunit.TestDoubles; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using TailwindMerge; | ||
|
||
namespace LumexUI.Tests.Components; | ||
|
||
public class NavLinkTests : TestContext | ||
{ | ||
public NavLinkTests() | ||
{ | ||
Services.AddSingleton<TwMerge>(); | ||
} | ||
|
||
[Fact] | ||
public void NavLink_ShouldRenderCorrectly() | ||
{ | ||
var action = () => RenderComponent<LumexNavLink>(); | ||
|
||
action.Should().NotThrow(); | ||
} | ||
|
||
[Fact] | ||
public void NavLink_IfUrlEqualsHref_ShouldBeActive() | ||
{ | ||
var navMan = Services.GetRequiredService<FakeNavigationManager>(); | ||
var cut = RenderComponent<LumexNavLink>( p => p | ||
.Add( p => p.Href, "some-url" ) | ||
); | ||
|
||
navMan.NavigateTo( "some-url" ); | ||
|
||
// faking re-render on location change (NavLink base implementation) | ||
cut.Render(); | ||
|
||
cut.Find( "a" ).GetAttribute( "data-active" ).Should().Be( "true" ); | ||
} | ||
} |