Kebechet.Blazor.ClassBuilder 1.2.1

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

"Buy Me A Coffee"

Blazor.ClassBuilder

NuGet Version NuGet Downloads CI codecov Twitter

A fluent builder library for constructing CSS classes, inline styles, and HTML attributes in Blazor components.

Features

  • ClassBuilder - Build CSS class strings with conditional logic
  • StyleBuilder - Build inline CSS styles with type-safe numeric values
  • AttributeBuilder - Build HTML attribute dictionaries
  • ToCssString - Extension for locale-safe CSS numeric values

Installation

dotnet add package Kebechet.Blazor.ClassBuilder

Quick Start

@using Blazor.ClassBuilder
@using Blazor.ClassBuilder.Extensions

// Classes
var css = new ClassBuilder("btn")
    .AddIf(isActive, "active")
    .Build(); // "btn active"

// Styles
var style = new StyleBuilder()
    .Add("color", "red")
    .Add("width", 50.5, "%")
    .Build(); // "color: red; width: 50.5%;"

// Attributes
var attrs = new AttributeBuilder()
    .Add("type", "button")
    .AddIf(isDisabled, "disabled")
    .Build(); // Dictionary<string, object>

ClassBuilder

Build CSS class strings with a fluent API:

// Basic usage
var css = new ClassBuilder()
    .Add("btn")
    .Add("btn-primary")
    .Build(); // "btn btn-primary"

// Multiple classes at once
var css = new ClassBuilder()
    .Add("btn", "btn-primary", "rounded")
    .Build();

// Conditional classes
var css = new ClassBuilder("btn")
    .AddIf(isActive, "active")
    .AddIf(isDisabled, "disabled")
    .Build();

// If-else logic
var css = new ClassBuilder("btn")
    .AddIfElse(isPrimary, "btn-primary", "btn-secondary")
    .Build();

// Nested conditions with actions
var css = new ClassBuilder("btn")
    .AddIf(isComplex, b => b
        .Add("complex-class")
        .Add("another-class"))
    .Build();

In Blazor Components

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

@code {
    [Parameter] public bool IsActive { get; set; }

    private string CssClass => new ClassBuilder("card")
        .AddIf(IsActive, "active")
        .Build();
}

StyleBuilder

Build inline CSS styles with support for numeric values:

// Basic usage
var style = new StyleBuilder()
    .Add("color", "red")
    .Add("font-size", "14px")
    .Build(); // "color: red; font-size: 14px;"

// Numeric values with units
var style = new StyleBuilder()
    .Add("width", 50.5, "%")      // "width: 50.5%;"
    .Add("margin", 10, "px")      // "margin: 10px;"
    .Add("opacity", 0.8)          // "opacity: 0.8;"
    .Add("z-index", 100)          // "z-index: 100;"
    .Build();

// Conditional styles
var style = new StyleBuilder()
    .Add("display", "block")
    .AddIf(isHidden, "visibility", "hidden")
    .AddIf(hasWidth, "width", width, "%")
    .Build();

// Raw style strings
var style = new StyleBuilder()
    .Add("color: red; font-weight: bold")
    .Build();

Locale-Safe Numeric Values

The StyleBuilder automatically handles locale differences for decimal numbers. In some locales (German, French, etc.), decimals use commas (50,5) instead of dots (50.5), which breaks CSS. StyleBuilder always outputs valid CSS with decimal points.

// Works correctly in all locales
var style = new StyleBuilder()
    .Add("width", 50.5, "%")
    .Build(); // Always "width: 50.5%;" never "width: 50,5%;"

ToCssString Extension

For inline scenarios, use the ToCssString() extension:

@using Blazor.ClassBuilder.Extensions

<div style="left: calc(@(percentage.ToCssString())% - 12px)">

In Blazor Components

<div style="@DynamicStyle">Content</div>

@code {
    [Parameter] public double Progress { get; set; }

    private string DynamicStyle => new StyleBuilder()
        .Add("width", Progress, "%")
        .Add("background", "green")
        .Build();
}

AttributeBuilder

Build HTML attribute dictionaries:

// Basic usage
var attrs = new AttributeBuilder()
    .Add("id", "my-element")
    .Add("disabled")  // Value-less attribute
    .Build();

// Conditional attributes
var attrs = new AttributeBuilder()
    .Add("type", "text")
    .AddIf(isDisabled, "disabled")
    .AddIfFilled("placeholder", text)  // Only adds if text is not null/empty
    .Build();

// Combine builders
var baseAttrs = new AttributeBuilder().Add("class", "btn");
var attrs = new AttributeBuilder()
    .Add(baseAttrs)
    .Add("type", "submit")
    .Build();

// Validation
var attrs = new AttributeBuilder()
    .Add("value", value)
    .Throw(value < 0, "Value must be positive")
    .Build();

In Blazor Components

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

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

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

API Reference

ClassBuilder

Method Description
Add(string) Add a CSS class
Add(params string[]) Add multiple CSS classes
AddIf(bool, string) Add class if condition is true
AddIf(bool, Action<ClassBuilder>) Execute builder action if condition is true
AddIfElse(bool, string, string) Add first or second class based on condition
Clear() Remove all classes
Build() Get the final class string

StyleBuilder

Method Description
Add(string, string) Add a style property
Add(string) Add a raw style string
Add(string, double, string) Add numeric value with unit
Add(string, int, string) Add integer value with unit
AddIf(bool, ...) Add style if condition is true
AddIfElse(bool, ...) Add style based on condition
Clear() Remove all styles
Build() Get the final style string

AttributeBuilder

Method Description
Add(string, object?) Add an attribute
Add(AttributeBuilder) Merge another builder
AddIf(bool, string, object?) Add if condition is true
AddIfFilled(string, object?) Add if value is not null/empty
Throw(bool, string) Throw exception if condition is true
Build() Get the attribute dictionary

License

This project is licensed under 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.2.1 655 1/10/2026
1.2.0 634 12/30/2025
1.1.0 867 9/23/2025
1.0.0 186 9/23/2025

Enabled XML documentation generation for IntelliSense support