fluentui-blazor: fix: `FluentButton` has `Icon` in wrong `slot` if `ChildContent`, when `null`, is left in markup

🐛 Bug Report

Using FluentButton to be, depending on a given parameter, either in “ico-only mode” or with a label next to the icon, the icon is added to the wrong slot.

💻 Repro or Code Sample

Create a razor component with this markup:

@* NOTE: Formatted for readability, bug still occurs if written as one-liner! *@
<FluentButton IconStart="new Microsoft.FluentUI.AspNetCore.Components.Icons.Regular.Size24.SignOut()">
  @ChildContent
</FluentButton>

🤔 Expected Behavior

Button: Image Code: Image

😯 Current Behavior

Button: Image Code: Image

💁 Possible Solution

If ChildContent is null, i.e., “not there”, Icon should act accordingly & move into the default slot instead of start - this is even the default behavior of Razor, not only Blazor.

For now, I have to violate DRY by doing this:

@if (ChildContent is not null)
{
  <FluentButton IconStart="Icon" Id="@Id">@ChildContent</FluentButton>
}
else
{
  @* NOTE: In reality, I simplify the line below into an empty / self-closing / standalone tag *@
  <FluentButton IconStart="Icon" Id="@Id"></FluentButton>
}

🔦 Context

Implementation of a button component, toggleable to either “icon-only” or “icon with label” mode.

🌍 Your Environment

  • OS & Device: negligible
  • Browser: Firefox Dev Edition, latest version
  • .NET & Fluent UI Blazor: 8.0.10 & 4.10.2

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16

Most upvoted comments

Now that’s for sure! I already dreamed of FluentUI-Blazor code last night! 🙈

But November 2nd will be “demo day” in front of hundreds of people for my employer & afterwards, if I’m still alive, all pressure will be gone. And my brain probably, too! 🤪

Best of luck with the demo!! I’ll be away a couple of days starting Wednesday, so not a lot of time anymore for any last minute fixes, I’m afraid. We’ll probably release v4.10.3 later today or tomorrow.

@Eagle3386 This is a problem when ‘transferring’ your ChildContent object. You must use the ChildContent property and not render this ChildContent in your component. By requesting this rendering in your component, the FluentButton.ChildContent property is no longer null but Empty.

<FluentButton IconStart="Icon" ChildContent="@ChildContent" />
<!--                           ^^^^^^^^^^^^                            -->
@code
{
  [Parameter] public RenderFragment? ChildContent { get; set; }
  [Parameter] public Icon Icon { get; set; }
}

Image

I guess we both are in need of the weekend… 😁. Let’s look at it again next week.