Use Legacy v4 SVG Icons with New Telerik UI for Blazor Versions
Environment
| Product | UI for Blazor |
| Version | 16.0 and above |
Description
Telerik UI for Blazor 15.2.0 and older versions depend on Telerik.SvgIcons 4.x. Telerik UI for Blazor 16.0.0 and newer versions depend on Telerik.SvgIcons 5.x.
The icons in versions 4 and 5 use different styling. This article explains how to keep using old legacy v4 Telerik SVG icons after upgrading to Telerik UI for Blazor 16.0.0 or a newer version.
The suggested algorithms in this article are not applicable to apps that use Telerik font icons. If you want to use v4 font icons with the latest Telerik UI for Blazor version, then load an old font icon stylesheet instead:
HTML<link rel="stylesheet" href="https://unpkg.com/@progress/kendo-font-icons@4.12.0/dist/index.css" />
Solution
The app must use a modified version of the Telerik.SvgIcons v4 NuGet package. You can choose one of the following options:
Use Prebuilt SVG Icon NuGet Package
- Download the
TelerikSvgIcons4.4.12.0.nupkgNuGet package. It packs a modified version of theTelerik.SvgIconsproject that is used to publish theTelerik.SvgIconsNuGet package. - Add the
TelerikSvgIcons4package to a local NuGet feed or another custom feed in your development and deployment environments. - Install the
TelerikSvgIcons4package in your Blazor app.XML<ItemGroup> <PackageReference Include="Telerik.UI.for.Blazor" Version="16.*" /> <PackageReference Include="TelerikSvgIcons4" Version="4.12.0" /> </ItemGroup> - Add an icon replacement service to your Blazor app:
C#
using System; using System.Collections.Concurrent; using System.Reflection; using Telerik.Blazor.Services; using Telerik.SvgIcons; using TelerikSvgIcons4; namespace MyBlazorAppRootNamespace.Services; public class TelerikSvgIconsV4Service : ITelerikIconService { private static readonly ConcurrentDictionary<string, PropertyInfo?> Cache = new(); public object? ReplaceIcon(IconInfo iconInfo) { var prop = Cache.GetOrAdd(iconInfo.IconName, name => typeof(SvgIcon4).GetProperty(name, BindingFlags.Public | BindingFlags.Static)); return prop?.GetValue(null) as ISvgIcon; } } - Register the
TelerikSvgIconsV4Serviceicon replacement service inProgram.csafter the Telerik services:C#builder.Services.AddTelerikBlazor(); builder.Services.AddSingleton<ITelerikIconService, TelerikSvgIconsV4Service>(); - Add the
TelerikSvgIcons4namespace to_Imports.razor:RAZOR@using Telerik.Blazor @using Telerik.Blazor.Components @using Telerik.SvgIcons @using TelerikSvgIcons4 - At this point, all Telerik Blazor components that render SVG icons internally without public API control will switch to v4 icons.
- If you need to display a v5 SVG icon, use a
<TelerikSvgIcon>component:RAZOR<TelerikSvgIcon Icon="@SvgIcon.Bold" />
The Sample App with v4 Telerik SVG Icons demonstrates all the above steps. The application includes both the TelerikSvgIcons4.4.12.0.nupkg NuGet package and its source code.
Create Custom SVG Icon NuGet Package
The following algorithm describes how to create your own version of the TelerikSvgIcons4.4.12.0.nupkg NuGet package with a name and icon set that suits your preferences. The main goal is to use the legacy icon package alongside the original Telerik.SvgIcons package without namespace and class name conflicts.
- Clone the
kendo-iconsGitHub repository. - Checkout the
release-v4.xbranch. - All the following steps concern only the
Telerik.SvgIconsproject inside thekendo-icons/packages/svg-icons/src-cs/Telerik.SvgIconsfolder of the repository. - Delete the following files and folders:
Modelsfolder—it contains the definitions forSvgIconBaseandISvgIcon, which will be reused from the original referencedTelerik.SvgIconspackage.NOTICE.txtfileREADME.mdfileTelerik.snkfile
- Rename the
Telerik.SvgIconsproject folder and project file. This will also rename the project itself and the root namespace. This guide assumes the new project name isTelerikSvgIcons4. - Replace the
TelerikSvgIcons4.csprojcontent. Use the latestTelerik.SvgIcons4.x version or set one according to your preferences.XML<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <PackageId>TelerikSvgIcons4</PackageId> <Version>4.12.0</Version> <NoWarn>$(NoWarn);CS0618</NoWarn> </PropertyGroup> <ItemGroup> <PackageReference Include="Telerik.SvgIcons" Version="4.12.0" /> </ItemGroup> </Project> - Modify
SvgIcon.cs:- Rename the file according to your preferences, for example,
SvgIcon4.cs. - Rename the
SvgIconclass in the file accordingly toSvgIcon4. - Change the
Telerik.SvgIconsnamespace to the root project namespaceTelerikSvgIcons4 - Add
using Telerik.SvgIcons;in order to recognize theISvgIconinterface from the originalTelerik.SvgIconspackage.
- Rename the file according to your preferences, for example,
- Modify all
.csfiles in theIconsfolder:- Replace
namespace Telerik.SvgIconswithnamespace TelerikSvgIcons4 - Add
using Telerik.SvgIcons;in order to recognize theSvgIconBaseclass from the originalTelerik.SvgIconspackage.
- Replace
- Build and pack the NuGet package:
sh
dotnet pack -c Release - Get the NuGet package and follow the steps from section Use Prebuilt SVG Icon NuGet Package. Make sure that the
<Version>in theTelerikSvgIcons4.csprojproject file matches the version of the NuGet package reference in the consuming project.
The Sample App with v4 Telerik SVG Icons includes a TelerikSvgIcons4 project that already applies all the above steps.