Kebechet.Blazor.ClassBuilder 1.1.0

Prefix Reserved
dotnet add package Kebechet.Blazor.ClassBuilder --version 1.1.0
                    
NuGet\Install-Package Kebechet.Blazor.ClassBuilder -Version 1.1.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="Kebechet.Blazor.ClassBuilder" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Kebechet.Blazor.ClassBuilder" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Kebechet.Blazor.ClassBuilder" />
                    
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 Kebechet.Blazor.ClassBuilder --version 1.1.0
                    
#r "nuget: Kebechet.Blazor.ClassBuilder, 1.1.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 Kebechet.Blazor.ClassBuilder@1.1.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=Kebechet.Blazor.ClassBuilder&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Kebechet.Blazor.ClassBuilder&version=1.1.0
                    
Install as a Cake Tool

"Buy Me A Coffee"

Blazor.ClassBuilder

NuGet Version NuGet Downloads Twitter

A fluent builder library for constructing CSS class names and HTML attributes in Blazor components. This library provides a clean and intuitive way to conditionally apply CSS classes and HTML attributes in your Blazor applications.

Installation

Install the package via NuGet Package Manager:

dotnet add package Kebechet.Blazor.ClassBuilder

Or via Package Manager Console:

Install-Package Kebechet.Blazor.ClassBuilder

Usage

ClassBuilder

The ClassBuilder provides a fluent API for building CSS class strings:

Basic Usage
using Blazor.ClassBuilder;

// Simple class building
var cssClass = new ClassBuilder()
    .Add("btn")
    .Add("btn-primary")
    .Build(); // Returns: "btn btn-primary"

// With initial value
var cssClass = new ClassBuilder("btn")
    .Add("btn-primary")
    .Build(); // Returns: "btn btn-primary"
Conditional Classes
var isActive = true;
var isDisabled = false;

var cssClass = new ClassBuilder("btn")
    .AddIf(isActive, "active")
    .AddIf(isDisabled, "disabled")
    .Build(); // Returns: "btn active"
Conditional with Actions
var cssClass = new ClassBuilder("btn")
    .AddIf(someCondition, builder => builder
        .Add("complex-class")
        .Add("another-class")
    )
    .Build();
If-Else Logic
var cssClass = new ClassBuilder("btn")
    .AddIfElse(isPrimary, "btn-primary", "btn-secondary")
    .Build();

// With actions
var cssClass = new ClassBuilder("btn")
    .AddIfElse(isComplex, 
        builder => builder.Add("complex").Add("primary"),
        builder => builder.Add("simple").Add("secondary")
    )
    .Build();
In Blazor Components
@using Blazor.ClassBuilder

<div class="@CssClass">
    Content here
</div>

@code {
    [Parameter] public bool IsActive { get; set; }
    [Parameter] public bool IsDisabled { get; set; }
    [Parameter] public string? Size { get; set; }

    private string CssClass => new ClassBuilder("component")
        .AddIf(IsActive, "active")
        .AddIf(IsDisabled, "disabled")
        .AddIf(!string.IsNullOrEmpty(Size), $"size-{Size}")
        .Build();
}

AttributeBuilder

The AttributeBuilder provides a fluent API for building HTML attribute dictionaries:

Basic Usage
using Blazor.ClassBuilder;

var attributes = new AttributeBuilder()
    .Add("id", "my-element")
    .Add("disabled")
    .Build(); // Returns: Dictionary with "id" -> "my-element", "disabled" -> ""
Conditional Attributes
var attributes = new AttributeBuilder()
    .Add("type", "button")
    .AddIf(isDisabled, "disabled")
    .AddIfFilled("placeholder", placeholderText) // Only adds if placeholderText is not null/empty
    .Build();
Combining AttributeBuilders
var baseAttributes = new AttributeBuilder()
    .Add("class", "btn")
    .Add("type", "button");

var finalAttributes = new AttributeBuilder()
    .Add(baseAttributes)
    .AddIf(isDisabled, "disabled")
    .Build();
Exception Handling
var attributes = new AttributeBuilder()
    .Add("type", "button")
    .Throw(invalidCondition, "Invalid state detected") // Throws ArgumentException if condition is true
    .Build();
In Blazor Components
@using Blazor.ClassBuilder

<button @attributes="Attributes">
    Click me
</button>

@code {
    [Parameter] public bool IsDisabled { get; set; }
    [Parameter] public string? Tooltip { get; set; }
    [Parameter] public string? AriaLabel { get; set; }

    private Dictionary<string, object> Attributes => new AttributeBuilder()
        .Add("type", "button")
        .Add("class", "btn btn-primary")
        .AddIf(IsDisabled, "disabled")
        .AddIfFilled("title", Tooltip)
        .AddIfFilled("aria-label", AriaLabel)
        .Build();
}

License

This repository is licensed with the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 825 9/23/2025
1.0.0 170 9/23/2025

Added .ToString() override for ClassBuilder