fluentui-blazor icon indicating copy to clipboard operation
fluentui-blazor copied to clipboard

docs: add how to set an icon dynamically

Open madcoda9000 opened this issue 3 years ago • 7 comments

🙋 Documentation Request

It would be nice if the documentation can show on how to set a icon dynamically.

💁 Possible Solution

I am thinking of something like that.

razor page:

<FluentIcon ref="@myIcon" Icon="@Icons.Filled.Size24.WeatherSunny"  OnClick="@changeIcon"  />

@code {
    private FluentIcon<Icon>? myIcon {get; set;}

    private viod changeIcon() {
       myIcon.Icon = Icons.Filled.Size24.WeatherMoon;
    } 
}

madcoda9000 avatar Aug 21 '23 09:08 madcoda9000

Yes. It's a good idea. I'll do that.

In summary, the new FluentIcon component has 2 attributes for displaying a specific icon:

  • Icon is a Type representing the icon class that will be instantiated by the component.
  • Value is an Icon object that will be used by the component.

dvoituron avatar Aug 21 '23 10:08 dvoituron

Yes. It's a good idea. I'll do that.

In summary, the new FluentIcon component has 2 attributes for displaying a specific icon:

  • Icon is a Type representing the icon class that will be instantiated by the component.
  • Value is an Icon object that will be used by the component.

Thank you for the explanation. But how can i dynmically assign an icon to a <FluentIcoin > Tag?

madcoda9000 avatar Aug 21 '23 11:08 madcoda9000

Thank you for the explanation. But how can i dynmically assign an icon to a <FluentIcoin > Tag?

By setting the Value parameter, i.e.: <FluentIcon Value="Icons.GetInstance(item)" Color=IconColor Title="@item.Name" /> where item is of Type IconInfo

Or use your own custom image by calling the Icon.FromImageUrl method:

<FluentSystemIcon Value="@(Icon.FromImageUrl("/Blazor.png"))" Width="32px" />

vnbaaij avatar Aug 21 '23 12:08 vnbaaij

Thank you for the explanation. But how can i dynmically assign an icon to a <FluentIcoin > Tag?

By setting the Value parameter, i.e.: <FluentIcon Value="Icons.GetInstance(item)" Color=IconColor Title="@item.Name" /> where item is of Type IconInfo

Or use your own custom image by calling the Icon.FromImageUrl method:

<FluentSystemIcon Value="@(Icon.FromImageUrl("/Blazor.png"))" Width="32px" />

Man, this is awsome!

I did it now like that:

example.razor: <FluentIcon Value="Icons.GetInstance(dmIcon)" Color="Color.Accent" OnClick="@changeColorMode" class="dmIcom" />

example.razor.cs

public partial class Example
{
    private IconInfo dmIcon = new IconInfo { Name = "WeatherSunny", Size = IconSize.Size24, Variant = IconVariant.Filled };
    protected override async Task OnParametersSetAsync() {
        if(darkMode) {
            dmIcon = new IconInfo { Name = "WeatherMoon", Size = IconSize.Size24, Variant = IconVariant.Filled };
       } else {
            dmIcon = new IconInfo { Name = "WeatherSunny", Size = IconSize.Size24, Variant = IconVariant.Filled };
       }
    }
}

The important hint for me was Icons.GetInstance!

Many thanks, that is working like a charm.

madcoda9000 avatar Aug 21 '23 13:08 madcoda9000

How would one do this, as of v4.11.0, since Icons no longer has a GetInstance() method?

@{
  var myIconName = "ChevronRight";
  var iconInfo = new IconInfo() { Name = myIconName, Size = IconSize.Size20, Variant = IconVariant.Regular };
  var myIcon = Icons.GetInstance(iconInfo);

  <FluentIcon Value="@myIcon" />
}

Produces this error message: The type or namespace name 'GetInstance' does not exist in the namespace 'Microsoft.FluentUI.AspNetCore.Components.Icons' (are you missing an assembly reference?)

pmi24 avatar Feb 06 '25 14:02 pmi24

There is always a GetInstance method in the IconsExtensions class. See https://github.com/microsoft/fluentui-blazor/blob/main/src/Core/Components/Icons/IconsExtensions.cs#L28

dvoituron avatar Feb 06 '25 15:02 dvoituron

Thank you, works like a charm!

pmi24 avatar Feb 07 '25 13:02 pmi24