Creating custom components
A custom component is a Razor component with a [VevaComponent] id, registered by a module. Editors then find it under Add content in the page designer.
Editors build pages from components. You add your own by writing a Razor component and registering it with Veva. This article builds the smallest one that works, on Veva 7.26 and .NET 10.
A Veva component is an ordinary Blazor Razor component with one extra attribute: [VevaComponent], which gives it a fixed id. Pages store that id, so never change it once content uses the component.
This article assumes a site set up as in Getting started.
Where the component lives
A component belongs to a module. A module is a class that implements IModule (namespace Veva.Core.Domain.Modules). Veva finds every module at startup by scanning the loaded assemblies, and calls its ConfigureComponents method, where you register your components.
The component can sit in the web project, in your theme project, or in a class library of its own if other sites will reuse it. A class library needs the Razor SDK, <Project Sdk="Microsoft.NET.Sdk.Razor">, and references to Veva.Core.Domain and Veva.Core.Application.BlazorComponents. The web project already has them through Veva.Web.
A basic component
Add a Razor component, here Components/MyCustomComponent.razor, and give it the [VevaComponent] attribute with a new GUID (Visual Studio: Tools, Create GUID).
@using Veva.Core.Domain.Components
@attribute [VevaComponent("70d9fb01-5eab-425d-9e7d-423bf8c078fe")]
<h3>My custom component</h3>
That is a working component. It has no properties and prints fixed markup.
Registering it in a module
IModule asks for Name, Priority and ConfigureServices. Its other methods, ConfigureComponents among them, have empty default bodies, so you only write the ones you need. Use ModulePriorityEnum.Default unless your module must load before or after another.
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Veva.Core.Domain.Components; using Veva.Core.Domain.Modules; using MySite.Components; namespace MySite; public sealed class MySiteModule : IModule { public string Name => "My site"; public int Priority => (int)ModulePriorityEnum.Default; public void ConfigureServices(IServiceCollection services, IConfiguration configuration) { } public void ConfigureComponents(IComponentService componentService, IComponentViewService componentViewService) { // Uses the id from the component's [VevaComponent] attribute. componentService.RegisterComponent<MyCustomComponent>(); } }
RegisterComponent<T>() reads the id from [VevaComponent]. The overload RegisterComponent<T>(Guid id) is for a component without the attribute.
Trying it out
Run the site, sign in and open a page in the designer. Choose Add content and search for MyCustomComponent. Click it to add it to the page. Its properties panel says it has nothing to configure, which is true for now.
Until you decorate it, the component is listed under the category Uncategorized with its class name as its name. The next articles fix that and give it settings:
- Decorating the component with optional attributes: name, description, category, icon, keywords and a hint for AI agents.
- Adding user-configurable properties: settings the editor fills in, with
[EditorBrowsable]and[Parameter]. - Creating components which can accept and host other components: areas that hold other components.