FarzanHajian.Classmate 1.0.0

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

Classmate

Classmate is a simple tool to conditionally add CSS classes to HTML elements in Razor components (and also Razor pages). The library is heavily inspired by ReactJS classnames and brings such capabilities to Blazor applications.

Installation

The preferred method for installing the library is to copy the Classmate.cs file into the destination project, but for those who prefer installing using package, the Classmate package is available on NuGet.

Demo

Consider FetchData.razor component in the Blazor demo application. It generates an HTML table contaning some weather forecasts using the following code block:

...
<table class="table">
    <thead>
        ...
    </thead>
    <tbody>
        @foreach (var forecast in forecasts)
        {
            <tr>
                <td>@forecast.Date.ToShortDateString()</td>
                <td>@forecast.TemperatureC</td>
                <td>@forecast.TemperatureF</td>
                <td>@forecast.Summary</td>
            </tr>
        }
    </tbody>
</table>
...

Now imagine we would like to use different styles for each row based on temperature values. To achieve the effect, first of all, three CSS classes are needed.

.cold {
    color: blue;
    font-style: italic;
    font-weight: normal;
}
.moderate {
    color: gray;
    font-style:normal;
    font-weight: normal;
}
.hot {
    color:red;
    font-style:normal;
    font-weight: bold;
}

And then the @foreach loop must be rewritten in order to make use of Classmate. Two different ways of doing so are provided below:

@foreach (var forecast in forecasts)
{
    int tp = forecast.TemperatureC;

    <tr class=@(
                Classes(
                    "cold".If(tp<=0),
                    "moderate".If(tp>0 && tp<40),
                    "hot".If(tp>=40)
                )
            )>
        <td>@forecast.Date.ToShortDateString()</td>
        <td>@forecast.TemperatureC</td>
        <td>@forecast.TemperatureF</td>
        <td>@forecast.Summary</td>
    </tr>
}
@foreach (var forecast in forecasts)
{
    int tp = forecast.TemperatureC;

    <tr class=@(
                Classes(new {
                    cold = tp<=0,
                    moderate = tp>0 && tp<40,
                    hot = tp>=40}
                )
            )>
        <td>@forecast.Date.ToShortDateString()</td>
        <td>@forecast.TemperatureC</td>
        <td>@forecast.TemperatureF</td>
        <td>@forecast.Summary</td>
    </tr>
}

Pay attention to how the class attribute of <tr> is set. For each row, only one of the style names is returned from the Classes method and assigned to the row HTML.

Usage

The first step is to add the following using directive to your razor file:

@using static FarzanHajian.Classmate.Classmate;

Now Classmate is accessible via calling Classes method. It accepts different inputs.

// List of strings
Classes("btn", "btn-primary", "", "active btn-lg", null, " col   dropdown ", " ")   // "btn btn-primary active btn-lg col   dropdown"

// List of strings with boolean queries
Classes("info".If(true),"error".If(false));    // "info"

// List of strings with boolean queries and else values
Classes("info".If(true, "error"),"bold".If(false, "italic"));    // "info italic"

// List of strings with lambda queries
Classes("info".If(() => true), "error".If(() => false));  // "info"

// List of strings with lambda queries and else values
Classes("info".If(() => true, "error"), "bold".If(() => false, "italic"));  // "info italic"

// List of objects with boolean values
Classes(
    new { info = true },
    new { bold = false, italic = true, underlined = true }
);  // "info italic underlined"

// List of objects with lambda values
Classes(
    new { info = If(() => true) },
    new { bold = If(() => false), italic = If(() => true) }
);  // "info italic"

// A mixure of different values
Classes(
    "btn",
    "btn_primary",
    "active".If(() => false),
    "italic",
    new
    {
        bold = true,
        italic = If(() => true),
        underlined = If(() => false)
    },
    "hidden".If(true || false)
);  // "btn btn-primary italic bold italic hidden"

Moreover, some APIs let you define if/else rules.

// Strings with boolean predicate - Either the first or the second string is chosen
Classes(true, "info", "error");         // "info"

// Strings with a lambda predicate - Either the first or the second string is chosen
Classes(() => true, "info", "error");   // "info"

// Objects with boolean predicate - Either the first or the second object is evaluated
Classes(
    true,
    new
    {
        info = true,
        bold = If(() => false),
        active = If(() => true)
    },
    new
    {
        error = true,
        disabled = If(() => false)
    }
);  // "info active"

// Objects with a lambda predicate - Either the first or the second object is evaluated
Classes(
    () => true,
    new
    {
        info = true,
        bold = If(() => false),
        active = If(() => true)
    },
    new
    {
        error = true,
        disabled = If(() => false)
    }
);  // "info active"
Product Compatible and additional computed target framework versions.
.NET 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.
  • net6.0

    • 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.0.0 131 11/11/2024
1.0.0-rc1 93 10/26/2024