Toolbelt.Web.CssClassInlineBuilder 2.0.0

Build CSS class string for "class" attribute dynamically based on boolean switch values, in Razor files in-line. (designed for "Blazor")

Install-Package Toolbelt.Web.CssClassInlineBuilder -Version 2.0.0
dotnet add package Toolbelt.Web.CssClassInlineBuilder --version 2.0.0
<PackageReference Include="Toolbelt.Web.CssClassInlineBuilder" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.Web.CssClassInlineBuilder --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Toolbelt.Web.CssClassInlineBuilder, 2.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Toolbelt.Web.CssClassInlineBuilder as a Cake Addin
#addin nuget:?package=Toolbelt.Web.CssClassInlineBuilder&version=2.0.0

// Install Toolbelt.Web.CssClassInlineBuilder as a Cake Tool
#tool nuget:?package=Toolbelt.Web.CssClassInlineBuilder&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

CSS Class Inline Builder (designed for Blazor) NuGet Package

Summary

Build CSS class string for "class" attribute dynamically based on boolean switch and enum values, in Razor files in-line.

fig1

How to use?

1. Install the package to your project.

Package Manager Console:

PM> Install-Package Toolbelt.Web.CssClassInlineBuilder

dotnet CLI:

$ dotnet add package Toolbelt.Web.CssClassInlineBuilder

2. Declare for using "CSS class inline builder".

Add @using static Toolbelt.Web.CssClassInlineBuilder declaration in head of each .razor file which you want to use CSS class inline builder.

You can also add the declaration in _Imports.razor once, instead.

@using static Toolbelt.Web.CssClassInlineBuilder

3. Use "CssClass(...)" method to build CSS class string!

You can use the CssClass(...) method anywhere you want to build a CSS class string.

3-1. CSS class name strings

Basically, you can pass any number of CSS class name strings to the arguments of CssClass() method.

The CssClass() method returns a string that is concatenated with all of those passed to argument strings with a space separator.

<div class="@CssClass("foo", "bar")">

3-2. objects which has bool properties

Next, you can pass any number of objects (include anonymous type) that contains bool properties to the arguments of the CssClass() method.

The CssClass() method picks up the bool properties of argument objects which it values is true, and concatenate those property's name strings with space separator, and return it. (The name of properties are converted lowercase.)

<div class="@CssClass(new {Foo=true, Bar=false}, new {Fizz=true})">

As you know, the anonymous type can omit explicit property names if the name is the same with a variable name.

<div class="@CssClass(new {Foo, Bar}, new {Fizz})">

