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

Allow overriding of NavLink's ShouldMatch method #59903

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

ilonatommy
Copy link
Member

@ilonatommy ilonatommy commented Jan 16, 2025

ShouldMatch method in NavLink can be overridden by the developers

Description

Appending a query to home url that is marked as NavLink.All in the navigation layout, resulted in un-selecting it in the navigation tab. This was reported as confusing for the users.
In #32168 a property to skip query in the uri comparison was proposed, but it does not solve such cases as appended fragments etc. From this reason, this PR implements the other approach from #32168 (comment).

We expose ShouldMatch and users can create their own version of NavLink, e.g. NavLinkIgnoreQueryAndFragmentString (see tests). Using NavLinkIgnoreQueryAndFragmentString solves the reported issue.

Fixes #31312.

@ilonatommy ilonatommy requested a review from javiercn January 16, 2025 15:35
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jan 16, 2025
@ilonatommy ilonatommy changed the title Fix: NavLink.All on "\" location does not match urls with queries nor fragments Fix: NavLink.All should match urls with queries and fragments Jan 16, 2025
@ilonatommy
Copy link
Member Author

ilonatommy commented Jan 16, 2025

CanFollowLinkToDefaultPageWithHash_NoTrailingSlash and similar are already testing both: queries and fragments with expected behavior. A better approach would be to follow the design from #32168 (comment).

The option of exposing ShouldMatch for inheritance looks more suitable - queries are not the only cases we could choose to ignore, there are also fragments and that might not be the end of the possibilities.

@ilonatommy ilonatommy changed the title Fix: NavLink.All should match urls with queries and fragments Allow overriding of NavLink's ShouldMatch method Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When using a querystring with Blazor apps the the default NavItem is not selected.
1 participant