ComponentBuilder 0.5.0
See the version list below for details.
dotnet add package ComponentBuilder --version 0.5.0
NuGet\Install-Package ComponentBuilder -Version 0.5.0
<PackageReference Include="ComponentBuilder" Version="0.5.0" />
paket add ComponentBuilder --version 0.5.0
#r "nuget: ComponentBuilder, 0.5.0"
// Install ComponentBuilder as a Cake Addin #addin nuget:?package=ComponentBuilder&version=0.5.0 // Install ComponentBuilder as a Cake Tool #tool nuget:?package=ComponentBuilder&version=0.5.0
ComponentBuilder
A framework can easily help you to create blazor component.
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>
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; }
}
Support
- .NET 5
- .NET 6
Guideline
Component definition
BlazorComponentBase
This is the base component to get all features for component, any component that contains features need to inherited from this class.
public class MyComponent : BlazorComponentBase
{
}
<MyComponent />
<div />
BlazorChildContentComponentBase
This is a base component that inherited from
BlazorComponentBase
and has child content parameter.
public class MyComponent : BlazorChildContentComponentBase
{
}
<MyComponent>...</MyComponent>
<MyComponent />
<MyComponent>
<ChildContent>....</ChildContent>
</MyComponent>
<div>...</div>
or you can inplement IHasChildContent
or IHasChildContent<TValue>
to create child content attribute automatically.
public class MyChildContentComponent : BlazorComponentBase, IHasChildContent
{
[Parameter]public RenderFragment ChildContent { get; set; }
}
<MyChildContentComponent>...</MyChildContentComponent>
<MyChildContentComponent />
<MyChildContentComponent>
<ChildContent>....</ChildContent>
</MyChildContentComponent>
<div>...</div>
OR
public class MyChildModelComponent : BlazorComponentBase, IHasChildContent<MyModel>
{
[Parameter]public RenderFragment<MyModel> ChildContent { get; set; }
}
<MyChildModelComponent>
@context.ModelProperty
</MyChildModelComponent>
Strong relationship for child component
Inherit
BlazorParentComponentBase
andBlazorChildComponentBase
to create child component with strong relationship between them.
public class MyParentComponent : BlazorParentComponentBase<MyParentComponent>
{
}
public class MyChildComponent : BlazorChildComponentBase<MyParentComponent>
{
}
<MyParentComponent>
<MyChildComponent>...</MyChildComponent>
</MyParentComponent>
OR
<MyParentComponent>
...
<ChildContent>
<MyChildComponent>...</MyChildComponent>
</ChildContent>
...
</MyParentComponent>
<MyChildComponent>...</MyChildComponent>
Quick CSS apply
Define
CssClassAttribute
forParameters
,Class
,Enum members
... and will be applied when component created.
Basic
Apply for parameters
- Boolean type, apply value when value is
true
[Parameter][CssClass("disabled")]public bool Disabled { get;set;}
<MyComponent Disabled/>
<div class="disabled"></div>
- Int,String,Double ... those directly value types, append value behind
CssClass
defined
[Parameter][CssClass("margin-")]public int Margin { get; set; }
[Parameter][CssClass("padding-")]public int Padding { get; set; }
<MyComponent Margin="3" Padding="2">...</MyComponent>
<div class="margin-3 padding-2">...</div>
- Enum type, append value for member
public enum Color
{
Primary,
Secondary, //apply member value with lowercase
[CssClass("warn")]Warning //..apply CssClass value
}
//..parameters in component
[Parameter][CssClass]public Color? Color { get; set; }
[Parameter][CssClass("bg-")]public Color? BgColor { get; set; }
<MyComponent Color="Color.Primary"/>
<div class="primary">...</div>
<MyComponent Color="Color.Warining"/>
<div class="warn">...</div>
<MyComponent Color="Color.Secondary" BgColor="Color.Primary"/>
<div class="secondary bg-primary">...</div>
Enum has default value for first member if parameter is not null
[Parameter][CssClass]public Color Color { get; set; }
<MyComponent/>
<div class="primary">...</div>
Apply for component class
To apply default CSS class without any parameters
[CssClass("btn")]
public class Button : BlazorChildContentComponentBase
{
[Parameter][CssClass("btn-")]public Color? Color { get; set; }
}
<Button>...</Button>
<div class="btn">...</div>
<Button Color="Color.Primary">...</Button>
<div class="btn btn-primary">...</div>
Apply for interface
Define parameters using interface to reuse same CSS class.
public interface IHasDisabled
{
[Parameter][CssClass("disabled")]bool Disabled{ get; set; }
}
public interface IHasMarginSpace
{
[Parameter][CssClass("margin-")]int Margin { get; set; }
}
public class MyComponent1 : BlazorComponentBase, IHasDisabled
{
[Parameter]public bool Disabled{ get; set; }
}
public class MyComponent2 : BlazorComponentBase, IHasDisabled, IHasMargin
{
[Parameter]public bool Disabled{ get; set; }
[Parameter]public int Margin { get; set; }
}
public class MyComponent3 : BlazorComponentBase, IHasMargin
{
//override interface pre-define value
[Parameter][CssClass("m-")]public int Margin { get; set; }
}
<MyComponent1 Disabled />
<div class="disabled" />
<MyComponent2 Disabled Margin="3" />
<div class="disabled margin-3" />
<MyComponent3 Margin="5" />
<div class="m-5" />
Order CSS class
Set Order
parameter for CssClassAttribute
to order CSS class string when component built followed from small to large.
[Parameter][CssClass("block", Order=5)]public bool Block { get; set; }
[Parameter][CssClass("padding-")]public int Padding { get; set; }
<MyComponent Block Padding="3"/>
<div class="padding-3 block" />
Disabled to apply CSS class
Set Disabled
to true
to disable apply CSS class when parameter has value.
[Parameter][CssClass("block", Disabled=true)]public bool Block { get; set; }
<MyComponent Block />
<div>...</div>
Advance
Override BuildCssClass
method
To control logical code to create CSS class
protected override void BuildCssClass(ICssClassBuilder builder)
{
//self logical here
if(Name is not null)
{
builder.Append("show active");
}
//enum type can invoke GetCssClass() method
builder.Append(Color.GetCssClass(), this._hasColor)
.Append("active")
.Append("basic");
}
Html tag definition
Html tag name
Define HtmlTagAttribute
for component to define elemen tag name. Default is div
.
[HtmlTag("button")]
public class Button : BlazorComponentBase
{
}
<Button>...</Button>
<button>...</buton>
Override TagName
property with logical code to output tag name.
public class MyComponent : BlazorComponentBase
{
protected override string TagName
{
get
{
if(Name is null)
{
return "a";
}
return "span";
}
}
}
Html attributes
Parameters can be html attributes when value is applied using HtmlAttribute
- Use parameter value for attribute value
[Parameter][HtmlAttribute("href")]public string Link { get; set; }
<MyComponent Link="www.bing.com"/>
<div href="www.bing.com"/>
- Use bool value of parameter for fixed attribute value when
true
[Parameter][HtmlAttribute("data-toggle", "toggle")]public bool Toggle{ get; set; }
<MyComponent Block />
<div data-toggle="toggle" />
- Use parameter name to be attribute's name
[Parameter][HtmlAttribute]public string Id { get; set; }
<MyComponent Id="id-node" />
<div id="id-node" />
- Pre-define html attribute for component
[HtmlAttribute("role","alert")]
public class MyComponent : BlazorComponentBase
{
}
OR
use HtmlRoleAttribute
to instead HtmlAttribute("role", value)
[HtmlRole("alert")]
public class MyComponent : BlazorComponentBase
{
}
<MyComponent />
<div role="alert" />
Parameter pre-definition
Parameter pre-definition always named starts with
IHasXXX
for specification.
IHasChildContent
Contains parameter ChildContent
in IHasChildContent
or ChildContent<TValue>
in IHasChildContent<TValue>
Product | Versions 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. |
-
net5.0
- Microsoft.AspNetCore.Components.Web (>= 5.0.0)
- Microsoft.JSInterop (>= 5.0.0)
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.0)
- Microsoft.JSInterop (>= 6.0.0)
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 | 129 | 3/7/2024 |
5.0.0 | 186 | 12/21/2023 |
5.0.0-beta-1 | 155 | 11/16/2023 |
4.1.2 | 1,001 | 7/17/2023 |
4.1.1 | 200 | 7/17/2023 |
4.1.0 | 743 | 6/4/2023 |
4.0.0 | 479 | 5/30/2023 |
4.0.0-beta-3 | 257 | 5/22/2023 |
4.0.0-beta-2 | 138 | 5/19/2023 |
4.0.0-beta-1 | 143 | 5/17/2023 |
3.1.4 | 529 | 3/17/2023 |
3.1.3 | 196 | 3/16/2023 |
3.1.0 | 213 | 3/10/2023 |
3.0.0 | 737 | 2/24/2023 |
3.0.0-beta-0217 | 100 | 2/16/2023 |
3.0.0-beta-0206 | 183 | 2/6/2023 |
3.0.0-beta-0130 | 110 | 1/30/2023 |
3.0.0-beta-0114 | 117 | 1/13/2023 |
2.3.0 | 297 | 12/28/2022 |
2.2.0 | 293 | 12/13/2022 |
2.1.0 | 1,450 | 11/24/2022 |
2.0.0 | 361 | 10/28/2022 |
1.5.0.4 | 373 | 10/18/2022 |
1.5.0.3 | 391 | 10/13/2022 |
1.5.0.2 | 1,219 | 10/5/2022 |
1.5.0.1 | 393 | 10/3/2022 |
1.5.0 | 400 | 10/1/2022 |
1.4.1.1 | 664 | 9/19/2022 |
1.4.1 | 404 | 9/16/2022 |
1.4.0 | 416 | 9/15/2022 |
1.3.0 | 413 | 8/29/2022 |
1.2.1 | 575 | 7/12/2022 |
1.2.0 | 411 | 7/11/2022 |
1.1.0 | 555 | 5/22/2022 |
1.0.0 | 610 | 3/23/2022 |
0.7.0 | 438 | 3/11/2022 |
0.6.0 | 436 | 2/9/2022 |
0.5.0 | 270 | 1/6/2022 |
0.4.0 | 280 | 12/23/2021 |
0.3.0 | 293 | 12/16/2021 |
0.2.0 | 300 | 12/7/2021 |
0.1.0 | 6,146 | 11/24/2021 |