Skip to content

IIconProvider

An icon provider is an icon library. It lists its icons for the icon picker in the page designer and returns the markup for one icon when a page renders it. Implement IIconProvider to offer your own icon set, such as a brand's SVG icons, next to the built-in libraries.

Interface declaration

	namespace Veva.Core.Domain.Components;

	public interface IIconProvider
	{
		string Title { get; }

		string Description { get; }

		Task<string> GetIconMarkupAsync(string identifier, Units sizeUnit, decimal sizeValue, string color);

		Task<IEnumerable<Icon>> GetIconsAsync(Units sizeUnit, decimal sizeValue, string color, IconContext context);
	}

Example

This provider offers a small set of line icons drawn as SVG paths in code. Each icon takes the size and colour the editor picks; with no colour it uses currentColor, so it follows the text colour.

	using System.Net;
	using Veva.Core.Domain.Components;
	using Veva.Core.Domain.Components.CSS;
	using Veva.Core.Domain.Model.Icons;

	public class BrandIconProvider : IIconProvider
	{
		private static readonly Dictionary<string, string> Paths = new()
		{
			["arrow-right"] = "M5 12h14M13 5l7 7-7 7",
			["check"] = "M5 13l4 4L19 7",
			["close"] = "M6 6l12 12M18 6L6 18",
		};

		public string Title => "Brand icons";

		public string Description => "Line icons drawn for this website";

		public Task<string> GetIconMarkupAsync(string identifier, Units sizeUnit, decimal sizeValue, string color)
		{
			if (identifier is null || !Paths.TryGetValue(identifier, out var path))
			{
				return Task.FromResult("");
			}

			return Task.FromResult(Svg(path, sizeUnit.ToCss(sizeValue), color));
		}

		public Task<IEnumerable<Icon>> GetIconsAsync(Units sizeUnit, decimal sizeValue, string color, IconContext context)
		{
			// The picker filters and pages the list itself: return every icon.
			IEnumerable<Icon> icons = Paths.Select(pair => new Icon
			{
				Name = pair.Key,
				Identifier = pair.Key,
				Description = "",
				Markup = Svg(pair.Value, sizeUnit.ToCss(sizeValue), color),
			});

			return Task.FromResult(icons);
		}

		private static string Svg(string path, string size, string color)
		{
			string stroke = string.IsNullOrEmpty(color) ? "currentColor" : WebUtility.HtmlEncode(color);

			return "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\" width=\"" + size + "\" height=\"" + size
				+ "\" fill=\"none\" stroke=\"" + stroke + "\" stroke-width=\"2\"><path d=\"" + path + "\"/></svg>";
		}
	}

Registration

This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).

	using Veva.Core.Application.DependencyInjection;
	using Veva.Core.Domain.Components;

	public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
	{
		// The key is the class name. Every icon an editor picks is stored with it.
		services.AddNamed<IIconProvider, BrandIconProvider>(ServiceLifetime.Singleton);
	}

Where icons come from. Every registered icon provider is a library in the icon picker. The picker shows the library under its Title and Description and fills it from GetIconsAsync. An icon that an editor picks is stored as two values: the provider's registration name and the icon's Identifier. Veva's own libraries are Asset icons (AssetIconProvider, the SVG files in the website's SVG Icons asset folder) and, from the Veva.Modules.FontAwesome package in veva-integrations, FontAwesome Free, registered as FontAwesomeFree.

Turning it on. Register the provider with AddNamed and it shows up in the picker as a library of its own; there is no website setting. The registration name is stored with each icon, so renaming the class, or the name you pass to AddNamed, leaves the icons already picked from it blank.

How it runs. When a page renders an icon, Veva looks up the provider by its stored name and calls GetIconMarkupAsync with the identifier, the size and the colour. The Icon component, Link, the action and form buttons, the data table and the document table's icon field all render icons this way. The string you return is written to the page as HTML. Return an empty string for an identifier you do not know: the icon is then left out. Veva adds aria-hidden="true" to the root element of decorative icons, unless it is already there.

What you get. sizeUnit and sizeValue are the size the editor set; sizeUnit.ToCss(sizeValue) (in Veva.Core.Domain.Components.CSS) turns them into a CSS length such as 1.5em. color is a CSS colour, or empty when the editor chose none. In GetIconsAsync, IconContext.WebsiteId is the website the editor works on, so a provider can offer different icons per website, as Asset icons does. Each Icon needs a Name, an Identifier and the Markup the picker shows as a preview; the picker's search reads Name and Description.

Lifetime and speed. GetIconsAsync returns the whole library: the picker filters and limits the list itself. GetIconMarkupAsync runs for every icon on every page, so keep it quick. Build the list once and keep it, and register the provider as a singleton if it holds no per-request state, as FontAwesome Free does. The provider is created from the service container, so constructor injection works.

Safety. Your markup is not encoded, so encode anything you insert into it that does not come from your own code, such as the colour.

Type to search the docs, components and API.

↑↓ to move ↵ to open Esc to close Search by Veva