-
-
Notifications
You must be signed in to change notification settings - Fork 362
feat(Navbar): add Navbar component #6586
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
Merged
Merged
Conversation
This file contains hidden or 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ArgoZhang - I've reviewed your changes - here's some feedback:
- In Navbars.razor.cs sample code, initialize
_dropdownItems
usingnew List<SelectedItem>()
(ornew()
) instead of the invalid[]
literal. - You renamed the site CSS selector from
.navbar
to.navbar-header
; make sure the Navbar component’s markup and classes align with this change so your styles still apply. - The media‐query blocks in Navbar.razor.scss are almost identical—consider using a mixin or loop to DRY up the repetitive code.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In Navbars.razor.cs sample code, initialize `_dropdownItems` using `new List<SelectedItem>()` (or `new()`) instead of the invalid `[]` literal.
- You renamed the site CSS selector from `.navbar` to `.navbar-header`; make sure the Navbar component’s markup and classes align with this change so your styles still apply.
- The media‐query blocks in Navbar.razor.scss are almost identical—consider using a mixin or loop to DRY up the repetitive code.
## Individual Comments
### Comment 1
<location> `src/BootstrapBlazor/Components/Navbar/NavbarGroup.razor.cs:31` </location>
<code_context>
+ [Parameter]
+ public string? Height { get; set; }
+
+ private string? ClassString => CssBuilder.Default("navbar-nav")
+ .AddClass("navbar-nav-scroll", IsScrolling)
+ .AddClassFromAttributes(AdditionalAttributes)
</code_context>
<issue_to_address>
Consider refactoring the repeated CSS builder logic into a shared base class or helper to centralize and simplify navbar component styling.
Consider extracting the duplicated CSS‐builder logic into a shared base class or helper. This both DRYs up your Navbar* components and avoids pulling in the same `AdditionalAttributes` twice (which can collide). For example:
1) Shared abstract base:
```csharp
// NavbarBase.cs
public abstract class NavbarBase : ComponentBase
{
[Parameter] public bool IsScrolling { get; set; }
[Parameter] public string? Height { get; set; } = "200px";
[Parameter(CaptureUnmatchedValues = true)]
public IDictionary<string, object>? AdditionalAttributes { get; set; }
protected string BuildNavClass(string defaultClass) => CssBuilder
.Default(defaultClass)
.AddClass("navbar-nav-scroll", IsScrolling)
.AddClassFromAttributes(AdditionalAttributes)
.Build();
protected string BuildNavStyle() => CssBuilder
.Default()
.AddStyle("--bs-scroll-height", Height, IsScrolling)
.AddStyleFromAttributes(AdditionalAttributes)
.Build();
}
```
```csharp
// NavbarGroup.razor.cs
public partial class NavbarGroup : NavbarBase
{
private string? ClassString => BuildNavClass("navbar-nav");
private string? StyleString => BuildNavStyle();
// … other parameters / logic …
}
```
2) Or as extension methods:
```csharp
public static class NavbarCssExtensions
{
public static CssBuilder AddNavbarAttributes(this CssBuilder builder, bool scrolling, IDictionary<string, object>? attrs) =>
builder.AddClass("navbar-nav-scroll", scrolling)
.AddClassFromAttributes(attrs);
public static CssBuilder AddNavbarAttributes(this CssBuilder builder, string? height, bool scrolling, IDictionary<string, object>? attrs) =>
builder.AddStyle("--bs-scroll-height", height, scrolling)
.AddStyleFromAttributes(attrs);
}
```
```csharp
// usage in NavbarGroup.razor.cs
private string? ClassString =>
CssBuilder.Default("navbar-nav")
.AddNavbarAttributes(IsScrolling, AdditionalAttributes)
.Build();
private string? StyleString =>
CssBuilder.Default()
.AddNavbarAttributes(Height, IsScrolling, AdditionalAttributes)
.Build();
```
Either approach removes the nearly‐identical pattern from every navbar subcomponent and centralizes the `class`/`style` merging logic.
</issue_to_address>
### Comment 2
<location> `src/BootstrapBlazor/Components/Navbar/NavbarItem.razor.cs:19` </location>
<code_context>
+ [Parameter]
+ public RenderFragment? ChildContent { get; set; }
+
+ private string? ClassString => CssBuilder.Default("navbar-item")
+ .AddClassFromAttributes(AdditionalAttributes)
+ .Build();
</code_context>
<issue_to_address>
Consider refactoring shared logic into a base class so each navigation child only overrides its default CSS class.
Consider extracting the common bits into a small base class so each nav-child only needs to override its default css class. For example:
```csharp
// NavItemBase.cs
public abstract class NavItemBase : ComponentBase
{
[Parameter] public RenderFragment? ChildContent { get; set; }
[Parameter(CaptureUnmatchedValues = true)]
public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }
protected abstract string DefaultCss { get; }
protected string ClassString => CssBuilder
.Default(DefaultCss)
.AddClassFromAttributes(AdditionalAttributes)
.Build();
}
```
Then your NavbarItem becomes trivial:
```csharp
// NavbarItem.razor.cs
public partial class NavbarItem : NavItemBase
{
protected override string DefaultCss => "navbar-item";
}
```
If you have other siblings (e.g. `NavLink`, `NavDropdown`), they all just override `DefaultCss` and inherit the same `ChildContent` + `ClassString` logic.
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This was referenced Aug 15, 2025
This was referenced Aug 22, 2025
This was referenced Sep 3, 2025
This was referenced Sep 11, 2025
This was referenced Sep 19, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Link issues
fixes #6585
Summary By Copilot
Regression?
Risk
Verification
Packaging changes reviewed?
☑️ Self Check before Merge
Summary by Sourcery
Introduce a full-featured Navbar component suite with responsive layout, subcomponents, styling, documentation, and tests
New Features:
Enhancements:
Documentation:
Tests: