ComponentBuilder 0.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ComponentBuilder --version 0.4.0
                    
NuGet\Install-Package ComponentBuilder -Version 0.4.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ComponentBuilder" Version="0.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ComponentBuilder" Version="0.4.0" />
                    
Directory.Packages.props
<PackageReference Include="ComponentBuilder" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ComponentBuilder --version 0.4.0
                    
#r "nuget: ComponentBuilder, 0.4.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ComponentBuilder@0.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ComponentBuilder&version=0.4.0
                    
Install as a Cake Addin
#tool nuget:?package=ComponentBuilder&version=0.4.0
                    
Install as a Cake Tool

ComponentBuilder

A framework can easily help you to create blazor component from code behind.

QuickStart

1. Install package

> Install-Package ComponentBuilder

2. Add Service

service.AddComponentBuilder();

3. Define your component in behind code

[ElementTag("button")]
[CssClass("btn")]
public class Button : BlazorComponentBase, IHasChildContent
{
    [Parameter] [CssClass("btn-")] public Color? Color { get; set; }
    [Parameter] public RenderFragment ChildContent { get; set; }
}

public enum Color
{
    Primary,
    Secondary,
    Danger,
    Warning,
    Info,
    Dark,
    Light,
    Success
}

4. Use your component in razor

<Button Color="Color.Primary">Primary</Button>
<Button Color="Color.Danger">Danger</Button>

alternate text is missing from this package README image

Html display

<button class="btn btn-primary">Primary</button>
<button class="btn btn-danger">Danger</button>

Razor file VS Code behind

In Button.razor

<button class="@(GetCssClass())" @attributes="Attributes">@ChildContent</button>


@code {
    [Parameter] public RenderFragment ChildContent { get; set; }

    [Parameter(CaptureUnmatchedValues = true)] public IDictionary<string, object> Attributes { get; set; }

    [Parameter] public Color? Color { get; set; }

    [Parameter] public bool Active { get; set; }


    string GetCssClass()
    {
        var cssList = new List<string>();

        if (Active)
        {
            cssList.Add("active");
        }
        if (Color.HasValue)
        {
            cssList.Add($"btn-{Color.Value.ToString().ToLower()}");
        }
        return string.Join(" ", cssList);
    }
}

In Button.cs

[ElementTag("button")]
[CssClass("btn")]
public class Button : BlazorComponentBase, IHasChildContent
{
    [Parameter] public RenderFragment ChildContent { get; set; }

    [Parameter] [CssClass("btn-")] public Color? Color { get; set; }

    [Parameter] [CssClass("active")] public bool Active { get; set; }
}

Customization

Conditional building css class

Overrides BuildCssClass method to build css by logical code

[ElementTag("button")]
[CssClass("btn")]
public class Button : BlazorComponentBase, IHasChildContent
{
    [Parameter] public RenderFragment ChildContent { get; set; }

    [Parameter] [CssClass("btn-")] public Color? Color { get; set; }

    [Parameter] [CssClass("active")] public bool Active { get; set; }

    [Inject]IHostingEnvironment Env { get; set; }

    protected override void BuildCssClass(ICssClassBuilder builder)
    {
        if(Env.IsDevelopment())
        {
            builder.Append("container-sm");
        }
    }
}

Element attribute by parameter

Set ElementAttribute for pameters to create attribute of element

[ElementTag("a")]
public class Anchor : BlazorChildContentComponentBase
{
    [ElementAttribute("name")][Parameter]public string Alias { get; set; }
    [ElementAttribute("href")][Parameter]public string Link { get; set; }

    //build same name of parameter with lowercase
    [ElementAttribute][Parameter]public string Title { get; set; } 
}

<Anchor Link="www.bing.com" Alias="link" Title="Go To Bing">Click Here</Anchor>

<a href="www.bing.com" name="link" title="Go To Bing">Click Here</a>

Additional attributes captured

[ElementTag("a")]
public class LinkButton : BlazorChildContentComponentBase
{
}

<Button data-toggle="modal">Link</Button>


<a data-toggle="modal">Link</a>

Create extensions for css class utility

Build extensions for ICssClassUtility interface.

public static class MyCssClassUtility
{
    public static ICssClassUtility Show(this ICssClassUtility utility) 
        => utility.Append("show");

    public static ICssClassUtility Center(this ICssClassUtility utility) 
        => utility.Append("text-center");
}

Define component

[ElementTag("button")]
[CssClass("btn")]
public class Button : BlazorComponentBase
{
    ...
}

Set CssClass parameter using Css.Class instance in component


<Button CssClass="Css.Class.Show().Center()">Submit</Button>


<button class="btn show text-center">Submit</button>

Import js module

Define js module

export function alert(msg){
    window.alert(msg);
}

export function prompt(msg){
    return window.prompt(msg);
}

Invoke js function in C#

@inject IJSRuntime JS

var module = await JS.Import("./content/myScript.js");

// call js function
module.alert('hello world'); 

// call js function with return type
var value = module.prompt<string>('your name?');

Support

  • .NET 5
  • .NET 6
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on ComponentBuilder:

Package Downloads
TDesign

基于腾讯 TDesign 的 Blazor 企业级组件库。腾讯 TDesign 官方地址:https://tdesign.tencent.com/

BlamanticUI

The css framework from Semantic-UI for blazor without jQuery.

ComponentBuilder.FluentRenderTree

用链式编程的方式简化 RenderTreeBuilder 的操作。 示例: builder.Element("div").Content("hello").Close(); builder.Component<Button>().Content("Button").Close(); builder.Div(Id is not null).Content(content => content.Component<Icon>().Attribute(m => m.Name, "user").Close()).Close();

ComponentBuilder.Interceptors.Diagnostics.Console

在控制台中用于组件生命周期诊断的拦截器,该拦截器可以用于调试阶段的生命周期运行的输出。

ComponentBuilder.Resolvers.FluentClass

组件参数支持 IFluentClassProvider 自动解析成 CSS 类。 [Parameter]public IFluentClassProvider Parameter{ get; set; } <Component Parameter="Provider.Is3.FromTop.HasSmall" />

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on ComponentBuilder:

Repository Stars
tdesign-blazor/TDesignBlazor
基于腾讯 TDesign 的 Blazor 组件库
Version Downloads Last Updated
5.1.0 214 3/7/2024
5.0.0 248 12/21/2023
5.0.0-beta-1 199 11/16/2023
4.1.2 1,222 7/17/2023
4.1.1 279 7/17/2023
4.1.0 865 6/4/2023
4.0.0 574 5/30/2023
4.0.0-beta-3 336 5/22/2023
4.0.0-beta-2 221 5/19/2023
4.0.0-beta-1 226 5/17/2023
3.1.4 602 3/17/2023
3.1.3 267 3/16/2023
3.1.0 282 3/10/2023
3.0.0 819 2/24/2023
3.0.0-beta-0217 165 2/16/2023
3.0.0-beta-0206 246 2/6/2023
3.0.0-beta-0130 175 1/30/2023
3.0.0-beta-0114 182 1/13/2023
2.3.0 366 12/28/2022
2.2.0 360 12/13/2022
2.1.0 1,551 11/24/2022
2.0.0 433 10/28/2022
1.5.0.4 447 10/18/2022
1.5.0.3 464 10/13/2022
1.5.0.2 1,317 10/5/2022
1.5.0.1 471 10/3/2022
1.5.0 473 10/1/2022
1.4.1.1 747 9/19/2022
1.4.1 486 9/16/2022
1.4.0 498 9/15/2022
1.3.0 488 8/29/2022
1.2.1 659 7/12/2022
1.2.0 494 7/11/2022
1.1.0 647 5/22/2022
1.0.0 710 3/23/2022
0.7.0 530 3/11/2022
0.6.0 528 2/9/2022
0.5.0 359 1/6/2022
0.4.0 374 12/23/2021
0.3.0 386 12/16/2021
0.2.0 390 12/7/2021
0.1.0 6,243 11/24/2021