@code {
  private bool Foo = false;
  private bool Bar = true;
  private bool Fizz = true;
  ...

The property name will be converted from camel case/snake case naming convention to hyphenated lower case.

<div class="@CssClass(new {FizzBuzz})">

@code {
  private bool FizzBuzz = true;
  ...

3-3. any other type properties in an object

If you pass an object with non-boolean type properties, a CSS class name will be built for each of those properties.
That CSS class name will be concatenated with a hyphen of the property name and its property value.

@code {
  private int Stars = 5;
  ...
}

<div class="@CssClass(new {NumberOfStars = this.Stars})">

3-4. enum values

You can also pass any number of enum values to the arguments of the CssClass() method.

The enum value will be converted to a string to use as a CSS class name.

The name of enum value will be converted from camel case/snake case naming convention to hyphenated lower case.

@code {
  enum StateValues {
    NotReady,
    Complete,
    Error
  }

  private StateValues State = StateValues.NotReady;
  ...
}

<div class="@CssClass(this.State)">

Finally, you can mix those all!

You can pass mixing strings, objects, and enum values to the argument of the CssClass() method.

@code {
  enum StateValues {
    NotReady,
    Complete,
    Error
  }

  private bool Fizz = true;
  private bool Buzz = false;
  private int NumOfStars = 5;
  private StateValues State = StateValues.Complete;
  ...
}
...
<div class="@CssClass(new {Fizz, Buzz}, $"stars-{NumOfStars}", State)">

Notice

The CssClass() method uses the .NET CLR "Reflection" feature to parse the object's properties.

This means that using the CssClass() method can degrade performance.

The CssClass() method includes a caching mechanism to avoid performance degradation, but it will be better to let you know this information anyway.

Release Notes

You can see the release notes here.

License

Mozilla Public License Version 2.0

CSS Class Inline Builder (designed for Blazor) NuGet Package

Summary

Build CSS class string for "class" attribute dynamically based on boolean switch and enum values, in Razor files in-line.

fig1

How to use?

1. Install the package to your project.

Package Manager Console:

PM> Install-Package Toolbelt.Web.CssClassInlineBuilder

dotnet CLI:

$ dotnet add package Toolbelt.Web.CssClassInlineBuilder

2. Declare for using "CSS class inline builder".

Add @using static Toolbelt.Web.CssClassInlineBuilder declaration in head of each .razor file which you want to use CSS class inline builder.

You can also add the declaration in _Imports.razor once, instead.

@using static Toolbelt.Web.CssClassInlineBuilder

3. Use "CssClass(...)" method to build CSS class string!

You can use the CssClass(...) method anywhere you want to build a CSS class string.

3-1. CSS class name strings

Basically, you can pass any number of CSS class name strings to the arguments of CssClass() method.

The CssClass() method returns a string that is concatenated with all of those passed to argument strings with a space separator.

<div class="@CssClass("foo", "bar")">

3-2. objects which has bool properties

Next, you can pass any number of objects (include anonymous type) that contains bool properties to the arguments of the CssClass() method.

The CssClass() method picks up the bool properties of argument objects which it values is true, and concatenate those property's name strings with space separator, and return it. (The name of properties are converted lowercase.)

<div class="@CssClass(new {Foo=true, Bar=false}, new {Fizz=true})">

As you know, the anonymous type can omit explicit property names if the name is the same with a variable name.

<div class="@CssClass(new {Foo, Bar}, new {Fizz})">

@code {
  private bool Foo = false;
  private bool Bar = true;
  private bool Fizz = true;
  ...

The property name will be converted from camel case/snake case naming convention to hyphenated lower case.

<div class="@CssClass(new {FizzBuzz})">

@code {
  private bool FizzBuzz = true;
  ...

3-3. any other type properties in an object

If you pass an object with non-boolean type properties, a CSS class name will be built for each of those properties.
That CSS class name will be concatenated with a hyphen of the property name and its property value.

@code {
  private int Stars = 5;
  ...
}

<div class="@CssClass(new {NumberOfStars = this.Stars})">

3-4. enum values

You can also pass any number of enum values to the arguments of the CssClass() method.

The enum value will be converted to a string to use as a CSS class name.

The name of enum value will be converted from camel case/snake case naming convention to hyphenated lower case.

@code {
  enum StateValues {
    NotReady,
    Complete,
    Error
  }

  private StateValues State = StateValues.NotReady;
  ...
}

<div class="@CssClass(this.State)">

Finally, you can mix those all!

You can pass mixing strings, objects, and enum values to the argument of the CssClass() method.

@code {
  enum StateValues {
    NotReady,
    Complete,
    Error
  }

  private bool Fizz = true;
  private bool Buzz = false;
  private int NumOfStars = 5;
  private StateValues State = StateValues.Complete;
  ...
}
...
<div class="@CssClass(new {Fizz, Buzz}, $"stars-{NumOfStars}", State)">

Notice

The CssClass() method uses the .NET CLR "Reflection" feature to parse the object's properties.

This means that using the CssClass() method can degrade performance.

The CssClass() method includes a caching mechanism to avoid performance degradation, but it will be better to let you know this information anyway.

Release Notes

You can see the release notes here.

License

Mozilla Public License Version 2.0

Release Notes

v.2.0.0
- [Breaking Change] Add support for non-boolean type properties in an object.

  • .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 History

Version Downloads Last updated
2.0.0 279 6/20/2021
1.1.1 920 8/27/2020
1.1.0 549 5/6/2020
1.0.0.2 315 1/3/2020