Blazor Component Libraries: Building Reusable UI

Create a Razor Class Library to share components across Blazor projects.

Create RCL

dotnet new razorclasslib -o MyComponents

Component with CSS

@* Button.razor *@
<button class="my-button @CssClass" @onclick="OnClick">
    @ChildContent
</button>

@code {
    [Parameter] public RenderFragment ChildContent { get; set; }
    [Parameter] public string CssClass { get; set; }
    [Parameter] public EventCallback OnClick { get; set; }
}

Usage

<Button CssClass="primary" OnClick="HandleClick">
    Click Me
</Button>

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.